From charlesreid1

Line 99: Line 99:




=Creating Maps with Information=
=Creating Maps with Python=


My ultimate goal was to create a map from census data. I can use the Census Reporter API to turn a place name (like Yuma, AZ) into a geographic ID, and that geographic ID into census data using further API calls.
My ultimate goal was to create a map from census data. I can use the Census Reporter API to turn a place name (like Yuma, AZ) into a geographic ID, and that geographic ID into census data using further API calls. Because the API returns JSON, which is easily translated into Python dictionaries, the output of the Census Reporter API is easy to manipulate with Python.


This information can be combined (somehow) with county-by-county map information, or you can just use the API to iterate over, e.g., all counties in a state.
Python can therefore be used to iterate, parse, and collect information, and dump it all out in a final GeoJSON file to be visualized in QGIS.
 
The data is presumably manipulated all with Python, dumped to GeoJSON, then vizzed in QGIS.


Then the only remaining question is, how do you turn that GeoJSON into something Geoserver can serve up? (Which I'm sure is an easy and straightforward question.)
Then the only remaining question is, how do you turn that GeoJSON into something Geoserver can serve up? (Which I'm sure is an easy and straightforward question.)

Revision as of 07:37, 8 February 2015

My goal is to set up a map that connects census tract data with some other census data, e.g., population or median income.

This covers some of the steps necessary to do this. It is a work in progress...

Picking an API

Census.gov API Key

I started with the SunglightLabs Census library, which provides a Python wrapper to the 2010 Census data API (wow!):

https://github.com/sunlightlabs/census

But it requires a developer key from census.gov (obtained here http://www.census.gov/developers/), which I requested, and then waited, and waited, and waited...

Census Reporter API

After giving up on waiting for a Census.gov developer key in the next day, I found the census reporter API, which allows for queries of census data in a very sensible REST format.

Detailed documentation of their API available here: https://github.com/censusreporter/census-api/blob/master/API.md

A very, very nice interface!

Mapping Census Regions

In order to accomplish the ultimate goal of mapping census data, we have to be able to first map census regions - counties, states, congressional districts, and so on. This section covers attempts to do this.

Shapefiles for U.S. Counties

I started with a shapefile defining US county boundaries.

Here is a list of shapefile information that is provided by the Census Bureau: http://www.census.gov/cgi-bin/geo/shapefiles2010/main

I started with a county map of California, tl_2010_06_county10.zip. The zip file contained a set of files, including the shapefile. I fired up QGIS to have a look at it.

QGIS CaliforniaCounties.png

Next, I loaded up a (quite large) US counties map - 70 MB total. It was straightforward to find, download, and view in QGIS:

QGIS USCounties.png

Note that you can also obtain geographic information from various APIs - for example, the Census Reporter API returns GeoJSON for geographic entities by ID:

$ curl "http://api.censusreporter.org/1.0/geo/tiger2013/04000US55?geom=true"
{
    "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
            [
                [
                    [
                        -92.674543,
                        45.382868
                    ],
                    ...
                ]
            ]
        ]
    },
    "type": "Feature",
    "properties": {
        "awater": 29365986992,
        "display_name": "Wisconsin",
        "simple_name": "Wisconsin",
        "sumlevel": "040",
        "population": 5664893,
        "full_geoid": "04000US55",
        "aland": 140268861626
    }
}

Visualizing County Boundaries with GeoJSON in QGIS

I'll show you another way to visualize county boundaries, this one using GeoJSON instead of shape files.

First, use the Census Reporter API to extract GeoJSON data for a particular county (in this case, Yuma County in Arizona):

$ curl "http://api.censusreporter.org/1.0/geo/tiger2013/05000US04027?geom=True"

{"geometry": {"type": "MultiPolygon", "coordinates": [[[[-114.563779, 32.418853999999996], [-114.813613, 32.494276], [-114.816591, 32.507695999999996], [-114.802211, 32.513191], [-114.813348, 32.524186], [-114.802181, 32.536414], [-114.80583, 32.546354], [-114.791551, 32.557023], [-114.793224, 32.569459], [-114.80683, 32.55888], [-114.813995, 32.562201], [-114.812995, 32.568706], [-114.801877, 32.576009], [-114.799737, 32.592177], [-114.807906, 32.602782999999995], [-114.809393, 32.617118999999995], [-114.799302, 32.625115], [-114.782573, 32.624303999999995], [-114.779215, 32.633578], [-114.764382, 32.642666], [-114.76495, 32.649391], [-114.74948, 32.66178], [-114.730086, 32.704298], [-114.702223, 32.74541], [-114.68823, 32.73753], [-114.617395, 32.728257], [-114.615112, 32.734515], [-114.581784, 32.734946], [-114.581736, 32.74232], [-114.564508, 32.742274], [-114.564447, 32.749553999999996], [-114.539224, 32.749812], [-114.539092, 32.756949], [-114.526856, 32.757093999999995], [-114.532432, 32.776922], [-114.529633, 32.795477], [-114.510327, 32.816488], [-114.494116, 32.823287], [-114.468971, 32.845155], [-114.46289, 32.905797], [-114.48092, 32.935252], [-114.468061, 32.955259999999996], [-114.468605, 32.971649], [-114.47653199999999, 32.975173999999996], [-114.492938, 32.971781], [-114.501226, 33.007556], [-114.516454, 33.027617], [-114.268748, 33.029815], [-114.267815, 33.463574], [-113.958042, 33.464655], [-113.958046, 33.377578], [-113.33392, 33.377424999999995], [-113.333767, 32.038607], [-114.563779, 32.418853999999996]]]]}, "type": "Feature", "properties": {"awater": 13175612, "display_name": "Yuma County, AZ", "simple_name": "Yuma County", "sumlevel": "050", "population": 199026, "full_geoid": "05000US04027", "aland": 14281187259}}

Now we can save that output to a GeoJSON file,

$ curl "http://api.censusreporter.org/1.0/geo/tiger2013/05000US04027?geom=True" > yuma.geojson

and open up the GeoJSON file in QGIS, by picking Add Vector Layer, then picking GeoJSON or All File Formats from the file format drop-down box.

FileFormatDropDown.png

This vector layer can then be plotted on top of the existing US county map for comparison, or added by itself in a new document. Either way, it works out great:

QGIS YumaCounty.png


Creating Maps with Python

My ultimate goal was to create a map from census data. I can use the Census Reporter API to turn a place name (like Yuma, AZ) into a geographic ID, and that geographic ID into census data using further API calls. Because the API returns JSON, which is easily translated into Python dictionaries, the output of the Census Reporter API is easy to manipulate with Python.

Python can therefore be used to iterate, parse, and collect information, and dump it all out in a final GeoJSON file to be visualized in QGIS.

Then the only remaining question is, how do you turn that GeoJSON into something Geoserver can serve up? (Which I'm sure is an easy and straightforward question.)