|
| 1 | +# Census boundaries |
| 2 | + |
| 3 | +Here’s how to fetch high-resolution shapefiles from the [U.S. Census Bureau](https://www.census.gov/geographies/mapping-files/time-series/geo/cartographic-boundary.html) and convert them into a web-friendly format in a bash data loader. |
| 4 | + |
| 5 | +First, you’ll need to install a few packages: |
| 6 | + |
| 7 | +```sh |
| 8 | +npm install shapefile topojson-client topojson-server topojson-simplify |
| 9 | +``` |
| 10 | + |
| 11 | +Next, here’s a bash script, `ca.json.sh`: |
| 12 | + |
| 13 | +```bash |
| 14 | +# Download the ZIP archive from the Census Bureau (if needed). |
| 15 | +if [ ! -f src/.observablehq/cache/cb_2023_06_cousub_500k.zip ]; then |
| 16 | + curl -o src/.observablehq/cache/cb_2023_06_cousub_500k.zip 'https://www2.census.gov/geo/tiger/GENZ2023/shp/cb_2023_06_cousub_500k.zip' |
| 17 | +fi |
| 18 | + |
| 19 | +# Unzip the ZIP archive to extract the shapefile. |
| 20 | +unzip -oqd src/.observablehq/cache src/.observablehq/cache/cb_2023_06_cousub_500k.zip |
| 21 | + |
| 22 | +# Convert the shapefile to GeoJSON, then to TopoJSON, simplify, and merge counties. |
| 23 | +shp2json --encoding utf-8 -n src/.observablehq/cache/cb_2023_06_cousub_500k.shp > src/.observablehq/cache/cb_2023_06_cousub_500k.ndjson |
| 24 | +geo2topo -q 1e5 -n counties=src/.observablehq/cache/cb_2023_06_cousub_500k.ndjson \ |
| 25 | + | toposimplify -f -s 1e-7 \ |
| 26 | + | topomerge state=counties |
| 27 | +``` |
| 28 | + |
| 29 | +The census.gov URL comes from the Census Bureau page linked above. Here “06” is the state of California’s FIPS code, and “cousub” means county subdivisions. |
| 30 | + |
| 31 | +And here’s the result displayed with Plot: |
| 32 | + |
| 33 | +```js echo |
| 34 | +Plot.plot({ |
| 35 | + width: 640, |
| 36 | + height: 720, |
| 37 | + projection: { |
| 38 | + type: "conic-conformal", |
| 39 | + parallels: [37 + 4 / 60, 38 + 26 / 60], |
| 40 | + rotate: [120 + 30 / 60, 0], |
| 41 | + domain: castate |
| 42 | + }, |
| 43 | + marks: [ |
| 44 | + Plot.geo(castate), |
| 45 | + Plot.geo(cacounties, {strokeOpacity: 0.2}) |
| 46 | + ] |
| 47 | +}) |
| 48 | +``` |
| 49 | + |
| 50 | +```js echo |
| 51 | +const ca = FileAttachment("data/ca.json").json(); |
| 52 | +``` |
| 53 | + |
| 54 | +```js echo |
| 55 | +const castate = topojson.feature(ca, ca.objects.state); |
| 56 | +const cacounties = topojson.mesh(ca, ca.objects.counties, (a, b) => a !== b); |
| 57 | +``` |
0 commit comments