Google Earth Engine – Get Output from Reducer (Frequency Histogram) for Polygons

google-earth-enginegoogle-earth-engine-javascript-apireducerszonal statistics

Context:
I'm trying to calculate forest cover for each districts in a province. I used the reducer function (frequency histogram) on a raster, using a feature collection object as its "cookie cutter".

Problem:
The function works perfectly fine, but it spits out the output for the whole feature collection, and does not disaggregate it by features (the number is for the whole province, but what I need is the breakdown for each districts).

Here's the code that I used. Is there any way to use the reducer function and make the output disaggregated?

// IMPORT DATASETS
var aceh = ee.FeatureCollection("users/putraditama/AdministrativeBoundaries/Prov_Aceh") // Aceh boundary from asset
var districts = ee.FeatureCollection("users/putraditama/AdministrativeBoundaries/idn_adm2_2020");
var margono = ee.Image("users/putraditama/Margono_resized_reproject");

var acehdistricts = (districts)
            .filter(ee.Filter.eq("province","Aceh"));
            
// TEST ZONAL 
var acehstats = margono.reduceRegion({
  reducer: ee.Reducer.frequencyHistogram(),
  geometry: acehdistricts,
  // scale: 45,
  maxPixels: 1e15
});

print(acehstats)
Map.addLayer(acehdistricts, {}, 'Aceh')

Best Answer

This is simple: use reduceRegions instead of reduceRegion.

var acehstats = margono.reduceRegion({
  reducer: ee.Reducer.frequencyHistogram(),
  collection: acehdistricts,
});

The result will be a FeatureCollection where each feature has the reducer's outputs added as properties.

Related Question