-
Notifications
You must be signed in to change notification settings - Fork 0
/
wazidata.py
89 lines (82 loc) · 2.5 KB
/
wazidata.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import csv, sys, requests
# Returns the header fields of a CSV
def headerfields(filename):
with open(filename, 'rb') as csvfile:
csvreader = csv.reader(csvfile)
return csvreader.next()
def points(filename):
points = []
with open(filename, 'rb') as csvfile:
csvreader = csv.DictReader(csvfile)
for row in csvreader:
points.append(row)
return points
def wards(points):
wards = []
for point in points:
lat = point['Latitude']
lng = point['Longitude']
addr = lat + "," + lng
url = "http://wards.code4sa.org/?database=wards_2011&address=" + addr
print "Fetching ", url
r = requests.get(url)
ward_data = r.json()
point["ward"] = ward_data[0]["ward"]
print "Ward ", ward_data[0]["ward"]
wards.append(point)
# print point
return wards
def wazi(wards):
wazi = []
tables = {"ELECTRICITYFORCOOKING_ELECTRICITYFORHEATING_ELECTR": "Electricity", "SOURCEOFWATER": "Water", "TOILETFACILITIES": "Toilets"}
for ward in wards:
for table in tables:
url = "http://wazimap.co.za/api/1.0/data/show/latest?table_ids=" + table + "&geo_ids=ward-" + ward["ward"]
print "Fetching ", url
r = requests.get(url)
wazi_data = r.json()
cols = wazi_data["tables"][table]["columns"]
data = wazi_data["data"]["ward-" + ward["ward"]][table]["estimate"]
# print data
new_data = {}
for key in data:
if cols[key]["name"] in new_data:
new_data[cols[key]["name"]] = new_data[cols[key]["name"]] + data[key]
else:
new_data[cols[key]["name"]] = data[key]
ward[tables[table]] = new_data
wazi.append(ward)
return wazi
def reduce_data(wazi, headerfields):
reduced = []
for row in wazi:
newrow = {}
for col in row:
if type(row[col]) is dict:
for key in row[col]:
newkey = col + "." + key
newval = row[col][key]
newrow[newkey] = newval
if not newkey in headerfields:
headerfields.append(newkey)
else:
newrow[col] = row[col]
reduced.append(newrow)
return reduced, headerfields
def output_csv(reduced, output_filename, headerfields):
with open(output_filename, 'wb') as csvfile:
dictwriter = csv.DictWriter(csvfile, headerfields)
dictwriter.writeheader()
dictwriter.writerows(reduced)
print "Wrote file ", output_filename
pass
if __name__ == "__main__":
filename = sys.argv[1]
output_filename = sys.argv[2]
headerfields = headerfields(filename)
headerfields.append("ward")
points = points(filename)
wards = wards(points)
wazi = wazi(wards)
reduced, headerfields = reduce_data(wazi, headerfields)
output_csv(reduced, output_filename, headerfields)