Created
August 2, 2016 21:20
-
-
Save morganherlocker/b393157b7c51ae3a02d77aad6359433d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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