After receiving some advice from the group, I am trying to recalculate the water occurrence percentage using Landsat. To achieve this, I extracted the water mask using NDWI for the entire collection and computed the statistics using the reducer.mean() for the NDWI_mask, which I then multiplied by 100%. However, I am experiencing an issue where all the pixel values are displaying as 100 instead of a range between 0 and 100. Does anyone have any idea what might be causing this problem?

Here is the code link: https://code.earthengine.google.com/9cf6dabc0b79caacb8c208d6fd00e6a5

var roi = ee.FeatureCollection("FAO/GAUL_SIMPLIFIED_500m/2015/level0").filter(ee.Filter.eq('ADM0_NAME', 'Pakistan'));
Map.addLayer(roi, {color: 'black'}, 'Study Area',1);
Map.centerObject(roi, 8);

// Landsat5
var cloudMaskC2L5 = function(image) {
  var dilatedCloud = (1 << 1)
  var cloud = (1 << 3)
  var cloudShadow = (1 << 4)
  var qa = image.select('QA_PIXEL');
  var mask = qa.bitwiseAnd(dilatedCloud)
  // var mask = qa.bitwiseAnd(dilatedCloud).eq(0).and(
  //   qa.bitwiseAnd(cloud).eq(0)).and(qa.bitwiseAnd(cloudShadow).eq(0))
  var mask2 = image.mask().reduce(ee.Reducer.min());
  return image.updateMask(mask.not()).updateMask(mask2);
var datasetl5 = ee.ImageCollection('LANDSAT/LT05/C02/T1_L2')
    .filterDate('1984-04-01', '2012-05-01')
// Applies scaling factors.
function applyScaleFactors(image) {
  var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2);
  var thermalBand = image.select('ST_B6').multiply(0.00341802).add(149.0);
  return image.addBands(opticalBands, null, true)
              .addBands(thermalBand, null, true);
datasetl5 = datasetl5.map(applyScaleFactors)
// landsat 8
function maskL8sr(image) {

  var qaMask = image.select('QA_PIXEL').bitwiseAnd(parseInt('11111', 2)).eq(0);
  var saturationMask = image.select('QA_RADSAT').eq(0);

  // Apply the scaling factors to the appropriate bands.
  var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2);
  var thermalBands = image.select('ST_B.*').multiply(0.00341802).add(149.0);

  // Replace the original bands with the scaled ones and apply the masks.
  return image.addBands(opticalBands, null, true)
      .addBands(thermalBands, null, true)
var datasetl8 = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
    .filterDate('2013-05-01', '2022-12-31')


// add NDWI
function addNdwil5(img) {
var ndwi = img.normalizedDifference(['SR_B2', 'SR_B4']).rename('NDWI');
return img.addBands(ndwi);

function addNdwil8(img) {
var ndwi = img.normalizedDifference(['SR_B3', 'SR_B5']).rename('NDWI');
return img.addBands(ndwi);
// Function to mask out NDWI
  var addwaterMaskl5 = function(image) {
   var NDWI = image.select(['NDWI']);
    return image.addBands(ee.Image(1).updateMask(NDWI.gte(0.4)).rename('NDWI_mask'));
  var addwaterMaskl8 = function(image) {
   var NDWI = image.select(['NDWI']);
    return image.addBands(ee.Image(1).updateMask(NDWI.gte(0.1)).rename('NDWI_mask'));
// Add NDWI to the collection
var l5ndwi = datasetl5.select(['SR_B2', 'SR_B4'])

var l8ndwi = datasetl8
.select(['SR_B3', 'SR_B5'])

// Water occurrence
var sum = l8ndwi.merge(l5ndwi)
var water_sum = sum.select('NDWI_mask').reduce(ee.Reducer.mean());
var freq = water_sum.multiply(100);
var visualization_waterfreq = {
  bands: ['NDWI_mask_mean'],
  min: 0.0,
  max: 100.0,
  palette: ['darkblue','lightblue','yellow','red']

var snow_dataset = ee.FeatureCollection('GLIMS/20210914');
var visParams = {
  palette: ['gray', 'cyan', 'blue'],
  min: 0.0,
  max: 10.0,
  opacity: 0.8,
/var snow_mask = ee.Image().float().paint(snow_dataset, 'area');

Map.addLayer(snow_mask.clip(roi), visParams, 'GLIMS/20210914');
var water_mask = freq.where(snow_mask,0);
var water = water_mask.updateMask(water_mask);
      // Mask out areas with more than 5 percent slope using a Digital Elevation Model 
      var DEM = ee.Image('WWF/HydroSHEDS/03VFDEM');
      var terrain = ee.Algorithms.Terrain(DEM);
      var slope = terrain.select('slope');
      var waterfinal = water.updateMask(slope.lt(5));
      var connections = waterfinal.int16().connectedPixelCount(3); 
      var wom = waterfinal.updateMask(connections.gte(3));
Map.addLayer(wom.clip(roi), visualization_waterfreq, 'Occurrence');

1 Answer 1


Here's your problem:

return image.addBands(ee.Image(1).updateMask(NDWI.gte(0.1)).rename('NDWI_mask'));

The resulting image is 1 or the pixel is masked, so your mean is 1. The below snippet would instead give you 1 for water and 0 for no water, and mean will give you the fraction of observations that have water:

return image.addBands(NDWI.gte(0.1).rename('NDWI_mask'))

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.