Created
December 1, 2018 16:11
-
-
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
This file contains 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
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