I am a week 1 beginner at Google Earth Engine. I assumed that the subtract method would display the difference (the increased area) between my two layers: afterMap and beforeMap, but it does not.

var region = ee.Geometry.Polygon(
    [[[-95.2274658203125, 56.30841555552546],

        [-95.07228393554688, 56.37083168879506],
          [-95.47191162109375, 56.38756011275328],
          [-95.57971496582032, 56.303082617975605]]]);
var sentinel = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED");


var beforeSentinel = sentinel
                  .filterDate('2019-01-01', '2019-10-31');
var afterSentinel = sentinel
                  .filterDate('2022-06-01', '2023-06-05');
var beforeArray = waterArea(beforeSentinel, region);
var beforeArea = beforeArray[0];
var beforeMap = beforeArray[1];

var afterArray = waterArea(afterSentinel, region);
var afterArea = afterArray[0];
var afterMap = afterArray[1];

var totalArea = region.area().divide(1e6);

Map.addLayer(beforeMap, {palette:['blue']}, 'NDWI Mask: before');
Map.addLayer(afterMap, {palette:['blue']}, 'NDWI Mask: after');

//this is the code that does not work, does not display the difference between the 2 areas
var increaseMap = afterMap.subtract(beforeMap);

// Display the increase map on the map
Map.addLayer(increaseMap, {palette: '00ff00'}, 'Area Increase');

function waterArea(Imagery, region)

var sentinelImage = 
    Imagery.sort('SNOW_ICE_PERCENTAGE', false)
// print("Sentinel image taken at = ", sentinelImage.date()); - to see both dates of images taken
    {min: 0.0, max: 3000, bands: ['B4', 'B3', 'B2']},
    'Sentinel Imagery');
    var ndwi =
    sentinelImage.normalizedDifference(['B3', 'B8']).rename('NDWI');
    {palette: ['red', 'yellow', 'green', 'cyan', 'blue']},
    var ndwiThreshold = ndwi.gte(0.0);
var ndwiMask = ndwiThreshold.updateMask(ndwiThreshold);
//Map.addLayer(ndwiMask, {palette:['blue']}, 'NDWI Mask');

var pixelArea =  ee.Image.pixelArea().divide(1e6); // convert to square km
var area = ndwiMask.multiply(pixelArea)
  reducer: ee.Reducer.sum(), //area of each pixel * amount of pixels
  geometry: region,
  scale:10 // meters 
return([area, ndwiMask]);

1 Answer 1


I'm not clear on what you're trying to do.

var increaseMap = afterMap.subtract(beforeMap);

beforeMap and afterMap are either masked out or they have a value of 1. When a pixel is masked in either of these images, the difference between them will also be masked. This means, increaseMap will have a value of 0 for all pixels where neither beforeMap or afterMap is masked. Otherwise, it will be masked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.