Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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