2

I created a small image on Google Earth Engine and now would like to export all its bands in a csv file. With a format like

| pixel id | lat | lng | band1 | ....|  
|----------|-----|-----|-------|-----|  
|__________|_____|_____|_______|_____| 

I find in the doc that it was possible to obtain this result for a FeatureCollection but nothing for Image.

The small image that I would like to export :

//load dataset
var gfc2014 = ee.Image('UMD/hansen/global_forest_change_2015');

var tree = gfc2014.select('treecover2000');

// Export the image, specifying scale and region.
Export.image.toDrive({
  image: tree.updateMask(tree),
  description: 'foret St Genis',
  scale: 30,
  region: geometry //defined with the inspector
  fileFormat: 'CSV' //of course I need to work on this one
});

1 Answer 1

2

Yes that's easily possible by exporting a table to drive. Although this is useless for large areas/high resolution (you would better just export the image), the following should get you in the right direction:

// select a band
var bandNames = ['treecover2000']; // add more if you want

//load dataset
var geometry = /* color: #d63000 */ee.Geometry.Polygon(
    [[[-98.82986842488617, 30.51973226951964],
    [-98.83055507039398, 30.50701389220806],
    [-98.81956874226898, 30.507235738627383],
    [-98.81871043538422, 30.519806208708616]]]),
    gfc2014 = ee.Image("UMD/hansen/global_forest_change_2015");
var tree = gfc2014.select(bandNames);
Map.addLayer(tree)

// use get region to get a list of long, lat, bandvalue, date
var scale = tree.projection().nominalScale();
var list = ee.ImageCollection(tree).getRegion(geometry, scale); // Don't try to print this list

// tranform to feat collection (each feat will be a row in Excel export)
var names = ee.List(list.get(0)); // first row are the names
var listIDs = ee.List.sequence(1, list.length().subtract(1));
var feats = ee.FeatureCollection(listIDs.map(function (id) {
    var data = list.get(id);
    return ee.Feature(null, ee.Dictionary.fromLists(names, data)).set('ID', id);
}));
print(feats.limit(2));

// Export the featureCollection
Export.table.toDrive({
    collection: feats,
    description: 'foretStGenis',
    fileFormat: 'CSV',
    selectors: ['ID', 'latitude', 'longitude'].concat(bandNames)
});

Link code

1
  • Of course I will use it only for very small area, but it's exactly the behavior I was looking for! Dec 11, 2019 at 6:33

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.