I need to obtain coordinates (longitude, latitude) of the centroids of a FeatureCollection of polygons in Google Earth Engine. I would like to download the coordinates if possible. I am new to GEE and to JavaScript (normally I use R). Here is what I have so far:

//Mapping GAUL
var gaul1 = ee.FeatureCollection("FAO/GAUL/2015/level1");

Map.setCenter(12.876, 42.682, 5);

Map.addLayer(gaul1, {}, 'GAUL');

//Subset Italy
var italy = gaul1.filter("ADM0_NAME == 'Italy'");

print('italy', italy);

// Compute centroids of each polygon
var getCentroid = function(feature) {
  // Keep this list of properties.
  var keepProperties = ['ADM0_CODE', 'ADM0_NAME', 'ADM1_CODE', 'ADM1_NAME', 'ADM2_CODE', 'ADM2_NAME',];
  // Get the centroid of the feature's geometry.
  var centroid = feature.geometry().centroid();
  // Return a new Feature, copying properties from the old Feature.
  return ee.Feature(centroid).copyProperties(feature, keepProperties);

// Map the centroid getting function over the features.
var centroids = italy.map(getCentroid);

// Display the results.
Map.addLayer(centroids, {color: 'FF0000'}, 'centroids');

// Print centroids
print('centroids', centroids);

How does one abstract the coordinates from the centroids?


You are almost there. You just have to export your collection as a csv using the following code:

    // Export centroids to csv
      collection: centroids, 
      description: 'centroids',
      fileFormat: 'csv'

You will get a csv file that has a column (.geo) with the coordinates:

enter image description here

  • That looks good thanks. I can use some string operations in R to extract the coordinates as numbers. Perhaps there is a clean way to get these numbers out of GEE but this will do for now.
    – Dan B
    May 25 at 12:43
  • Exactly, if you are familiar with R, I am sure you can use it to get the "clean" coordinates. If you found my answer useful, please upvote it
    – HMSP
    May 26 at 13:57
  • I'd love to upvote but apparently I don't have enough points or something
    – Dan B
    May 27 at 16:07

Add this at the end of the code, you can export the data in the Tasks button on the right panel of the GEE code, then click run. When you click run, you can choose any filetype you want to export.

// Export the FeatureCollection to a KML or SHP file.
  collection: centroids,
  fileFormat: 'SHP'

source: Export data

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.