0

I have a dataset where I have calculated NDVI and added it as a band in each image. I have then masked those images leaving just pixels that meet my NDVI threshold. I have then added the total number of pixels left as a property for each image.

From here I want to extract those values and add them to a list that can be exported or turned into a chart.

Here is the link to my current code and the shapefile that I'm using to define my ROI: https://code.earthengine.google.com/a462d9b8883925db866451977ac1e1fb https://code.earthengine.google.com/?asset=projects/ee-agill6/assets/WDPA_WDOECM_Oct2023_Public_555573540_shp-polygons

This is the code used to calculate the pixel count:

// add pixel count as a property to the image
var calculatePixelCount = function(image) {
  var pixelCount =image.reduceRegion({
    reducer: ee.Reducer.count(),
    geometry: smallAlps.geometry(),
    scale: 30, //  adjust the scale to match the image resolution
        maxPixels: 1e9,
  });
  return image.set('NDVI_pixel_count', pixelCount.get('NDVI')); 
};

1 Answer 1

0

I can't seem to run your analysis because I do not have access to most of the assets you are using, however there is a very straightforward way of turning the properties of any object into an array. My suggestion would be to return a feature collection instead of an image collection using the following syntax (you may need to explicitly cast your output by wrapping your output in ee.FeatureCollection()):

return ee.Feature(null).set({
  image: image,
  NDVI_pixel_count: pixelCount.get('NDVI')
})

Then you can easily join your pixel counts into an array using:

var pixelCountArray = outputFeatureCollection.aggregate_array('NDVI_pixel_count')

This format can then be exported or used in a chart using ui.Chart.array.values.

1
  • 1
    You don't need to convert to features; image are features, and you can just call aggregate_array on the image collection. Commented Nov 9, 2023 at 19:47

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.