Created
April 9, 2015 01:02
-
-
Save prashtx/c8e4661fee245047ab01 to your computer and use it in GitHub Desktop.
Simplify a Polygon and MultiPolygon FeatureCollection
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
#!/usr/bin/env node | |
/* | |
* cat my-features.json | ./simplify.js 0.0001 > my-simplified-features.json | |
*/ | |
'use strict'; | |
var simplify = require('turf-simplify'); | |
var concat = require('concat-stream'); | |
var TOLERANCE = 0.01; | |
if (process.argv.length === 3) { | |
TOLERANCE = parseFloat(process.argv[2]); | |
} | |
process.stdin.pipe(concat(function (input) { | |
var data = JSON.parse(input); | |
data.features = data.features.map(function (feature) { | |
if (feature.geometry.type === 'MultiPolygon') { | |
return { | |
type: 'Feature', | |
properties: feature.properties, | |
geometry: { | |
type: 'MultiPolygon', | |
coordinates: feature.geometry.coordinates.map(function (polyCoords) { | |
return simplify({ | |
type: 'Feature', | |
geometry: { | |
type: 'Polygon', | |
coordinates: polyCoords | |
} | |
}, TOLERANCE, true).geometry.coordinates; | |
}) | |
} | |
}; | |
} | |
return simplify(feature, TOLERANCE, true); | |
}); | |
process.stdout.write(JSON.stringify(data)); | |
process.stdout.write('\n'); | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment