Skip to content

Instantly share code, notes, and snippets.

@asleepwalker
Last active December 4, 2021 05:13
Show Gist options
  • Save asleepwalker/60ac02294a659859372e to your computer and use it in GitHub Desktop.
Save asleepwalker/60ac02294a659859372e to your computer and use it in GitHub Desktop.
Remove holes from multipolygons in geojson file
// DEPS: npm install jsts@0.17.0
// USAGE: node remove_holes.js holey_coverage.geojson solid_coverage.geojson
var fs = require('fs');
var jsts = require("jsts");
var parser = new jsts.io.GeoJSONParser();
var geojson = JSON.parse(fs.readFileSync(process.argv[2], 'utf8'));
var removedHoles = 0;
var holeyPolygons = 0;
for (var l = 0, totalFeatures = geojson.features.length; l < totalFeatures; l++) {
if (geojson.features[l].geometry.type == 'MultiPolygon') {
var geoms = parser.read(geojson.features[l].geometry);
var holes = [];
for (var i = 0, totalPolygons = geoms.geometries.length; i < totalPolygons; i++) {
if (geoms.geometries[i].holes.length) {
for (var j = 0, totalHoles = geoms.geometries[i].holes.length; j < totalHoles; j++) {
var hole = [];
for (var k = 0, totalPoints = geoms.geometries[i].holes[j].points.length; k < totalPoints; k++) {
var point = geoms.geometries[i].holes[j].points[k];
hole.push([point.x, point.y]);
}
holes.push(hole);
removedHoles++;
}
holeyPolygons++;
}
}
geojson.features[l].geometry.coordinates.push(holes);
}
}
console.log('Removed holes: ' + removedHoles + ' total in ' + holeyPolygons + ' holey polygons');
fs.writeFile(process.argv[3], JSON.stringify(geojson));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment