Skip to content

Instantly share code, notes, and snippets.

@mapmeld
Created August 2, 2012 23:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mapmeld/3241693 to your computer and use it in GitHub Desktop.
Save mapmeld/3241693 to your computer and use it in GitHub Desktop.
Using the Census API
NOTE: This how-to was written for the Census API at http://thedataweb.rm.census.gov/ -- it has since been moved to http://api.census.gov/
Mike Stucka, our contact at the Macon Telegraph, sent us a link to the Census's official API which is launching next month. You can skip ahead to the site - http://www.census.gov/developers/ - and get an API key, but also read my notes after using this yesterday:
1) The datasets
--- The 2010 Census Summary comes from everyone filling out census forms, and you can get stats at state level down to a super-detailed block level. Info from this includes population, age, gender, race, home ownership, members of a household, and various combinations of that. Full list: http://www.census.gov/developers/data/sf1.xml
--- The 2006-2010 American Community Survey is a longer form given to fewer households over 5 years (so its numbers are incompatible with the 2010 Census). You can get stats down only to the block group level. In addition to the standard census stats, you get: education level, individual earnings, household income, mortgages, poverty, rent as percent of income, citizenship status, and various combinations ( earnings for women over 25, by level of education ). Full list: http://www.census.gov/developers/data/2010acs5_variables.xml API requests start with http://thedataweb.rm.census.gov/data/2010/acs5
2) Using the JSON/JSONP API
--- I want to get Name and Population of Bibb County, Georgia in the 2010 Census Summary. I looked up total population in the list above and found the variable named P0010001. It's also a good idea to include NAME. So my URL will say get=P0010001,NAME
--- I need to specify both "for" ( what area I want stats on ) and "in" (anything higher up - in this case, the state). These are given using FIPS codes. Georgia is the 13th state. So the API expects in=state:13. When I Googled Bibb County's FIPS number, I saw 13021. The 13 is just the state prefix, so the API expects for=county:021
--- JSONP: add &jsonp=callbackFunction
http://thedataweb.rm.census.gov/data/2010/sf1?get=P0010001,NAME&for=county:021&in=state:13&key=d343614e8f46717c1ffe54bd67ae76f6bf2c9b2d
3) Using * to see all lower-level options
--- The next level below county is called a "tract". If you wanted zipcodes or districts, check this table: http://thedataweb.rm.census.gov/data/sf1geo.html
--- I don't know my tract codes, so I can say for=tract:* to list all tracts in the county. Then I need to update my higher-level areas to include: &in=state:13+county:021
http://thedataweb.rm.census.gov/data/2010/sf1?get=P0010001,NAME&for=tract:*&in=state:13+county:021&key=d343614e8f46717c1ffe54bd67ae76f6bf2c9b2d
--- Notice that a NAME looks like "Census Tract 101" or "Census Tract 121.01" but the tract is 010100 or 012101. To see all blocks in a tract, the API expects +tract:010100
http://thedataweb.rm.census.gov/data/2010/sf1?get=P0010001,NAME&for=block:*&in=state:13+county:021+tract:010100&key=d343614e8f46717c1ffe54bd67ae76f6bf2c9b2d
--- In the 2nd dataset, (which has acs5 in the URL) we can only go down to the for=block+group:* level. We also need to look up ACS5's name for Total Population, which is B01003_001E.
http://thedataweb.rm.census.gov/data/2010/acs5?get=B01003_001E,NAME&for=block+group:*&in=state:13+county:021+tract:010100&key=d343614e8f46717c1ffe54bd67ae76f6bf2c9b2d
4) Mapping census tracts, block groups, and blocks:
--- You can see PDF maps of tracts and blocks here: http://www.census.gov/geo/www/maps/DC10_GUBlkMap/dc10blk_main.html Remember that tracts such as 101 and 121.01 are sent to the API as 010100 and 012101.
--- You can download shapefiles here: http://www.census.gov/cgi-bin/geo/shapefiles2010/main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment