[GIS] Google Earth Engine: How to obtain coordinates of centroids of polygons of a FeatureCollection

centroidscoordinatesfeature-collectiongoogle-earth-engine

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?

Best Answer

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

    // Export centroids to csv
    Export.table.toDrive({
      collection: centroids, 
      description: 'centroids',
      fileFormat: 'csv'
    })

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

enter image description here