Created
February 1, 2017 18:00
-
-
Save wilson428/6d54d839ef0f0746b58937345e4620a2 to your computer and use it in GitHub Desktop.
Precompute the Cartesian length of shared borders in a topojson-powered map
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
var fs = require("fs"); | |
var d3 = require("d3"); | |
var topojson = require("topojson"); | |
var point = require('point-at-length'); | |
var ProgressBar = require('progress'); | |
var us = require("./us-10m.v1.json"); | |
var path = d3.geoPath().projection(null); // pre-projected | |
var counties = topojson.feature(us, us.objects.counties).features, | |
neighbors = topojson.neighbors(us.objects.counties.geometries); | |
function border(id0, id1) { | |
return function(a, b) { | |
return a.id === id0 && b.id === id1 || a.id === id1 && b.id === id0; | |
}; | |
} | |
var start = new Date(); | |
var bar = new ProgressBar(':bar (:current / :total counties)', { total: counties.length, width: 50 }); | |
var distances = {}; | |
for (var n = 0; n < counties.length; n += 1) { | |
var county = counties[n]; | |
neighbors[n].forEach(function(neighbor_index) { | |
var neighbor = counties[neighbor_index]; | |
// getting border-to-self for 31087 | |
if (county.id == neighbor.id) { | |
return; | |
} | |
var id = county.id + "_" + neighbor.id; | |
var b = topojson.mesh(us, us.objects.counties, border(county.id, neighbor.id)); | |
var p = path(b); | |
var d = point(p).length(); | |
distances[id] = parseFloat(d.toFixed(2)); | |
}); | |
bar.tick(); | |
} | |
var end = new Date(); | |
var elapsed = Math.round((end.getTime() - start.getTime()) / 1000); | |
console.log("Completed in", elapsed, "seconds"); | |
fs.writeFileSync("border_distances.json", JSON.stringify(distances)); |
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
{ | |
"name": "compute_borders", | |
"version": "0.0.1", | |
"description": "Precompute the Cartesian length of shared borders in a topojson-powered map", | |
"main": "get_county_borders.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [ | |
"topojson", | |
"borders" | |
], | |
"author": "Chris Wilson", | |
"license": "MIT", | |
"dependencies": { | |
"d3": "^4.5.0", | |
"point-at-length": "^1.0.2", | |
"progress": "^1.1.8", | |
"topojson": "^2.2.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment