-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_to_osm.py
More file actions
30 lines (24 loc) · 1.32 KB
/
Copy pathjson_to_osm.py
File metadata and controls
30 lines (24 loc) · 1.32 KB
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 json
def convert_to_osm():
with open('xuhui_roads.json', 'r', encoding='utf-8') as f:
data = json.load(f)
elements = data.get('elements', [])
with open('xuhui_roads.osm', 'w', encoding='utf-8') as out:
out.write('<?xml version="1.0" encoding="UTF-8"?>\n')
out.write('<osm version="0.6" generator="python_script">\n')
for elem in elements:
if elem['type'] == 'node':
out.write(f' <node id="{elem["id"]}" lat="{elem["lat"]}" lon="{elem["lon"]}"/>\n')
elif elem['type'] == 'way':
out.write(f' <way id="{elem["id"]}">\n')
for nid in elem.get('nodes', []):
out.write(f' <nd ref="{nid}"/>\n')
for k, v in elem.get('tags', {}).items():
k_clean = str(k).replace('"', '"').replace('<', '<').replace('>', '>').replace('&', '&')
v_clean = str(v).replace('"', '"').replace('<', '<').replace('>', '>').replace('&', '&')
out.write(f' <tag k="{k_clean}" v="{v_clean}"/>\n')
out.write(' </way>\n')
out.write('</osm>\n')
print("Successfully converted xuhui_roads.json to xuhui_roads.osm")
if __name__ == '__main__':
convert_to_osm()