0

I want to export the latitude and longitude values as a CSV file from GEE. I tried the below code, but this code gives me round off values of the coordinates

var sentImage = ee.ImageCollection('COPERNICUS/S2_SR')
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 5))
.filter(ee.Filter.lt('NOT_VEGETATED_PERCENTAGE', 70))
.filterDate("2016-01-02", "2020-12-30")
.filterBounds(ROI);
//print(sentImage)
var first = sentImage.median().clip(ROI);
// TEST IMAGE
 
// get image projection
var proj = first.select([0]).projection()

// get coordinates image
var latlon = ee.Image.pixelLonLat().reproject(proj)

// put each lon lat in a list
var coords = latlon.select(['longitude', 'latitude'])
                 .reduceRegion({
  reducer: ee.Reducer.toList(),
  geometry: rectangle,
  scale: 10,
  
})

// get lat & lon
var lat = ee.List(coords.get('latitude'));
var lon = ee.List(coords.get('longitude'));

var featureCollection = ee.FeatureCollection(lon
                        .map(function(element){
                        return ee.Feature(null,{prop:element})}));


//print(featureCollection);

//Export.table.toDrive(ee.Element(chartArray));
Export.table.toDrive({
 collection: featureCollection,
 folder: 'Test_Folder',
description:'test_chartArray',
  fileFormat: 'CSV'
}); 

1 Answer 1

1

Your reproject() was causing some trouble so remove that. Also you don't need to recreate a feature by mapping over the lat coordinates. This should work:

// put each lon lat in a list
var coords = latlon.select(['longitude', 'latitude'])
                 .reduceRegion({
  reducer: ee.Reducer.toList(),
   bestEffort: true,
  geometry: ROI,
  maxPixels: 1e9,
  tileScale: 16,
  scale: 10
})


print(coords, 'coords')
var feat = ee.Feature(null, coords)
print(feat, 'feat')
Export.table.toDrive(feat) 

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

5
  • Thanks for the reply. When I run your code it gives me this error Error: Invalid argument: 'collection' must be a FeatureCollection. (Error code: 3) Feb 3, 2022 at 6:13
  • I am new to GEE, and I want to export coordinates along with dem value in the CSV file. can you help me in this regard Feb 3, 2022 at 6:15
  • The problem is solved .. the issue is reprojection. thanks Feb 3, 2022 at 6:35
  • The previous error probably had to do with your assets I couldn't access. Glad it worked. If you believe this answered your question you can accept it for future reference: meta.stackexchange.com/questions/5234/…
    – Jobbo90
    Feb 3, 2022 at 8:20
  • yes, reprojection was the issue. in your code instead of a collection, feature collection should be provided, because to import data as in table, collection must be feature collection Feb 3, 2022 at 10:05

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.