Command-Line Cartography, Census BlockGroup level data

Mike Bostock published a 4-parts series of excellent articles on generating maps with census population data. Those were at the Census TRACT level. I have adopted them for BLOCK GROUP level data, and am sharing my commands here.

For a quick peek, the map on the left is by TRACT, right is by BLOCK GROUP. There are just a bit more detailed granularity of where people live 🤗

The detailed command are provided below. Explanations are in Mike Bostock original article. So, when I refer to step “2c”, it is in Part 2, command “c” of his article.

Part 1: Download map shape data and generate a svg graphics file for it.

California State FIPS code is 06. Filename prefix changed from Bostok’s “ca-” to “ca2018bg-” to indicate the newer data and change to Block Group level data:

curl ‘' -o cb_2018_06_bg_500k.zipunzip cb_2018_06_bg_500k.zipshp2json cb_2018_06_bg_500k.shp -o ca2018bg.json

The resulting .json is a geojson file with [longitude,latitude] coordinates:

Step 1b: Apply California Albers Equal Area Conic projection to the json file. Note that this projection will convert the coordinates and the result will no longer be a true geojson. Thus, omit this step is want to keep the coordinate in Longitude/Latitude:

geoproject 'd3.geoConicEqualArea().parallels([34, 40.5]).rotate([120, 0]).fitSize([960, 960], d)' < ca2018bg.json > ca2018bg-albers.json

The above resulting in a file with coordinates like [164.1468809671912,437.62295438355295]. More specifically, it looks like this:

Step 1c: we convert to svg:

geo2svg -w 960 -h 960 < ca2018bg-albers.json > ca2018bg-albers.svg

Which result in fairly large file, 11 MB. And if visualized by the browser based GIS tool . Alternatively, VScode also have a previewer. Both show map “upside down”:

And that was for Part 1 😃

Part 2: Join shape data with population data.

Step “2a”, convert [geo]json to ndjson:

ndjson-split 'd.features' < ca2018bg-albers.json  > ca2018bg-albers.ndjson

You should get something like this for TRACTCE: 980401

census block group (2018) TRACTCE: 980401

Step “2b”

ndjson-map ' =, d'  < ca2018bg-albers.ndjson  > ca2018bg-albers-id.ndjson

Next step is to download population data. You need to download more data via Census API, which require that you have an API KEY. Example below download all block group in State FIPS=06 (california) and County FIPS=025 (Imperial county, the newest county addition to CA):

curl ",B01003_001E&for=block%20group:*&in=state:06%20county:025&key=$ApiKey" -o cb_2018_06_bg_B01003.025.json

Full county FIPS are listed in Wikipedia. They are sequentially numbered, skipping 1 (ie always odd-numbered). To download all 58 counties of Califoria, run:

for FIPS in $(seq -w 19 2 115); do
echo curl ",B01003_001E&for=block%20group:*&in=state:06%20county:$FIPS&key=$ApiKey" -o cb_2018_06_bg_B01003.$FIPS.json

At this point, the first 3 lines of your json file for County=001 (Alameda) should look like:

