1

I am struggling with transforming a single image to a list and then exporting it to my drive.

I am using the ee.Reducer.toList() but the output looks like this: enter image description here

Insted of this:

enter image description here

When I print the featureCollection that is to be written to the file - it seems to contain all the data needed.

Here is my code:

var region = ee.Geometry.Polygon([
  [[-5, 40], [-5, 60], [-5, 60]]
]);

//Dates of Interest
var start = ee.Date("2018-04-01");
var finish = ee.Date("2018-05-01");

var sen2 = ee.ImageCollection('COPERNICUS/S2_SR')
  .filterDate(start,finish)
  .filterBounds(region)
  .map(function(image){
    return image
    .clip(region)
    .normalizedDifference(['B8','B4'])
    .rename('NDVI')
  })

print(sen2)

var sngl = ee.Image(sen2.first())

var list = sngl.reduceRegion({
  reducer: ee.Reducer.toList(),
  geometry: region,
  maxPixels: 1e8,
  scale: 10
});

// Make a feature without geometry and set the properties to the dictionary of means.
var feature = ee.Feature(null, list);

// Wrap the Feature in a FeatureCollection for export.
var featureCollection = ee.FeatureCollection([feature]);

print(featureCollection)

// Export the FeatureCollection.
Export.table.toDrive({
  collection: featureCollection,
  fileNamePrefix: 'field_test',
  folder: 'field_test',
  fileFormat: 'CSV'
});

Any ideas?

1 Answer 1

2

When you export a table, each row is a feature. Your code is making a collection with exactly one feature in it; you need to produce many features instead. Also, your variable 'list' is misnamed as it is a dictionary containing a list, not a list.

So, changing this section of your code will get you a table shaped like you are asking for:

var list = ee.List(sngl.reduceRegion({
  reducer: ee.Reducer.toList(),
  geometry: region,
  scale: 10
}).get('NDVI'));

var featureCollection = ee.FeatureCollection(list.map(function (ndviValue) {
  return ee.Feature(null, {'NDVI': ndviValue});
}));

https://code.earthengine.google.com/1041cd6787dd04dc81d8725aa14af356

The map on the list converts the list of numbers to a list of Features, and then can be converted to a FeatureCollection for export.

1
  • Many thanks @Kevin Reid May 13, 2020 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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