Skip to main content
1 of 3
Lana
  • 27
  • 6

How to calculate area based on a certain value range in google earth engine?

So i'm trying to calculate the area with certain color, which those colors are representing certain range of value.

here's the map

var indon = ee.FeatureCollection('users/sdwijati/IDN_adm2')
var Sesar = ee.FeatureCollection('users/sdwijati/Sesar_Jawa')
var nama_prov = ['Jawa Barat', 'Jakarta Raya', 'Jawa Tengah',
                  'Jawa Timur', 'Yogyakarta', 'Banten']
var prov = indon.filter(ee.Filter.inList('NAME_1', nama_prov));
var s = ee.Image("OpenLandMap/SOL/SOL_SAND-WFRACTION_USDA-3A1A1A_M/v02")
var savg = s.reduce(ee.Reducer.mean());
var gtsavg = savg.updateMask(savg.gte(45))

var n = ee.Image("OpenLandMap/SOL/SOL_WATERCONTENT-33KPA_USDA-4B1C_M/v01")
var navg = n.reduce(ee.Reducer.mean())

var xx = gtsavg.add(navg)

var sesar = Sesar
var bufferBy = function(size) {
  return function(feature) {
    return feature.buffer(size);   
  };
};

var intervals =
  '<RasterSymbolizer>' +
    '<ColorMap type="intervals" extended="false" >' +
      '<ColorMapEntry color="#0CE84A" quantity="45" label="40" />' +
      '<ColorMapEntry color="#FFF300" quantity="62.658" label="62.658" />' +
      '<ColorMapEntry color="#FF1500" quantity="80.316" label="80.316" />' +
      '<ColorMapEntry color="#000000" quantity="97.974" label="97.974" />' +
    '</ColorMap>' +
  '</RasterSymbolizer>';

var sarbuff = sesar.map(bufferBy(25000*4))
Map.addLayer(xx.sldStyle(intervals).clipToCollection(sarbuff).clip(prov));
Map.centerObject(prov, 7)

i'm trying to calculate the area of each color (each range of value), here's the code but it doesn't work, because the actual area of the island itself is about 138.000 km sq, and the result is way bigger than that. these lines are supposed to calculate the area with the color black (value 80.316-97.974)

var sgttinggi = xx.updateMask(xx.lte(97.974))
var sgttinggi2 = sgttinggi.updateMask(sgttinggi.gt(80.316))
var stinggi = sgttinggi.clipToCollection(sarbuff).clip(prov)

var area_pxa = stinggi.multiply(ee.Image.pixelArea()) 
                    .reduceRegion(ee.Reducer.sum(),prov,250,null,null,false,1e13)
                    .get('mean')
                   
 area_pxa = ee.Number(area_pxa).divide(100000)
  print ('Area Dengan Potensi Kerentanan Sangat Tinggi (km²)', area_pxa)
Lana
  • 27
  • 6