0

I am new to earth engine and am struggling with a very simple task. I want to calculate the average NDVI for the counties of Maine for the month of January 2018. I manage to do so but, before exporting it to .csv, I wanted to add a property to each of the sub-features (counties) with the date ('2018-01-01') of the NDVI.

I try it with the .set() and setMulti functions but do not manage to achieve what I want. In particular, these functions add a property to the "overall" feature collection, and not to each sub-feature of the former. When I do that and export it as a .csv, the information is lost.

In short: I would like to add a property {date: '2018-01-01'} to each of the sub-features. Namely that the property in figure 1 is actually inside the properties in figure 2. I am attaching the code after the images.

Figure 1:

Figure 1

Figure 2:

Figure 2

Code:

// Load a collection of US counties with census data properties.
var maine = ee.FeatureCollection('ft:1S4EB6319wWW2sWQDPhDvmSBIVrD3iEmCLYB7nMM');

// let us use only counties from Maine:
maine = maine.filter(ee.Filter.eq('StateName', 'Maine'));

// loading recent NDVI data:
var dataset = ee.ImageCollection('MODIS/006/MOD13Q1')
                  .filter(ee.Filter.date('2018-01-01', '2018-01-31'));
var ndvi = dataset.select('NDVI');
ndvi = ndvi.mean();  // monthly mean -- need to average out as there are 2 images in this time period

// clipping into my AOI:
var ndvi_maine = ndvi.clip(maine);

// Average NDVI per county:
var means = ndvi_maine.reduceRegions({
  collection: maine,
  reducer: ee.Reducer.mean(),
  scale: 30,
});

// selecting only desired columns and transforming it into a feature object:
means = ee.Feature(means.select(['CntyFips', 'mean']));

means = means.setMulti({date: '2018-01-01'});   // trying to add properties

print(means);
1

You have to map a function over the collection:

// Load a collection of US counties with census data properties.
var maine = ee.FeatureCollection('ft:1S4EB6319wWW2sWQDPhDvmSBIVrD3iEmCLYB7nMM');

// let us use only counties from Maine:
maine = maine.filter(ee.Filter.eq('StateName', 'Maine'));

// loading recent NDVI data:
var dataset = ee.ImageCollection('MODIS/006/MOD13Q1')
                  .filter(ee.Filter.date('2018-01-01', '2018-01-31'));
var ndvi = dataset.select('NDVI');
ndvi = ndvi.mean();  // monthly mean -- need to average out as there are 2 images in this time period

// clipping into my AOI:
var ndvi_maine = ndvi.clip(maine);

// Average NDVI per county:
var means = ndvi_maine.reduceRegions({
  collection: maine,
  reducer: ee.Reducer.mean(),
  scale: 30,
});

means = means.map(function(ft){
  return ee.Feature(ft).set('date', '2018-01-01')
})

print(means)

https://code.earthengine.google.co.in/b5a3f45d65c6f102960be2dc2252c0bd

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.