1

I want to mask water bodies with two different conditions and then add the two indexed layers together to create one layer with different values of 0 (no water), 1 (water: based on index 1), 2 (water: based on index2) and 3 (water: based on both index 1 and 2). here is the code, I don't get any error but the final water band has pixel values of 3 for all the pixels. I can't figure out where the problem is. What am I doing wrong?

s2 = s2.filterBounds(region)
     .filterDate('2021-01-16', '2021-01-18')
     .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 35));


function mNDWI(img){
    var img = ee.Image(img).divide(10000)
    var mndwi = img.normalizedDifference(['B3', 'B11']).rename('mNDWI')
                .copyProperties(img,['system:time_start'])
    return img.addBands(mndwi)
}

function Fisher(img){
    var img = ee.Image(img).divide(10000)
    var fisher = img.expression('1.7204 + 171 * Green + 3 * Red - 70 * NIR - 45 * SWIR1 - 71 * SWIR2',
                          {'Green':img.select('B3'),'Red':img.select('B4'), 
                           'NIR':img.select('B8'),'SWIR1':img.select('B11'),
                           'SWIR2':img.select('B12')}).rename('Fisher').copyProperties(img, ['system:time_start'])
    return img.addBands(fisher) 
}


var ic_in= s2.map(mNDWI).map(Fisher)

function water_thresholding(img){
    var img = ee.Image(img)
    var rvrs = img.select('mNDWI').gt(-0.3).rename('rivers')
                                          
    var fi_index = img.select('Fisher').gt(0.63)
    var mndwi_index = img.select('mNDWI').gt(0)
    var otrs = (fi_index.or(mndwi_index)).remap([1, 0], [2, 0], null).rename('others')
    
    var water = (rvrs.add(otrs)).rename('Water')       
    return img.addBands(water)
}

var s2_ic_msk = ic_in.map(water_thresholding)

print(s2_ic_msk.first())

Map.addLayer(s2_ic_msk.select('Water'), {}, 'Water' )
4
  • 1
    In both functions (mNDWI and Fisher) you are dividing by 10,000. Therfeore you end up with the original data divided by 100,000,000, Did you take this into account?
    – HMSP
    Jul 12, 2022 at 12:50
  • @Vince :based on DEA, coefficient-based indices produce different results if surface reflectance values are not scaled between 0.0 and 1.0 prior to calculating the index. I am scaling values to a 0.0-1.0 range by dividing by 10000.0.
    – Sarash
    Jul 12, 2022 at 23:33
  • Hi @SaraShaeri, it'd be great if you could share "projects/ee-sshaeri63/assets/Gwydir" publicly, so we can run the code as is. Meanwhile I'll make up a table to test the code. Jul 13, 2022 at 10:55
  • @HMSP is correct, and that is the issue. You have to scale the values once, then use the scaled values for the rest of the computations. Just remove .divide(10000) in line 14 and you'll see that it works. Jul 13, 2022 at 11:02

1 Answer 1

1

Your issue is produced because you are dividing by 100,000,000 instead 10,000; as it was well pointed out in comments. One of different ways for avoiding that is combining all functions in only one as follows.

var s2 = ee.ImageCollection("COPERNICUS/S2_SR");
var region = ee.FeatureCollection("projects/ee-sshaeri63/assets/Gwydir");

s2 = s2.filterBounds(region)
     .filterDate('2021-01-16', '2021-01-18')
     .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 35));


var s2_ic_msk = s2.map(function (img){

  img = ee.Image(img).divide(10000);
  var mndwi = img.normalizedDifference(['B3', 'B11'])
    .rename('mNDWI')
    .copyProperties(img, ['system:time_start']);

  var fisher = img.expression('1.7204 + 171 * Green + 3 * Red - 70 * NIR - 45 * SWIR1 - 71 * SWIR2',
                           {'Green':img.select('B3'),'Red':img.select('B4'), 
                            'NIR':img.select('B8'),'SWIR1':img.select('B11'),
                            'SWIR2':img.select('B12')})
                            .rename('Fisher')
                            .copyProperties(img, ['system:time_start']);
  
  var rvrs = ee.Image(mndwi).gt(-0.3).rename('rivers');
                     
  //var wtls = img.select('TCW').gt(-0.035).remap([1, 0], [2, 0], None).rename('wetlands')
                        
  var fi_index = ee.Image(fisher).gt(0.63);
  var mndwi_index = ee.Image(mndwi).gt(0);
  var otrs = (fi_index.or(mndwi_index)).remap([1, 0], [2, 0], null).rename('others');
    
  var water = (rvrs.add(otrs)).rename('Water');  
  
  return img.addBands([mndwi, fisher, water]);
  
});

var mean = s2_ic_msk.mean().clip(region);

var imageVisParam = {"opacity":1,
                     "bands":["Water"],
                     "palette":["ff8a23","ff2710","1814ff"]};

Map.addLayer(region);
Map.addLayer(mean.select('Water'), imageVisParam, 'Water' );
Map.centerObject(region, 9);

Result is the same with the previous three functions (corroborated by me) and, it can be observed in following picture.

enter image description here

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.