//Access in the Google Earth Engine code editor: https://code.earthengine.google.com/?accept_repo=users/tatiana/SeteSetembro ////////////////////////////// // // // VISUALISATION SCRIPT // // // ////////////////////////////// /////////////////////////////////////////////////// //ENTER IMPORT AND EXPORT VARIABLES //Enter these 4 variables var Clear = false; //Is the image clear (true) or cloudy (false)? var datatake = '20190123'; //Enter datatake as in classified images name var dateFrom = '2019-01-23'; //ENTER IMAGE DATES var dateTo = '2019-01-24'; //ENTER IMAGE DATES //Automatic variables var ClassifClouds = datatake+'_classifiedClouds'; var ClassifForest = datatake+'_classifiedForest'; var nameExp = datatake; /////////////////////////////////////////////////// //Study zone //Polygone around the study zone var studySite = ee.Geometry.Polygon( [[[-61.4417, -10.7464], [-61.4383, -11.2584], [-60.8973, -11.2551], [-60.9098, -10.7198]]]); //Center on study zone Map.centerObject(studySite,11); //Polygone of the SSIL //Source: "WCMC/WDPA/current/polygons").filterMetadata('NAME','equals','Sete de Setembro') var protectedArea = ee.FeatureCollection([ee.Geometry.Polygon( [[[-61.430890367207255,-10.751891444900613], [-61.42519163574619,-11.11447884042015], [-61.37005491417673,-11.114298205538441], [-61.31491808398053,-11.114117608447993], [-61.31458365364846,-11.249759040437475], [-61.23209698577483,-11.249121383106058], [-61.149610266132704,-11.248483719229005], [-61.09324164199908,-11.175238503746515], [-61.036873050995155,-11.101993360397458], [-60.98050448006274,-11.028748124678014], [-60.92413582759241,-10.955502938986513], [-60.92913000110267,-10.738589998072223], [-60.99185011285132,-10.740252698436697], [-61.05457014507297,-10.741915331211235], [-61.11729013718773,-10.743578015404607], [-61.180010217902435,-10.745240724477434], [-61.24273024957612,-10.746903404731825], [-61.30545027262106,-10.748566097570336], [-61.368170327808194,-10.750228766894775], [-61.430890367207255,-10.751891444900613]]])]); // New empty image (bytes) in which the SSIL polygone will be painted var empty = ee.Image().byte(); var seteSetembro = empty.paint({ featureCollection: protectedArea, width: 1.5 }); //Bitmap of the indigenous land + mask var borderBM = ee.Image('users/tatiana/Masks/BorderBM'); //Bitmap of the rivers + mask var waterBM = ee.Image('users/tatiana/Masks/WaterBM'); var waterBMMask = waterBM.updateMask(waterBM); var waterBMMaskDisp = waterBM.updateMask(waterBM.not()); ///////////////////// IF CLOUDY IMAGE ////////////////////// if (Clear === false) { print('The image is cloudy.'); /////////////////////////////////////////// //LOAD ALL IMAGES AND CLIP ON STUDY SITE //Clip function var clipStudySite = function(image){ return image.clip(studySite)}; //Clouds classification var imgClouds = ee.Image('users/tatiana/'+ClassifClouds); var imgClouds = clipStudySite(imgClouds); //Forest classification var imgForest = ee.Image('users/tatiana/'+ClassifForest) var imgForest = clipStudySite(imgForest); //Original image var imgS2 = ee.ImageCollection('COPERNICUS/S2') .filterDate(dateFrom,dateTo) .filterBounds(studySite); //Cloud percentage print('Images in image collection (must be 4):',imgS2.size()); var meanClouds = imgS2.aggregate_mean('CLOUDY_PIXEL_PERCENTAGE'); print('Mean cloud cover percentage: ',meanClouds); //Make the image collection to a single image and clip it var imgS2 = imgS2 .map(clipStudySite) .mean(); /////////////////////////////////////////////////// //GENERATING BITMAPS FOR CLOUDS, SHADOW, VEGETATION //Where 1 = yes / 0 = other //Please note that the prediction map for the forest ist already a bitmap with 1 = not vegetation //Vegetation bitmap function var addBMVegetation = function(image) { var imagePred = image.select('notVegetationBM').not().rename('vegetationBM'); return image.addBands(imagePred); }; //Clouds bitmap function var addBMClouds = function(image) { var imagePred = image.select('predictions'); var BitMapValues = imagePred.gt(4).and(imagePred.lt(7)).rename('cloudsBM'); return image.addBands(BitMapValues); }; //Shadows and water bitmap function var addMBShW = function(image) { var imagePred = image.select('predictions'); var BitMapValues = imagePred.expression( 'water + shadow', { 'water': imagePred.gt(3).and(imagePred.lt(5)), 'shadow': imagePred.gt(6) }).rename('shadowBM'); return image.addBands(BitMapValues); }; var imgForest = addBMVegetation(imgForest); var imgClouds = addBMClouds(imgClouds); var imgClouds = addMBShW(imgClouds); /////////////////////////////////////////////////// //GENERATING ALL MASKS //Clouds mask function var maskClouds = function(image) { var imagePred = image.select('cloudsBM'); var BitMask = imagePred.updateMask(imagePred).rename('cloudsMask'); return image.addBands(BitMask); }; //Shadows and water mask function var maskShW = function(image) { var imagePred = image.select('shadowBM'); var BitMask = imagePred.updateMask(imagePred).rename('shadowMask'); return image.addBands(BitMask); }; //Vegetation mask function (+ masked clouds and shadow) var maskVegetation = function(image) { var imagePred = image.select('vegetationBM'); var maskClouds = imgClouds.select('cloudsBM').not(); var maskShadow = imgClouds.select('shadowBM').not(); var mask = imagePred.and(maskClouds).and(maskShadow).and(waterBMMask); var BitMask = imagePred.updateMask(mask).rename('vegetationMask'); return image.addBands(BitMask); }; //Not Vegetation mask function (+ masked clouds and shadow) var maskNotVegetation = function(image) { var imagePred = image.select('notVegetationBM'); var maskClouds = imgClouds.select('cloudsBM').not(); var maskShadow = imgClouds.select('shadowBM').not(); var mask = imagePred.and(maskClouds).and(maskShadow).and(waterBMMask); var BitMask = imagePred.updateMask(mask).rename('notVegetationMask'); return image.addBands(BitMask); }; //Add the masks var imgClouds = maskClouds(imgClouds); var imgClouds = maskShW(imgClouds); var imgForest = maskVegetation(imgForest); var imgForest = maskNotVegetation(imgForest); /////////////////////////////////////////////////// //DISPLAY PARAMETERS //True colors var visTrueColors = { bands: ['B4','B3','B2'], min: 200, max: 6200, gamma: 1.8 }; //Clouds var visClouds = { bands: ['cloudsMask'], palette: 'ffffff' }; //Shadows var visShadow = { bands: ['shadowMask'], palette: '876f9d' }; //Vegetation var visVeg = { bands: ['vegetationMask'], palette: '89d4a4' }; //Bare var visNotVeg = { bands: ['notVegetationMask'], palette: 'e46346' }; /////////////////////////////////////////////////// //LOAD MAPS Map.addLayer(imgS2, visTrueColors, 'True Colors', true); Map.addLayer(imgForest,visVeg,'Vegetation', true); Map.addLayer(imgForest,visNotVeg,'Not vegetation', true); Map.addLayer(imgClouds, visShadow, 'Shadows',true); Map.addLayer(waterBMMaskDisp, {palette: '5a7cba'}, 'Water',true); Map.addLayer(imgClouds, visClouds, 'Clouds', true); Map.addLayer(seteSetembro, {palette: '525252'}, 'Sete de Setembro',true); /////////////////////////////////////////////////// //PREPARE IMAGES FOR EXPORT //Prepare export for visualisation var cloudsBM = imgClouds.select('cloudsBM').multiply(5) var shadowBM = imgClouds.select('shadowBM').subtract(waterBM.not()).eq(1).multiply(4) var ForestBM = imgForest.select('vegetationBM').subtract(waterBM.not()).subtract(cloudsBM).subtract(shadowBM).eq(1); var NotForestBM = imgForest.select('notVegetationBM').subtract(waterBM.not()).subtract(cloudsBM).subtract(shadowBM).eq(1).multiply(2); var RiverBM = waterBM.not().subtract(cloudsBM).eq(1).multiply(3) var AllBM = ForestBM.add(NotForestBM).add(RiverBM).add(cloudsBM).add(shadowBM).rename('all').byte(); //Visualisation parameters for the export var visExport1 = { bands: ['all'], palette: ['89d4a4','e46346','5a7cba','876f9d','ffffff'], min: 1, max: 5, }; Map.addLayer(AllBM, visExport1, 'Export image 1',false); //Prepare export for data storage var ForestBMS = imgForest.select('vegetationBM').subtract(waterBM.not()).subtract(borderBM.not()).subtract(cloudsBM).subtract(shadowBM).eq(1); var NotForestBMS = imgForest.select('notVegetationBM').subtract(waterBM.not()).subtract(borderBM.not()).subtract(cloudsBM).subtract(shadowBM).eq(1).multiply(2); var AllBMS = ForestBMS.add(NotForestBMS).rename('all').byte(); //Visualisation parameters for the export var visExport2 = { bands: ['all'], palette: ['ffffff','89d4a4','e46346'], min: 0, max: 2, }; Map.addLayer(AllBMS, visExport2, 'Export image 2',false); /////////////////////////////////////////// //EXPORT IMAGES //Current date and time var datetime = new Date(); var datetime = datetime.getFullYear() + "" + ("0" + (datetime.getMonth() + 1)).slice(-2) + "" + ("0" + datetime.getDate()).slice(-2) + "_" + ("0" + datetime.getHours()).slice(-2) + "" + ("0" + datetime.getMinutes()).slice(-2); //Export Export.image.toDrive({ image: AllBM, description: nameExp+'_ExportVis_'+datetime, folder: 'GEEexportVIS', scale: 30, region: studySite, fileFormat: 'GeoTIFF'}); Export.image.toDrive({ image: AllBMS, description: nameExp+'_ExportSt_'+datetime, folder: 'GEEexportST', scale: 30, region: studySite, fileFormat: 'GeoTIFF'}); Export.image.toAsset({ image: AllBMS, description: nameExp+'_classifiedStorage', scale: 30, region: studySite}); ///////////////////// IF THE IMAGE IS CLEAR ////////////////////// } else { print('The image is clouds-free.') /////////////////////////////////////////// //LOAD ALL IMAGES AND CLIP ON STUDY SITE //Clip function var clipStudySite = function(image){ return image.clip(studySite)}; //Forest classification var imgForest = ee.Image('users/tatiana/'+ClassifForest) var imgForest = clipStudySite(imgForest); //Original image var imgS2 = ee.ImageCollection('COPERNICUS/S2') .filterDate(dateFrom,dateTo) .filterBounds(studySite); //Cloud percentage print('Images in image collection (must be 4):',imgS2.size()); var meanClouds = imgS2.aggregate_mean('CLOUDY_PIXEL_PERCENTAGE'); print('Mean cloud cover percentage: ',meanClouds); //Make the image collection to a single image and clip it var imgS2 = imgS2 .map(clipStudySite) .mean(); /////////////////////////////////////////////////// //GENERATING BITMAPS FOR VEGETATION //Where 1 = yes / 0 = other //Please note that the prediction map for the forest ist already a bitmap with 1 = not vegetation //Vegetation bitmap function var addBMVegetation = function(image) { var imagePred = image.select('notVegetationBM').not().rename('vegetationBM'); return image.addBands(imagePred); }; var imgForest = addBMVegetation(imgForest); /////////////////////////////////////////////////// //GENERATING ALL MASKS //Vegetation mask function (+ masked clouds and shadow) var maskVegetation = function(image) { var imagePred = image.select('vegetationBM'); var mask = imagePred.and(waterBMMask); var BitMask = imagePred.updateMask(mask).rename('vegetationMask'); return image.addBands(BitMask); }; //Not Vegetation mask function (+ masked clouds and shadow) var maskNotVegetation = function(image) { var imagePred = image.select('notVegetationBM'); var mask = imagePred.and(waterBMMask); var BitMask = imagePred.updateMask(mask).rename('notVegetationMask'); return image.addBands(BitMask); }; //Add the masks var imgForest = maskVegetation(imgForest); var imgForest = maskNotVegetation(imgForest); /////////////////////////////////////////////////// //Display parameters //true colors var visTrueColors = { bands: ['B4','B3','B2'], min: 200, max: 6200, gamma: 1.8 }; //Vegetation var visVeg = { bands: ['vegetationMask'], palette: '89d4a4' }; //Bare var visNotVeg = { bands: ['notVegetationMask'], palette: 'e46346' }; /////////////////////////////////////////////////// //LOAD MAPS Map.addLayer(imgS2, visTrueColors, 'True Colors', true); Map.addLayer(waterBMMaskDisp, {palette: '5a7cba'}, 'Water', true); Map.addLayer(imgForest,visVeg,'Vegetation', true); Map.addLayer(imgForest,visNotVeg,'Not vegetation', true); Map.addLayer(seteSetembro, {palette: '525252'}, 'Sete de Setembro', true); /////////////////////////////////////////////////// //PREPARE IMAGES FOR EXPORT //Export for visualisation var ForestBM = imgForest.select('vegetationBM').subtract(waterBM.not()).eq(1); var NotForestBM = imgForest.select('notVegetationBM').subtract(waterBM.not()).eq(1).multiply(2); var RiverBM = waterBM.not().multiply(3) var AllBM = ForestBM.add(NotForestBM).add(RiverBM).rename('all').byte(); //Visualisation parameters for the export var visExport1 = { bands: ['all'], palette: ['89d4a4','e46346','5a7cba'], min: 1, max: 3, }; Map.addLayer(AllBM, visExport1, 'Export image 1',false); //Export for data storage var ForestBMS = imgForest.select('vegetationBM').subtract(waterBM.not()).subtract(borderBM.not()).eq(1); var NotForestBMS = imgForest.select('notVegetationBM').subtract(waterBM.not()).subtract(borderBM.not()).eq(1).multiply(2); var AllBMS = ForestBMS.add(NotForestBMS).rename('all').byte(); //Visualisation parameters for the export var visExport2 = { bands: ['all'], palette: ['ffffff','89d4a4','e46346'], min: 0, max: 2, }; Map.addLayer(AllBMS, visExport2, 'Export image 2',false); /////////////////////////////////////////// //EXPORT IMAGES //Current date and time var datetime = new Date(); var datetime = datetime.getFullYear() + "" + ("0" + (datetime.getMonth() + 1)).slice(-2) + "" + ("0" + datetime.getDate()).slice(-2) + "_" + ("0" + datetime.getHours()).slice(-2) + "" + ("0" + datetime.getMinutes()).slice(-2); //Export Export.image.toDrive({ image: AllBM, description: nameExp+'_ExportVis_'+datetime, folder: 'GEEexportVIS', scale: 30, region: studySite, fileFormat: 'GeoTIFF'}); Export.image.toDrive({ image: AllBMS, description: nameExp+'_ExportSt_'+datetime, folder: 'GEEexportST', scale: 30, region: studySite, fileFormat: 'GeoTIFF'}); Export.image.toAsset({ image: AllBMS, description: nameExp+'_classifiedStorage', scale: 30, region: studySite}); }