//////////////////////////////////////// ///////////// PREPARE IMAGE //////////// //////////////////////////////////////// // Center map on Belize study area Map.centerObject(belize_mar); // Establish function for masking clouds from Sentinel-2 image collection function maskS2clouds(image) { var qa = image.select('QA60'); // Bits 10 and 11 are clouds and cirrus, respectively. var cloudBitMask = 1 << 10; var cirrusBitMask = 1 << 11; // Both flags should be set to zero, indicating clear conditions. var mask = qa.bitwiseAnd(cloudBitMask).eq(0) .and(qa.bitwiseAnd(cirrusBitMask).eq(0)); return image.updateMask(mask).divide(10000); } // Load image collection of Sentinel-2 surface reflectance data var sentinel_collection = ee.ImageCollection('COPERNICUS/S2_SR') .filterDate('2020-01-01', '2020-05-31') // Pre-filter to get less cloudy granules. .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20)) .map(maskS2clouds); // Establish variable for visualizing data on screen (true color) var median_vis = { min: 0.0, max: 0.2, bands: ['B4_median', 'B3_median', 'B2_median'], }; // Reduce Sentinel image collection to single image var sentinel_image = sentinel_collection.reduce(ee.Reducer.median()); // Clip Sentinel image to Belize boundary var belize_image = sentinel_image.clip(belize_mar); // Display Belize image Map.addLayer(belize_image, median_vis, "Belize Image"); //////////////////////////////////////// ///////////// FILTER IMAGE ///////////// //////////////////////////////////////// // Create "NDWI" layer to mask out water features var ndwi_layer = belize_image.normalizedDifference(['B3_median', 'B8_median']); // Mask water out of Sentinel image using NDWI layer var belize_ndwi = belize_image.updateMask(ndwi_layer.lte(0.0)); //Map.addLayer(belize_ndwi, median_vis, 'NDWI mask applied'); // Create "elevation" layer to mask out high-elevation features var srtm = ee.Image('USGS/SRTMGL1_003'); var elevation_layer = srtm.select('elevation'); // Mask high elevations out of Sentinel image using elevation layer var belize_elevation = belize_ndwi.updateMask(elevation_layer.lte(20.0)); //Map.addLayer(belize_elevation, median_vis, 'Elevation mask applied'); // Create "NDMI" layer to mask out non-mangrove candidates var ndmi_layer = belize_image.normalizedDifference(['B12_median', 'B3_median']); // Mask non-mangrove candidates out of Sentinel image using NDMI layer var belize_ndmi = belize_elevation.updateMask(ndmi_layer.lte(0.1)); //Map.addLayer(belize_ndmi, median_vis, "NDMI mask applied"); //////////////////////////////////////// /////////// CLASSIFY IMAGE ///////////// //////////////////////////////////////// // Designate which spectral bands to include in the classification var bands = ['B8_median', 'B4_median', 'B3_median', 'B2_median']; // Train the classifier using the training points var training = belize_ndmi.select(bands).sampleRegions({ collection: belize_training, properties: ['class'], scale: 10 }); // Establish the classifier var classifier = ee.Classifier.smileRandomForest(200) .train({ features: training, classProperty: 'class', inputProperties: bands }); // Classify the image var belize_classified = belize_ndmi.select(bands).classify(classifier); // Mask out all non-mangrove pixels from the classified thematic map var belize_class_mask = belize_classified.updateMask(belize_classified.lte(0)); // Display the preliminary classified thematic map //Map.addLayer(belize_class_mask, {palette: ('FF0000')}, 'Preliminary Belize Mangrove Classification'); //////////////////////////////////////// //////// REFINE CLASSIFICATION ///////// //////////////////////////////////////// // Display Bunting mangrove layer for reference //var belize_bunting_2016 = belize_bunting.reduceToImage({ // properties: ['pxlval'], // reducer: ee.Reducer.first() //}); //Map.addLayer(belize_bunting_2016, {palette: ('00FF00')}, 'Bunting 2016'); // Display only the mangrove training points for reference //Map.addLayer(belize_mang_training, {palette: ('00FF00')}, 'Belize Mangrove Training Points'); // Make sure that the manual adjustment geometry is of type FeatureCollection var belize_include = ee.FeatureCollection(belize_include); var belize_include_2 = ee.FeatureCollection(belize_include_2); // Merge the the two manual adjustment FeatureCollections var belize_include_merge = belize_include.merge(belize_include_2); // Dissolve the polygons in the Feature Collection, so the overlapping polygons // don't "cancel each other out" var belize_include_merge = belize_include_merge.union(); // Clip the classification to the Feature Collection of manual adjustments, and display var belize_mangroves = belize_class_mask.clip(belize_include_merge); Map.addLayer(belize_mangroves, {palette: ('00FF00')}, 'Belize MAR Mangroves'); //////////////////////////////////////// //////////// CALCULATE AREA //////////// //////////////////////////////////////// // Calculate mangrove area for Belize, and print to console var belize_mangrove_area = belize_mangroves.reduceRegion({ reducer: ee.Reducer.count(), geometry: belize_include_merge, scale: 10, maxPixels: 1e9 }); var belize_mangrove_area = ee.Number(belize_mangrove_area.get('classification')).divide(10000); print('Belize MAR mangrove area (km2) as of 2020:', belize_mangrove_area); //////////////////////////////////////// ///////// MAINLAND VS ISLANDS ////////// //////////////////////////////////////// // Clip the Belize mangrove raster to the mainland polygon var belize_mainland_mangroves = belize_mangroves.clip(mainland); //Map.addLayer(belize_mainland_mangroves, {palette: ('FF0000')}, 'Mainland Mangroves'); // Calculate mangrove area for the mainland mangroves, and print to console var belize_mainland_mangrove_area = belize_mainland_mangroves.reduceRegion({ reducer: ee.Reducer.count(), geometry: mainland, scale: 10, maxPixels: 1e9 }); var belize_mainland_mangrove_area = ee.Number(belize_mainland_mangrove_area.get('classification')).divide(10000); print('Belize mainland mangrove area (km2) as of 2020:', belize_mainland_mangrove_area); // Subtract the mainland mangrove area from the total mangrove area to calculate // the island mangrove area var belize_island_mangrove_area = (belize_mangrove_area).subtract(belize_mainland_mangrove_area); print('Belize island mangrove area (km2) as of 2020:', belize_island_mangrove_area); //////////////////////////////////////// ////////////// EXPORT MAP ////////////// //////////////////////////////////////// // Convert Belize mangrove raster to polygon //var belize_mangroves_polygon = belize_mangroves.toInt().reduceToVectors({ // geometry: belize_include_merge, // crs: belize_mangroves.projection(), // scale: 10, // geometryType: 'polygon', // eightConnected: false, // maxPixels: 1e9 //}); // Export Belize mangrove polygon to Google Drive //Export.table.toDrive({ // collection: belize_mangroves_polygon, // description:'belize_mar_mangroves', // fileFormat: 'KMZ' //});