0

When using ee.reduceRegions() I try to estimate the mean values of copernicus landcover values within all features in a feature collection I get 'null' as a result for all reducers I have tried (mean, min, first, etc). However when I use ee.reduceRegion() (notice no 's') I get a result for the mean value for the entire feature collection. How do I get mean values for each feature? Here is a reproducible example:

// Load input imagery: Copernicus land cover.
var image = ee.Image('COPERNICUS/Landcover/100m/Proba-V-C3/Global/2019');

print(image)
// Load a FeatureCollection of counties in Maine.
var maineCounties = ee.FeatureCollection('TIGER/2016/Counties')
  .filter(ee.Filter.eq('STATEFP', '23'));
print(maineCounties)

var clip = image.clip(maineCounties);
Map.addLayer(clip, {}, "Tree Cover");

// Add reducer output to the Features in the collection.
var maineMeansFeatures = image.reduceRegions({
  collection: maineCounties,
  reducer: ee.Reducer.mean(),
  scale: 1000,
});

// Print the first feature, to illustrate the result.
print(ee.Feature(maineMeansFeatures.first()).select(image.bandNames()));


// Reduce the region. The region parameter is the Feature geometry.
var meanDictionary = image.reduceRegion({
  reducer: ee.Reducer.mean(),
  geometry: maineCounties.geometry(),
  scale: 1000,
  maxPixels: 1e9
});

print(meanDictionary)

2 Answers 2

0

You've hit a bug where the system essentially thinks the collection is empty for some purposes (filter propagation).

The workaround is, indeed, to .limit() the collection with some large number. (Anything that causes the collection to be completely manifested in memory works; that's the easiest one).

0

Specifying a CRS seems to solve the issue:

var maineMeansFeatures = image.reduceRegions({
  collection: maineCounties,
  reducer: ee.Reducer.mean(),
  scale: 1000,
  crs: "EPSG:5070",
});

print(maineMeansFeatures.aggregate_count_distinct("tree-coverfraction")); // 16

I'm not sure why that's necessary with reduceRegions but not reduceRegion. Even weirder, it looks like you can also solve the issue by limiting the size of maineCounties to its current size, which should have no effect:

var maineMeansFeatures = image.reduceRegions({
  collection: maineCounties.limit(maineCounties.size()),
  reducer: ee.Reducer.mean(),
  scale: 1000,
});

print(maineMeansFeatures.aggregate_count_distinct("tree-coverfraction")); // 16

I'm not quite sure what's going on, but you could consider opening a bug report.

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.