0

How do you geospatial cluster a FeatureCollection of polygons in Google Earth Engine? For example, the FeatureCollection fc consists of polygons.

var fc = ee.FeatureCollection('GLIMS/current')
  .filter(ee.Filter.eq('geog_area', 'French Alps'))

Map.addLayer(fc, {},'unclustered', false)
Map.centerObject(fc, 7)

In other words, I want each polygons to have an additional property 'cluster' which can be used to group geospatially close polygons.

1

I calculated the centroid longitude and latitude of each polygon. Then I used k-means clustering to cluster the Feature Collection based on these properties.

var fc = ee.FeatureCollection('GLIMS/current')
  .filter(ee.Filter.eq('geog_area', 'French Alps'))

Map.addLayer(fc, {},'unclustered', false)
Map.centerObject(fc, 7)

// Get centroids, set centroid Lon and Lat as seperate properties
fc = fc.map(function(f){
  var centroid = f.geometry().centroid()
  return f.set('centroidLon', centroid.coordinates().get(0))
          .set('centroidLat', centroid.coordinates().get(1))
})

var nClusts = 3
// Train the clusterer, using the same data ('fc')
var clusterer = ee.Clusterer.wekaKMeans(nClusts).train(fc, ['centroidLat','centroidLon'])
// Run the clustering algorithm. 
var clustered = fc.cluster(clusterer)

// Display the clusters with random colors.
var empty = ee.Image().byte();
var fills = empty.paint({
  featureCollection: clustered,
  color: 'cluster',
});
var palette = ['FF0000', '00FF00', '0000FF'];
Map.addLayer(fills, {palette: palette, max: 2}, 'clusters');

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.