0

I am trying to obtain a count of how many pixels (data points, or measurements) there are in the MODIS dataset (chlor_a band) in a specific region in a period of several months.

The problem is that when I try to use image.reduceRegion() with no scale argument, Earth Engine complains that The default WGS84 projection is invalid for aggregations. Specify a scale or crs & crs_transform.

So I specify a scale but for some reason this drastically affects the output when the reducer is sum(). When computing other statistics (mean, median etc) the difference is minimal; but here it is giving me incorrect results!

When I add the image and the geometry as a map layer and I manually sum the pixels (using inspector), I get 815. This is the value I want (or a value very close to that, accounting for partially covered pixels).

This is the code I have (broken):

print(image.reduceRegion({
  geometry: point.buffer(20000),
  scale: 500,
  reducer: ee.Reducer.sum()
}));
// result: 61015.317...

print(image.reduceRegion({
  geometry: point.buffer(20000),
  scale: 5000,
  reducer: ee.Reducer.sum()
}));
// result: 583.247...

Map.addLayer(image);
Map.addLayer(point.buffer(20000));

Link to EarthEngine code

As you can see, setting the scale to 500 returns an enormous number, which is incorrect. A scale of around 5000 returns a result closer to what I can manually count in the map, but still incorrect.

How can I obtain a true and correct count?

1 Answer 1

0

The scale argument indicates the pixel resolution at which the reduction will be applied. Playing with the scale value can be thought of as applying a resampling process to the image. Thus, by using different scale values it is expected that the raster's values will change. In addition, measures of central tendency such as mean and median will tend to vary less than results obtained from a sum, as the first ones come from a data distribution, while a simple sum, does not. Therefore, using a very large scale value will decrease the number of counted pixels, which will decrease the value of the final sum; while, decreasing the scale argument will do the opposite.

On the other hand, the reduceRegion function counts differently pixels that are found completely inside the region of interest (roi), than pixels that are partially included in this roi. This answer further explains this: https://gis.stackexchange.com/a/388521/156904. This helps explain the difference in the final sum obtained by GEE and the one you made manually, as GEE sums partially the values of pixels not completely inside the roi, while you probably took into account the complete count values for every pixel that was touched by the roi.

4
  • Thank you for your answer! I understand why scale changes the result, but how can I choose the "correct" value---i.e. the value that will count each pixel once? Also thank you for the link to the other question, looks like sum().unweighted() will solve the problem of fractional result.
    – giusb
    Jul 1, 2021 at 19:26
  • I found the "correct" value after a bit of trial and error. A value of scale: 4500 gives me the same result I get by counting manually from the map preview. But how could I have found this value without trial and error? I am guessing it is the dimension in meters of an individual pixel... how could I have found it?
    – giusb
    Jul 1, 2021 at 19:40
  • You should use the native resolution of the image indicated in the dataset info. In this case it's 4616 meters. Jul 1, 2021 at 19:40
  • Two things: Jonathan is almost right. You shouldn't use the native resolution, you should use the entire native projection. proj = modis.select('chlor_a').first().projection(); ... image.reduceRegion({... crs: proj}). Also, if you're counting pixels, you're probably already doing something fishy. Perhaps you're trying to get a total area? In which case, use pixelArea: developers.google.com/earth-engine/tutorials/… Jul 2, 2021 at 11:42

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.