forked from balrog-kun/osm-addr-tools
-
Notifications
You must be signed in to change notification settings - Fork 3
/
converter.py
42 lines (34 loc) · 1.07 KB
/
converter.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
import lxml.etree
import json
def convert_element(elem: lxml.etree.ElementBase):
ret = {"type": elem.tag, "tags": {}, "nodes": [], "members": []}
ret.update(elem.attrib)
for n in elem:
# child tags
if n.tag == "tag":
ret["tags"][n.get("k")] = n.get("v")
if n.tag == "nd":
ret["nodes"].append(n.get("ref"))
if n.tag == "member":
ret["members"].append(dict(n.attrib))
if n.tag == "bounds":
ret["bounds"] = dict(n.attrib)
for i in ("tags", "nodes", "members"):
if len(ret[i]) == 0:
del ret[i]
return ret
def osm_to_json(root: lxml.etree.ElementTree):
osm = root.getroot()
assert osm.tag == "osm"
return {
"version": osm.get("version"),
"generator": osm.get("generator"),
"elements": [
convert_element(x) for x in osm if x.tag in ["node", "way", "relation"]
],
}
def main():
root = lxml.etree.parse(open("1205092.osm"))
print(json.dumps(osm_to_json(root)))
if __name__ == "__main__":
main()