Populate FeatureCollection with values from bands of each individual image in an image collection in Google Earth Engine

孤街醉人 提交于 2020-01-24 17:33:50

问题


In Google Earth Engine, I have loaded in a Featurecollection as a JSON which contains a few polygons. I would like to add columns to this FeatureCollection which gives me the mean values of two bands for each polygon and from each of the multiple images contained within the Image Collection.

Here is the code I have so far.

//Polygons

var polygons = ee.FeatureCollection('ft:1_z8-9NMZnJie34pXG6l-3StxlcwSKSTJFfVbrdBA');

Map.addLayer(polygons);

//Date of interest

var start = ee.Date('2008-01-01');
var finish = ee.Date('2010-12-31');

//IMPORT Landsat IMAGEs
var Landsat = ee.ImageCollection('LANDSAT/LT05/C01/T1') //Landsat images
.filterBounds(polygons)
.filterDate(start,finish)
.select('B4','B3');

//Add ImageCollection to Map
Map.addLayer(Landsat);

//Map the function over the collection and display the result
print(Landsat);

// Empty Collection to fill
var ft = ee.FeatureCollection(ee.List([]))

var fill = function(img, ini) {
  // type cast
  var inift = ee.FeatureCollection(ini)

  // gets the values for the points in the current img
  var mean = img.reduceRegions({
    collection:polygons,
    reducer: ee.Reducer.mean(),
 });

 // Print the first feature, to illustrate the result.
print(ee.Feature(mean.first()).select(img.bandNames()));

  // writes the mean in each feature
  var ft2 = polygons.map(function(f){return f.set("mean", mean)})

  // merges the FeatureCollections
  return inift.merge(ft2)

  // gets the date of the img
  var date = img.date().format()

  // writes the date in each feature
  var ft3 = polygons.map(function(f){return f.set("date", date)})

  // merges the FeatureCollections
  return inift.merge(ft3)
}

// Iterates over the ImageCollection
var newft = ee.FeatureCollection(Landsat.iterate(fill, ft))

// Export
Export.table.toDrive(newft,
"anyDescription",
"anyFolder",
"test")

In the console I get an error message

Element (Error) Failed to decode JSON. Error: Field 'value' of object '{"type":"ArgumentRef","value":null}' is missing or null. Object: {"type":"ArgumentRef","value":null}.

In my csv file which is generated I get a new column called mean but this is populated with and no actual values.


回答1:


There is no reason to use iterate() here. What you can do is a nested map(). Over polygons and then over images. You can flatten the resulting list of lists to turn it into a single list like this:

// compute mean band values by mapping over polygons and then over images
var results = polygons.map(function(f) {
  return images.map(function(i) {
    var mean = i.reduceRegion({
      geometry: f.geometry(),
      reducer: ee.Reducer.mean(),
    });

    return f.setMulti(mean).set({date: i.date()})
  })
})

// flatten
results = results.flatten()

Script: https://code.earthengine.google.com/b65a731c78f78a6f9e08300dcf552dff

The same approach can be used with reduceRegions() as well, mapping over images and then over regions. However, you will have to map over the resulting features to set dates.

images.filterBounds(f) can be probably also added if your features cover a larger area.

PS: your table is not shared



来源:https://stackoverflow.com/questions/47718053/populate-featurecollection-with-values-from-bands-of-each-individual-image-in-an

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!