0

I want to convert data presented in a list of lists (JSON) in a table so that I can export it in CSV format. The following is the link to my code.

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

There's a list called 'DataList' which I want to convert to table and export in CSV.

    var collection = ee.ImageCollection(sentinel.filterDate('2016-11-01','2016-11-30')
                         .filterBounds(gulbarga)
                         .sort('CLOUD_COVER_ASSESSMENT')
                         .first());
print(collection.size());
print(collection);


var data = collection.getRegion(gulbarga,30);
print(data,'DataList');
  • what isn't working? – Ian Turton May 15 at 13:09
  • I am not able to get as to how to get 'DataList' in a table format so that I can export it to drive. – Jyoti May 15 at 13:14
  • When the question is reopened, I can provide the answer in text. For now, here is the link to rework the getRegion() output. code.earthengine.google.com/defdba7e5f5bd5b87b1abd967885d6f8. Although I would suggest using reduceRegions() and add the date of the image to each feature. You can then export the feature collection as CSV. – Kuik May 15 at 13:37
  • Thank you so much. this works pretty well. There's one more problem though. Can the 'time' column elements somehow be modified in 'yyyy-mm-dd' format? – Jyoti May 15 at 14:11
0

To export data as a table (CSV) in the GEE, you need to make a feature collection with each feature a dictionary of properties. The getRegion() function puts out a list of which the first are the keys and the others are the values related to it. We can rework that into a featureCollection so it can be exported:

// get the information using getRegion()
var data = collection.getRegion(gulbarga,30);
// keys of the variables are in the first list
var keys = ee.List(data.get(0));

// make a feature collection from all the values from row 1 till end
var featureCollection = ee.FeatureCollection(data.slice(1).map(function(singleData){
  singleData = ee.List(singleData);
  var dict = ee.Dictionary.fromLists(keys, singleData);
  var point = ee.Geometry.Point([dict.get('longitude'), dict.get('latitude')]);
  var timeFormat = ee.Date(dict.get('time')).format('YYYY-MM-dd');
  return ee.Feature(point, dict).set('timeFormat', timeFormat);
}));

Here is the link to rework the getRegion() output. Link

Although I would suggest using reduceRegions() and add the date of the image to each feature. You can then export the feature collection as CSV.

var featureCollection = collection.map(function(image){
  var data = image.reduceRegions(gulbarga,'mean',30).map(function(feat){
    return ee.Feature(feat).set('time', image.get('system:time_start'), 'id', image.get('system:index'),
          'timeFormat', ee.Date(image.get('system:time_start')).format('YYYY-MM-dd'),
          'latitude', feat.geometry().coordinates().get(1), 'longitude', feat.geometry().coordinates().get(0));
  });
  return data;
}).flatten();

Link code

You can export both (similar) feature collection to CSV in drive using:

Export.table.toDrive(featureCollection, 'exportAsCSV', 'GEE', 'CSVexport', 'CSV');

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.