forked from storiesofsolidarity/us-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit_zipcodes.py
67 lines (55 loc) · 1.84 KB
/
split_zipcodes.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
import sys, csv
from constants import STATE_ABBR
from utils import relative_path, load_csv_columns, split_dict_by, write_json
# map colum names from csv db to output json
ZIPCODE_COLUMNS = {
'zip': 'zip',
'primary_city': 'city',
'acceptable_cities': 'other_cities',
'state': 'state',
'county': 'county',
'latitude': 'lat',
'longitude': 'lon',
}
def geojson_feature(d):
return {
"type": "Feature",
"id": d['zip'],
"geometry": {
"type": "Point",
"coordinates": [float(d['lon']), float(d['lat'])]
},
"properties": {
"city": d['city'],
"other_cities": d['other_cities'],
"state": d['state'],
"county": d['county'],
}
}
if __name__ == "__main__":
if len(sys.argv) > 1:
fn = sys.argv[1]
else:
fn = relative_path("../raw/zip_code_database.csv")
try:
zipcode_db = load_csv_columns(fn, ZIPCODE_COLUMNS)
except IOError:
print "unable to load", fn
sys.exit(-1)
print "loaded %s zipcodes" % len(zipcode_db)
for (abbr, data) in split_dict_by(zipcode_db, 'state').items():
state_name = STATE_ABBR.get(abbr, '').replace(' ', '_')
geojson_collection = {
"type": "FeatureCollection",
"features": list()
}
for place in data:
geojson_collection['features'].append(geojson_feature(place))
print "writing %d places in %s" % (len(data), state_name)
out_fn = relative_path('../zipcodes/%s.geo.json' % state_name)
write_json(out_fn, geojson_collection)
print "writing %d places in %s" % (len(zipcode_db), 'all')
for z in zipcode_db:
out_fn = relative_path('../zipcodes/all/%s.geo.json' % z['zip'])
data = geojson_feature(z)
write_json(out_fn, data)