-
Notifications
You must be signed in to change notification settings - Fork 6
/
overpass.py
639 lines (500 loc) Β· 21.6 KB
/
overpass.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
from collections import defaultdict
from collections.abc import Iterable, Sequence
from dataclasses import replace
from itertools import chain
from typing import NamedTuple
import httpx
import xmltodict
from asyncache import cached
from cachetools import TTLCache
from fastapi import HTTPException
from starlette import status
from bus_collection_builder import build_bus_stop_collections
from config import DOWNLOAD_RELATION_GRID_CELL_EXPAND, DOWNLOAD_RELATION_WAY_BB_EXPAND, OVERPASS_API_INTERPRETER
from models.bounding_box import BoundingBox
from models.bounding_box_collection import BoundingBoxCollection
from models.download_history import Cell, DownloadHistory
from models.element_id import ElementId, element_id
from models.fetch_relation import FetchRelationBusStop, FetchRelationBusStopCollection, FetchRelationElement
from utils import get_http_client
from xmltodict_postprocessor import postprocessor
# TODO: right hand side detection by querying roundabouts, and first/last bus stop
class QueryParentsResult(NamedTuple):
id_relations_map: dict[int, list[dict]]
ways_map: dict[int, dict]
def split_by_count(elements: Iterable[dict]) -> list[list[dict]]:
result = []
current_split = []
for e in elements:
if e['type'] == 'count':
result.append(current_split)
current_split = []
else:
current_split.append(e)
assert not current_split, 'Last element must be count type'
return result
def build_bb_query(relation_id: int, timeout: int) -> str:
return f'[out:json][timeout:{timeout}];rel({relation_id});way(r);out ids bb qt;'
def build_query(
cell_bbs: Sequence[BoundingBox],
cell_bbs_expanded: Sequence[BoundingBox],
timeout: int,
route_type: str,
) -> str:
if route_type == 'bus':
return (
f'[out:json][timeout:{timeout}];'
f'(' + ''.join(f'way[highway][!footway]({bb});' for bb in cell_bbs) + ');'
'out body qt;'
'out count;'
'>;'
'out skel qt;'
'out count;'
+ ''.join(
f'node[highway=bus_stop][public_transport=platform][name]({bb});'
f'out tags center qt;'
f'nwr[highway=platform][public_transport=platform][name]({bb});'
f'out tags center qt;'
f'nwr[highway=platform][public_transport=platform][ref]({bb});'
f'out tags center qt;'
f'node[public_transport=stop_position][name]({bb});'
f'out tags center qt;'
for bb in cell_bbs_expanded
)
+ 'out count;'
'(' + ''.join(f'rel[public_transport=stop_area]({bb});' for bb in cell_bbs_expanded) + ')->.r;'
'.r out body qt;'
'.r out count;'
'node(r.r:platform);'
'out tags center qt;'
'way(r.r:platform);'
'out tags center qt;'
'rel(r.r:platform);'
'out tags center qt;'
'out count;'
'node(r.r:stop);'
'out tags center qt;'
'out count;'
)
if route_type == 'tram':
return (
f'[out:json][timeout:{timeout}];'
f'(' + ''.join(f'way[railway=tram]({bb});' for bb in cell_bbs) + ');'
'out body qt;'
'out count;'
'>;'
'out skel qt;'
'out count;'
+ ''.join(
f'node[railway=tram_stop][public_transport=stop_position][name]({bb});'
f'out tags center qt;'
f'nwr[railway=platform][public_transport=platform][name]({bb});'
f'out tags center qt;'
f'nwr[railway=platform][public_transport=platform][ref]({bb});'
f'out tags center qt;'
f'nwr[tram][public_transport=platform][name]({bb});'
f'out tags center qt;'
for bb in cell_bbs_expanded
)
+ 'out count;'
'(' + ''.join(f'rel[public_transport=stop_area]({bb});' for bb in cell_bbs_expanded) + ')->.r;'
'.r out body qt;'
'.r out count;'
'node(r.r:platform);'
'out tags center qt;'
'way(r.r:platform);'
'out tags center qt;'
'rel(r.r:platform);'
'out tags center qt;'
'out count;'
'node(r.r:stop);'
'out tags center qt;'
'out count;'
)
def build_parents_query(way_ids: Iterable[int], timeout: int) -> str:
def _parents(way_id: int) -> str:
return f'way({way_id});(rel(bw);.r;)->.r;'
return (
f'[out:xml][timeout:{timeout}];'
f'._->.r;' + ''.join(_parents(way_id) for way_id in way_ids) + '.r out meta qt;'
'way(r.r);'
'out skel qt;'
)
def is_routable(tags: dict[str, str], route_type: str) -> bool:
if route_type == 'bus':
highway_valid = tags['highway'] in {
'residential',
'service',
'unclassified',
'tertiary',
'tertiary_link',
'secondary',
'secondary_link',
'primary',
'primary_link',
'living_street',
'trunk',
'trunk_link',
'motorway',
'motorway_link',
'motorway_junction',
'road',
'busway',
'bus_guideway',
}
highway_designated_valid = tags['highway'] in {
'pedestrian',
}
service_valid = tags.get('service', 'no') not in {
'driveway',
'parking_aisle',
'alley',
'emergency_access',
}
access_designated = False
access_valid = True
if 'bus:conditional' in tags:
access_designated = access_valid = True
elif 'bus' in tags:
access_designated = access_valid = tags['bus'] not in {'no'}
elif 'psv' in tags:
access_designated = access_valid = tags['psv'] not in {'no'}
elif 'motor_vehicle' in tags:
access_valid = tags['motor_vehicle'] not in {'private', 'customers', 'no'}
elif 'access' in tags:
access_valid = tags['access'] not in {'private', 'customers', 'no'}
noarea_valid = tags.get('area', 'no') in {'no'}
return all(
(
(highway_valid or (highway_designated_valid and access_designated)),
(service_valid or access_designated),
access_valid,
noarea_valid,
)
)
if route_type == 'tram':
# all overpass-fetched elements are routable
return True
def is_oneway(tags: dict[str, str]) -> bool:
# TODO: it would be nice to support oneway=-1
roundabout_valid = False
if 'junction' in tags:
roundabout_valid = tags['junction'] in {'roundabout'}
oneway_valid = roundabout_valid
if 'oneway:bus' in tags:
oneway_valid = tags['oneway:bus'] in {'yes'}
elif 'oneway:psv' in tags:
oneway_valid = tags['oneway:psv'] in {'yes'}
elif 'oneway' in tags:
oneway_valid = tags['oneway'] in {'yes'}
return oneway_valid
def is_roundabout(tags: dict[str, str]) -> bool:
return tags.get('junction', 'no') in {'roundabout'}
def is_bus_explicit(tags: dict[str, str]) -> bool:
return tags.get('bus') == 'yes'
def is_any_rail_related(tags: dict[str, str]) -> bool:
rail_valid = 'railway' in tags
tram_valid = tags.get('tram', 'no') in {'yes'}
train_valid = tags.get('train', 'no') in {'yes'}
subway_valid = tags.get('subway', 'no') in {'yes'}
return any((rail_valid, tram_valid, train_valid, subway_valid))
def is_tram_element(tags: dict[str, str]) -> bool:
rail_valid = 'railway' in tags
tram_valid = tags.get('tram', 'no') in {'yes'}
train_valid = tags.get('train', 'no') in {'yes'}
subway_valid = tags.get('subway', 'no') in {'yes'}
return tram_valid or (rail_valid and not train_valid and not subway_valid)
def _merge_relation_tags(element: dict, relation: dict, extra: dict) -> None:
element['tags'] = relation.get('tags', {}) | element.get('tags', {}) | extra
def merge_relations_tags(relations: Iterable[dict], elements: Iterable[dict], role: str, public_transport: str) -> None:
element_map = {(e['type'], e['id']): e for e in elements}
for relation in sorted(relations, key=lambda r: r['id']):
for member in (m for m in relation['members'] if m['role'] == role):
platform = element_map.get((member['type'], member['ref']), None)
if platform is None:
print(f'π§ Warning: Platform {member["type"]}/{member["ref"]} not found in map')
continue
_merge_relation_tags(platform, relation, {'public_transport': public_transport})
def _create_node_counts(ways: list[dict]) -> dict[int, int]:
node_counts = defaultdict(int)
for way in ways:
for node in way['nodes']:
node_counts[node] += 1
return node_counts
def _split_way_on_intersection(way: dict, node_counts: dict[int, int]) -> list[list[int]]:
segments: list[list[int]] = []
current_segment: list[int] = []
for node in way['nodes']:
current_segment.append(node)
if node_counts[node] > 1 and len(current_segment) > 1:
segments.append(current_segment)
current_segment = [node]
if len(current_segment) > 1:
segments.append(current_segment)
return segments
def organize_ways(ways: list[dict]) -> tuple[list[dict], dict[ElementId, set[ElementId]], dict[int, list[ElementId]]]:
node_counts = _create_node_counts(ways)
node_to_way_map = defaultdict(set)
split_ways: list[dict] = []
connected_ways_map: dict[ElementId, set[ElementId]] = defaultdict(set)
id_map = defaultdict(list)
for way in ways:
split_segments = _split_way_on_intersection(way, node_counts)
for extra_num, segment in enumerate(split_segments, 1):
extra_num = extra_num if len(split_segments) > 1 else None
max_num = len(split_segments) if extra_num is not None else None
split_way = {
**way,
'id': element_id(way['id'], extra_num=extra_num, max_num=max_num),
'nodes': segment,
}
split_ways.append(split_way)
id_map[way['id']].append(split_way['id'])
for node in segment:
if node_counts[node] > 1:
for other_way_id in node_to_way_map[node]:
connected_ways_map[split_way['id']].add(other_way_id)
connected_ways_map[other_way_id].add(split_way['id'])
node_to_way_map[node].add(split_way['id'])
return split_ways, connected_ways_map, id_map
def preprocess_elements(elements: Iterable[dict]) -> Sequence[dict]:
# deduplicate
map = {(e['type'], e['id']): e for e in elements}
result = tuple(map.values())
# extract center
for e in result:
if 'center' in e:
e |= e['center']
return result
def optimize_cells_and_get_bbs(
cells: Sequence[Cell],
*,
start_horizontal: bool,
) -> tuple[Sequence[BoundingBox], Sequence[BoundingBox]]:
def merge(sorted: list[tuple[int, int, int, int]]) -> list[tuple[int, int, int, int]]:
result = []
current = sorted[0]
for next in sorted[1:]:
# merge horizontally
if current[2] + 1 == next[0] and current[1] == next[1] and current[3] == next[3]:
current = (current[0], current[1], next[2], current[3])
# merge vertically
elif current[3] + 1 == next[1] and current[0] == next[0] and current[2] == next[2]:
current = (current[0], current[1], current[2], next[3])
# add to merged cells if cells can't be merged
else:
result.append(current)
current = next
# add the last cell
result.append(current)
return result
cells_bounds = ((c.x, c.y, c.x, c.y) for c in cells)
if start_horizontal:
cells_bounds = sorted(cells_bounds, key=lambda c: (c[1], c[0]))
else:
cells_bounds = sorted(cells_bounds, key=lambda c: (c[0], c[1]))
cells_bounds = merge(cells_bounds)
if start_horizontal:
cells_bounds = sorted(cells_bounds, key=lambda c: (c[0], c[1]))
else:
cells_bounds = sorted(cells_bounds, key=lambda c: (c[1], c[0]))
cells_bounds = merge(cells_bounds)
bbs = tuple(BoundingBox.from_grid_cell(*c) for c in cells_bounds)
return bbs, tuple(bb.extend(unit_degrees=DOWNLOAD_RELATION_GRID_CELL_EXPAND) for bb in bbs)
def get_download_triggers(
bbc: BoundingBoxCollection,
cells: Sequence[Cell],
ways: dict[ElementId, FetchRelationElement],
) -> dict[ElementId, Sequence[tuple[int, int]]]:
cells_set = frozenset(cells)
result: dict[ElementId, Sequence[Cell]] = {}
for way_id, way in ways.items():
way_new_cells = set()
for latLng in way.latLngs:
if bbc.contains(latLng):
continue
new_cells = BoundingBox(
minlat=latLng[0],
minlon=latLng[1],
maxlat=latLng[0],
maxlon=latLng[1],
).get_grid_cells(expand=1) # 3x3 grid
way_new_cells |= new_cells - cells_set
if way_new_cells:
result[way_id] = tuple(way_new_cells)
return dict(result)
# TODO: check data freshness
class Overpass:
def __init__(self):
pass
def _get_http_client(self) -> httpx.AsyncClient:
return get_http_client(OVERPASS_API_INTERPRETER)
@cached(TTLCache(maxsize=1024, ttl=7200)) # 2 hours
async def _query_relation_history_post(
self,
session: str, # cache busting # noqa: ARG002
query: str,
timeout: float,
) -> list[list[dict]]:
async with self._get_http_client() as http:
r = await http.post('', data={'data': query}, timeout=timeout * 2)
r.raise_for_status()
elements: list[dict] = r.json()['elements']
return split_by_count(elements)
async def _query_relation_history(
self,
relation_id: int,
download_hist: DownloadHistory,
route_type: str,
) -> tuple[list[list[dict]], BoundingBoxCollection]:
if not download_hist.history or not all(download_hist.history):
raise ValueError('No grid cells to download')
all_elements_split = None
all_bbs = []
for cells in download_hist.history:
hor_bbs_t = optimize_cells_and_get_bbs(cells, start_horizontal=True)
ver_bbs_t = optimize_cells_and_get_bbs(cells, start_horizontal=False)
# pick more optimal cells
cell_bbs_t = hor_bbs_t if len(hor_bbs_t) <= len(ver_bbs_t) else ver_bbs_t
cell_bbs, cell_bbs_expand = cell_bbs_t
all_bbs.extend(cell_bbs)
print(f'[OVERPASS] Downloading {len(cell_bbs)} cells for relation {relation_id}')
timeout = 180
query = build_query(cell_bbs, cell_bbs_expand, timeout, route_type)
elements_split = await self._query_relation_history_post(download_hist.session, query, timeout)
if all_elements_split is None:
all_elements_split = elements_split
else:
for i, elements in enumerate(elements_split):
all_elements_split[i].extend(elements)
bbc = BoundingBoxCollection(all_bbs)
return all_elements_split, bbc
@cached(TTLCache(maxsize=128, ttl=60))
async def query_relation(
self,
relation_id: int,
download_hist: DownloadHistory | None,
download_targets: Sequence[Cell] | None,
route_type: str, # bus, tram...
) -> tuple[
BoundingBox,
DownloadHistory,
dict[ElementId, Sequence[tuple[int, int]]],
dict[ElementId, FetchRelationElement],
dict[int, list[ElementId]],
list[FetchRelationBusStopCollection],
]:
if download_targets is None:
timeout = 60
query = build_bb_query(relation_id, timeout)
async with self._get_http_client() as http:
r = await http.post('', data={'data': query}, timeout=timeout * 2)
r.raise_for_status()
elements: list[dict] = r.json()['elements']
if not elements:
raise HTTPException(status.HTTP_400_BAD_REQUEST, 'Relation is empty, which is not supported')
relation_way_members = {e['id'] for e in elements}
union_grid_cells_set: set[Cell] = set()
for way in elements:
union_grid_cells_set.update(
BoundingBox(
minlat=way['bounds']['minlat'],
minlon=way['bounds']['minlon'],
maxlat=way['bounds']['maxlat'],
maxlon=way['bounds']['maxlon'],
)
.extend(DOWNLOAD_RELATION_WAY_BB_EXPAND)
.get_grid_cells()
)
union_grid_cells = tuple(union_grid_cells_set)
else:
# in merge mode, members are set by the client
relation_way_members = set()
union_grid_cells = download_targets
if download_hist is None:
download_hist = DownloadHistory(session=DownloadHistory.make_session(), history=(union_grid_cells,))
elif union_grid_cells:
download_hist = replace(download_hist, history=(*download_hist.history, union_grid_cells))
elements_split, bbc = await self._query_relation_history(relation_id, download_hist, route_type)
maybe_road_elements = elements_split[0]
maybe_road_elements = preprocess_elements(maybe_road_elements)
node_elements = elements_split[1]
node_elements = preprocess_elements(node_elements)
bus_elements = elements_split[2]
stop_area_relations = elements_split[3]
stop_area_platform_elements = elements_split[4]
stop_area_stop_position_elements = elements_split[5]
merge_relations_tags(
stop_area_relations,
stop_area_platform_elements,
role='platform',
public_transport='platform',
)
merge_relations_tags(
stop_area_relations,
stop_area_stop_position_elements,
role='stop',
public_transport='stop_position',
)
road_elements = tuple(e for e in maybe_road_elements if is_routable(e['tags'], route_type))
nodes_map = {e['id']: e for e in node_elements}
for e in road_elements:
e['_member'] = e['id'] in relation_way_members
e['_oneway'] = is_oneway(e['tags'])
e['_roundabout'] = is_roundabout(e['tags'])
road_elements, connected_ways_map, id_map = organize_ways(road_elements)
ways = {
e['id']: FetchRelationElement(
id=e['id'],
member=e['_member'],
oneway=e['_oneway'],
roundabout=e['_roundabout'],
nodes=e['nodes'],
latLngs=[(nodes_map[n_id]['lat'], nodes_map[n_id]['lon']) for n_id in e['nodes']],
connectedTo=list(connected_ways_map[e['id']]),
)
for e in road_elements
}
elements_ex = chain(stop_area_platform_elements, stop_area_stop_position_elements, bus_elements)
elements_ex = preprocess_elements(elements_ex)
if route_type == 'bus':
elements_ex = (e for e in elements_ex if is_bus_explicit(e['tags']) or not is_any_rail_related(e['tags']))
elif route_type == 'tram':
elements_ex = (e for e in elements_ex if is_tram_element(e['tags']))
stops = tuple(FetchRelationBusStop.from_data(e) for e in elements_ex)
bus_stop_collections = build_bus_stop_collections(stops)
bus_stop_collections = tuple(c for c in bus_stop_collections if bbc.contains(c.best.latLng))
global_bb = BoundingBox(*bbc.idx.bounds)
download_triggers = get_download_triggers(bbc, union_grid_cells, ways)
return global_bb, download_hist, download_triggers, ways, id_map, bus_stop_collections
@cached(TTLCache(maxsize=128, ttl=60))
async def query_parents(self, way_ids_set: frozenset[int]) -> QueryParentsResult:
timeout = 60
query = build_parents_query(way_ids_set, timeout)
async with self._get_http_client() as http:
r = await http.post('', data={'data': query}, timeout=timeout * 2)
r.raise_for_status()
data: dict[str, list[dict]] = xmltodict.parse(
r.text,
postprocessor=postprocessor,
force_list=('relation', 'way', 'member', 'tag', 'nd'),
)['osm']
relations = data.get('relation', [])
id_relations_map = defaultdict(list)
for relation in relations:
members = relation['member'] = relation.get('member', [])
# tags = relation['tag'] = relation.get('tag', [])
if len(members) <= 1:
continue
for member in members:
if member['@type'] == 'way' and member['@ref'] in way_ids_set:
id_relations_map[member['@ref']].append(relation)
# unique relations
for way_id, relations in id_relations_map.items():
id_relations_map[way_id] = list({r['@id']: r for r in relations}.values())
ways = data.get('way', [])
ways_map = {w['@id']: w for w in ways}
return QueryParentsResult(
id_relations_map=id_relations_map,
ways_map=ways_map,
)