[GIS] Compute centroids and buffers for multiple polygons in Google Earth Engine

buffercentroidsgoogle-earth-enginegoogle-fusion-tablespolygon

I have a dataset with many polygons that come from a fusion table ('xxxxx'):

var polygons = ee.FeatureCollection('xxxxx');

I want to compute the centroid and a buffer of Z meters for each of these polygons. How do I do that?

Best Answer

The task you are asking to do is termed mapping over a FeatureCollection in Google Earth Engine. You can see GEE documentation on doing so here.

To compute the tasks you want, you can write small functions that append the centroid coordinates for each feature as an attribute, or return a FeatureCollection with the specified buffer:

var polygons = ee.FeatureCollection('xxxxx');

var getCentroids = function(feature) {
  return feature.set({polyCent: feature.centroid()});
};

var bufferPoly = function(feature) {
  return feature.buffer(Z);   // substitute in your value of Z here
};

var fcWithCentroids = polygons.map(getCentroids);
print(fcWithCentroids) // access the features and look at the properties, for which there should be a point with the centroid coords

var bufferedPolys = polygons.map(bufferPoly);
Map.addLayer(bufferedPolys, {}, 'Buffered polygons');
Map.addLayer(polygons, {}, 'Unbuffered polygons'); // for comparison

There may be more elegant ways of getting the map function to return both tasks with a single go, but I'll let you explore how to do that as it will be a good learning experience!

Related Question