Created
January 24, 2015 22:42
-
-
Save xverges/621c01fc625871cb6158 to your computer and use it in GitHub Desktop.
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
"use strict"; | |
var fs = require('fs'); | |
var path = require('path'); | |
var _ = require('lodash'); | |
var turf = require('turf'); | |
var bcnByDistrict = fs.readFileSync(path.join(__dirname, 'barcelona-neighborhoods.geojson')); | |
var polygons = JSON.parse(bcnByDistrict); | |
function removeHolesFromPolygon(p) { | |
p.geometry.coordinates = [p.geometry.coordinates[0]]; | |
} | |
function mergeUsingTurfMerge(fc) { | |
return turf.merge(fc); // Returns a MultiPolygon. Go figure. | |
} | |
function mergeUsingTurfBuffer(fc) { | |
var meters = 10; | |
var buffer = turf.buffer(fc, meters/1000, 'kilometers'); | |
removeHolesFromPolygon(buffer.features[0]); | |
return buffer; | |
} | |
function simplify(fc) { | |
var tolerance = 0.001; | |
fc.features = _.map(fc.features, function(pol) { | |
return turf.simplify(pol, tolerance, false); }); | |
} | |
var singlePolygon = mergeUsingTurfBuffer(polygons); | |
singlePolygon.attribution = 'Built from https://gist.github.com/guillelopez/26f57e5966b5f8825e9b'; | |
fs.writeFileSync(path.join(__dirname, 'barcelona.detailed.geojson'), | |
JSON.stringify(singlePolygon, null, 2)); | |
simplify(singlePolygon); | |
fs.writeFileSync(path.join(__dirname, 'barcelona.simple.geojson'), | |
JSON.stringify(singlePolygon, null, 2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment