-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathconvert_cities.py
30 lines (26 loc) · 957 Bytes
/
convert_cities.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
import os
import sys
import json
import argparse
def load_cities(path):
with open(path) as f:
cities = json.load(f)
features = []
for city in cities:
x1, y1, x2, y2 = [float(city['bbox'][k]) for k in ('left', 'bottom', 'right', 'top')]
feature = dict(type='Feature')
feature['geometry'] = dict(type='Polygon')
feature['geometry']['coordinates'] = [[[x1, y1], [x1, y2], [x2, y2], [x2, y1], [x1, y1]]]
feature['properties'] = dict(id=city['id'], name=city['name'])
features.append(feature)
return dict(type='FeatureCollection', features=features)
def write_extracts(path, fc):
with open(path, 'wb') as f:
json.dump(fc, f, sort_keys=True, indent=2)
if __name__ == "__main__":
inpath = sys.argv[1]
outpath = sys.argv[2]
if os.path.exists(outpath):
print "File exists, exiting: %s"%(outpath)
else:
write_extracts(outpath, load_cities(inpath))