Skip to content

Instantly share code, notes, and snippets.

@milroc
Forked from mbostock/.block
Last active August 29, 2015 14:08
Show Gist options
  • Save milroc/a646e33e972c8c128dca to your computer and use it in GitHub Desktop.
Save milroc/a646e33e972c8c128dca to your computer and use it in GitHub Desktop.

Source: American Community Survey, 2011 5-Year Estimate

This map was inspired by a similar map found on Wikipedia. I wasn’t wild about the diverging color scale, so I thought it would be a fun challenge to recreate.

Finding the shapefiles was easy; I used the U.S. National Atlas 1:1,000,000 scale dataset, conveniently packaged in my U.S. Atlas repository. I reprojected the shapefiles to the California Albers projection using ogr2ogr:

ogr2ogr \
	-f 'ESRI Shapefile' \
	-t_srs 'EPSG:3310' \
	tracts.shp \
	shp/ca/tracts.shp

I used both the census tracts and the county shapefiles, since drawing county boundaries makes the geography more familiar.

Getting the population density per census tract was harder. I knew the data was available as part of the Census Bureau’s American Community Survey, but the American FactFinder is quite onerous. It takes almost twenty steps to find and download the data!

  1. Go to factfinder2.census.gov.
  2. Find where it says “American Community Survey” and click “get data »”.
  3. Click the blue “Geographies” button on the left.
  4. In the pop-up, select census tract in the “geographic type” menu.
  5. Select California in the resulting “state” menu.
  6. Click “All Census Tracts within California”.
  7. Click the “ADD TO YOUR SELECTIONS” button.
  8. Click “CLOSE” to dismiss the pop-up.
  9. Click the blue “Topics” button on the left.
  10. In the pop-up, expand the “People” submenu.
  11. Expand the “Basic Count/Estimate” submenu.
  12. Click “Population Total”.
  13. Click “CLOSE” to dismiss the pop-up.
  14. In the table, click on the most recent ACS 5-year estimate named “TOTAL POPULATION”.
  15. On the next page, click the “Download” link under “Actions”.
  16. In the pop-up, click “OK”.
  17. Wait for it to “build” your file.
  18. When it’s ready, click “DOWNLOAD”.
  19. Finally, expand the downloaded zip file (ACS_11_5YR_B01003.zip).

I mean, does that seem like too many steps to anyone else? How about a simple URL where I can substitute a year and a state FIPS code and just download the data directly without fighting through ridiculous wizards? Anyway, assuming you didn’t die of old age while folllowing those instructions, you should now have a ACS_11_5YR_B01003_with_ann.csv file which has the data.

Lastly, all we have to do is join the population data with the shapefile using topojson -e. (See the TopoJSON command-line reference for full details.)

topojson
	-e ACS_11_5YR_B01003_with_ann.csv \
	--id-property GEOID,GEO.id2 \
	-p population=+HD01_VD01,area=+ALAND \
	-s 1 \
	--width 960 \
	--margin 10 \
	-o ca.json \
	-- tracts.shp counties.shp

The resulting TopoJSON file has the 2011 estimated population of each census tract as the population property, and the land area in square meters as the area property. Dividing one by the other and converting to square miles, we get the desired map!

Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View raw

(Sorry about that, but we can’t show files that are this big right now.)

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