2021-01-13 10:31:25 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2015-05-02 13:45:17 +00:00
|
|
|
"""
|
|
|
|
OpenStreetMap (Map)
|
|
|
|
"""
|
2014-09-06 12:09:03 +00:00
|
|
|
|
2020-06-09 15:01:59 +00:00
|
|
|
import re
|
2014-09-06 12:09:03 +00:00
|
|
|
from json import loads
|
2020-06-09 15:01:59 +00:00
|
|
|
from flask_babel import gettext
|
2014-09-06 12:09:03 +00:00
|
|
|
|
2021-01-13 10:31:25 +00:00
|
|
|
# about
|
|
|
|
about = {
|
|
|
|
"website": 'https://www.openstreetmap.org/',
|
|
|
|
"wikidata_id": 'Q936',
|
|
|
|
"official_api_documentation": 'http://wiki.openstreetmap.org/wiki/Nominatim',
|
|
|
|
"use_official_api": True,
|
|
|
|
"require_api_key": False,
|
|
|
|
"results": 'JSON',
|
|
|
|
}
|
|
|
|
|
2014-09-06 12:09:03 +00:00
|
|
|
# engine dependent config
|
|
|
|
categories = ['map']
|
|
|
|
paging = False
|
|
|
|
|
|
|
|
# search-url
|
2014-12-16 16:01:25 +00:00
|
|
|
base_url = 'https://nominatim.openstreetmap.org/'
|
|
|
|
search_string = 'search/{query}?format=json&polygon_geojson=1&addressdetails=1'
|
2014-09-06 12:09:03 +00:00
|
|
|
result_base_url = 'https://openstreetmap.org/{osm_type}/{osm_id}'
|
|
|
|
|
2020-06-09 22:34:57 +00:00
|
|
|
route_url = 'https://graphhopper.com/maps/?point={}&point={}&locale=en-US&vehicle=car&weighting=fastest&turn_costs=true&use_miles=false&layer=Omniscale' # noqa
|
2020-06-09 15:01:59 +00:00
|
|
|
route_re = re.compile('(?:from )?(.+) to (.+)')
|
|
|
|
|
2014-09-06 12:09:03 +00:00
|
|
|
|
|
|
|
# do search-request
|
|
|
|
def request(query, params):
|
2020-06-09 15:01:59 +00:00
|
|
|
|
2020-08-11 14:25:03 +00:00
|
|
|
params['url'] = base_url + search_string.format(query=query)
|
|
|
|
params['route'] = route_re.match(query)
|
2014-12-16 14:01:05 +00:00
|
|
|
|
2014-09-06 12:09:03 +00:00
|
|
|
return params
|
|
|
|
|
|
|
|
|
|
|
|
# get response from search-request
|
|
|
|
def response(resp):
|
|
|
|
results = []
|
|
|
|
json = loads(resp.text)
|
|
|
|
|
2020-06-09 15:01:59 +00:00
|
|
|
if resp.search_params['route']:
|
|
|
|
results.append({
|
|
|
|
'answer': gettext('Get directions'),
|
|
|
|
'url': route_url.format(*resp.search_params['route'].groups()),
|
|
|
|
})
|
|
|
|
|
2014-09-06 12:09:03 +00:00
|
|
|
# parse results
|
|
|
|
for r in json:
|
2015-02-07 00:15:04 +00:00
|
|
|
if 'display_name' not in r:
|
|
|
|
continue
|
|
|
|
|
2020-08-06 15:42:46 +00:00
|
|
|
title = r['display_name'] or ''
|
2014-09-06 12:09:03 +00:00
|
|
|
osm_type = r.get('osm_type', r.get('type'))
|
|
|
|
url = result_base_url.format(osm_type=osm_type,
|
|
|
|
osm_id=r['osm_id'])
|
2014-11-02 12:00:28 +00:00
|
|
|
|
2014-12-07 15:37:56 +00:00
|
|
|
osm = {'type': osm_type,
|
|
|
|
'id': r['osm_id']}
|
2014-11-19 13:59:30 +00:00
|
|
|
|
2014-12-07 15:37:56 +00:00
|
|
|
geojson = r.get('geojson')
|
2014-11-02 12:00:28 +00:00
|
|
|
|
|
|
|
# if no geojson is found and osm_type is a node, add geojson Point
|
2015-02-07 00:15:04 +00:00
|
|
|
if not geojson and osm_type == 'node':
|
2020-08-06 15:42:46 +00:00
|
|
|
geojson = {'type': 'Point', 'coordinates': [r['lon'], r['lat']]}
|
2014-11-02 12:00:28 +00:00
|
|
|
|
2014-11-03 17:46:58 +00:00
|
|
|
address_raw = r.get('address')
|
|
|
|
address = {}
|
|
|
|
|
|
|
|
# get name
|
|
|
|
if r['class'] == 'amenity' or\
|
|
|
|
r['class'] == 'shop' or\
|
|
|
|
r['class'] == 'tourism' or\
|
|
|
|
r['class'] == 'leisure':
|
|
|
|
if address_raw.get('address29'):
|
2014-12-07 15:37:56 +00:00
|
|
|
address = {'name': address_raw.get('address29')}
|
2014-11-03 17:46:58 +00:00
|
|
|
else:
|
2014-12-07 15:37:56 +00:00
|
|
|
address = {'name': address_raw.get(r['type'])}
|
2014-11-03 17:46:58 +00:00
|
|
|
|
|
|
|
# add rest of adressdata, if something is already found
|
|
|
|
if address.get('name'):
|
2014-12-07 15:37:56 +00:00
|
|
|
address.update({'house_number': address_raw.get('house_number'),
|
|
|
|
'road': address_raw.get('road'),
|
|
|
|
'locality': address_raw.get('city',
|
2014-12-16 16:01:25 +00:00
|
|
|
address_raw.get('town', # noqa
|
|
|
|
address_raw.get('village'))), # noqa
|
2014-12-07 15:37:56 +00:00
|
|
|
'postcode': address_raw.get('postcode'),
|
|
|
|
'country': address_raw.get('country'),
|
|
|
|
'country_code': address_raw.get('country_code')})
|
2014-11-03 17:46:58 +00:00
|
|
|
else:
|
|
|
|
address = None
|
|
|
|
|
2014-09-06 12:09:03 +00:00
|
|
|
# append result
|
2014-11-02 12:00:28 +00:00
|
|
|
results.append({'template': 'map.html',
|
|
|
|
'title': title,
|
2014-09-06 12:09:03 +00:00
|
|
|
'content': '',
|
2014-11-02 12:00:28 +00:00
|
|
|
'longitude': r['lon'],
|
|
|
|
'latitude': r['lat'],
|
|
|
|
'boundingbox': r['boundingbox'],
|
|
|
|
'geojson': geojson,
|
2014-11-03 17:46:58 +00:00
|
|
|
'address': address,
|
2014-11-19 13:59:30 +00:00
|
|
|
'osm': osm,
|
2014-09-06 12:09:03 +00:00
|
|
|
'url': url})
|
|
|
|
|
|
|
|
# return results
|
|
|
|
return results
|