2 # -* encoding: utf8 -*-
8 nodes
= gzip
.open("nodes.tsv.gz", "w")
9 node_tags
= gzip
.open("node_tags.tsv.gz", "w")
10 ways
= gzip
.open("ways.tsv.gz", "w")
11 way_tags
= gzip
.open("way_tags.tsv.gz", "w")
12 way_nodes
= gzip
.open("way_nodes.tsv.gz", "w")
13 relations
= gzip
.open("relations.tsv.gz", "w")
14 relation_tags
= gzip
.open("relation_tags.tsv.gz", "w")
15 relation_members
= gzip
.open("relation_members.tsv.gz", "w")
18 values
= [unicode(x
).replace("\t", "\\t") for x
in values
]
19 return ("\t".join(values
) + "\n").encode("utf8")
21 class OSMtoTSV(xml
.sax
.ContentHandler
):
26 def startElement(self
, name
, attrs
):
27 func_name
= "start_%s" % name
28 if hasattr(self
, func_name
) and callable(getattr(self
, func_name
)):
29 getattr(self
, func_name
)(attrs
)
32 def start_node(self
, attrs
):
33 nodes
.write(tab([attrs
.get(x
, "NULL") for x
in ('id', 'timestamp', 'user', 'lat', 'lon')]))
41 def start_way(self
, attrs
):
42 ways
.write(tab([attrs
.get(x
, "NULL") for x
in ('id', 'timestamp', 'user')]))
49 self
.node_offset
= None
52 def start_tag(self
, attrs
):
53 if self
.type == 'node':
54 node_tags
.write(tab([self
.id, attrs
['k'], attrs
['v']]))
55 elif self
.type == 'way':
56 way_tags
.write(tab([self
.id, attrs
['k'], attrs
['v']]))
57 elif self
.type == 'relation':
58 relation_tags
.write(tab([self
.id, attrs
['k'], attrs
['v']]))
60 assert False, "Got a tag of %s" % self
.type
62 def start_nd(self
, attrs
):
63 assert self
.type == 'way'
64 assert self
.node_offset
is not None
65 way_nodes
.write(tab([self
.id, self
.node_offset
, attrs
['ref']]))
68 def start_relation(self
, attrs
):
69 relations
.write(tab([attrs
.get(x
, "NULL") for x
in ('id', 'timestamp', 'user')]))
70 self
.type = "relation"
72 self
.member_offset
= 0
74 def end_relation(self
):
77 self
.member_offset
= None
79 def start_member(self
, attrs
):
80 assert self
.member_offset
is not None
81 relation_members
.write(tab([self
.id, attrs
['type'], attrs
['ref'], attrs
.get('role', "NULL"), self
.member_offset
]))
82 self
.member_offset
+= 1
85 def endElement(self
, name
):
86 func_name
= "end_%s" % name
87 if hasattr(self
, func_name
) and callable(getattr(self
, func_name
)):
88 getattr(self
, func_name
)()
91 xml
.sax
.parse(sys
.stdin
, OSMtoTSV())