| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							| 
									
										
										
										
											2015-05-02 15:45:17 +02:00
										 |  |  | """
 | 
					
						
							|  |  |  |  Photon (Map) | 
					
						
							|  |  |  | """
 | 
					
						
							| 
									
										
										
										
											2014-12-16 15:01:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | from json import loads | 
					
						
							| 
									
										
										
										
											2020-08-06 17:42:46 +02:00
										 |  |  | from urllib.parse import urlencode | 
					
						
							| 
									
										
										
										
											2014-12-16 15:01:05 +01:00
										 |  |  | from searx.utils import searx_useragent | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  | # about | 
					
						
							|  |  |  | about = { | 
					
						
							| 
									
										
										
										
											2021-09-18 11:02:39 +02:00
										 |  |  |     "website": 'https://photon.komoot.io', | 
					
						
							| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  |     "wikidata_id": None, | 
					
						
							| 
									
										
										
										
											2021-09-18 11:02:39 +02:00
										 |  |  |     "official_api_documentation": 'https://photon.komoot.io/', | 
					
						
							| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  |     "use_official_api": True, | 
					
						
							|  |  |  |     "require_api_key": False, | 
					
						
							|  |  |  |     "results": 'JSON', | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-16 15:01:05 +01:00
										 |  |  | # engine dependent config | 
					
						
							|  |  |  | categories = ['map'] | 
					
						
							|  |  |  | paging = False | 
					
						
							|  |  |  | number_of_results = 10 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # search-url | 
					
						
							| 
									
										
										
										
											2020-11-27 08:22:28 +01:00
										 |  |  | base_url = 'https://photon.komoot.io/' | 
					
						
							| 
									
										
										
										
											2014-12-16 17:01:25 +01:00
										 |  |  | search_string = 'api/?{query}&limit={limit}' | 
					
						
							| 
									
										
										
										
											2014-12-16 15:01:05 +01:00
										 |  |  | result_base_url = 'https://openstreetmap.org/{osm_type}/{osm_id}' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-20 10:25:53 +01:00
										 |  |  | # list of supported languages | 
					
						
							| 
									
										
										
										
											2016-08-06 06:34:56 +02:00
										 |  |  | supported_languages = ['de', 'en', 'fr', 'it'] | 
					
						
							| 
									
										
										
										
											2014-12-20 10:25:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-16 15:01:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | # do search-request | 
					
						
							|  |  |  | def request(query, params): | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |     params['url'] = base_url + search_string.format(query=urlencode({'q': query}), limit=number_of_results) | 
					
						
							| 
									
										
										
										
											2014-12-16 15:01:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-06 15:27:46 +01:00
										 |  |  |     if params['language'] != 'all': | 
					
						
							|  |  |  |         language = params['language'].split('_')[0] | 
					
						
							|  |  |  |         if language in supported_languages: | 
					
						
							|  |  |  |             params['url'] = params['url'] + "&lang=" + language | 
					
						
							| 
									
										
										
										
											2014-12-16 15:01:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # using searx User-Agent | 
					
						
							|  |  |  |     params['headers']['User-Agent'] = searx_useragent() | 
					
						
							| 
									
										
										
										
											2014-12-16 17:01:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-16 15:01:05 +01:00
										 |  |  |     return params | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # get response from search-request | 
					
						
							|  |  |  | def response(resp): | 
					
						
							|  |  |  |     results = [] | 
					
						
							|  |  |  |     json = loads(resp.text) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # parse results | 
					
						
							|  |  |  |     for r in json.get('features', {}): | 
					
						
							| 
									
										
										
										
											2014-12-16 17:01:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-16 15:01:05 +01:00
										 |  |  |         properties = r.get('properties') | 
					
						
							| 
									
										
										
										
											2014-12-16 17:01:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-16 15:01:05 +01:00
										 |  |  |         if not properties: | 
					
						
							|  |  |  |             continue | 
					
						
							| 
									
										
										
										
											2014-12-16 17:01:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-16 15:01:05 +01:00
										 |  |  |         # get title | 
					
						
							| 
									
										
										
										
											2015-02-10 18:44:49 +01:00
										 |  |  |         title = properties.get('name') | 
					
						
							| 
									
										
										
										
											2014-12-16 17:01:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-16 15:01:05 +01:00
										 |  |  |         # get osm-type | 
					
						
							|  |  |  |         if properties.get('osm_type') == 'N': | 
					
						
							|  |  |  |             osm_type = 'node' | 
					
						
							|  |  |  |         elif properties.get('osm_type') == 'W': | 
					
						
							|  |  |  |             osm_type = 'way' | 
					
						
							|  |  |  |         elif properties.get('osm_type') == 'R': | 
					
						
							|  |  |  |             osm_type = 'relation' | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2022-09-27 17:01:00 +02:00
										 |  |  |             # continue if invalid osm-type | 
					
						
							| 
									
										
										
										
											2014-12-16 15:01:05 +01:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2014-12-16 17:01:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         url = result_base_url.format(osm_type=osm_type, osm_id=properties.get('osm_id')) | 
					
						
							| 
									
										
										
										
											2014-12-16 15:01:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         osm = {'type': osm_type, 'id': properties.get('osm_id')} | 
					
						
							| 
									
										
										
										
											2014-12-16 15:01:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         geojson = r.get('geometry') | 
					
						
							| 
									
										
										
										
											2014-12-16 17:01:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if properties.get('extent'): | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |             boundingbox = [ | 
					
						
							|  |  |  |                 properties.get('extent')[3], | 
					
						
							|  |  |  |                 properties.get('extent')[1], | 
					
						
							|  |  |  |                 properties.get('extent')[0], | 
					
						
							|  |  |  |                 properties.get('extent')[2], | 
					
						
							|  |  |  |             ] | 
					
						
							| 
									
										
										
										
											2014-12-16 15:01:05 +01:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  |             # better boundingbox calculation? | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |             boundingbox = [ | 
					
						
							|  |  |  |                 geojson['coordinates'][1], | 
					
						
							|  |  |  |                 geojson['coordinates'][1], | 
					
						
							|  |  |  |                 geojson['coordinates'][0], | 
					
						
							|  |  |  |                 geojson['coordinates'][0], | 
					
						
							|  |  |  |             ] | 
					
						
							| 
									
										
										
										
											2014-12-16 17:01:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # address calculation | 
					
						
							| 
									
										
										
										
											2014-12-16 15:01:05 +01:00
										 |  |  |         address = {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # get name | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         if ( | 
					
						
							|  |  |  |             properties.get('osm_key') == 'amenity' | 
					
						
							|  |  |  |             or properties.get('osm_key') == 'shop' | 
					
						
							|  |  |  |             or properties.get('osm_key') == 'tourism' | 
					
						
							|  |  |  |             or properties.get('osm_key') == 'leisure' | 
					
						
							|  |  |  |         ): | 
					
						
							| 
									
										
										
										
											2014-12-16 15:01:05 +01:00
										 |  |  |             address = {'name': properties.get('name')} | 
					
						
							| 
									
										
										
										
											2014-12-16 17:01:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-16 15:01:05 +01:00
										 |  |  |         # add rest of adressdata, if something is already found | 
					
						
							|  |  |  |         if address.get('name'): | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |             address.update( | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     'house_number': properties.get('housenumber'), | 
					
						
							|  |  |  |                     'road': properties.get('street'), | 
					
						
							|  |  |  |                     'locality': properties.get( | 
					
						
							|  |  |  |                         'city', properties.get('town', properties.get('village'))  # noqa | 
					
						
							|  |  |  |                     ),  # noqa | 
					
						
							|  |  |  |                     'postcode': properties.get('postcode'), | 
					
						
							|  |  |  |                     'country': properties.get('country'), | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2014-12-16 15:01:05 +01:00
										 |  |  |         else: | 
					
						
							|  |  |  |             address = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # append result | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         results.append( | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 'template': 'map.html', | 
					
						
							|  |  |  |                 'title': title, | 
					
						
							|  |  |  |                 'content': '', | 
					
						
							|  |  |  |                 'longitude': geojson['coordinates'][0], | 
					
						
							|  |  |  |                 'latitude': geojson['coordinates'][1], | 
					
						
							|  |  |  |                 'boundingbox': boundingbox, | 
					
						
							|  |  |  |                 'geojson': geojson, | 
					
						
							|  |  |  |                 'address': address, | 
					
						
							|  |  |  |                 'osm': osm, | 
					
						
							|  |  |  |                 'url': url, | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2014-12-16 15:01:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # return results | 
					
						
							|  |  |  |     return results |