Graphics Programs Reference
In-Depth Information
Open the SVG file in a text editor. It is of course all text formatted as
XML, but it's formatted slightly differently than your counties example.
Paths don't have useful ids and the style attribute is not used. The paths
do, however, have classes that look like country codes. They have only
two letters, though. The country codes used in the World Bank data have
three letters.
According to World Bank documentation, it uses ISO 3166-1 alpha 3 codes.
The SVG file from Wikipedia, on the other hand, uses ISO 3166-1 alpha 2
codes. The names are horrible, I know, but don't worry; you don't have to
remember that. All you need to know is that Wikipedia provides a conver-
sion chart at http://en.wikipedia.org/wiki/ISO_3166-1 . I copied and pasted
the table into excel and then saved the important bits as a text file. It has
one column for the alpha 2 and another for the alpha 3. Download it here:
http://book.flowingdata.com/ch08/worldmap/country-codes.txt . Use this
chart to switch between the two codes.
As for styling each country, take a slightly different route to do that, too.
Instead of changing attributes directly in the path tags, use CSS outside of
the paths to color the regions. Now jump right in.
Create a file named generate_css.py in the same directory as the SVG and
CSV files. Again, import the CSV package to load the data in the CSV files
with the country codes and water access percentages.
import csv
codereader = csv.reader(open('country-codes.txt', 'r'), delimiter=”\t”)
waterreader = csv.reader(open('water-source1.txt', 'r'), delimiter=”\t”)
Then store the country codes so that it's easy to switch from alpha 3 to
alpha 2.
alpha3to2 = {}
i = 0
next(codereader)
for row in codereader:
alpha3to2[row[1]] = row[0]
This stores the codes in a Python dictionary where alpha 3 is the key and
alpha 2 is the value.
Search WWH ::




Custom Search