{"id":963,"date":"2013-01-20T11:05:28","date_gmt":"2013-01-20T17:05:28","guid":{"rendered":"http:\/\/www.mooreds.com\/wordpress\/?p=963"},"modified":"2013-01-22T22:26:12","modified_gmt":"2013-01-23T04:26:12","slug":"a-tutorial-for-the-census-data-api","status":"publish","type":"post","link":"https:\/\/www.mooreds.com\/wordpress\/archives\/963","title":{"rendered":"A tutorial for the Census Data API"},"content":{"rendered":"<p>I&#8217;ve been exploring <a href=\"http:\/\/en.wikipedia.org\/wiki\/Application_programming_interface\">APIs<\/a> ever since attending the hackathon at <a href=\"http:\/\/gluecon.com\/\">Gluecon<\/a> last year.\u00a0 Since I work for a real estate firm, I&#8217;m interested in APIs with a geographic component.\u00a0 I had heard that USA Today had APIs for census data.\u00a0 <a href=\"http:\/\/developer.usatoday.com\/docs\/read\/Census\">They do<\/a>.\u00a0 I also wondered if the census bureau had any APIs available.\u00a0 <a href=\"http:\/\/www.census.gov\/developers\/\">They do<\/a>.\u00a0 However, the USA Today APIs are much much friendlier.<\/p>\n<p>But the USA Today APIs have some limitations.\u00a0 Also, the data sets of these two APIs intersect, but neither is a subset of the other.\u00a0 So, I thought it&#8217;d be useful to have a quick start for the census data API.<\/p>\n<ol>\n<li>First, <a href=\"http:\/\/www.census.gov\/developers\/tos\/key_request.html\">register for an API key<\/a>.\u00a0 This only requires an email address and a name.\u00a0 The key got sent to my spam folder, so make sure you check that if you don&#8217;t see the email about five minutes after you submit.<\/li>\n<li>Decide what type of information you want to find.\u00a0 There are two sets of information&#8211;the 2010 census summary file or thee <a href=\"http:\/\/www.census.gov\/acs\/www\/about_the_survey\/american_community_survey\/\">American community survey<\/a> (for the last five orsix years).\u00a0 Since the ACS is not available in the USA Today\u00a0 API, that&#8217;s what I&#8217;ll explore, but the methodology is similar for the summary file.<\/li>\n<li>The first place to look is the variables XML file showing you what data is available.\u00a0 Here is the <a href=\"http:\/\/www.census.gov\/developers\/data\/acs_5yr_2011_var.xml\">ACS variables file for the most recent five years<\/a>, and here is the <a href=\"http:\/\/www.census.gov\/developers\/data\/sf1.xml\">census summary variables file<\/a>.\u00a0 For a high level overview of what you can find, you&#8217;ll want to focus on the &#8216;concept&#8217; tags.\u00a0 You&#8217;ll see names like &#8220;C24070.\u00a0 Industry by Class of Worker for the Civilian Employed Population 16 Years and Over&#8221; and &#8220;B24123.\u00a0 Detailed Occupation by Median Earnings for the Full-Time, Year-Round Civilian Employed Female Population 16 Yrs and Over&#8221;.\u00a0 While the Census has documents explaining what each of these mean, <a href=\"http:\/\/www.socialexplorer.com\/pub\/ReportData\/MetaBrowser.aspx\">this website seems to have aggregated the metadata<\/a> that will let you explore the types of data available.<\/li>\n<li>OK, you&#8217;ve found your interesting data.\u00a0 I&#8217;m going to look at the concept &#8220;B08126.\u00a0 MEANS OF TRANSPORTATION TO WORK BY INDUSTRY&#8221; for the rest of this example.\u00a0 You need to pick a variable from the concept, so I&#8217;ll pick B08126_003E\/Construction and some subsets: B08126_018E\/Car, truck, or van &#8211; drove alone, B08126_033E\/Car, truck, or van &#8211; carpooled, and B08126_048E\/Public transportation (excluding taxicab). As you see, you can request multiple variables at once.<\/li>\n<li>Now you need to find the area that you want to look at.\u00a0 The census developer site <a href=\"http:\/\/api.census.gov\/data\/2011\/acs5\/geo.html\">has examples<\/a> which I found to be massively confusing when I first looked at them.\u00a0 What you need to do is decide what level of geography you want, and then find the appropriate code.\u00a0 Here are <a href=\"http:\/\/www.census.gov\/geo\/www\/ansi\/state.txt\">state codes<\/a>.\u00a0 If you want anything more detailed, you&#8217;ll probably want a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Federal_Information_Processing_Standard\">FIPS code<\/a> which ranges from states to as small as the Census data is gathered. \u00a0Here&#8217;s a <a href=\"https:\/\/www.census.gov\/geo\/www\/cob\/\">list of valid geographic delineations<\/a>.\u00a0 You may have the information if you are interested in a county, but if all you have is a lat\/lng (as if, say, you are looking at a property listing), you can <a href=\"http:\/\/www.fcc.gov\/developers\/census-block-conversions-api\">look up the FIPS code<\/a>.\u00a0 If you need to get a lat\/lng for a city, I&#8217;d suggest google.<\/li>\n<li>I&#8217;m going to look up Lafayette, CO, which google tells me is at 39.9936, -105.0892.\u00a0 So, the FIPS request looks like http:\/\/data.fcc.gov\/api\/block\/find?latitude=39.9936&amp;longitude=-105.0892&amp;showall=false&amp;format=json and reveals that the FIPS code for this lat\/lng is 080130608005010.<\/li>\n<li>A FIPS code is broken into sections.\u00a0 The first two numbers are the state, so 08 is Colorado&#8217;s code.\u00a0 The next three are the county, so 013 represents Boulder county.\u00a0 The next six are the tract code, so 060800 is the tract that we are looking at (tracts <a href=\"https:\/\/www.census.gov\/geo\/www\/cob\/tr_metadata.html\">&#8220;provide a stable set of geographic units for the presentation of decennial census data&#8221;)<\/a>.\u00a0 You can see <a href=\"http:\/\/www.census.gov\/geo\/www\/maps\/pl10_map_suite\/tract.html\">tracts on maps<\/a>, and <a href=\"http:\/\/www2.census.gov\/geo\/maps\/dc10map\/tract\/st08_co\/c08013_boulder\/DC10CT_C08013_001.pdf\">our specific tract [pdf]<\/a>.\u00a0 After the tract comes the block, which is an even smaller subsection of homes.\u00a0 The block is 5010.\u00a0 Apparently FIPS codes are on their way out, in favor of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Geographic_Names_Information_System\">GNIS codes<\/a>.\u00a0 I couldn&#8217;t find a lat\/lng to GNIS code service, however.<\/li>\n<li>Now you know what area you are looking at.\u00a0 Rather than the county, I want to examine census data based on the tract I&#8217;ve found (good old tract 060800).<\/li>\n<li>Next step is to build the URL for the API request.\u00a0 Here&#8217;s where the examples come in handy.\u00a0 For tract level data for the ACS, we <a href=\"http:\/\/api.census.gov\/data\/2011\/acs5\/geo.html\">look at this example<\/a>, and see the format of the call: http:\/\/api.census.gov\/data\/2011\/acs5?get=[the data code that you found above]&amp;for=tract:[tract code or *]&amp;in=state:[state code]+county:[county code]&amp;key=[your key]<\/li>\n<li>Therefore, my API request looks like http:\/\/api.census.gov\/data\/2011\/acs5?get=B08126_033E,B08126_018E,B08126_048E&amp;for=tract:060800&amp;in=state:08+county:013&amp;key=&#8230; and I get something like this: [[&#8220;B08126_003E&#8221;,&#8221;B08126_033E&#8221;,&#8221;B08126_018E&#8221;,&#8221;B08126_048E&#8221;,&#8221;state&#8221;,&#8221;county&#8221;,&#8221;tract&#8221;],[&#8220;309&#8243;,&#8221;57&#8243;,&#8221;188&#8243;,&#8221;0&#8243;,&#8221;08&#8243;,&#8221;013&#8243;,&#8221;060800&#8221;]] stating that there are 309 construction jobs reported in this tract that answered the transportation question, and that no one took public transport, but more carpooled than drove alone.<\/li>\n<\/ol>\n<p>Note that I ignored the margin of error fields, but they are there.\u00a0 Now you have the data, you can use something like <a href=\"http:\/\/d3js.org\/\">d3.js<\/a> to display it.<\/p>\n<p>Note that the TOS of the Census APIs don&#8217;t mention explicit limits but if you are planning to make a lot of calls, consider downloading the full dataset and using that instead.<\/p>\n<p>Finally, you can sign up for the <a href=\"http:\/\/apiforum.ideascale.com\/\">Census API developer forums<\/a>, though this seems to be more of a feature request forum and less of a technical help arena.\u00a0 And there doesn&#8217;t appear to be anonymous browsing of forums&#8211;you have to sign in to view anything.<\/p>\n<p><strong>Update 1\/22: From the developer forums <a href=\"http:\/\/apiforum.ideascale.com\/a\/dtd\/Documentation-needed\/368591-18378\">comes the comment<\/a> that the forums are meant to be both technical support and feature requests.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been exploring APIs ever since attending the hackathon at Gluecon last year.\u00a0 Since I work for a real estate firm, I&#8217;m interested in APIs with a geographic component.\u00a0 I [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62],"tags":[],"class_list":["post-963","post","type-post","status-publish","format-standard","hentry","category-apis"],"_links":{"self":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/963","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/comments?post=963"}],"version-history":[{"count":5,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/963\/revisions"}],"predecessor-version":[{"id":968,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/963\/revisions\/968"}],"wp:attachment":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/media?parent=963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/categories?post=963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/tags?post=963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}