| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 | # SPDX-License-Identifier: AGPL-3.0-or-later""" Photon (Map)"""from json import loadsfrom urllib.parse import urlencodefrom searx.utils import searx_useragent# aboutabout = {    "website": 'https://photon.komoot.io',    "wikidata_id": None,    "official_api_documentation": 'https://photon.komoot.io/',    "use_official_api": True,    "require_api_key": False,    "results": 'JSON',}# engine dependent configcategories = ['map']paging = Falsenumber_of_results = 10# search-urlbase_url = 'https://photon.komoot.io/'search_string = 'api/?{query}&limit={limit}'result_base_url = 'https://openstreetmap.org/{osm_type}/{osm_id}'# list of supported languagessupported_languages = ['de', 'en', 'fr', 'it']# do search-requestdef request(query, params):    params['url'] = base_url + search_string.format(query=urlencode({'q': query}), limit=number_of_results)    if params['language'] != 'all':        language = params['language'].split('_')[0]        if language in supported_languages:            params['url'] = params['url'] + "&lang=" + language    # using searx User-Agent    params['headers']['User-Agent'] = searx_useragent()    return params# get response from search-requestdef response(resp):    results = []    json = loads(resp.text)    # parse results    for r in json.get('features', {}):        properties = r.get('properties')        if not properties:            continue        # get title        title = properties.get('name')        # 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:            # continue if invalid osm-type            continue        url = result_base_url.format(osm_type=osm_type, osm_id=properties.get('osm_id'))        osm = {'type': osm_type, 'id': properties.get('osm_id')}        geojson = r.get('geometry')        if properties.get('extent'):            boundingbox = [                properties.get('extent')[3],                properties.get('extent')[1],                properties.get('extent')[0],                properties.get('extent')[2],            ]        else:            # better boundingbox calculation?            boundingbox = [                geojson['coordinates'][1],                geojson['coordinates'][1],                geojson['coordinates'][0],                geojson['coordinates'][0],            ]        # address calculation        address = {}        # get name        if (            properties.get('osm_key') == 'amenity'            or properties.get('osm_key') == 'shop'            or properties.get('osm_key') == 'tourism'            or properties.get('osm_key') == 'leisure'        ):            address = {'name': properties.get('name')}        # add rest of adressdata, if something is already found        if address.get('name'):            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'),                }            )        else:            address = None        # append result        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,            }        )    # return results    return results
 |