1

In the code below (from here), I want to export countiesClimate to Drive as a shapefile. To do this I first use flatten() to convert a feature collection of feature collections to a feature collection of features. But when I do this the 'date' property that I assign in the function disappears. Instead, the columns of the feature collections in countiesClimate become the properties of the features in countiesClimate.flatten().

So my question is: how do I make the properties countiesClimate carry over to countiesClimate.flatten()? Or, how do I create a new column in countiesClimate? (so the data gets carried over to countiesClimate.flatten())

var counties = ee.FeatureCollection('TIGER/2018/Counties')
                   .filter('STATEFP == "35"');

var climate = ee.ImageCollection('IDAHO_EPSCOR/TERRACLIMATE')
                  .filterDate('2020-01', '2020-04');

var countiesClimate = climate.map(function(image) {
  return image.reduceRegions({
    collection: counties,
    reducer: ee.Reducer.mean(),
    scale: 5000,
    crs: 'EPSG:4326'
  })
  // ***this is the new line of code which is saved as a property, not a column, and is not carried over to countiesClimate.flatten(): ***
  .set('date', image.get('system:time_end'));
});

print('FeatureCollection of FeatureCollections', countiesClimate);

// What I want is for the output of flatten() to include the 'date' property that I assign in the above function
print('Flattened FeatureCollection of FeatureCollections',
      countiesClimate.flatten());

1 Answer 1

1

Map a function over the results of the reduceRegions call to set the date on each feature, instead of on the collection wrapper.

var countiesClimate = climate.map(function(image) {
  return image.reduceRegions({
    collection: counties,
    reducer: ee.Reducer.mean(),
    scale: 5000,
    crs: 'EPSG:4326'
  }).map(function(f) {
      return f.set('date', image.get('system:time_end'))
  })
})
1
  • That did it - thank you!
    – Alex M
    Aug 11, 2022 at 18:38

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.