Inequality from Space

Here are the richest census tracts in the US when viewed from space:

Here are the poorest census tracts in the US when viewed from space:

The data
The ranking of census tracts is based on median household incomes from the American Community Survey (2006-2010). The threshold for inclusion in the top category is a median household income of $220,938. Those in the bottom category had a median household income of $7,474. Images are from Google Maps and show the centroid of the tract.

How you can do this
While these images can’t be easily integrated into a regression model (although it isn’t impossible), they could add some whiz-bang to a lecture or your ASA presentation. Similar images could also be useful for inspecting your data or examining outliers. As a reviewer, I would be quite impressed with any sentence that began, “After reviewing satellite imagery of the locations with extreme values…” You also don’t have to put them together in a collage–I just thought it looked nice.

I picked tracts based on income, but I could have also done it by racial composition, density, or any other census variables. It doesn’t even have to be census data; the locations could be based on any data where you have the physical address, the latitude and longitude, or the census block/tract, which you can use to get the latitude and longitude. So it is possible to map schools, prisons or neighborhoods, for instance, as long as you have some physical address for them. For example, here are the locations of Superfund sites in North Carolina:

The images are produced using the Google Maps API. You are likely familiar with these types of images from searching for driving directions. As part of their toolkit for developers, Google makes available a nifty way to grab an image of a specific location without using any fancy tools through their Static Maps API. If you want to pursue this in more depth, feel free to check out all the API has to offer, but I discuss most of the relevant options for producing static images below.

Basically, you insert the address in as part of the URL and Google displays the map, like it had been waiting for you all along. For example, to map Denver, CO the URL is:,co&zoom=12&size=1000x1000&sensor=false&maptype=satellite

This URL should give you a nice image of Denver.

The API has only a couple of parameters that are a part of the URL:

  • center This is where you put the address. Since it uses the Google Maps engine, you can be pretty flexible about how you enter the address. It can be a specific address, a town, the latitude and longitude, or just about anything else map related. You might need to replace all your spaces with %20, depending on which program/browser you are using. It won’t hurt in any case.
  • zoom This determines the level of detail. A value of 1 shows the entire earth. A value of 20 and you can count the number of people in an intersection. Depending on what you are doing, you most likely want something in the 15-20 range.
  • size How big do you want the picture in pixels? This determines both the size of the resulting image and how big an area you capture. At a zoom of 20, 1000x1000 shows the entire intersection, while 50x50 covers just the crosswalk.
  • sensor This is always false, unless you are doing this from your iPhone.

    maptype Satellite returns the picture from space; roadmap returns the default Google Maps view, and hybrid blends them.

    You probably want to play around with all these parameters until you get something that best tells the story you want to convey.

    Since the data is accessible through a HTTP GET request and because the resulting file is a picture and not some complicated JSON or XML object that you need to read, you don’t need any fancy software to download the images. It is possible to just use your browser to search, save the files and then put them together in your Powerpoint slide. Or, if you wanted to download a bunch of them in an automated fashion and your address data is already in Stata, you can use Stata’s under-appreciated “copy” command. The following lines show you a sample .do file that downloads four images to your current hard drive:

    *Fill in some random data
    clear all
    set obs 4
    gen double latitude=.
    gen double longitude=.

    replace latitude=41.167937 in 1
    replace longitude=-073.476747 in 1
    replace latitude=40.809560 in 2
    replace longitude=-073.571619 in 2
    replace latitude=29.751209 in 3
    replace longitude=-095.433984 in 3  
    replace latitude=26.432297 in 4
    replace longitude=-080.158781 in 4
    gen tract=_n*100

    *Loop over the four observations
    forvalues i=1/4 {
        *store the tract info in local macros
        local latitude= latitude[`i']
        local longitude= longitude[`i']
        local tract=tract[`i']

        *Store the Google Maps paramaters in local macors
        local size="200x200"
        local zoom="17"
        local url `"`latitude',`longitude'&zoom=`zoom'&size=`size'&maptype=satellite&sensor=false"'

        *Download the file from the internet
        copy "`url'" "`tract'.png", replace

    The downloaded images are in the PNG format, so you can just drag them into your presentation.

    To make the pictures above, I used a little script in Python. The advantage that Python has over something like Stata in this case is the ability to manipulate images. I didn’t want to do anything fancy, but I did want to put together multiple images into one big picture and crop out the copyright text at the bottom. I used the Image module for this, and the syntax is fairly straightforward (e.g., to crop your image you can use square=square.crop((0,0,200,200))). Feel free to check out the full script for the Superfund image.

    The first step in Python is to open up your data file. Assuming, for example, that the images you wanted to get were in a CSV-formatted file with the identifier in the first column, followed by the latitude and longitude, you could read in the file with:

    #import the built-in module for reading csv data
    import csv

    #open and read the file and store it in the list sites
    sites=csv.reader(open('superfund.csv', 'rb'))

    To get the pictures, loop over each line, extract the coordinates, and download the png:

    #import a module for accessing the internet
    import urllib

    for site in sites:
        #get the information you need from the site

        url=',%s&zoom=%s&size=%s&maptype=satellite&sensor=false' % (latitude,longitude,zoom,size)

    The Python script follows the same logic as the Stata .do file. One unfamiliar part may be in the construction of the URL, where I used the string format operator %. Everywhere I wanted to include one of my values (e.g., zoom) in the URL, I put a % in the URL. Then, after the string, I put a % followed by a tuple that contained each of the values I wanted to include. This is the more Pythonic way to construct a string. In this case, it has the advantage of reducing the number of plus signs and string() commands in the statement, making it more readable.

    A better version of the script would probably put the download into its own function, checking first whether you had already downloaded the file so that you only download any given file once and alert you when the download doesn’t work. If you were downloading a large number of pictures, you would want to apply for a developer key from Google. They are free and allow you to download up to 25,000 images a day. The other great thing about the developer key is that you just add &key= followed by your key to use them, as opposed to more complicated methods of authenticating used by sites like Facebook and Twitter, such as OAuth authentication.

    In sum, Google Maps API offers a useful research tool that’s easy to use and sounds impressive.

    Bonus: Street View has similar capabilities, but its much harder to get an interesting picture in an automated fashion. You control what angle the picture is taken at relative to the compass directions, but what you really want is to take it relative to the car. Too often, you just get pictures of the road ahead, which are somewhat boring. Street View also has much less coverage–most of the wealthy neighborhoods don’t have images available. Here’s what the wealthiest neighborhoods look like from a car, compared to the poorest neighborhoods.

  • About Neal Caren

    This entry was posted in Uncategorized. Bookmark the permalink.

    Comments are closed.