TransWikia.com

The ee.Reducer.toList() reducer does not return the same number of features as the feature collection

Geographic Information Systems Asked by Iago Mendes de Oliveira on January 18, 2021

I have a feature collection (samplePlots) with 2755 features. When I use the code below, I expect a list with 2755 elements, but the result is a list with only 2515 elements. What would be the reason? Unfortunately I can’t share the feature collection.

image.reduceRegion({
  reducer: ee.Reducer.toList(),
  geometry: samplePlots,
  scale: 30
})

One Answer

You are using reduceRegion, which does not do anything in particular about feature collections; it takes a geometry. Or, it can also accept a feature and use the feature's geometry. A feature collection is considered a kind of feature, and its geometry is the union of the geometries of its features.

So, you're asking reduceRegion to find every pixel in the image which is intersected by your collection (which I assume is made of points). This process will:

  • report a pixel only once if more than one point are inside its bounds, and
  • omit all fully masked pixels.

Instead, you should use reduceRegions, which actually takes a feature collection, and returns a feature collection, having the same features but with added properties corresponding to the reducer outputs. This will be guaranteed to have the same number of outputs as inputs. (The reducer can be almost anything, in this case, since it always gets at most one input when the feature is a point; ee.Reducer.median() might be a good choice, for example.)

Then, if you specifically need a ee.List instead of a feature collection, you can use .reduceColumns(ee.Reducer.toList(), ['name_of_band_goes_here']) to get that out of the feature collection. But, working with feature collections as much as possible will give you better performance when the number of features is large.

Correct answer by Kevin Reid on January 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP