2023-01-21 01:18:16 +01:00
|
|
|
const cache = require('memory-cache')
|
|
|
|
const providerCache = new cache.Cache()
|
2023-02-17 00:23:35 +01:00
|
|
|
const get = require('lodash/get')
|
2023-01-19 12:06:23 +01:00
|
|
|
|
2023-01-21 01:18:16 +01:00
|
|
|
const nominatim = {
|
2023-01-19 12:06:23 +01:00
|
|
|
commonName: 'Nominatim',
|
|
|
|
DEFAULT_ENDPOINT: 'https://nominatim.openstreetmap.org/search',
|
|
|
|
endpoint: (req, res) => {
|
2023-01-21 01:18:16 +01:00
|
|
|
return res.locals.settings.geocoding_provider || nominatim.DEFAULT_ENDPOINT
|
2023-01-19 12:06:23 +01:00
|
|
|
},
|
2023-01-21 01:18:16 +01:00
|
|
|
cache: providerCache,
|
2023-01-19 12:06:23 +01:00
|
|
|
|
|
|
|
getParams (req, res) {
|
|
|
|
const countrycodes = res.locals.settings.geocoding_countrycodes || []
|
|
|
|
const details = req.params.place_details
|
|
|
|
|
|
|
|
return {
|
|
|
|
countrycodes: countrycodes.join(','),
|
|
|
|
q: details,
|
|
|
|
limit: 3,
|
|
|
|
format: 'json',
|
|
|
|
addressdetails: 1,
|
|
|
|
namedetails: 1,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-02-20 07:05:21 +01:00
|
|
|
/**
|
2023-02-17 00:23:35 +01:00
|
|
|
* Icons to nominatim `osm_type` and `class` conversion
|
|
|
|
*/
|
|
|
|
searchIcons_nominatim_osm_type: {
|
|
|
|
way: 'mdiRoadVariant',
|
|
|
|
house: 'mdiHome',
|
|
|
|
node: 'mdiMapMarker',
|
|
|
|
relation: 'mdiCityVariant',
|
|
|
|
},
|
2023-02-20 07:05:21 +01:00
|
|
|
searchIcons_nominatim_class: ['place', 'amenity', 'shop', 'tourism', 'leisure', 'building'],
|
|
|
|
|
|
|
|
loadResultIcon (item) {
|
|
|
|
if (this.searchIcons_nominatim_class.includes(item.class)) {
|
|
|
|
return 'mdiHome'
|
|
|
|
}
|
|
|
|
return this.searchIcons_nominatim_osm_type[item.type]
|
2023-02-17 00:23:35 +01:00
|
|
|
},
|
|
|
|
|
2023-02-20 07:05:21 +01:00
|
|
|
/**
|
|
|
|
* Map results from provider
|
|
|
|
*/
|
2023-02-17 00:23:35 +01:00
|
|
|
filterNameFromAddress: ['place', 'amenity', 'shop', 'tourism', 'leisure', 'building'],
|
|
|
|
|
|
|
|
mapQueryResults (ret, addressList = []) {
|
|
|
|
if (ret && ret.length) {
|
|
|
|
addressList = ret.map(v => {
|
|
|
|
const name = get(v.namedetails, 'alt_name', get(v.namedetails, 'name'))
|
|
|
|
const address = this.filterNameFromAddress.includes(v.class) ? v.display_name.replace(name, '').replace(/^, ?/, '') : v.display_name.replace(/^, ?/, '')
|
|
|
|
return {
|
|
|
|
class: v.class,
|
|
|
|
type: v.osm_type,
|
|
|
|
lat: v.lat,
|
|
|
|
lon: v.lon,
|
|
|
|
name,
|
|
|
|
address
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return addressList
|
|
|
|
}
|
|
|
|
|
2023-01-21 01:18:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = nominatim
|