.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
photon.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""
3 Photon (Map)
4"""
5
6from json import loads
7from urllib.parse import urlencode
8from searx.utils import searx_useragent
9
10# about
11about = {
12 "website": 'https://photon.komoot.io',
13 "wikidata_id": None,
14 "official_api_documentation": 'https://photon.komoot.io/',
15 "use_official_api": True,
16 "require_api_key": False,
17 "results": 'JSON',
18}
19
20# engine dependent config
21categories = ['map']
22paging = False
23number_of_results = 10
24
25# search-url
26base_url = 'https://photon.komoot.io/'
27search_string = 'api/?{query}&limit={limit}'
28result_base_url = 'https://openstreetmap.org/{osm_type}/{osm_id}'
29
30# list of supported languages
31supported_languages = ['de', 'en', 'fr', 'it']
32
33
34# do search-request
35def request(query, params):
36 params['url'] = base_url + search_string.format(query=urlencode({'q': query}), limit=number_of_results)
37
38 if params['language'] != 'all':
39 language = params['language'].split('_')[0]
40 if language in supported_languages:
41 params['url'] = params['url'] + "&lang=" + language
42
43 # using searx User-Agent
44 params['headers']['User-Agent'] = searx_useragent()
45
46 return params
47
48
49# get response from search-request
50def response(resp):
51 results = []
52 json = loads(resp.text)
53
54 # parse results
55 for r in json.get('features', {}):
56
57 properties = r.get('properties')
58
59 if not properties:
60 continue
61
62 # get title
63 title = properties.get('name')
64
65 # get osm-type
66 if properties.get('osm_type') == 'N':
67 osm_type = 'node'
68 elif properties.get('osm_type') == 'W':
69 osm_type = 'way'
70 elif properties.get('osm_type') == 'R':
71 osm_type = 'relation'
72 else:
73 # continue if invalid osm-type
74 continue
75
76 url = result_base_url.format(osm_type=osm_type, osm_id=properties.get('osm_id'))
77
78 osm = {'type': osm_type, 'id': properties.get('osm_id')}
79
80 geojson = r.get('geometry')
81
82 if properties.get('extent'):
83 boundingbox = [
84 properties.get('extent')[3],
85 properties.get('extent')[1],
86 properties.get('extent')[0],
87 properties.get('extent')[2],
88 ]
89 else:
90 # better boundingbox calculation?
91 boundingbox = [
92 geojson['coordinates'][1],
93 geojson['coordinates'][1],
94 geojson['coordinates'][0],
95 geojson['coordinates'][0],
96 ]
97
98 # address calculation
99 address = {}
100
101 # get name
102 if (
103 properties.get('osm_key') == 'amenity'
104 or properties.get('osm_key') == 'shop'
105 or properties.get('osm_key') == 'tourism'
106 or properties.get('osm_key') == 'leisure'
107 ):
108 address = {'name': properties.get('name')}
109
110 # add rest of adressdata, if something is already found
111 if address.get('name'):
112 address.update(
113 {
114 'house_number': properties.get('housenumber'),
115 'road': properties.get('street'),
116 'locality': properties.get(
117 'city', properties.get('town', properties.get('village')) # noqa
118 ), # noqa
119 'postcode': properties.get('postcode'),
120 'country': properties.get('country'),
121 }
122 )
123 else:
124 address = None
125
126 # append result
127 results.append(
128 {
129 'template': 'map.html',
130 'title': title,
131 'content': '',
132 'longitude': geojson['coordinates'][0],
133 'latitude': geojson['coordinates'][1],
134 'boundingbox': boundingbox,
135 'geojson': geojson,
136 'address': address,
137 'osm': osm,
138 'url': url,
139 }
140 )
141
142 # return results
143 return results
request(query, params)
Definition photon.py:35