-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcd112_snap_rate.py
60 lines (47 loc) · 1.29 KB
/
cd112_snap_rate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
### colorize_svg.py
import csv
from BeautifulSoup import BeautifulSoup
# Read in SNAP participation rates
snap_rate = {}
min_value = 100; max_value = 0
reader = csv.reader(open('districtData.csv'), delimiter=",")
for row in reader:
try:
dist_name = "\"" + row[1] + "\""
#print dist_name
totpop = float(row[2].strip())
snappop = float(row[3].strip())
#print rate
snap_rate[dist_name] = snappop/totpop
#print snap_rate[dist_name]
except:
pass
# Load the SVG map
svg = open('112th_US_House.svg', 'r').read()
# Load into Beautiful Soup
soup = BeautifulSoup(svg, selfClosingTags=['defs','sodipodi:namedview'])
# Find counties
paths = soup.findAll('path')
# Map colors
colors = ["#C2C2C2", "#E1E1E1", "#9ECAE1", "#6BAED6", "#3182BD", "08519C"]
for p in paths:
if (p['id'] != "State_Border") & (p['id'] != "State_Borders"):
try:
rate = snap_rate["\""+p['id']+"\""]
except:
continue
if rate > 0.4:
color_class = 5
elif rate > 0.25:
color_class = 4
elif rate > 0.2:
color_class = 3
elif rate > 0.143:
color_class = 2
elif rate > 0.07:
color_class = 1
else:
color_class = 0
color = colors[color_class]
p['fill'] = color
print soup.prettify()