/
twptranslation.py
347 lines (327 loc) · 11.8 KB
/
twptranslation.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import geom
import csv
import os
__location__=os.path.dirname(os.path.realpath(__file__))
def filterTags(tags):
if tags is None:
return
newtags = {}
# append " Township"? could also use LABEL
for t in ["name","county"]:
if t in tags:
newtags[t]=tags[t]
if "border_typ" in tags:
t=tags["border_typ"]
newtags["border_type"]=t.lower()
if t=="Township":
newtags["admin_level"]="7"
newtags["boundary"]="administrative"
# add gnis and wikidata tags.
if "ID" in tags and tags["ID"]!="":
newtags["gnis:feature_id"]=tags["ID"]
if "wikidata" in tags:
newtags["wikidata"]=tags["wikidata"]
if "wikipedia" in tags:
newtags["wikipedia"]=tags["wikipedia"]
if t=="City":
newtags["admin_level"]="8"
newtags["boundary"]="administrative"
return newtags
def splitWay(way, corners, features_map):
idxs=[i for i,c in enumerate(way.points) if c in corners]
splitends=0 in idxs or (len(way.points)-1) in idxs
new_points=list()
left=0
for cut in idxs:
if cut!=0:
new_points.append(way.points[left:(cut+1)])
left=cut
# remainder
if left < len(way.points)-1:
#closed way
if not splitends and isClosed(way):
new_points[0]=way.points[left:-1]+new_points[0]
else:
new_points.append(way.points[left:])
#~ print(len(way.points),[len(p) for p in new_points])
new_ways = [way, ] + [geom.Way() for i in range(len(new_points) - 1)]
if way in features_map:
way_tags = features_map[way].tags
for new_way in new_ways:
if new_way != way:
feat = geom.Feature()
feat.geometry = new_way
feat.tags = way_tags
new_way.addparent(feat)
for new_way, points in zip(new_ways, new_points):
new_way.points = points
if new_way.id != way.id:
for point in points:
point.removeparent(way, shoulddestroy=False)
point.addparent(new_way)
return new_ways
def mergeIntoNewRelation(way_parts):
new_relation = geom.Relation()
feat = geom.Feature()
feat.geometry = new_relation
new_relation.members = [(way, "outer") for way in way_parts]
for way in way_parts:
way.addparent(new_relation)
return feat
def splitWayInRelation(rel, way_parts):
way_roles = [m[1] for m in rel.members if m[0] == way_parts[0]]
way_role = "" if len(way_roles) == 0 else way_roles[0]
for way in way_parts[1:]:
way.addparent(rel)
rel.members.append((way, way_role))
def findSharedVertices(geometries):
points = [g for g in geometries if isinstance(g, geom.Point)]
vertices=list()
for p in points:
neighbors=set()
for way in p.parents:
for idx in findAll(way, p):
for step in [-1,1]:
pt=way.points[(idx+step)%len(way.points)]
if pt!=p:
neighbors.add(pt)
if len(neighbors) > 2:
vertices.append(p)
return vertices
def findSelfIntersections(way):
intersections=list()
seen=set()
points=way.points
if isClosed(way):
points=points[:-1]
for point in points:
if point in seen:
intersections.append(point)
seen.add(point)
return intersections
def similar(way1, way2):
if len(way1.points)!=len(way2.points):
return False
w1=[w.id for w in way1.points]
w2=[w.id for w in way2.points]
if w1[0] not in w2:
return False
if set(w1) != set(w2):
return False
# closed way
if w1[0]==w1[-1]:
idx1=w1.index(min(w1))
w1=w1[idx1:-1]+w1[:idx1]
if w2[0]==w2[-1]:
idx2=w2.index(min(w2))
w2=w2[idx2:-1]+w2[:idx2]
# second way is not closed
else:
return False
if w1==w2:
return True
if w1==w2[:1]+w2[1:][::-1]:
return True
else:
if w1==w2:
return True
if w1==w2[::-1]:
return True
return False
def isClosed(way):
return way.points[0] == way.points[-1]
def findAll(way, node, start=0):
i = start - 1
while True:
try:
i = way.points.index(node, i+1)
yield i
except ValueError:
break
def preOutputTransform(geometries, features):
if geometries is None and features is None:
return
lint(geometries, features,"At entry",True)
print("Patching features")
for f in features:
if f not in f.geometry.parents:
f.geometry.addparent(f)
lint(geometries, features,"After parent fix",True)
print("Moving tags to relations")
# move tags, remove member ways as Features.
rels=[g for g in geometries if isinstance(g, geom.Relation)]
featuresmap = {feature.geometry : feature for feature in features}
for rel in rels:
relfeat=featuresmap[rel]
# splitWayInRelation does not add the relation as a parent.
for member,role in rel.members:
if rel not in member.parents:
member.addparent(rel)
if relfeat.tags=={}:
outers=[m[0] for m in rel.members if m[1]=="outer"]
relfeat.tags.update(featuresmap[outers[0]].tags)
for member,role in rel.members:
if member in featuresmap:
memberfeature=featuresmap[member]
del featuresmap[member]
member.removeparent(memberfeature)
features.remove(memberfeature)
else:
pass
#~ print("Relation {} has tags.".format(rel.id),relfeat.tags)
# create relations for ways that are features.
ways = [g for g in geometries if isinstance(g, geom.Way)]
for way in ways:
if way in featuresmap:
feature=featuresmap[way]
newrel = geom.Relation()
way.addparent(newrel)
newrel.members.append((way,"outer"))
feature.replacejwithi(newrel, way)
featuresmap[newrel]=feature
del featuresmap[way]
lint(geometries, features,"Before split")
print("Finding shared vertices")
corners = set(findSharedVertices(geometries))
print("Splitting ways")
for way in ways:
is_way_in_relation = len([p for p in way.parents if isinstance(p, geom.Relation)]) > 0
thesecorners = corners.intersection(way.points)
#~ if intersections:
#~ print(len(intersections))
if len(thesecorners) > 0:
way_parts = splitWay(way, thesecorners, featuresmap)
if not is_way_in_relation:
rel = mergeIntoNewRelation(way_parts)
featuresmap[rel.geometry] = rel
if way in featuresmap:
rel.tags.update(featuresmap[way].tags)
for wg,role in rel.geometry.members:
if wg in featuresmap:
wg.removeparent(featuresmap[wg])
else:
for parent in way.parents:
if isinstance(parent, geom.Relation):
splitWayInRelation(parent, way_parts)
lint(geometries, features,"After split")
print("Merging relations")
worklist = sorted([g for g in geometries if isinstance(g, geom.Way)], key=lambda g: len(g.points))
# combine duplicate ways.
removed=list()
comparisons=0
for way in list(worklist):
# skip ways that are already gone
worklist.remove(way)
if way in removed:
continue
for otherway in worklist:
if len(otherway.points) > len(way.points):
break
comparisons+=1
if similar(way,otherway):
for parent in list(otherway.parents):
if isinstance(parent, geom.Relation):
parent.replacejwithi(way, otherway)
removed.append(otherway)
print("Comparisons:",comparisons)
#~ # results in duplicates
#~ worklist = [g for g in geometries if isinstance(g, geom.Way)]
#~ for way in worklist:
#~ # similar ways must share first point
#~ for otherway in way.points[0].parents:
#~ # skip self and ways that are already merged
#~ if otherway==way or way in removed:
#~ continue
#~ \ if similar(way,otherway):
#~ for parent in list(otherway.parents):
#~ if isinstance(parent, geom.Relation):
#~ parent.replacejwithi(way, otherway)
#~ removed.append(otherway)
# merge adjacent ways
#~ ways = [g for g in geometries if isinstance(g, geom.Way)]
#~ junctions=set()
#~ for way in ways:
#~ for point in [way.points[0],way.points[-1]]:
#~ if len(point.parents) == 2:
#~ junctions.add(point)
#~ print(len(junctions))
#~ c=0
#~ for j in junctions:
#~ for p in j.parents:
#~ for p2 in p.parents:
#~ for p3 in p2.parents:
#~ print(j.id,p3.tags["name"],len(p.points))
#~ if c==10:
#~ break
#~ c+=1
# add tags to all ways
print("Tagging member ways")
ways=[g for g in geometries if isinstance(g, geom.Way)]
featuresmap = {feature.geometry : feature for feature in features}
for way in ways:
for parent in way.parents:
admin_levels=[]
boundaries=[]
if parent in featuresmap:
parfeat=featuresmap[parent]
if "admin_level" in parfeat.tags:
admin_levels.append(parfeat.tags["admin_level"])
if "boundary" in parfeat.tags:
boundaries.append(parfeat.tags["boundary"])
newtags={}
if admin_levels:
newtags["admin_level"]=min(admin_levels,key=int)
if boundaries:
newtags["boundary"]=boundaries.pop()
if newtags:
if way not in featuresmap:
feat = geom.Feature()
feat.geometry = way
way.addparent(feat)
else:
feat = featuresmap[way]
feat.tags.update(newtags)
lint(geometries, features,"After combine.")
for feat in features:
if isinstance(feat.geometry, geom.Relation):
feat.tags["type"]="boundary"
def lint(geometries, features, message="", blat=False):
if False:
return
ways=[g for g in geometries if isinstance(g, geom.Way)]
rels=[g for g in geometries if isinstance(g, geom.Relation)]
results=list()
# check for geometries with no parents.
noparents=list()
results.append(("{} geometries with no parents.",noparents))
for geo in geometries:
if len(geo.parents)==0:
noparents.append(geo)
# check for features not listed as parents
notlisted=list()
results.append(("{} features not used as parents.",notlisted))
for f in features:
if f not in f.geometry.parents:
notlisted.append(f)
# check for duplicate nodes in ways
dupenodes=list()
results.append(("{} duplicate nodes in ways.",dupenodes))
onenodeways=list()
results.append(("{} one node ways.",onenodeways))
count=0
for way in ways:
for i in range(1,len(way.points)):
if way.points[i-1]==way.points[i]:
dupenodes.append(way, i)
count+=1
if len(way.points)==1:
onenodeways.append(way)
count+=1
if message and any(result[1] for result in results):
print(message)
for msg, result in results:
if result:
print(msg.format(len(result)))
if blat:
for p in result:
print(p)