0

I'm currently using the code below to attempt to download a mean monthly NDVI value for different zipcodes in a state, and I am looking at a period of 4 years (so I would want around ~50 mean values for each zipcode).

When I run the code below however, my csv only has each zipcode featured once, so I'm assuming that the mean NDVI value isn't calculated per month. Additionally, the systemindex column, which I would want to give me information about the date & month, has values like "0000000000000000000c_0000000000000000000c". How could I change my code?

var shp = table

// Load the Landsat 8 Collection 1 Tier 1 32-Day NDVI Composite
var ndvi = ee.ImageCollection('LANDSAT/LC08/C01/T1_32DAY_NDVI')
              .filterDate('2015-12-01', '2020-01-31')
              .select('NDVI')
              ;


var newNDVI = ndvi.map(function(image) {
  return image.updateMask(image.select('NDVI').gt(0));
});

// Split the FeatureCollection into subsets
var subsets = shp.limit(5000, 'system:index');

// Calculate the mean NDVI for each subset of features
var ndviByFeature = subsets.map(function(subset) {
  var mean = newNDVI.mean().reduceRegions({
    collection: subset,
    reducer: ee.Reducer.mean(),
    scale: 30
  });
  return mean.set('date', ee.Date(ndvi.first().get('system:time_start')).format('YYYY-MM-dd'));
}).flatten();

// Print the resulting table
print('Mean NDVI by feature:', ndviByFeature);

// Export the table to your Google Drive
Export.table.toDrive({
  collection: ndviByFeature,
  description: 'mean_ndvi_by_feature',
  fileFormat: 'CSV'
});
'''

1 Answer 1

0

reduceRegions() calculates the mean for each zip code. You are, however, also calculating the mean across all months with this:

newNDVI.mean()

If you want mean NDVI per zip-code per month, you can do something like this:

var meanNDVI = ndvi.map(function(image) {
  return image
    .updateMask(image.select('NDVI').gt(0))
    .reduceRegions({
      collection: subsets,
      reducer: ee.Reducer.mean(),
      scale: 30
    })
    .map(function (feature) {
      return feature.set('date', image.date().format('yyyy-MM'))
    })
}).flatten()
print(meanNDVI)

https://code.earthengine.google.com/c6272afd1ef6e130d01d4db27aad32d6

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.