[["NAME","B01003_001E","state","county","tract","block group"],
["Block Group 2, Census Tract 4384, Alameda County, California","1623","06","001","438400","2"],
["Block Group 1, Census Tract 4384, Alameda County, California","945" ,"06","001","438400","1"],

Step “2d”. The ndjson-map changed a bit here, as we now have to combine 3 columns (d[3] + d[4] + d[5] to generate the id field. The population count remains the same and placed in B01003. We also need to loop for all 58 counties:

for FIPS in $(seq -w 001 2 115); do
ndjson-cat cb_2018_06_bg_B01003.$FIPS.json | ndjson-split 'd.slice(1)' | ndjson-map '{id: d[3] + d[4] +d[5], B01003: +d[1]}' > cb_2018_06_bg_B01003.$FIPS.ndjson
cat cb_2018_06_bg_B01003.???.ndjson > cb_2018_06_bg_B01003.CA.ndjson

Your new ndjson should now looks like:


Step “2e”:

ndjson-join '' \
ca2018bg-albers-id.ndjson \
cb_2018_06_bg_B01003.CA.ndjson \
> ca2018bg-albers-join.ndjson

The result here differs from Bostock example, as we have Block Group level data here. For the same TRACTCE=400300, we now have 4 records instead of 1. Further more, there is a new field added: BLKGRPCE:


Step “2f”. The input data here has changed. However, we really only care for the population data (keyed under “B01003”) and the land area, ALAND. Population density is calculated as:

ndjson-map 'd[0].properties = {density: Math.floor(d[1].B01003 / d[0].properties.ALAND * 2589975.2356)}, d[0]' \
< ca2018bg-albers-join.ndjson \
> ca2018bg-albers-density.ndjson

Step “2g alt”. Here I am using the alternate method which worked more reliably for me:

ndjson-reduce 'p.features.push(d), p' '{type: "FeatureCollection", features: []}' \
< ca2018bg-albers-density.ndjson \
> ca2018bg-albers-density.json

The above result is a json file that you can view with your GIS program of choice. Bostock hinted at web tool, which shows the map Australian-style (ie, South is top). If in step 1c you have skipped the Calif Albers projection step, the json file is a true geojson with [Lng,Lat] coordinates in it.

Next, step “2h” is to color the map according to population density. Bostock used d3 tool that he wrote, and this step only works when the coordinates are in California Albers:

ndjson-map -r d3 \
'( = d3.scaleSequential(d3.interpolateViridis).domain([0, 4000])(, d)' \
< ca2018bg-albers-density.ndjson \
> ca2018bg-albers-color.ndjson
geo2svg -n --stroke none -p 1 -w 960 -h 960 \
< ca2018bg-albers-color.ndjson \
> ca2018bg-albers-color.purple.svg

The above produced a svg map that looks all purple, similar map that Bostock presented at the end of Part 2 of his article. Here we have population density by the Block Group rather than Tract. If you are using Linux Mint, you can use the program xviewer to look at the resulting svg file.

Part 3: TopoJSON

In Part 3, Bostock simplified the map (reducing its size) by using TopoJSON. All the steps are exactly the same, except that I changed filenames. Here is the whole enchilada :)

geo2topo -n \
tracts=ca2018bg-albers-density.ndjson \
> ca2018bg-tracts-topo.json
toposimplify -p 1 -f \
< ca2018bg-tracts-topo.json \
> ca2018bg-simple-topo.json
topoquantize 1e5 \
< ca2018bg-simple-topo.json \
> ca2018bg-quantized-topo.json
topomerge -k ', 3)' counties=tracts \
< ca2018bg-quantized-topo.json \
> ca2018bg-merge-topo.json
topomerge --mesh -f 'a !== b' counties=counties \
< ca2018bg-merge-topo.json \
> ca2018bg-topo.json

Part 4: Coloring map

Bostock showed several way to color the map. The last result in step “4e” is the prettiest and most useful one, using log scale and the OrRd color scheme. So that’s what we will use here. Note that I had to drop “d3=” in the second -r clause for it to work (ie, use -r d3-scale-chromatic)

topo2geo tracts=- \
< ca2018bg-topo.json \
| ndjson-map -r d3 -r d3-scale-chromatic 'z = d3.scaleThreshold().domain([1, 10, 50, 200, 500, 1000, 2000, 4000]).range(d3.schemeOrRd[9]), d.features.forEach(f => = z(, d' \
| ndjson-split 'd.features' \
| geo2svg -n --stroke none -p 1 -w 960 -h 960 \
> ca-2018bg-threshold.svg

Finally, in step “4e”, county borders are added to help orient the viewer of what is where:

(topo2geo tracts=- \
< ca2018bg-topo.json \
| ndjson-map -r d3 -r d3-scale-chromatic 'z = d3.scaleThreshold().domain([1, 10, 50, 200, 500, 1000, 2000, 4000]).range(d3.schemeOrRd[9]), d.features.forEach(f => = z(, d' \
| ndjson-split 'd.features'; \
topo2geo counties=- \
< ca2018bg-topo.json \
| ndjson-map ' = {"stroke": "#000", "stroke-opacity": 0.3}, d')\
| geo2svg -n --stroke none -p 1 -w 960 -h 960 \
> ca2018bg.svg

Here is the resulting svg (converted to png cuz that’s what Medium supports):

Detailed map of Calif population density by census block groups.
color scalebar for map above

You can also download the final json (with CA Albers projection) and .svg.

For those interested in using other Census data, I found this really useful example page. Note that for the 2018 Estimate data, it seems that Census have changed things a bit? Or at least the API I used changed the result, it has an extra column with NAME in it, thus offsetting column number by 1. (I have accounted these in my examples above).

For those wanting to use GIS programs that use [Longitude, Latitude] coordinate, eg Mapbox, you would need to avoid geo projection steps like 1c above. You can still run the commands up to step 2g and create a true geojson file, which you can download here as well. A JavaScript program using Mapbox GL JS API can render the data on an interactive web page that looks like this:

Population density by Census Block Group near Stockton via Mapbox

How to generate geojson with simplified/compressed geometry using TopoJSON is left as an exercise for the reader. Show/link your work in the comment section! Cheers! 🥰



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tin Ho

Tin Ho


Linux geek at Univ of Calif. Opinions expressed are my own and not my employers.