openstreetmap.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """OpenStreetMap (Map)
  4. """
  5. import re
  6. from json import loads
  7. from urllib.parse import urlencode
  8. from functools import partial
  9. from flask_babel import gettext
  10. from searx.data import OSM_KEYS_TAGS, CURRENCIES
  11. from searx.utils import searx_useragent
  12. from searx.external_urls import get_external_url
  13. from searx.engines.wikidata import send_wikidata_query, sparql_string_escape, get_thumbnail
  14. # about
  15. about = {
  16. "website": 'https://www.openstreetmap.org/',
  17. "wikidata_id": 'Q936',
  18. "official_api_documentation": 'http://wiki.openstreetmap.org/wiki/Nominatim',
  19. "use_official_api": True,
  20. "require_api_key": False,
  21. "results": 'JSON',
  22. }
  23. # engine dependent config
  24. categories = ['map']
  25. paging = False
  26. language_support = True
  27. send_accept_language_header = True
  28. # search-url
  29. base_url = 'https://nominatim.openstreetmap.org/'
  30. search_string = 'search?{query}&polygon_geojson=1&format=jsonv2&addressdetails=1&extratags=1&dedupe=1'
  31. result_id_url = 'https://openstreetmap.org/{osm_type}/{osm_id}'
  32. result_lat_lon_url = 'https://www.openstreetmap.org/?mlat={lat}&mlon={lon}&zoom={zoom}&layers=M'
  33. route_url = 'https://graphhopper.com/maps/?point={}&point={}&locale=en-US&vehicle=car&weighting=fastest&turn_costs=true&use_miles=false&layer=Omniscale' # pylint: disable=line-too-long
  34. route_re = re.compile('(?:from )?(.+) to (.+)')
  35. wikidata_image_sparql = """
  36. select ?item ?itemLabel ?image ?sign ?symbol ?website ?wikipediaName
  37. where {
  38. hint:Query hint:optimizer "None".
  39. values ?item { %WIKIDATA_IDS% }
  40. OPTIONAL { ?item wdt:P18|wdt:P8517|wdt:P4291|wdt:P5252|wdt:P3451|wdt:P4640|wdt:P5775|wdt:P2716|wdt:P1801|wdt:P4896 ?image }
  41. OPTIONAL { ?item wdt:P1766|wdt:P8505|wdt:P8667 ?sign }
  42. OPTIONAL { ?item wdt:P41|wdt:P94|wdt:P154|wdt:P158|wdt:P2910|wdt:P4004|wdt:P5962|wdt:P8972 ?symbol }
  43. OPTIONAL { ?item wdt:P856 ?website }
  44. SERVICE wikibase:label {
  45. bd:serviceParam wikibase:language "%LANGUAGE%,en".
  46. ?item rdfs:label ?itemLabel .
  47. }
  48. OPTIONAL {
  49. ?wikipediaUrl schema:about ?item;
  50. schema:isPartOf/wikibase:wikiGroup "wikipedia";
  51. schema:name ?wikipediaName;
  52. schema:inLanguage "%LANGUAGE%" .
  53. }
  54. }
  55. ORDER by ?item
  56. """
  57. # key value that are link: mapping functions
  58. # 'mapillary': P1947
  59. # but https://github.com/kartaview/openstreetcam.org/issues/60
  60. # but https://taginfo.openstreetmap.org/keys/kartaview ...
  61. def value_to_https_link(value):
  62. http = 'http://'
  63. if value.startswith(http):
  64. value = 'https://' + value[len(http) :]
  65. return (value, value)
  66. def value_to_website_link(value):
  67. value = value.split(';')[0]
  68. return (value, value)
  69. def value_wikipedia_link(value):
  70. value = value.split(':', 1)
  71. return ('https://{0}.wikipedia.org/wiki/{1}'.format(*value), '{1} ({0})'.format(*value))
  72. def value_with_prefix(prefix, value):
  73. return (prefix + value, value)
  74. VALUE_TO_LINK = {
  75. 'website': value_to_website_link,
  76. 'contact:website': value_to_website_link,
  77. 'email': partial(value_with_prefix, 'mailto:'),
  78. 'contact:email': partial(value_with_prefix, 'mailto:'),
  79. 'contact:phone': partial(value_with_prefix, 'tel:'),
  80. 'phone': partial(value_with_prefix, 'tel:'),
  81. 'fax': partial(value_with_prefix, 'fax:'),
  82. 'contact:fax': partial(value_with_prefix, 'fax:'),
  83. 'contact:mastodon': value_to_https_link,
  84. 'facebook': value_to_https_link,
  85. 'contact:facebook': value_to_https_link,
  86. 'contact:foursquare': value_to_https_link,
  87. 'contact:instagram': value_to_https_link,
  88. 'contact:linkedin': value_to_https_link,
  89. 'contact:pinterest': value_to_https_link,
  90. 'contact:telegram': value_to_https_link,
  91. 'contact:tripadvisor': value_to_https_link,
  92. 'contact:twitter': value_to_https_link,
  93. 'contact:yelp': value_to_https_link,
  94. 'contact:youtube': value_to_https_link,
  95. 'contact:webcam': value_to_website_link,
  96. 'wikipedia': value_wikipedia_link,
  97. 'wikidata': partial(value_with_prefix, 'https://wikidata.org/wiki/'),
  98. 'brand:wikidata': partial(value_with_prefix, 'https://wikidata.org/wiki/'),
  99. }
  100. KEY_ORDER = [
  101. 'cuisine',
  102. 'organic',
  103. 'delivery',
  104. 'delivery:covid19',
  105. 'opening_hours',
  106. 'opening_hours:covid19',
  107. 'fee',
  108. 'payment:*',
  109. 'currency:*',
  110. 'outdoor_seating',
  111. 'bench',
  112. 'wheelchair',
  113. 'level',
  114. 'building:levels',
  115. 'bin',
  116. 'public_transport',
  117. 'internet_access:ssid',
  118. ]
  119. KEY_RANKS = {k: i for i, k in enumerate(KEY_ORDER)}
  120. def request(query, params):
  121. """do search-request"""
  122. params['url'] = base_url + search_string.format(query=urlencode({'q': query}))
  123. params['route'] = route_re.match(query)
  124. params['headers']['User-Agent'] = searx_useragent()
  125. if 'Accept-Language' not in params['headers']:
  126. params['headers']['Accept-Language'] = 'en'
  127. return params
  128. def response(resp):
  129. """get response from search-request"""
  130. results = []
  131. nominatim_json = loads(resp.text)
  132. user_language = resp.search_params['language']
  133. if resp.search_params['route']:
  134. results.append(
  135. {
  136. 'answer': gettext('Get directions'),
  137. 'url': route_url.format(*resp.search_params['route'].groups()),
  138. }
  139. )
  140. fetch_wikidata(nominatim_json, user_language)
  141. for result in nominatim_json:
  142. title, address = get_title_address(result)
  143. # ignore result without title
  144. if not title:
  145. continue
  146. url, osm, geojson = get_url_osm_geojson(result)
  147. img_src = get_thumbnail(get_img_src(result))
  148. links, link_keys = get_links(result, user_language)
  149. data = get_data(result, user_language, link_keys)
  150. results.append(
  151. {
  152. 'template': 'map.html',
  153. 'title': title,
  154. 'address': address,
  155. 'address_label': get_key_label('addr', user_language),
  156. 'url': url,
  157. 'osm': osm,
  158. 'geojson': geojson,
  159. 'img_src': img_src,
  160. 'links': links,
  161. 'data': data,
  162. 'type': get_tag_label(result.get('category'), result.get('type', ''), user_language),
  163. 'type_icon': result.get('icon'),
  164. 'content': '',
  165. 'longitude': result['lon'],
  166. 'latitude': result['lat'],
  167. 'boundingbox': result['boundingbox'],
  168. }
  169. )
  170. return results
  171. def get_wikipedia_image(raw_value):
  172. if not raw_value:
  173. return None
  174. return get_external_url('wikimedia_image', raw_value)
  175. def fetch_wikidata(nominatim_json, user_language):
  176. """Update nominatim_json using the result of an unique to wikidata
  177. For result in nominatim_json:
  178. If result['extratags']['wikidata'] or r['extratags']['wikidata link']:
  179. Set result['wikidata'] to { 'image': ..., 'image_sign':..., 'image_symbal':... }
  180. Set result['extratags']['wikipedia'] if not defined
  181. Set result['extratags']['contact:website'] if not defined
  182. """
  183. wikidata_ids = []
  184. wd_to_results = {}
  185. for result in nominatim_json:
  186. e = result.get("extratags")
  187. if e:
  188. # ignore brand:wikidata
  189. wd_id = e.get("wikidata", e.get("wikidata link"))
  190. if wd_id and wd_id not in wikidata_ids:
  191. wikidata_ids.append("wd:" + wd_id)
  192. wd_to_results.setdefault(wd_id, []).append(result)
  193. if wikidata_ids:
  194. user_language = 'en' if user_language == 'all' else user_language.split('-')[0]
  195. wikidata_ids_str = " ".join(wikidata_ids)
  196. query = wikidata_image_sparql.replace('%WIKIDATA_IDS%', sparql_string_escape(wikidata_ids_str)).replace(
  197. '%LANGUAGE%', sparql_string_escape(user_language)
  198. )
  199. wikidata_json = send_wikidata_query(query)
  200. for wd_result in wikidata_json.get('results', {}).get('bindings', {}):
  201. wd_id = wd_result['item']['value'].replace('http://www.wikidata.org/entity/', '')
  202. for result in wd_to_results.get(wd_id, []):
  203. result['wikidata'] = {
  204. 'itemLabel': wd_result['itemLabel']['value'],
  205. 'image': get_wikipedia_image(wd_result.get('image', {}).get('value')),
  206. 'image_sign': get_wikipedia_image(wd_result.get('sign', {}).get('value')),
  207. 'image_symbol': get_wikipedia_image(wd_result.get('symbol', {}).get('value')),
  208. }
  209. # overwrite wikipedia link
  210. wikipedia_name = wd_result.get('wikipediaName', {}).get('value')
  211. if wikipedia_name:
  212. result['extratags']['wikipedia'] = user_language + ':' + wikipedia_name
  213. # get website if not already defined
  214. website = wd_result.get('website', {}).get('value')
  215. if (
  216. website
  217. and not result['extratags'].get('contact:website')
  218. and not result['extratags'].get('website')
  219. ):
  220. result['extratags']['contact:website'] = website
  221. def get_title_address(result):
  222. """Return title and address
  223. title may be None
  224. """
  225. address_raw = result.get('address')
  226. address_name = None
  227. address = {}
  228. # get name
  229. if (
  230. result['category'] == 'amenity'
  231. or result['category'] == 'shop'
  232. or result['category'] == 'tourism'
  233. or result['category'] == 'leisure'
  234. ):
  235. if address_raw.get('address29'):
  236. # https://github.com/osm-search/Nominatim/issues/1662
  237. address_name = address_raw.get('address29')
  238. else:
  239. address_name = address_raw.get(result['category'])
  240. elif result['type'] in address_raw:
  241. address_name = address_raw.get(result['type'])
  242. # add rest of adressdata, if something is already found
  243. if address_name:
  244. title = address_name
  245. address.update(
  246. {
  247. 'name': address_name,
  248. 'house_number': address_raw.get('house_number'),
  249. 'road': address_raw.get('road'),
  250. 'locality': address_raw.get(
  251. 'city', address_raw.get('town', address_raw.get('village')) # noqa
  252. ), # noqa
  253. 'postcode': address_raw.get('postcode'),
  254. 'country': address_raw.get('country'),
  255. 'country_code': address_raw.get('country_code'),
  256. }
  257. )
  258. else:
  259. title = result.get('display_name')
  260. return title, address
  261. def get_url_osm_geojson(result):
  262. """Get url, osm and geojson"""
  263. osm_type = result.get('osm_type', result.get('type'))
  264. if 'osm_id' not in result:
  265. # see https://github.com/osm-search/Nominatim/issues/1521
  266. # query example: "EC1M 5RF London"
  267. url = result_lat_lon_url.format(lat=result['lat'], lon=result['lon'], zoom=12)
  268. osm = {}
  269. else:
  270. url = result_id_url.format(osm_type=osm_type, osm_id=result['osm_id'])
  271. osm = {'type': osm_type, 'id': result['osm_id']}
  272. geojson = result.get('geojson')
  273. # if no geojson is found and osm_type is a node, add geojson Point
  274. if not geojson and osm_type == 'node':
  275. geojson = {'type': 'Point', 'coordinates': [result['lon'], result['lat']]}
  276. return url, osm, geojson
  277. def get_img_src(result):
  278. """Get image URL from either wikidata or r['extratags']"""
  279. # wikidata
  280. img_src = None
  281. if 'wikidata' in result:
  282. img_src = result['wikidata']['image']
  283. if not img_src:
  284. img_src = result['wikidata']['image_symbol']
  285. if not img_src:
  286. img_src = result['wikidata']['image_sign']
  287. # img_src
  288. if not img_src and result.get('extratags', {}).get('image'):
  289. img_src = result['extratags']['image']
  290. del result['extratags']['image']
  291. if not img_src and result.get('extratags', {}).get('wikimedia_commons'):
  292. img_src = get_external_url('wikimedia_image', result['extratags']['wikimedia_commons'])
  293. del result['extratags']['wikimedia_commons']
  294. return img_src
  295. def get_links(result, user_language):
  296. """Return links from result['extratags']"""
  297. links = []
  298. link_keys = set()
  299. for k, mapping_function in VALUE_TO_LINK.items():
  300. raw_value = result['extratags'].get(k)
  301. if raw_value:
  302. url, url_label = mapping_function(raw_value)
  303. if url.startswith('https://wikidata.org'):
  304. url_label = result.get('wikidata', {}).get('itemLabel') or url_label
  305. links.append(
  306. {
  307. 'label': get_key_label(k, user_language),
  308. 'url': url,
  309. 'url_label': url_label,
  310. }
  311. )
  312. link_keys.add(k)
  313. return links, link_keys
  314. def get_data(result, user_language, ignore_keys):
  315. """Return key, value of result['extratags']
  316. Must be call after get_links
  317. Note: the values are not translated
  318. """
  319. data = []
  320. for k, v in result['extratags'].items():
  321. if k in ignore_keys:
  322. continue
  323. if get_key_rank(k) is None:
  324. continue
  325. k_label = get_key_label(k, user_language)
  326. if k_label:
  327. data.append(
  328. {
  329. 'label': k_label,
  330. 'key': k,
  331. 'value': v,
  332. }
  333. )
  334. data.sort(key=lambda entry: (get_key_rank(entry['key']), entry['label']))
  335. return data
  336. def get_key_rank(k):
  337. """Get OSM key rank
  338. The rank defines in which order the key are displayed in the HTML result
  339. """
  340. key_rank = KEY_RANKS.get(k)
  341. if key_rank is None:
  342. # "payment:*" in KEY_ORDER matches "payment:cash", "payment:debit card", etc...
  343. key_rank = KEY_RANKS.get(k.split(':')[0] + ':*')
  344. return key_rank
  345. def get_label(labels, lang):
  346. """Get label from labels in OSM_KEYS_TAGS
  347. in OSM_KEYS_TAGS, labels have key == '*'
  348. """
  349. tag_label = labels.get(lang.lower())
  350. if tag_label is None:
  351. # example: if 'zh-hk' is not found, check 'zh'
  352. tag_label = labels.get(lang.split('-')[0])
  353. if tag_label is None and lang != 'en':
  354. # example: if 'zh' is not found, check 'en'
  355. tag_label = labels.get('en')
  356. if tag_label is None and len(labels.values()) > 0:
  357. # example: if still not found, use the first entry
  358. tag_label = labels.values()[0]
  359. return tag_label
  360. def get_tag_label(tag_category, tag_name, lang):
  361. """Get tag label from OSM_KEYS_TAGS"""
  362. tag_name = '' if tag_name is None else tag_name
  363. tag_labels = OSM_KEYS_TAGS['tags'].get(tag_category, {}).get(tag_name, {})
  364. return get_label(tag_labels, lang)
  365. def get_key_label(key_name, lang):
  366. """Get key label from OSM_KEYS_TAGS"""
  367. if key_name.startswith('currency:'):
  368. # currency:EUR --> get the name from the CURRENCIES variable
  369. # see https://wiki.openstreetmap.org/wiki/Key%3Acurrency
  370. # and for exampe https://taginfo.openstreetmap.org/keys/currency:EUR#values
  371. # but there is also currency=EUR (currently not handled)
  372. # https://taginfo.openstreetmap.org/keys/currency#values
  373. currency = key_name.split(':')
  374. if len(currency) > 1:
  375. o = CURRENCIES['iso4217'].get(currency)
  376. if o:
  377. return get_label(o, lang).lower()
  378. return currency
  379. labels = OSM_KEYS_TAGS['keys']
  380. for k in key_name.split(':') + ['*']:
  381. labels = labels.get(k)
  382. if labels is None:
  383. return None
  384. return get_label(labels, lang)