-
Notifications
You must be signed in to change notification settings - Fork 0
/
osm.py
1510 lines (1310 loc) · 63.5 KB
/
osm.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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
title: OpenStreetMap Tool
author: projectmoon
author_url: https://git.agnos.is/projectmoon/open-webui-filters
version: 1.1.1
license: AGPL-3.0+
required_open_webui_version: 0.3.21
requirements: openrouteservice
"""
import itertools
import json
import math
import requests
import openrouteservice
from openrouteservice.directions import directions as ors_directions
from urllib.parse import urljoin
from operator import itemgetter
from typing import List, Optional
from pydantic import BaseModel, Field
NOMINATIM_LOOKUP_TYPES = {
"node": "N",
"route": "R",
"way": "W"
}
OLD_VALVE_SETTING = """ Tell the user that you cannot search
OpenStreetMap until the configuration is fixed. The Nominatim URL
valve setting needs to be updated. There has been a breaking change in
1.0 of the OpenStreetMap tool. The valve setting is currently set to:
`{OLD}`.
It shoule be set to the root URL of the Nominatim endpoint, for
example:
`https://nominatim.openstreetmap.org/`
Inform the user they need to fix this configuration setting.
""".replace("\n", " ").strip()
VALVES_NOT_SET = """
Tell the user that the User-Agent and From headers
must be set to comply with the OSM Nominatim terms
of use: https://operations.osmfoundation.org/policies/nominatim/
""".replace("\n", " ").strip()
NO_RESULTS = ("No results found. Tell the user you found no results. "
"Do not make up answers or hallucinate. Only say you "
"found no results.")
NO_CONFUSION = ("**IMPORTANT!:** Check that the results match the location "
"the user is talking about, by analyzing the conversation history. "
"Sometimes there are places with the same "
"names, but in different cities or countries. If the results are for "
"a different city or country than the user is interested in, say so: "
"tell the user that the results are for the wrong place, and tell them "
"to be more specific in their query.")
# Give examples of OSM links to help prevent wonky generated links
# with correct GPS coords but incorrect URLs.
EXAMPLE_OSM_LINK = "https://www.openstreetmap.org/#map=19/<lat>/<lon>"
OSM_LINK_INSTRUCTIONS = (
"Make friendly human-readable OpenStreetMap links when possible, "
"by using the latitude and longitude of the amenities: "
f"{EXAMPLE_OSM_LINK}\n\n"
)
def chunk_list(input_list, chunk_size):
it = iter(input_list)
return list(
itertools.zip_longest(*[iter(it)] * chunk_size, fillvalue=None)
)
def to_lookup(thing) -> Optional[str]:
lookup_type = NOMINATIM_LOOKUP_TYPES.get(thing['type'])
if lookup_type is not None:
return f"{lookup_type}{thing['id']}"
def specific_place_instructions() -> str:
return (
"# Result Instructions\n"
"These are search results ordered by relevance for the "
"address, place, landmark, or location the user is asking "
"about. **IMPORTANT!:** Tell the user all relevant information, "
"including address, contact information, and the OpenStreetMap link. "
"Make the map link into a nice human-readable markdown link."
)
def detailed_instructions(tag_type_str: str) -> str:
"""
Produce detailed instructions for models good at following
detailed instructions.
"""
return (
"# Detailed Search Result Instructions\n"
f"These are some of the {tag_type_str} points of interest nearby. "
"These are the results known to be closest to the requested location. "
"When telling the user about them, make sure to report "
"all the information (address, contact info, website, etc).\n\n"
"Tell the user about ALL the results, and give closer results "
"first. Closer results are higher in the list. When telling the "
"user the distance, use the TRAVEL DISTANCE. Do not say one "
"distance is farther away than another. Just say what the "
"distances are. "
f"{OSM_LINK_INSTRUCTIONS}"
"Give map links friendly, contextual labels. Don't just print "
f"the naked link:\n"
f' - Example: You can view it on [OpenStreetMap]({EXAMPLE_OSM_LINK})\n'
f' - Example: Here it is on [OpenStreetMap]({EXAMPLE_OSM_LINK})\n'
f' - Example: You can find it on [OpenStreetMap]({EXAMPLE_OSM_LINK})\n'
"\n\nAnd so on.\n\n"
"Only use relevant results. If there are no relevant results, "
"say so. Do not make up answers or hallucinate. "
f"\n\n{NO_CONFUSION}\n\n"
"Remember that the CLOSEST result is first, and you should use "
"that result first.\n\n"
"The results (if present) are below, in Markdown format.\n\n"
"**ALWAYS SAY THE CLOSEST RESULT FIRST!**"
)
def simple_instructions(tag_type_str: str) -> str:
"""
Produce simpler markdown-oriented instructions for models that do
better with that.
"""
return (
"# OpenStreetMap Result Instructions\n"
f"These are some of the {tag_type_str} points of interest nearby. "
"These are the results known to be closest to the requested location. "
"For each result, report the following information: \n"
" - Name\n"
" - Address\n"
" - OpenStreetMap Link (make it a human readable link like 'View on OpenStreetMap')\n"
" - Contact information (address, phone, website, email, etc)\n\n"
"Tell the user about ALL the results, and give the CLOSEST result "
"first. The results are ordered by closeness as the crow flies. "
"When telling the user about distances, use the TRAVEL DISTANCE only. "
"Only use relevant results. If there are no relevant results, "
"say so. Do not make up answers or hallucinate. "
"Make sure that your results are in the actual location the user is talking about, "
"and not a place of the same name in a different country."
"The search results are below."
)
def merge_from_nominatim(thing, nominatim_result) -> Optional[dict]:
"""Merge information into object missing all or some of it."""
if thing is None:
return None
if 'address' not in nominatim_result:
return None
nominatim_address = nominatim_result['address']
# prioritize actual name, road name, then display name. display
# name is often the full address, which is a bit much.
nominatim_name = nominatim_result.get('name')
nominatim_road = nominatim_address.get('road')
nominatim_display_name = nominatim_result.get('display_name')
thing_name = thing.get('name')
if nominatim_name and not thing_name:
thing['name'] = nominatim_name.strip()
elif nominatim_road and not thing_name:
thing['name'] = nominatim_road.strip()
elif nominatim_display_name and not thing_name:
thing['name'] = nominatim_display_name.strip()
tags = thing.get('tags', {})
for key in nominatim_address:
obj_key = f"addr:{key}"
if obj_key not in tags:
tags[obj_key] = nominatim_address[key]
thing['tags'] = tags
return thing
def thing_is_useful(thing):
"""
Determine if an OSM way entry is useful to us. This means it
has something more than just its main classification tag, and
(usually) has at least a name. Some exceptions are made for ways
that do not have names.
"""
tags = thing.get('tags', {})
has_tags = len(tags) > 1
has_useful_tags = (
'leisure' in tags or
'shop' in tags or
'amenity' in tags or
'car:rental' in tags or
'rental' in tags or
'car_rental' in tags or
'service:bicycle:rental' in tags
)
return has_tags and has_useful_tags
def thing_has_info(thing):
has_name = any('name' in tag for tag in thing['tags'])
return thing_is_useful(thing) and has_name
# is_exception = way['tags'].get('leisure', None) is not None
# return has_tags and (has_name or is_exception)
def process_way_result(way) -> Optional[dict]:
"""
Post-process an OSM Way dict to remove the geometry and node
info, and calculate a single GPS coordinate from its bounding
box.
"""
if 'nodes' in way:
del way['nodes']
if 'geometry' in way:
del way['geometry']
if 'bounds' in way:
way_center = get_bounding_box_center(way['bounds'])
way['lat'] = way_center['lat']
way['lon'] = way_center['lon']
del way['bounds']
return way
return None
def get_bounding_box_center(bbox):
def convert(bbox, key):
return bbox[key] if isinstance(bbox[key], float) else float(bbox[key])
min_lat = convert(bbox, 'minlat')
min_lon = convert(bbox, 'minlon')
max_lat = convert(bbox, 'maxlat')
max_lon = convert(bbox, 'maxlon')
return {
'lon': (min_lon + max_lon) / 2,
'lat': (min_lat + max_lat) / 2
}
def haversine_distance(point1, point2):
R = 6371 # Earth radius in kilometers
lat1, lon1 = point1['lat'], point1['lon']
lat2, lon2 = point2['lat'], point2['lon']
d_lat = math.radians(lat2 - lat1)
d_lon = math.radians(lon2 - lon1)
a = (math.sin(d_lat / 2) * math.sin(d_lat / 2) +
math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) *
math.sin(d_lon / 2) * math.sin(d_lon / 2))
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
return R * c
def sort_by_closeness(origin, points, *keys: str):
"""
Sorts a list of { lat, lon }-like dicts by closeness to an origin point.
The origin is a dict with keys of { lat, lon }.
"""
return sorted(points, key=itemgetter(*keys))
def get_or_none(tags: dict, *keys: str) -> Optional[str]:
"""
Try to extract a value from a dict by trying keys in order, or
return None if none of the keys were found.
"""
for key in keys:
if key in tags:
return tags[key]
return None
def all_are_none(*args) -> bool:
for arg in args:
if arg is not None:
return False
return True
def friendly_shop_name(shop_type: str) -> str:
"""
Make certain shop types more friendly for LLM interpretation.
"""
if shop_type == "doityourself":
return "hardware"
else:
return shop_type
def parse_thing_address(thing: dict) -> Optional[str]:
"""
Parse address from either an Overpass result or Nominatim
result.
"""
if 'address' in thing:
# nominatim result
return parse_address_from_address_obj(thing['address'])
else:
return parse_address_from_tags(thing['tags'])
def parse_address_from_address_obj(address) -> Optional[str]:
"""Parse address from Nominatim address object."""
house_number = get_or_none(address, "house_number")
street = get_or_none(address, "road")
city = get_or_none(address, "city")
state = get_or_none(address, "state")
postal_code = get_or_none(address, "postcode")
# if all are none, that means we don't know the address at all.
if all_are_none(house_number, street, city, state, postal_code):
return None
# Handle missing values to create complete-ish addresses, even if
# we have missing data. We will get either a partly complete
# address, or None if all the values are missing.
line1 = filter(None, [street, house_number])
line2 = filter(None, [city, state, postal_code])
line1 = " ".join(line1).strip()
line2 = " ".join(line2).strip()
full_address = filter(None, [line1, line2])
full_address = ", ".join(full_address).strip()
return full_address if len(full_address) > 0 else None
def parse_address_from_tags(tags: dict) -> Optional[str]:
"""Parse address from Overpass tags object."""
house_number = get_or_none(tags, "addr:housenumber", "addr:house_number")
street = get_or_none(tags, "addr:street")
city = get_or_none(tags, "addr:city")
state = get_or_none(tags, "addr:state", "addr:province")
postal_code = get_or_none(
tags,
"addr:postcode", "addr:post_code", "addr:postal_code",
"addr:zipcode", "addr:zip_code"
)
# if all are none, that means we don't know the address at all.
if all_are_none(house_number, street, city, state, postal_code):
return None
# Handle missing values to create complete-ish addresses, even if
# we have missing data. We will get either a partly complete
# address, or None if all the values are missing.
line1 = filter(None, [street, house_number])
line2 = filter(None, [city, state, postal_code])
line1 = " ".join(line1).strip()
line2 = " ".join(line2).strip()
full_address = filter(None, [line1, line2])
full_address = ", ".join(full_address).strip()
return full_address if len(full_address) > 0 else None
def parse_thing_amenity_type(thing: dict, tags: dict) -> Optional[dict]:
"""
Extract amenity type or other identifying category from
Nominatim or Overpass result object.
"""
if 'amenity' in tags:
return tags['amenity']
if thing.get('class') == 'amenity' or thing.get('class') == 'shop':
return thing.get('type')
# fall back to tag categories, like shop=*
if 'shop' in tags:
return friendly_shop_name(tags['shop'])
if 'leisure' in tags:
return friendly_shop_name(tags['leisure'])
return None
def parse_and_validate_thing(thing: dict) -> Optional[dict]:
"""
Parse an OSM result (node or post-processed way) and make it
more friendly to work with. Helps remove ambiguity of the LLM
interpreting the raw JSON data. If there is not enough data,
discard the result.
"""
tags: dict = thing['tags'] if 'tags' in thing else {}
# Currently we define "enough data" as at least having lat, lon,
# and a name. nameless things are allowed if they are in a certain
# class of POIs (leisure).
has_name = 'name' in tags or 'name' in thing
is_leisure = 'leisure' in tags or 'leisure' in thing
if 'lat' not in thing or 'lon' not in thing:
return None
if not has_name and not is_leisure:
return None
friendly_thing = {}
name: str = (tags['name'] if 'name' in tags
else thing['name'] if 'name' in thing
else str(thing['id']) if 'id' in thing
else str(thing['osm_id']) if 'osm_id' in thing
else "unknown")
address: string = parse_thing_address(thing)
distance: Optional[float] = thing.get('distance', None)
nav_distance: Optional[float] = thing.get('nav_distance', None)
lat: Optional[float] = thing.get('lat', None)
lon: Optional[float] = thing.get('lon', None)
amenity_type: Optional[str] = parse_thing_amenity_type(thing, tags)
# use the navigation distance if it's present. but if not, set to
# the haversine distance so that we at least get coherent results
# for LLM.
friendly_thing['distance'] = "{:.3f}".format(distance) if distance else "unknown"
if nav_distance:
friendly_thing['nav_distance'] = "{:.3f}".format(nav_distance) + " km"
else:
friendly_thing['nav_distance'] = f"a bit more than {friendly_thing['distance']}km"
friendly_thing['name'] = name if name else "unknown"
friendly_thing['address'] = address if address else "unknown"
friendly_thing['lat'] = lat if lat else "unknown"
friendly_thing['lon'] = lon if lon else "unknown"
friendly_thing['amenity_type'] = amenity_type if amenity_type else "unknown"
return friendly_thing
def create_osm_link(lat, lon):
return EXAMPLE_OSM_LINK.replace("<lat>", str(lat)).replace("<lon>", str(lon))
def convert_and_validate_results(
original_location: str,
things_nearby: List[dict],
sort_message: str="closeness",
use_distance: bool=True
) -> Optional[str]:
"""
Converts the things_nearby JSON into Markdown-ish results to
(hopefully) improve model understanding of the results. Intended
to stop misinterpretation of GPS coordinates when creating map
links. Also drops incomplete results. Supports Overpass and
Nominatim results.
"""
entries = []
for thing in things_nearby:
# Convert to friendlier data, drop results without names etc.
# No need to instruct LLM to generate map links if we do it
# instead.
friendly_thing = parse_and_validate_thing(thing)
if not friendly_thing:
continue
distance = (f" - Haversine Distance from Origin: {friendly_thing['distance']} km\n"
if use_distance else "")
travel_distance = (f" - Travel Distance from Origin: {friendly_thing['nav_distance']}\n"
if use_distance and 'nav_distance' in friendly_thing else "")
map_link = create_osm_link(friendly_thing['lat'], friendly_thing['lon'])
entry = (f"## {friendly_thing['name']}\n"
f" - Latitude: {friendly_thing['lat']}\n"
f" - Longitude: {friendly_thing['lon']}\n"
f" - Address: {friendly_thing['address']}\n"
f" - Amenity Type: {friendly_thing['amenity_type']}\n"
f"{distance}"
f"{travel_distance}"
f" - OpenStreetMap link: {map_link}\n\n"
f"Raw JSON data:\n"
"```json\n"
f"{str(thing)}\n"
"```")
entries.append(entry)
if len(entries) == 0:
return None
result_text = "\n\n".join(entries)
header = ("# Search Results\n"
f"Ordered by {sort_message} to {original_location}.")
return f"{header}\n\n{result_text}"
class OsmCache:
def __init__(self, filename="/tmp/osm.json"):
self.filename = filename
self.data = {}
# Load existing cache if it exists
try:
with open(self.filename, 'r') as f:
self.data = json.load(f)
except FileNotFoundError:
pass
def get(self, key):
return self.data.get(key)
def set(self, key, value):
self.data[key] = value
with open(self.filename, 'w') as f:
json.dump(self.data, f)
def get_or_set(self, key, func_to_call):
"""
Retrieve the value from the cache for a given key. If the key is not found,
call `func_to_call` to generate the value and store it in the cache.
:param key: The key to look up or set in the cache
:param func_to_call: A callable function that returns the value if key is missing
:return: The cached or generated value
"""
if key not in self.data:
value = func_to_call()
self.set(key, value)
return self.data[key]
def clear_cache(self):
"""
Clear all entries from the cache.
"""
self.data.clear()
try:
# Erase contents of the cache file.
with open(self.filename, 'w'):
pass
except FileNotFoundError:
pass
class OrsRouter:
def __init__(
self, valves, user_valves: Optional[dict], event_emitter=None
):
self.valves = valves
self.event_emitter = event_emitter
self.user_valves = user_valves
if self.valves.ors_api_key is not None and self.valves.ors_api_key != "":
if self.valves.ors_instance is not None:
self._client = openrouteservice.Client(
base_url=self.valves.ors_instance,
key=self.valves.ors_api_key
)
else:
self._client = openrouteservice.Client(key=self.valves.ors_api_key)
else:
self._client = None
def calculate_distance(
self, from_thing: dict, to_thing: dict
) -> Optional[float]:
"""
Calculate navigation distance between A and B. Returns the
distance calculated, if successful, or None if the distance
could not be calculated, or if ORS is not configured.
"""
if not self._client:
return None
# select profile based on distance for more accurate
# measurements. very close haversine distances use the walking
# profile, which should (usually?) essentially cover walking
# and biking. further away = use car.
if to_thing.get('distance', 9000) <= 1.5:
profile = "foot-walking"
else:
profile = "driving-car"
coords = ((from_thing['lon'], from_thing['lat']),
(to_thing['lon'], to_thing['lat']))
resp = ors_directions(self._client, coords, profile=profile,
preference="fastest", units="km")
routes = resp.get('routes', [])
if len(routes) > 0:
return routes[0].get('summary', {}).get('distance', None)
else:
return None
class OsmSearcher:
def __init__(self, valves, user_valves: Optional[dict], event_emitter=None):
self.valves = valves
self.event_emitter = event_emitter
self.user_valves = user_valves
self._ors = OrsRouter(valves, user_valves, event_emitter)
def create_headers(self) -> Optional[dict]:
if len(self.valves.user_agent) == 0 or len(self.valves.from_header) == 0:
return None
return {
'User-Agent': self.valves.user_agent,
'From': self.valves.from_header
}
async def event_resolving(self, done: bool=False, message="OpenStreetMap: resolving..."):
if not self.event_emitter or not self.valves.status_indicators:
return
await self.event_emitter({
"type": "status",
"data": {
"status": "in_progress",
"description": message,
"done": done,
},
})
async def event_fetching(self, done: bool=False, message="OpenStreetMap: fetching additional info"):
if not self.event_emitter or not self.valves.status_indicators:
return
await self.event_emitter({
"type": "status",
"data": {
"status": "in_progress",
"description": message,
"done": done,
},
})
async def event_searching(
self, category: str, place: str,
status: str="in_progress", done: bool=False
):
if not self.event_emitter or not self.valves.status_indicators:
return
await self.event_emitter({
"type": "status",
"data": {
"status": status,
"description": f"OpenStreetMap: searching for {category} near {place}",
"done": done,
},
})
async def event_search_complete(self, category: str, place: str, num_results: int):
if not self.event_emitter or not self.valves.status_indicators:
return
await self.event_emitter({
"type": "status",
"data": {
"status": "complete",
"description": f"OpenStreetMap: found {num_results} '{category}' results",
"done": True,
},
})
async def event_error(self, exception: Exception):
if not self.event_emitter or not self.valves.status_indicators:
return
await self.event_emitter({
"type": "status",
"data": {
"status": "error",
"description": f"Error searching OpenStreetMap: {str(exception)}",
"done": True,
},
})
def calculate_navigation_distance(self, start, destination) -> float:
"""Calculate real distance from A to B, instead of Haversine."""
return self._ors.calculate_distance(start, destination)
def attempt_ors(self, origin, things_nearby) -> bool:
"""Update distances to use ORS navigable distances, if ORS enabled."""
used_ors = False
cache = OsmCache()
for thing in things_nearby:
cache_key = f"ors_{origin}_{thing['id']}"
nav_distance = cache.get(cache_key)
if nav_distance:
print(f"Got nav distance for {thing['id']} from cache!")
else:
print(f"Checking ORS for {thing['id']}")
nav_distance = self.calculate_navigation_distance(origin, thing)
if nav_distance:
used_ors = True
cache.set(cache_key, nav_distance)
thing['nav_distance'] = nav_distance
return used_ors
def calculate_haversine(self, origin, things_nearby):
for thing in things_nearby:
if 'distance' not in thing:
thing['distance'] = haversine_distance(origin, thing)
def use_detailed_interpretation_mode(self) -> bool:
# Let user valve for instruction mode override the global
# setting.
print(str(self.user_valves))
if self.user_valves:
return self.user_valves.instruction_oriented_interpretation
else:
return self.valves.instruction_oriented_interpretation
def get_result_instructions(self, tag_type_str: str) -> str:
if self.use_detailed_interpretation_mode():
return detailed_instructions(tag_type_str)
else:
return simple_instructions(tag_type_str)
@staticmethod
def group_tags(tags):
result = {}
for tag in tags:
key, value = tag.split('=')
if key not in result:
result[key] = []
result[key].append(value)
return result
@staticmethod
def fallback(nominatim_result):
"""
If we do not have Overpass Turbo results, attempt to use the
Nominatim result instead.
"""
return ([nominatim_result] if 'type' in nominatim_result
and (nominatim_result['type'] == 'amenity'
or nominatim_result['type'] == 'shop'
or nominatim_result['type'] == 'leisure'
or nominatim_result['type'] == 'tourism')
else [])
async def nominatim_lookup_by_id(self, things, format="json"):
await self.event_fetching(done=False)
updated_things = [] # the things with merged info.
# handle last chunk, which can have nones in order due to the
# way chunking is done.
things = [thing for thing in things if thing is not None]
lookups = []
for thing in things:
if thing is None:
continue
lookup = to_lookup(thing)
if lookup is not None:
lookups.append(lookup)
# Nominatim likes it if we cache our data.
cache = OsmCache()
lookups_to_remove = []
for lookup_id in lookups:
from_cache = cache.get(lookup_id)
if from_cache is not None:
updated_things.append(from_cache)
lookups_to_remove.append(lookup_id)
# only need to look up things we do not have cached.
lookups = [id for id in lookups if id not in lookups_to_remove]
if len(lookups) == 0:
print("Got all Nominatim info from cache!")
await self.event_fetching(done=True)
return updated_things
else:
print(f"Looking up {len(lookups)} things from Nominatim")
url = urljoin(self.valves.nominatim_url, "lookup")
params = {
'osm_ids': ",".join(lookups),
'format': format
}
headers = self.create_headers()
if not headers:
raise ValueError("Headers not set")
response = requests.get(url, params=params, headers=headers)
if response.status_code == 200:
data = response.json()
if not data:
print("No results found for lookup")
await self.event_fetching(done=True)
return []
addresses_by_id = {item['osm_id']: item for item in data}
for thing in things:
nominatim_result = addresses_by_id.get(thing['id'], {})
if nominatim_result != {}:
updated = merge_from_nominatim(thing, nominatim_result)
if updated is not None:
lookup = to_lookup(thing)
cache.set(lookup, updated)
updated_things.append(updated)
await self.event_fetching(done=True)
return updated_things
else:
await self.event_error(Exception(response.text))
print(response.text)
return []
async def nominatim_search(self, query, format="json", limit: int=1) -> Optional[dict]:
await self.event_resolving(done=False)
cache_key = f"nominatim_search_{query}"
cache = OsmCache()
data = cache.get(cache_key)
if data:
print(f"Got nominatim search data for {query} from cache!")
await self.event_resolving(done=True)
return data[:limit]
print(f"Searching Nominatim for: {query}")
url = urljoin(self.valves.nominatim_url, "search")
params = {
'q': query,
'format': format,
'addressdetails': 1,
'limit': limit,
}
headers = self.create_headers()
if not headers:
await self.event_error("Headers not set")
raise ValueError("Headers not set")
response = requests.get(url, params=params, headers=headers)
if response.status_code == 200:
await self.event_resolving(done=True, message="OpenStreetMap: resolution complete.")
data = response.json()
if not data:
raise ValueError(f"No results found for query '{query}'")
print(f"Got result from Nominatim for: {query}")
cache.set(cache_key, data)
return data[:limit]
else:
await self.event_error(Exception(response.text))
print(response.text)
return None
async def overpass_search(
self, place, tags, bbox, limit=5, radius=4000
) -> (List[dict], List[dict]):
"""
Return a list relevant of OSM nodes and ways. Some
post-processing is done on ways in order to add coordinates to
them.
"""
print(f"Searching Overpass Turbo around origin {place}")
headers = self.create_headers()
if not headers:
raise ValueError("Headers not set")
url = self.valves.overpass_turbo_url
center = get_bounding_box_center(bbox)
around = f"(around:{radius},{center['lat']},{center['lon']})"
tag_groups = OsmSearcher.group_tags(tags)
search_groups = [f'"{tag_type}"~"{"|".join(values)}"'
for tag_type, values in tag_groups.items()]
searches = []
for search_group in search_groups:
searches.append(
f'nwr[{search_group}]{around}'
)
search = ";\n".join(searches)
if len(search) > 0:
search += ";"
# "out geom;" is needed to get bounding box info of ways,
# so we can calculate the coordinates.
query = f"""
[out:json];
(
{search}
);
out geom;
"""
print(query)
data = { "data": query }
response = requests.get(url, params=data, headers=headers)
if response.status_code == 200:
# nodes have have exact GPS coordinates. we also include
# useful way entries, post-processed to remove extra data
# and add a centered calculation of their GPS coords. any
# way that doesn't have enough info for us to use is
# dropped.
results = response.json()
results = results['elements'] if 'elements' in results else []
nodes = []
ways = []
things_missing_names = []
for res in results:
if 'type' not in res or not thing_is_useful(res):
continue
if res['type'] == 'node':
if thing_has_info(res):
nodes.append(res)
else:
things_missing_names.append(res)
elif res['type'] == 'way':
processed = process_way_result(res)
if processed is not None and thing_has_info(res):
ways.append(processed)
else:
if processed is not None:
things_missing_names.append(processed)
# attempt to update ways that have no names/addresses.
if len(things_missing_names) > 0:
print(f"Updating {len(things_missing_names)} things with info")
for way_chunk in chunk_list(things_missing_names, 20):
updated = await self.nominatim_lookup_by_id(way_chunk)
ways = ways + updated
return nodes, ways
else:
print(response.text)
raise Exception(f"Error calling Overpass API: {response.text}")
async def get_things_nearby(self, nominatim_result, place, tags, bbox, limit, radius):
nodes, ways = await self.overpass_search(place, tags, bbox, limit, radius)
# use results from overpass, but if they do not exist,
# fall back to the nominatim result. this may or may
# not be a good idea.
things_nearby = (nodes + ways
if len(nodes) > 0 or len(ways) > 0
else OsmSearcher.fallback(nominatim_result))
# in order to not spam ORS, we first sort by haversine
# distance and drop number of results to the limit. then, if
# enabled, we calculate ORS distances. then we sort again.
origin = get_bounding_box_center(bbox)
self.calculate_haversine(origin, things_nearby)
things_nearby = sort_by_closeness(origin, things_nearby, 'distance')
things_nearby = things_nearby[:limit] # drop down to requested limit
if self.attempt_ors(origin, things_nearby):
things_nearby = sort_by_closeness(origin, things_nearby, 'nav_distance', 'distance')
return things_nearby
async def search_nearby(
self, place: str, tags: List[str], limit: int=5, radius: int=4000,
category: str="POIs"
) -> str:
headers = self.create_headers()
if not headers:
return VALVES_NOT_SET
try:
nominatim_result = await self.nominatim_search(place, limit=1)
if not nominatim_result:
await self.event_search_complete(category, place, 0)
return NO_RESULTS
nominatim_result = nominatim_result[0]
# display friendlier searching message if possible
if 'display_name' in nominatim_result:
place_display_name = ",".join(nominatim_result['display_name'].split(",")[:3])
else:
place_display_name = place
await self.event_searching(category, place_display_name, done=False)
bbox = {
'minlat': nominatim_result['boundingbox'][0],
'maxlat': nominatim_result['boundingbox'][1],
'minlon': nominatim_result['boundingbox'][2],
'maxlon': nominatim_result['boundingbox'][3]
}
things_nearby = await self.get_things_nearby(nominatim_result, place, tags,
bbox, limit, radius)
if not things_nearby or len(things_nearby) == 0:
await self.event_search_complete(category, place, 0)
return NO_RESULTS
tag_type_str = ", ".join(tags)
# Only print the full result instructions if we
# actually have something.
search_results = convert_and_validate_results(place, things_nearby)
if search_results:
result_instructions = self.get_result_instructions(tag_type_str)
else:
result_instructions = ("No results found at all. "
"Tell the user there are no results.")
resp = (
f"{result_instructions}\n\n"
f"{search_results}"