|  | @@ -0,0 +1,121 @@
 | 
	
		
			
			|  | 1 | +## Photon (Map)
 | 
	
		
			
			|  | 2 | +#
 | 
	
		
			
			|  | 3 | +# @website     https://photon.komoot.de
 | 
	
		
			
			|  | 4 | +# @provide-api yes (https://photon.komoot.de/)
 | 
	
		
			
			|  | 5 | +#
 | 
	
		
			
			|  | 6 | +# @using-api   yes
 | 
	
		
			
			|  | 7 | +# @results     JSON
 | 
	
		
			
			|  | 8 | +# @stable      yes
 | 
	
		
			
			|  | 9 | +# @parse       url, title
 | 
	
		
			
			|  | 10 | +
 | 
	
		
			
			|  | 11 | +from urllib import urlencode
 | 
	
		
			
			|  | 12 | +from json import loads
 | 
	
		
			
			|  | 13 | +from searx.utils import searx_useragent
 | 
	
		
			
			|  | 14 | +
 | 
	
		
			
			|  | 15 | +# engine dependent config
 | 
	
		
			
			|  | 16 | +categories = ['map']
 | 
	
		
			
			|  | 17 | +paging = False
 | 
	
		
			
			|  | 18 | +language_support = True
 | 
	
		
			
			|  | 19 | +number_of_results = 10
 | 
	
		
			
			|  | 20 | +
 | 
	
		
			
			|  | 21 | +# search-url
 | 
	
		
			
			|  | 22 | +search_url = 'https://photon.komoot.de/api/?{query}&limit={limit}'
 | 
	
		
			
			|  | 23 | +result_base_url = 'https://openstreetmap.org/{osm_type}/{osm_id}'
 | 
	
		
			
			|  | 24 | +
 | 
	
		
			
			|  | 25 | +
 | 
	
		
			
			|  | 26 | +# do search-request
 | 
	
		
			
			|  | 27 | +def request(query, params):
 | 
	
		
			
			|  | 28 | +    params['url'] = search_url.format(query=urlencode({'q': query}),
 | 
	
		
			
			|  | 29 | +                                      limit=number_of_results)
 | 
	
		
			
			|  | 30 | +
 | 
	
		
			
			|  | 31 | +    if params['language'] != 'all':
 | 
	
		
			
			|  | 32 | +        params['url'] = params['url'] + "&lang=" + params['language'].replace('_', '-')
 | 
	
		
			
			|  | 33 | +
 | 
	
		
			
			|  | 34 | +    # using searx User-Agent
 | 
	
		
			
			|  | 35 | +    params['headers']['User-Agent'] = searx_useragent()
 | 
	
		
			
			|  | 36 | +    
 | 
	
		
			
			|  | 37 | +    # FIX: SSLError: [Errno 1] _ssl.c:510: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
 | 
	
		
			
			|  | 38 | +    params['verify'] = False
 | 
	
		
			
			|  | 39 | +
 | 
	
		
			
			|  | 40 | +    return params
 | 
	
		
			
			|  | 41 | +
 | 
	
		
			
			|  | 42 | +
 | 
	
		
			
			|  | 43 | +# get response from search-request
 | 
	
		
			
			|  | 44 | +def response(resp):
 | 
	
		
			
			|  | 45 | +    results = []
 | 
	
		
			
			|  | 46 | +    json = loads(resp.text)
 | 
	
		
			
			|  | 47 | +
 | 
	
		
			
			|  | 48 | +    # parse results
 | 
	
		
			
			|  | 49 | +    for r in json.get('features', {}):
 | 
	
		
			
			|  | 50 | +    
 | 
	
		
			
			|  | 51 | +        properties = r.get('properties')
 | 
	
		
			
			|  | 52 | +        
 | 
	
		
			
			|  | 53 | +        if not properties:
 | 
	
		
			
			|  | 54 | +            continue
 | 
	
		
			
			|  | 55 | +        
 | 
	
		
			
			|  | 56 | +        # get title
 | 
	
		
			
			|  | 57 | +        title = properties['name']
 | 
	
		
			
			|  | 58 | +        
 | 
	
		
			
			|  | 59 | +        # get osm-type
 | 
	
		
			
			|  | 60 | +        if properties.get('osm_type') == 'N':
 | 
	
		
			
			|  | 61 | +            osm_type = 'node'
 | 
	
		
			
			|  | 62 | +        elif properties.get('osm_type') == 'W':
 | 
	
		
			
			|  | 63 | +            osm_type = 'way'
 | 
	
		
			
			|  | 64 | +        elif properties.get('osm_type') == 'R':
 | 
	
		
			
			|  | 65 | +            osm_type = 'relation'
 | 
	
		
			
			|  | 66 | +        else:
 | 
	
		
			
			|  | 67 | +            # continue if invalide osm-type
 | 
	
		
			
			|  | 68 | +            continue
 | 
	
		
			
			|  | 69 | +            
 | 
	
		
			
			|  | 70 | +        url = result_base_url.format(osm_type=osm_type,
 | 
	
		
			
			|  | 71 | +                                     osm_id=properties.get('osm_id'))
 | 
	
		
			
			|  | 72 | +
 | 
	
		
			
			|  | 73 | +        osm = {'type': osm_type,
 | 
	
		
			
			|  | 74 | +               'id': properties.get('osm_id')}
 | 
	
		
			
			|  | 75 | +
 | 
	
		
			
			|  | 76 | +        geojson = r.get('geometry')
 | 
	
		
			
			|  | 77 | +        
 | 
	
		
			
			|  | 78 | +        if  properties.get('extent'):
 | 
	
		
			
			|  | 79 | +            boundingbox = [properties.get('extent')[3], properties.get('extent')[1], properties.get('extent')[0], properties.get('extent')[2]]
 | 
	
		
			
			|  | 80 | +        else:
 | 
	
		
			
			|  | 81 | +            # TODO: better boundingbox calculation
 | 
	
		
			
			|  | 82 | +            boundingbox = [geojson['coordinates'][1], geojson['coordinates'][1], geojson['coordinates'][0], geojson['coordinates'][0]]
 | 
	
		
			
			|  | 83 | +        
 | 
	
		
			
			|  | 84 | +        # TODO: address calculation
 | 
	
		
			
			|  | 85 | +        address = {}
 | 
	
		
			
			|  | 86 | +
 | 
	
		
			
			|  | 87 | +        # get name
 | 
	
		
			
			|  | 88 | +        if properties.get('osm_key') == 'amenity' or\
 | 
	
		
			
			|  | 89 | +           properties.get('osm_key') == 'shop' or\
 | 
	
		
			
			|  | 90 | +           properties.get('osm_key') == 'tourism' or\
 | 
	
		
			
			|  | 91 | +           properties.get('osm_key') == 'leisure':
 | 
	
		
			
			|  | 92 | +            address = {'name': properties.get('name')}
 | 
	
		
			
			|  | 93 | +                
 | 
	
		
			
			|  | 94 | +        # add rest of adressdata, if something is already found
 | 
	
		
			
			|  | 95 | +        if address.get('name'):
 | 
	
		
			
			|  | 96 | +            address.update({'house_number': properties.get('housenumber'),
 | 
	
		
			
			|  | 97 | +                           'road': properties.get('street'),
 | 
	
		
			
			|  | 98 | +                           'locality': properties.get('city',
 | 
	
		
			
			|  | 99 | +                                       properties.get('town',
 | 
	
		
			
			|  | 100 | +                                       properties.get('village'))),
 | 
	
		
			
			|  | 101 | +                           'postcode': properties.get('postcode'),
 | 
	
		
			
			|  | 102 | +                           'country': properties.get('country')})
 | 
	
		
			
			|  | 103 | +        else:
 | 
	
		
			
			|  | 104 | +            address = None
 | 
	
		
			
			|  | 105 | +
 | 
	
		
			
			|  | 106 | +        # append result
 | 
	
		
			
			|  | 107 | +        results.append({'template': 'map.html',
 | 
	
		
			
			|  | 108 | +                        'title': title,
 | 
	
		
			
			|  | 109 | +                        'content': '',
 | 
	
		
			
			|  | 110 | +                        'longitude': geojson['coordinates'][0],
 | 
	
		
			
			|  | 111 | +                        'latitude': geojson['coordinates'][1],
 | 
	
		
			
			|  | 112 | +                        'boundingbox': boundingbox,
 | 
	
		
			
			|  | 113 | +                        'geojson': geojson,
 | 
	
		
			
			|  | 114 | +                        'address': address,
 | 
	
		
			
			|  | 115 | +                        'osm': osm,
 | 
	
		
			
			|  | 116 | +                        'url': url})
 | 
	
		
			
			|  | 117 | +
 | 
	
		
			
			|  | 118 | +        print r['properties']['name']
 | 
	
		
			
			|  | 119 | +
 | 
	
		
			
			|  | 120 | +    # return results
 | 
	
		
			
			|  | 121 | +    return results
 |