Skip to content

Instantly share code, notes, and snippets.

@maptastik
Created December 1, 2018 16:11
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 maptastik/08ff6b2988197e0340dd6320be20490b to your computer and use it in GitHub Desktop.
Save maptastik/08ff6b2988197e0340dd6320be20490b to your computer and use it in GitHub Desktop.
Consolidate multiple Polygon and MultiPolygon feature geometries into a single MultiPolygon geometry array
function consolidatePolygons(polygonsJson) {
let consolidatedPolygonsArray = [];
let polygonsFeatures = polygonsJson.features;
for (let i = 0; i < polygonsFeatures.length; i++) {
let polygonFeature = polygonsFeatures[i];
let coordinates = polygonFeature.geometry.coordinates;
if (polygonFeature.geometry.type === "MultiPolygon") {
for (let j = 0; j < coordinates.length; j++) {
consolidatedPolygonsArray.push(coordinates[j]);
}
} else if (polygonFeature.geometry.type === "Polygon") {
consolidatedPolygonsArray.push(coordinates);
} else {
console.log(
`Geometry at index ${i} is not a Polygon or MultiPolygon. Skipping.`
);
continue;
}
}
return consolidatedPolygonsArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment