Google Earth Engine – How to Find Actual Value from a Band

google-earth-enginegoogle-earth-engine-javascript-api

I'm trying to find the Mean aboveground biomass density (MU) of a specific region from a dataset and print the value. I was able to print the area of the biomass but was not able to print the biomass itself.

Here is the code:

var districts = ee.FeatureCollection("FAO/GAUL_SIMPLIFIED_500m/2015/level2");
//var mang_2020 = ee.FeatureCollection("projects/ee-ak7221/assets/2020");
var district = districts.filter(ee.Filter.eq('ADM2_NAME','Cidade da Beira'));
//var mang_district = mang_2020.filterBounds(district.geometry())
var styleParams = {
  color: '00909F',
  width: 1.0,
};
//The Biomass dataset and clipping it
var l4b = ee.Image('LARSE/GEDI/GEDI04_B_002').select('MU')
var clipped = l4b.clip(district)
var area = clipped;
Map.addLayer(clipped,{min: 10, max: 250, palette: '440154,414387,2a788e,23a884,7ad151,fde725'},'Mean Biomass');
//Map.addLayer(mang_district, {color: 'blue'}, '2020-2022');

and the dataset is here: Dataset

Best Answer

An image has many pixels with their own distinct values. In order to get one value, you need to reduce those values of the image, combining them according to some operation into one value. This is done with ee.Image.reduceRegion. To get the mean of all the pixels in your region of interest:

print(
  l4b
    .reduceRegion({
      reducer: ee.Reducer.mean(),
      geometry: district,
    })
    .get('MU')
);

The result of reduceRegion() is a dictionary whose elements are named according to the bands of the image, so I followed it with .get('MU') to get the one value of interest (which is also the only value in that dictionary).

Note that I specified l4b and not clipped. There is no need to clip an image before passing it to reduceRegion() since reduceRegion() always works within the bounds of a geometry.

Related Question