Skip to content

Instantly share code, notes, and snippets.

@morganherlocker
Created August 2, 2016 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morganherlocker/b393157b7c51ae3a02d77aad6359433d to your computer and use it in GitHub Desktop.
Save morganherlocker/b393157b7c51ae3a02d77aad6359433d to your computer and use it in GitHub Desktop.
var fc = // ... a featurecollection of polygons and multipolygons
var polys = turf.featurecollection([])
fc.features.forEach(function(poly){
if (poly.geometry.type === 'Polygon') {
polys.features.push(poly)
} else if (poly.geometry.type === 'MultiPolygon') {
// start max area at -Infinity
var max = {
feature: null,
area: -Infinity
};
poly.geometry.coordinates.forEach(function(rings){
var hull = turf.polygon(rings)
var area = turf.area(hull);
if(area >= max.area) {
max = {
feature: hull,
area: area
};
}
})
// push the largest hull to the polys featurcollection
polys.features.push(max.feature)
} else throw new Error('unknown geometry type detected')
})
console.log(JSON.stringify(polys))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment