Graphics Programs Reference
In-Depth Information
and parse the SVG file with Beautiful Soup, so start by importing the nec-
essary packages.
import csv
from BeautifulSoup import BeautifulSoup
Then open the CSV file and store it so that you can iterate through the
rows using csv.reader() . Note that the “r” in the open() function just means
that you want to open the file to read its contents, as opposed to writing
new rows to it.
reader = csv.reader(open('unemployment-aug2010.txt', 'r'), delimiter=”,”)
Now also load the blank SVG county map.
svg = open('counties.svg', 'r').read()
Cool, you loaded everything you need to create a choropleth map. The
challenge at this point is that you need to somehow link the data to the
SVG. What is the commonality between the two? I'll give you a hint. It
has to do with each county's unique id, and I mentioned it earlier. If you
guessed FIPS codes, then you are correct!
each path in the SVG file has a unique id, which happens to be the com-
bined FIPS state and county FIPS code. each row in the unemployment
data has the state and county FIPS codes, too, but they're separate. For
example, the state FIPS code for Autauga County, Alabama, is 01, and its
county FIPS code is 001. The path id in the SVG are those two combined:
01001.
Paths in SVG files,
geographic ones in
particular, usually
have a unique id.
It's not always
FIPS code, but the
same rules apply.
You need to store the unemployment data so that you can retrieve each
county's rate by FIPS code, as we iterate through each path. If you start to
become confused, stay with me; it'll be clearer with actual code. But the
main point here is that the FIPS codes are the common bond between your
SVG and CSV, and you can use that to your advantage.
To store the unemployment data so that it's easily accessible by FIPS code
later, use a construct in Python called a dictionary. It enables you to store
and retrieve values by a keyword. In this case, your keyword is a combined
state and county FIPS code, as shown in the following code.
unemployment = {}
min_value = 100; max_value = 0
for row in reader:
Search WWH ::




Custom Search