Last active
June 22, 2017 14:07
-
-
Save jseppi/f357897a792dd73b722a8e64e64eafb2 to your computer and use it in GitHub Desktop.
Script for creating topojson of congressional districts
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
SOURCE_FILE=src/tl_2016_us_cd115.shp # unzipped http://www2.census.gov/geo/tiger/TIGER2016/CD/tl_2016_us_cd115.zip to ./src/ | |
OUT_FILE=new_districts.json | |
SIMP_PARAM=0.015 # lower value = more simplification, ref: https://github.com/topojson/topojson-simplify/blob/master/README.md#toposimplify_spherical_quantile | |
QUANTIZE_PARAM=1e5 | |
PATH=$(npm bin):$PATH | |
# Process based on https://medium.com/@mbostock/command-line-cartography-part-3-1158e4c55a1e | |
set -eu | |
shp2json $SOURCE_FILE | \ | |
# convert to ndjson | |
ndjson-split 'd.features' | \ | |
# Only keep features that have strictly numeric GEOID props | |
# because some of them have "ZZ" in their GEOID | |
ndjson-filter '/^\d+$/.test(d.properties.GEOID)' | \ | |
# assign integer id based on the original GEOID string | |
ndjson-map 'd.id = parseInt(d.properties.GEOID, 10), d' | \ | |
# remove all properties | |
ndjson-map 'd.properties = {}, d' > .tmp.ndjson | |
# covert to topojson and simplify + quantize | |
geo2topo -n districts=.tmp.ndjson | \ | |
toposimplify -S $SIMP_PARAM -f | \ | |
topoquantize $QUANTIZE_PARAM > $OUT_FILE | |
rm .tmp.ndjson | |
ls -alh $OUT_FILE |
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
{ | |
"dependencies": { | |
"ndjson-cli": "^0.3.0", | |
"shapefile": "^0.6.2", | |
"topojson": "^3.0.0", | |
"topojson-client": "^3.0.0", | |
"topojson-server": "^3.0.0", | |
"topojson-simplify": "^3.0.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
src/
npm install
to install required tools./create_districts_topo.sh
to createnew_districts.json
. You might have to make the script executable first:chmod +x create_districts_topo.sh
.