I have a code that suppose to count number of pixels in a polygon and to add it to the polygon as a dictionary. First NDVI calculted and then differrent ranges inside the polygon should be calculte: the percent of NDVI pixels that their value is higher than 0.2, between 0.2-0.4, between 0.4-0.6 ect.... My problem is that my test polygon doesn't have all the values inside it and for that reason I get the next error message:
Collection.toList: The value of 'count' must be positive. Got: 0
How can I tell my code to still work/ ignor when he doesn't find match pixels?
This is link to my code: https://code.earthengine.google.com/ff24c08f6d3acd50a8108ca656da4b9f
This is the part where I create the ranges:
var stats = listOfImages.map(function(img){
var image = ee.Image(img).select('NDVI_mean');
var HighVeg = image.gt(0.6).selfMask().reduceRegion({
reducer: ee.Reducer.count(),
geometry: geometry,
crs: 'EPSG:4326',
//maxPixels:1310361348,
scale: 20,
}).values().get(0);
var LowModerate = image.gt(0.2).and(image.lt(0.4)).selfMask().reduceRegion({
reducer: ee.Reducer.count(),
geometry: geometry,
crs: 'EPSG:4326',
//maxPixels:1310361348,
scale: 20,
}).values().get(0);
var ModHigh = image.gt(0.4).and(image.lt(0.6)).selfMask().reduceRegion({
reducer: ee.Reducer.count(),
geometry: geometry,
crs: 'EPSG:4326',
//maxPixels:1310361348,
scale: 20,
}).values().get(0);
var low = image.lt(0.2).selfMask().reduceRegion({
reducer: ee.Reducer.count(),
geometry: geometry,
crs: 'EPSG:4326',
//maxPixels:1310361348,
scale: 20,
}).values().get(0);
var total = image.gt(-1).selfMask().reduceRegion({
reducer: ee.Reducer.count(),
geometry: geometry,
crs: 'EPSG:4326',
//maxPixels:1310361348,
scale: 20,
}).values().get(0);
return image.set('HighVeg', HighVeg).set('LowModerate', LowModerate).set('ModHigh', ModHigh).set('low', low).set('total', total);
});