Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. var S2collection = imageCollection.filterDate('2016-08-01','2019-04-10')
  2. .filterBounds(aoi)
  3. .filterMetadata('CLOUD_COVERAGE_ASSESSMENT', 'equals', 0);
  4. Map.addLayer(S2collection, {bands:['B4', 'B3', 'B2'], max:3000});
  5.  
  6. // Function to calculate and add an NDVI band
  7. var addNDVI = function(image) {
  8. return image.addBands(image.normalizedDifference(['B8', 'B4']));
  9. };
  10. // Add NDVI band to image collection
  11. var S2 = S2collection.map(addNDVI);
  12. // Extract NDVI band and create NDVI median composite image
  13. var NDVI = S2.select(['nd']);
  14. var NDVImed = NDVI.median(); //I just changed the name of this variable ;)
  15. var ndvi_vis = ['#d73027', '#f46d43', '#fdae61', '#fee08b', '#d9ef8b',
  16. '#a6d96a'];
  17. Map.addLayer(NDVI, {min:0, max:0.7, palette: ndvi_vis},'NDVI');
  18.  
  19. // Add NDWI band to image collection
  20. var addNDWI = function(image) {
  21. return image.addBands(image.normalizedDifference(['B3', 'B5']));
  22. };
  23. // Add NDWI band to image collection
  24. var S2S = S2collection.map(addNDWI);
  25. // Extract NDWI band and create NDWI median composite image
  26. var NDWI = S2S.select(['nd']);
  27. var NDWImed = NDVI.median();
  28. var NDWIViz = {min: 0, max: 0.5, palette: ['00FFFF', '0000FF']};
  29. Map.addLayer(NDWI, NDWIViz, 'NDWI', false);
  30.  
  31.  
  32. // Function to mask out NDVI
  33. var S2maskedVeg = function(image) {
  34. var NDVI = image.select(['NDVI']);
  35. return image.updateMask(NDVI.lte(0.15));
  36. };
  37. // Apply water and veg masking functions to image collection
  38. var S2MASKNDVI = S2collection.map(S2maskedVeg);
  39.  
  40. // Function to mask out NDWI
  41. var S2maskedWater = function(image) {
  42. var NDWI = image.select(['NDWI']);
  43. return image.updateMask(NDWI.lte(0.15));
  44. };
  45. // Apply water and veg masking functions to image collection
  46. var S2maskedW = S2MASKNDVI.map(S2maskedWater);
  47. Map.addLayer(S2maskedW,{max:1},'S2 masked');
  48.  
  49. var S2collection = imageCollection.filterDate('2016-08-01','2019-04-10')
  50. .filterBounds(aoi)
  51. .filterMetadata('CLOUD_COVERAGE_ASSESSMENT', 'equals', 0);
  52. Map.addLayer(S2collection, {bands:['B4', 'B3', 'B2'], max:3000});
  53.  
  54. // ####### DEFINE ALL FUNCTIONS #####
  55. // Function to calculate and add an NDVI band
  56. var addNDVI = function(image) {
  57. return image.addBands(image.normalizedDifference(['B8', 'B4']).rename('NDVI'));
  58. };
  59. // Add NDWI band to image collection
  60. var addNDWI = function(image) {
  61. return image.addBands(image.normalizedDifference(['B3', 'B5']).rename('NDWI'));
  62. };
  63. // Function to mask out NDVI
  64. var S2maskedVeg = function(image) {
  65. var NDVI = image.select(['NDVI']);
  66. return image.addBands(ee.Image(1).updateMask(NDVI.lte(0.15)).rename('NDVI_mask'));
  67. };
  68. // Function to mask out NDWI
  69. var S2maskedWater = function(image) {
  70. var NDWI = image.select(['NDWI']);
  71. return image.addBands(ee.Image(1).updateMask(NDWI.lte(0.15)).rename('NDWI_mask'));
  72. };
  73.  
  74. // ####### APPLY ALL FUNCTION TO THE IMAGE COLLECTION ####
  75. // Apply water and veg masking functions to image collection
  76. S2collection = S2collection.map(addNDWI).map(addNDVI).map(S2maskedVeg).map(S2maskedWater);
  77. print(S2collection)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement