2

I calculated the area of my river basin twice and got different results and do not know why/what exactly happens.

At first, I just wanted to have the whole area of my river basin, which I did by:

//calculating area whole basin
var areabasin = ee.Image.pixelArea().divide(1000000).reduceRegion({
  reducer: ee.Reducer.sum(),
  geometry: shape_basin,
  scale: 120,
  maxPixels: 1e13
});
print('Area of the whole basin: ', areabasin);

shape_basin is a shapefile I uploaded as an asset that covers the area of interest. As a result, I got 580,453, which I assume must be in km2 as ee.Image.pixelArea is calculating it in m2 and I divided it by 1.000.000 Later, I classified this area and calculated the area of the individual classes. This I did by:

// calculating the areas by class

var classes = result.select('classband');
var area = ee.Image.pixelArea().divide(1000000);

//water
var waterMask = result.updateMask(classes.eq(1));
var waterarea = waterMask.multiply(area).select('classband').rename('water');

//crops
var cropMask = result.updateMask(classes.eq(2));
var croparea = cropMask.multiply(area).select('classband').rename('cropland');

//urban
var urbanMask = result.updateMask(classes.eq(3));
var urbanarea = urbanMask.multiply(area).select('classband').rename('urban');
Map.addLayer(urbanMask, ['FFFFFF'],'urbanonly');

//bare
var bareMask = result.updateMask(classes.eq(4));
var barearea = bareMask.multiply(area).select('classband').rename('bareland');

//forest
var forestMask = result.updateMask(classes.eq(5));
var forestarea = forestMask.multiply(area).select('classband').rename('forest');

//calculating the area of the waterclasses

var area_image = waterarea.addBands(croparea)
                     .addBands(urbanarea)
                     .addBands(barearea)
                     .addBands(forestarea);

var areas = area_image.reduceRegion({
  reducer:ee.Reducer.sum(),
  geometry: shape_basin,
  scale: 120,
  maxPixels:1e13
});
print(areas);

As a result for the five classes, I get:

bareland: 769703.0088548877
cropland: 421111.16084957926
forest:   505036.9907116392
urban:    163561.49438641773
water:     21118.550734309763

Which results when added up in 1,880,531, or multiplied by 120 (scale), it results in 225,663,745.

Any ideas why the difference is so big? How could I change this, what am I doing wrong?

Here the link to the full script: https://code.earthengine.google.com/59afd4acda14d6b99effac96bf620015

2

The problem you're having is that you're multiplying pixelArea * class. so, for instance, barearea is off by a factor of 4 since it's class 4 (you probably wanted to multiply the mask by area, not mask the image and multiply that).

var waterMask = classes.eq(1);
var waterarea = waterMask.multiply(area).select('classband').rename('water');
Map.addLayer(waterMask, ['FFFFFF'], 'wateronly');

But instead of splitting out each class to mask it and then combining them together again, just use a grouped reducer.

var areas = area.addBands(classes).reduceRegion({
  reducer:ee.Reducer.sum().group(1),
  geometry: shape_basin,
  scale: 120,
  maxPixels:1e13
});

Also note that some of the pixels in your region have no value in either the input composite or in the classified image, so your sum of class areas might be slightly less than the sum of area over the entire region.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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