Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. // Creating a buffer around my region of interest
  2. var buffer = ee.Geometry.Point([103.83841283715412,1.3736828122619071]).buffer(10000);
  3. Map.centerObject(buffer, 12);
  4.  
  5. // Load Landsat 8 surface reflectance data
  6. // Look for adequate image filtered by date and cloud cover and region
  7. var l8sr = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
  8. .filterBounds(buffer)
  9. .filter(ee.Filter.calendarRange(2018,2019,'year'))
  10. .filter(ee.Filter.calendarRange(4,5,'month'))
  11. .map(function(image){return image.clip(buffer)});
  12.  
  13. // Function to cloud mask from the Fmask band of Landsat 8 SR data.
  14. function maskL8sr(image) {
  15. // Bits 3 and 5 are cloud shadow and cloud, respectively.
  16. var cloudShadowBitMask = ee.Number(2).pow(3).int();
  17. var cloudsBitMask = ee.Number(2).pow(5).int();
  18.  
  19. // Get the pixel QA band.
  20. var qa = image.select('pixel_qa');
  21.  
  22. // Both flags should be set to zero, indicating clear conditions.
  23. var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0)
  24. .and(qa.bitwiseAnd(cloudsBitMask).eq(0));
  25.  
  26. // Return the masked image, scaled to [0, 1].
  27. return image.updateMask(mask).divide(10000);
  28. }
  29.  
  30.  
  31.  
  32.  
  33. // Map the function over years of data and take the median.
  34. var composite = l8sr.map(maskL8sr)
  35. .median();
  36.  
  37.  
  38. //The Band Variables for Landsat 8
  39. var B2_Blue = composite.select('B2');
  40. var B3_Green = composite.select('B3');
  41. var B4_Red = composite.select('B4');
  42. var B5_NIR = composite.select('B5');
  43. var B6_SWIR1 = composite.select('B6');
  44. var B7_SWIR2 = composite.select('B7');
  45. var B10_TIR1 = composite.select('B10');
  46. var B11_TIR2 = composite.select('B11');
  47.  
  48.  
  49. // Creating an add variable function for Landsat 8 NDVI.
  50. var addNDVI_L8 = function(image) {
  51. var ndvi = composite.expression(
  52. '(B5-B4)/(B5+B4)', {
  53. 'B5': B5_NIR,
  54. 'B4': B4_Red
  55. }).rename('ndvi');
  56. return composite.addBands(ndvi);
  57. };
  58.  
  59.  
  60. // Creating an add variable function for Landsat 8 MSAVI2.
  61. var addMSAVI2_L8 = function(image) {
  62. var msavi2 = composite.expression(
  63. '(2 * B5 + 1 - sqrt(pow((2 * B5 + 1), 2) - 8 * (B5 - B4)) ) / 2', {
  64. 'B5': B5_NIR,
  65. 'B4': B4_Red
  66. }).rename('msavi2');
  67. return composite.addBands(msavi2);
  68. };
  69.  
  70. // Creating an add variable function for Landsat 8 EBBI.
  71. var addEBBI_L8 = function(image) {
  72. var ebbi = composite.expression(
  73. '(B6-B5)/(sqrt(B6+B10)*(10))', {
  74. 'B5': B5_NIR,
  75. 'B6': B6_SWIR1,
  76. 'B10': B10_TIR1
  77. }).rename('ebbi');
  78. return composite.addBands(ebbi);
  79. };
  80.  
  81. //Defining the composite image with the added NDVI band
  82. var composite_NDVI = addNDVI_L8(composite);
  83.  
  84. //printing to see if it has 13 bands (i.e. with the NDVI band added) (THIS WORKS!)
  85. print(composite_NDVI, 'Image with added NDVI band')
  86.  
  87. //This code doesn't work, "composite.map is not a function"
  88. var composite_indices = composite.map(addNDVI_L8).map(addMSAVI2_L8).map(addEBBI_L8);
  89.  
  90. print(composite_indices, 'composite indices');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement