Skip to content

Instantly share code, notes, and snippets.

@jseppi
Last active June 22, 2017 14:07
Show Gist options
  • Save jseppi/f357897a792dd73b722a8e64e64eafb2 to your computer and use it in GitHub Desktop.
Save jseppi/f357897a792dd73b722a8e64e64eafb2 to your computer and use it in GitHub Desktop.
Script for creating topojson of congressional districts
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
{
"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"
}
}
@jseppi
Copy link
Author

jseppi commented Jun 22, 2017

  1. Clone this gist or copy these files to a directory
  2. Download http://www2.census.gov/geo/tiger/TIGER2016/CD/tl_2016_us_cd115.zip and unzip it to a subdirectory called src/
  3. run npm install to install required tools
  4. run ./create_districts_topo.sh to create new_districts.json. You might have to make the script executable first: chmod +x create_districts_topo.sh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment