2

i'm pretty new on GEE, so i hope someone can help me with this. As the title say, i'm looking for a way to calculate how many pixels i have inside a polygon. I tried to do it with reduceRegion and ee.Reducer.count() but this always give me error trying to do it with my collection.

I also tried to do it with ui.Chart.image.series using the same reducer. The result is a graph showing a constant number, which represent the number of pixels on the polygon, but it looks like most of the times is wrong because i checked the number of pixels manually, and both numbers doesn't match.

Here's the code. It's used to calculate the annual NDVI of a certain region, which is selected by the geometry (which is drawn with the map menu). Originally i used it to calculate the mean annual NDVI of the region (that's why i select the NDVI on the second line) changing the Reducer.count by Reducer.mean, but now i want to use it to know how many pixels are inside that polygon:

var dataset = ee.ImageCollection('MODIS/006/MOD13A1').filterDate('2019-1-1', '2019-12-31');
var ndvi = dataset.select('NDVI');

var chart = ui.Chart.image.series(ndvi, geometry,  ee.Reducer.count(), 500, 'system:time_start').setOptions({
      title: 'Annual NDVI',
      vAxis: {title: 'NDVI'},
});

print(chart);

On the other hand, i tried to use this, but it gives error, saying that "dataset.reduceRegion is not a function":

var c = dataset.reduceRegion({
  reducer: ee.Reducer.count(),
  geometry: geometry,
  scale: 500,
});
print(c);

1 Answer 1

2

"dataset.reduceRegion is not a function"

dataset is an ImageCollection. reduceRegion is only for images. If you want to count the pixel area that's covered by at least one of the images, you can use dataset.mosaic().reduceRegion(...) to first combine the images in the collection into one image which will have a pixel anywhere any of the input images does.

The reason you can't do this without an extra operation is because Earth Engine doesn't know that you don't want to, for example, count the area in each image separately.

1
  • I see, so i need to use a single image of my product, or simply use that code you shared to me. Thanks! I ll try it. Commented Sep 15, 2020 at 22:31

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.