Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var S2collection = imageCollection.filterDate('2016-08-01','2019-04-10')
- .filterBounds(aoi)
- .filterMetadata('CLOUD_COVERAGE_ASSESSMENT', 'equals', 0);
- Map.addLayer(S2collection, {bands:['B4', 'B3', 'B2'], max:3000});
- // Function to calculate and add an NDVI band
- var addNDVI = function(image) {
- return image.addBands(image.normalizedDifference(['B8', 'B4']));
- };
- // Add NDVI band to image collection
- var S2 = S2collection.map(addNDVI);
- // Extract NDVI band and create NDVI median composite image
- var NDVI = S2.select(['nd']);
- var NDVImed = NDVI.median(); //I just changed the name of this variable ;)
- var ndvi_vis = ['#d73027', '#f46d43', '#fdae61', '#fee08b', '#d9ef8b',
- '#a6d96a'];
- Map.addLayer(NDVI, {min:0, max:0.7, palette: ndvi_vis},'NDVI');
- // Add NDWI band to image collection
- var addNDWI = function(image) {
- return image.addBands(image.normalizedDifference(['B3', 'B5']));
- };
- // Add NDWI band to image collection
- var S2S = S2collection.map(addNDWI);
- // Extract NDWI band and create NDWI median composite image
- var NDWI = S2S.select(['nd']);
- var NDWImed = NDVI.median();
- var NDWIViz = {min: 0, max: 0.5, palette: ['00FFFF', '0000FF']};
- Map.addLayer(NDWI, NDWIViz, 'NDWI', false);
- // Function to mask out NDVI
- var S2maskedVeg = function(image) {
- var NDVI = image.select(['NDVI']);
- return image.updateMask(NDVI.lte(0.15));
- };
- // Apply water and veg masking functions to image collection
- var S2MASKNDVI = S2collection.map(S2maskedVeg);
- // Function to mask out NDWI
- var S2maskedWater = function(image) {
- var NDWI = image.select(['NDWI']);
- return image.updateMask(NDWI.lte(0.15));
- };
- // Apply water and veg masking functions to image collection
- var S2maskedW = S2MASKNDVI.map(S2maskedWater);
- Map.addLayer(S2maskedW,{max:1},'S2 masked');
- var S2collection = imageCollection.filterDate('2016-08-01','2019-04-10')
- .filterBounds(aoi)
- .filterMetadata('CLOUD_COVERAGE_ASSESSMENT', 'equals', 0);
- Map.addLayer(S2collection, {bands:['B4', 'B3', 'B2'], max:3000});
- // ####### DEFINE ALL FUNCTIONS #####
- // Function to calculate and add an NDVI band
- var addNDVI = function(image) {
- return image.addBands(image.normalizedDifference(['B8', 'B4']).rename('NDVI'));
- };
- // Add NDWI band to image collection
- var addNDWI = function(image) {
- return image.addBands(image.normalizedDifference(['B3', 'B5']).rename('NDWI'));
- };
- // Function to mask out NDVI
- var S2maskedVeg = function(image) {
- var NDVI = image.select(['NDVI']);
- return image.addBands(ee.Image(1).updateMask(NDVI.lte(0.15)).rename('NDVI_mask'));
- };
- // Function to mask out NDWI
- var S2maskedWater = function(image) {
- var NDWI = image.select(['NDWI']);
- return image.addBands(ee.Image(1).updateMask(NDWI.lte(0.15)).rename('NDWI_mask'));
- };
- // ####### APPLY ALL FUNCTION TO THE IMAGE COLLECTION ####
- // Apply water and veg masking functions to image collection
- S2collection = S2collection.map(addNDWI).map(addNDVI).map(S2maskedVeg).map(S2maskedWater);
- print(S2collection)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement