-
-
Save swingley/02022e0727bf222ab1feb4ea156700c0 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
module.exports = { | |
"type": "FeatureCollection", | |
"features": [] | |
} |
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
module.exports = { | |
type: 'Feature', | |
properties: {}, | |
geometry: { | |
type: 'Polygon', | |
coordinates: [] | |
} | |
} |
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
const fs = require('fs') | |
const d3voronoi = require('d3-voronoi') | |
const fc = require('./fc') | |
const poly = require('./poly') | |
let sdhoods = 'sd-neighborhood-points.json' | |
fs.readFile(sdhoods, 'utf8', (err, data) => { | |
if ( err ) { | |
return console.error(err) | |
} | |
let geojson = JSON.parse(data) | |
let minX = Infinity | |
let minY = Infinity | |
let maxX = -Infinity | |
let maxY = -Infinity | |
geojson.features.forEach(feature => { | |
let c = feature.geometry.coordinates; | |
if ( c[0] < minX ) { | |
minX = c[0] | |
} | |
if ( c[0] > maxX ) { | |
maxX = c[0] | |
} | |
if ( c[1] < minY ) { | |
minY = c[1] | |
} | |
if ( c[1] > maxY ) { | |
maxY = c[1] | |
} | |
}); | |
console.log(minX, minY, maxX, maxY); | |
let neighborhoodPoints = geojson.features.map(feature => { | |
return feature.geometry.coordinates | |
}) | |
voronoi = d3voronoi.voronoi().extent([[minX, minY], [maxX, maxY]]) | |
let polygons = voronoi(neighborhoodPoints).polygons() | |
polygons.forEach(p => { | |
// Create a new feature object. | |
let feature = JSON.parse(JSON.stringify(poly)) | |
// Reverse coordinate order so it conforms to the right-hand rule. | |
// Close the ring by making the last position the same as the first. | |
p.reverse().push(p[0]) | |
feature.geometry.coordinates.push(p) | |
fc.features.push(feature) | |
}) | |
let out = 'sd-voronoi.json' | |
fs.writeFile(out, JSON.stringify(fc, null, 2), (err) => { | |
if ( err ) { | |
return console.error(err) | |
} | |
console.log(`Finished and wrote ${out}`) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment