0

I am attempting to calculate the area of water bodies in my classified map using landsat images in Google earth engine. from youtube I found a tutorial on GEE which showed me a method of calculating an area. However I struggled to apply it because I don't understand why my code is showing error.

Can someone guide me through how to calculate area, whether using the same method, or another?

Here's the link to my map:

https://code.earthengine.google.com/ff99d299831a9768fc70cd8fec15807b

the code is:

var l5 = ee.ImageCollection("LANDSAT/LT05/C01/T1_SR"),
    table = 
    /* color: #d63000 */
    /* displayProperties: [
      {
        "type": "rectangle"
      }
    ] */
    ee.Geometry.Polygon(
        [[[90.95591178825589, 23.73559234169589],
          [90.95591178825589, 23.528001478587907],
          [91.1265431969473, 23.528001478587907],
          [91.1265431969473, 23.73559234169589]]], null, false);

var cloudMaskL457 = function(image) {

  var qa = image.select('pixel_qa');


// If the cloud bit (5) is set and the cloud confidence (7) is high

// or the cloud shadow bit is set (3), then it's a bad pixel.

  var cloud = qa.bitwiseAnd(1 << 5)
                  .and(qa.bitwiseAnd(1 << 7))
                  .or(qa.bitwiseAnd(1 << 3));


// Remove edge pixels that don't occur in all bands
  var mask2 = image.mask().reduce(ee.Reducer.min());
  return image.updateMask(cloud.not()).updateMask(mask2);
};  


l5 = l5.filterDate('1989-09-01', '1990-03-30')
      .filterBounds(table)
      .filterMetadata('CLOUD_COVER_LAND', 'less_than', 10)
      .map(cloudMaskL457);


Map.centerObject(table, 12);
print('l1: ', l5);


var visParams = {bands: ['B3','B2', 'B1'], min: 600, max: 2500};


//['B3_median','B2_median', 'B1_median'], min: 600, max: 2500};

var visParamsFalse = {bands: ['B4','B3', 'B2'], min: 600, max: 2500};

var median = l5
//.reduce(ee.Reducer.median())
      .median()
      .clip(table);

print(median)

//Map.addLayer(median, visParams, 'median');
Map.addLayer(median, visParamsFalse, '1990False');




//Map.addLayer(awei1, {min: -1, max: 1, palette: ['FF0000', '0000FF']},'1990l1');

var awei2 = median.expression(
    'blue + 2.5 * green - 1.5 * (NIR + MIR) - 0.25 * SWIR', {
      'blue': median.select('B1'),
      'green': median.select('B2'),
      'NIR': median.select('B4'),
      'MIR': median.select('B5'),
      'SWIR': median.select('B7')
}).rename('awei2');


print(awei2)


Map.addLayer(awei2, {min: 1, max: 2, palette: ['FF0000', '0000FF']},'classified');

var area_pxa = awei2.multiply(ee.Image.pixelArea()) 
                    .reduceRegion(ee.Reducer.sum(),table,30,null,null,false,1e13)
                    .get('constant')

 area_pxa = ee.Number(area_pxa).divide(1e6)                 
 print ('Area using ee.Image.pixelArea (km²)', area_pxa)
2
  • 1
    I ran your script and it worked. // Area using ee.Image.pixelArea (km²). 158956.3631418291 // (but it is good practice to put the code in the question, as it makes it better for searches, and in most cases, people won't follow the link) Commented Aug 25, 2019 at 19:43
  • thank you ,Sir.I am extremely sorry for my mistake . I gave the wrong link there. Please, forgive me for the mistake.Now I have corrected it and If you could please help me , I will be ever grateful to you. Commented Aug 26, 2019 at 6:09

1 Answer 1

1

I believe you may have copied large portions of your code from the tutorial.

You unfortunately have kept some of the dictionary key calls. Modify the end of your script as such: change constant to awei2

var area_pxa = awei2.multiply(ee.Image.pixelArea()) 
                    .reduceRegion(ee.Reducer.sum(),table,30,null,null,false,1e13)
                    .get('awei2')

Your script should work as expected now.

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.