-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathosm-to-json.py
37 lines (26 loc) · 996 Bytes
/
osm-to-json.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
from sys import stdin, argv
from math import hypot, ceil
from json import dump
from shapely.geometry import Polygon, MultiLineString
from Skeletron import network_multiline, multiline_polygon, polygon_skeleton, skeleton_routes
from Skeletron.input import ParserOSM, merc
p = ParserOSM()
g = p.parse(stdin)
output = dict(type='FeatureCollection', features=[])
for key in g:
print key
network = g[key]
if not network.edges():
continue
lines = network_multiline(network)
poly = multiline_polygon(lines)
skeleton = polygon_skeleton(poly)
routes = skeleton_routes(skeleton)
if not routes:
continue
coords = [[merc(*point, inverse=True) for point in route] for route in routes]
geometry = MultiLineString(coords).__geo_interface__
properties = dict(name=key[0], highway=key[1])
feature = dict(geometry=geometry, properties=properties)
output['features'].append(feature)
dump(output, open(argv[2], 'w'))