-
Notifications
You must be signed in to change notification settings - Fork 5
/
OsmData.py
317 lines (310 loc) · 12.9 KB
/
OsmData.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# OsmData.py
#
# Data processing routines for CommandLine plugin
#
# Copyright 2010-2011 Hind <[email protected]>
# modified by OverQuantum:
# 2014-12-12 Deleting section reversed - relations 1st, then ways and nodes (to not break data consistency inside command stack)
# 2014-12-13 Fix to 4 spaces indentation; added this header
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
import xml.sax
import copy
LON = 1
LAT = 2
REF = 3
TAG = 4
ACTION = 5
VERSION = 6
USER = 7
UID = 8
CHANGESET = 9
CREATE = 0
MODIFY = 1
DELETE = 2
NODES = 0
WAYS = 1
RELATIONS = 2
class OsmData(xml.sax.ContentHandler):
def __init__(self):
self.nodes = {}
self.ways = {}
self.relations = {}
self.comments = []
self.bbox = [0, 0, 0, 0] # minlon, minlat, maxlon, maxlat
self.currnodeid = -1
self.currwayid = -1
self.currrelationid = -1
self.currentObj = None
self.currentId = None
def addnode(self, Id=0):
if Id == 0:
while self.nodes.get(self.currnodeid) != None:
self.currnodeid -= 1
self.nodes[self.currnodeid] = {LON: 0, LAT: 0, ACTION: CREATE, TAG: {}}
return self.currnodeid
else:
self.nodes[Id] = {ACTION: MODIFY}
return Id
def addway(self, Id=0):
if Id == 0:
while self.ways.get(self.currwayid) != None:
self.currwayid -= 1
self.ways[self.currwayid] = {ACTION: CREATE, TAG: {}, REF: []}
return self.currwayid
else:
self.ways[Id] = {ACTION: MODIFY}
return Id
def mergedata(self, other):
self.nodes.update(copy.deepcopy(other.nodes))
self.ways.update(copy.deepcopy(other.ways))
self.relations.update(copy.deepcopy(other.relations))
def addcomment(self, text):
self.comments += [text]
def read(self, sourceStream):
line = ''
iparser = xml.sax.make_parser()
iparser.setContentHandler(self)
#f = open("sad.osm", "a")
while line.find('</osm>') == -1:
line = sourceStream.readline()
iparser.feed(line)
#f.write(line)
#f.close()
def write(self, targetStream):
targetStream.write("<osm version=\"0.6\">\n")
# Creating
for node in self.nodes.items():
if node[1].get(ACTION) != CREATE:
continue
targetStream.write(self.xmlnode(node))
for way in self.ways.items():
if way[1].get(ACTION) != CREATE:
continue
targetStream.write(self.xmlway(way))
for relation in self.relations.items():
if relation[1].get(ACTION) != CREATE:
continue
targetStream.write(self.xmlrelation(relation))
# Modifying
for node in self.nodes.items():
if node[1].get(ACTION) != MODIFY:
continue
targetStream.write(self.xmlnode(node))
for way in self.ways.items():
if way[1].get(ACTION) != MODIFY:
continue
targetStream.write(self.xmlway(way))
for relation in self.relations.items():
if relation[1].get(ACTION) != MODIFY:
continue
targetStream.write(self.xmlrelation(relation))
# Deleting
for relation in self.relations.items():
if relation[1].get(ACTION) != DELETE:
continue
targetStream.write(self.xmlrelation(relation))
for way in self.ways.items():
if way[1].get(ACTION) != DELETE:
continue
targetStream.write(self.xmlway(way))
for node in self.nodes.items():
if node[1].get(ACTION) != DELETE:
continue
targetStream.write(self.xmlnode(node))
for text in self.comments:
targetStream.write("<!--" + text + "-->\n")
targetStream.write("</osm>")
def xmlnode(self, node):
string = ("<node id='" + str(node[0]) + "' ");
tags = {}
for attr in node[1].items():
if attr[0] == ACTION:
if attr[1] == MODIFY:
string += ("action='modify' ");
elif attr[1] == DELETE:
string += ("action='delete' ");
elif attr[0] == VERSION:
string += ("version='" + str(attr[1]) + "' ")
elif attr[0] == CHANGESET:
string += ("changeset='" + str(attr[1]) + "' ")
elif attr[0] == UID:
string += ("uid='" + str(attr[1]) + "' ")
elif attr[0] == LAT:
string += ("lat='" + str(attr[1]) + "' ")
elif attr[0] == LON:
string += ("lon='" + str(attr[1]) + "' ")
elif attr[0] == USER:
string += ("user='" + str(attr[1]) + "' ")
elif attr[0] == "timestamp" or attr[0] == "visible":
string += (attr[0] + "='" + str(attr[1]) + "' ")
if len(node[1][TAG]) > 0:
string += (">\n")
for tag in node[1][TAG].items():
string += ("<tag k='" + str(tag[0]) + "' v='" + str(tag[1]) + "' />\n")
string += ("</node>\n")
else:
string += ("/>\n")
return string
def xmlway(self, way):
string = ("<way id='" + str(way[0]) + "' ");
tags = {}
for attr in way[1].items():
if attr[0] == ACTION:
if attr[1] == MODIFY:
string += ("action='modify' ");
elif attr[1] == DELETE:
string += ("action='delete' ");
elif attr[0] == VERSION:
string += ("version='" + str(attr[1]) + "' ")
elif attr[0] == CHANGESET:
string += ("changeset='" + str(attr[1]) + "' ")
elif attr[0] == UID:
string += ("uid='" + str(attr[1]) + "' ")
elif attr[0] == USER:
string += ("user='" + str(attr[1]) + "' ")
elif attr[0] == "timestamp" or attr[0] == "visible":
string += (attr[0] + "='" + str(attr[1]) + "' ")
string += (">\n")
for ref in way[1][REF]:
string += ("<nd ref='" + str(ref) + "' />\n")
for tag in way[1][TAG].items():
string += ("<tag k='" + str(tag[0]) + "' v='" + str(tag[1]) + "' />\n")
string += ("</way>\n")
return string
def xmlrelation(self, relation):
string = ("<relation id='" + str(relation[0]) + "' ");
tags = {}
for attr in relation[1].items():
if attr[0] == ACTION:
if attr[1] == MODIFY:
string += ("action='modify' ");
elif attr[1] == DELETE:
string += ("action='delete' ");
elif attr[0] == VERSION:
string += ("version='" + str(attr[1]) + "' ")
elif attr[0] == CHANGESET:
string += ("changeset='" + str(attr[1]) + "' ")
elif attr[0] == UID:
string += ("uid='" + str(attr[1]) + "' ")
elif attr[0] == USER:
string += ("user='" + str(attr[1]) + "' ")
elif attr[0] == "timestamp" or attr[0] == "visible":
string += (attr[0] + "='" + str(attr[1]) + "' ")
string += (">\n")
for ref in relation[1][REF][NODES]:
string += ("<member type='node' ref='" + str(ref[0]) + "' role='" + str(ref[1]) + "' />\n")
for ref in relation[1][REF][WAYS]:
string += ("<member type='way' ref='" + str(ref[0]) + "' role='" + str(ref[1]) + "' />\n")
for ref in relation[1][REF][RELATIONS]:
string += ("<member type='relation' ref='" + str(ref[0]) + "' role='" + str(ref[1]) + "' />\n")
for tag in relation[1][TAG].items():
string += ("<tag k='" + str(tag[0]) + "' v='" + str(tag[1]) + "' />\n")
string += ("</relation>\n")
return string
def startElement(self, tag, attributes):
if tag == "node":
self.currentObj = {}
for attr in attributes.items():
if attr[0] == "id":
self.currentId = int(attr[1])
elif attr[0] == "version":
self.currentObj[VERSION] = int(attr[1])
elif attr[0] == "changeset":
self.currentObj[CHANGESET] = int(attr[1])
elif attr[0] == "uid":
self.currentObj[UID] = int(attr[1])
elif attr[0] == "lat":
self.currentObj[LAT] = float(attr[1])
if self.currentObj[LAT] < self.bbox[1] or self.bbox[1] == 0:
self.bbox[1] = self.currentObj[LAT]
if self.currentObj[LAT] > self.bbox[3] or self.bbox[3] == 0:
self.bbox[3] = self.currentObj[LAT]
elif attr[0] == "lon":
self.currentObj[LON] = float(attr[1])
if self.currentObj[LON] < self.bbox[0] or self.bbox[0] == 0:
self.bbox[0] = self.currentObj[LON]
if self.currentObj[LON] > self.bbox[2] or self.bbox[2] == 0:
self.bbox[2] = self.currentObj[LON]
elif attr[0] == "user":
self.currentObj[USER] = attr[1]
self.currentObj[TAG] = {}
elif tag == "way":
self.currentObj = {}
for attr in attributes.items():
if attr[0] == "id":
self.currentId = int(attr[1])
elif attr[0] == "version":
self.currentObj[VERSION] = int(attr[1])
elif attr[0] == "changeset":
self.currentObj[CHANGESET] = int(attr[1])
elif attr[0] == "uid":
self.currentObj[UID] = int(attr[1])
elif attr[0] == "user":
self.currentObj[USER] = attr[1]
self.currentObj[TAG] = {}
self.currentObj[REF] = []
elif tag == "relation":
self.currentObj = {}
for attr in attributes.items():
if attr[0] == "id":
self.currentId = int(attr[1])
elif attr[0] == "version":
self.currentObj[VERSION] = int(attr[1])
elif attr[0] == "changeset":
self.currentObj[CHANGESET] = int(attr[1])
elif attr[0] == "uid":
self.currentObj[UID] = int(attr[1])
elif attr[0] == "user":
self.currentObj[USER] = attr[1]
self.currentObj[TAG] = {}
self.currentObj[REF] = [[], [], []] # [Nodes, Ways, Relations]
elif tag == "tag":
self.currentObj[TAG][attributes.get("k")] = attributes.get("v")
elif tag == "nd":
for attr in attributes.items():
if attr[0] == "ref":
self.currentObj[REF].append(int(attr[1]))
elif tag == "member":
mtype = attributes.get("type")
if mtype == "node":
self.currentObj[REF][NODES].append((int(attributes.get("ref")), attributes.get("role")))
elif mtype == "way":
self.currentObj[REF][WAYS].append((int(attributes.get("ref")), attributes.get("role")))
elif mtype == "relation":
self.currentObj[REF][RELATIONS].append((int(attributes.get("ref")), attributes.get("role")))
def endElement(self, tag):
if self.currentId != None:
if tag == "node":
self.nodes[self.currentId] = self.currentObj
elif tag == "way":
self.ways[self.currentId] = self.currentObj
elif tag == "relation":
self.relations[self.currentId] = self.currentObj
class Map():
def __init__(self):
self.number = 0
self.omap = {}
def __getitem__(self, oldkey):
newkey = self.omap.get(oldkey)
if newkey == None:
self.number -= 1
newkey = self.number
self.omap[oldkey] = newkey
return newkey