summaryrefslogtreecommitdiff
path: root/searx/engines/openstreetmap.py
blob: d73a116ba3ebe3432f4f14f6809f3433d514fb1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
## OpenStreetMap (Map)
#
# @website     https://openstreetmap.org/
# @provide-api yes (http://wiki.openstreetmap.org/wiki/Nominatim)
#
# @using-api   yes
# @results     JSON
# @stable      yes
# @parse       url, title

from json import loads

# engine dependent config
categories = ['map']
paging = False

# search-url
url = 'https://nominatim.openstreetmap.org/search/{query}?format=json&polygon_geojson=1'

result_base_url = 'https://openstreetmap.org/{osm_type}/{osm_id}'


# do search-request
def request(query, params):
    params['url'] = url.format(query=query)

    return params


# get response from search-request
def response(resp):
    results = []
    json = loads(resp.text)

    # parse results
    for r in json:
        title = r['display_name']
        osm_type = r.get('osm_type', r.get('type'))
        url = result_base_url.format(osm_type=osm_type,
                                     osm_id=r['osm_id'])

        geojson =  r.get('geojson')

        # if no geojson is found and osm_type is a node, add geojson Point
        if not geojson and\
           osm_type == 'node':
            geojson = {u'type':u'Point', 
                       u'coordinates':[r['lon'],r['lat']]}

        # append result
        results.append({'template': 'map.html',
                        'title': title,
                        'content': '',
                        'longitude': r['lon'],
                        'latitude': r['lat'],
                        'boundingbox': r['boundingbox'],
                        'geojson': geojson,
                        'url': url})

    # return results
    return results