|
| 1 | +// See https://nominatim.org/release-docs/develop/api/Output/ for some more information |
| 2 | +import { UrlUtil } from '@terrestris/base-util'; |
| 3 | +import { Feature, Geometry } from 'geojson'; |
| 4 | +import { Extent } from 'ol/extent'; |
| 5 | + |
| 6 | +import { SearchFunction } from './useSearch/useSearch'; |
| 7 | + |
| 8 | +export type NominatimPlace = { |
| 9 | + // eslint-disable-next-line camelcase |
| 10 | + place_id: number; |
| 11 | + // eslint-disable-next-line camelcase |
| 12 | + osm_type: string; |
| 13 | + // eslint-disable-next-line camelcase |
| 14 | + osm_id: number; |
| 15 | + boundingbox: string[]; |
| 16 | + // eslint-disable-next-line camelcase |
| 17 | + display_name: string; |
| 18 | + category: string; |
| 19 | + type: string; |
| 20 | + importance: number; |
| 21 | + icon?: string; |
| 22 | + address?: any; |
| 23 | + extratags?: any; |
| 24 | + namedetails?: any; |
| 25 | + geojson: Geometry; |
| 26 | + licence: string; |
| 27 | +}; |
| 28 | + |
| 29 | +export type NominatimArgs = { |
| 30 | + /** |
| 31 | + * The Nominatim Base URL. See https://wiki.openstreetmap.org/wiki/Nominatim |
| 32 | + */ |
| 33 | + nominatimBaseUrl?: string; |
| 34 | + /** |
| 35 | + * The preferred area to find search results in [left],[top],[right],[bottom]. |
| 36 | + */ |
| 37 | + viewBox?: string; |
| 38 | + /** |
| 39 | + * Restrict the results to only items contained with the bounding box. |
| 40 | + * Restricting the results to the bounding box also enables searching by |
| 41 | + * amenity only. For example a search query of just "[pub]" would normally be |
| 42 | + * rejected but with bounded=1 will result in a list of items matching within |
| 43 | + * the bounding box. |
| 44 | + */ |
| 45 | + bounded?: number; |
| 46 | + /** |
| 47 | + * Include a breakdown of the address into elements. |
| 48 | + */ |
| 49 | + addressDetails?: number; |
| 50 | + /** |
| 51 | + * Limit the number of returned results. |
| 52 | + */ |
| 53 | + limit?: number; |
| 54 | + /** |
| 55 | + * Limit search results to a specific country (or a list of countries). |
| 56 | + * [countrycode] should be the ISO 3166-1alpha2 code, e.g. gb for the United |
| 57 | + * Kingdom, de for Germany, etc. |
| 58 | + */ |
| 59 | + countryCodes?: string; |
| 60 | + /** |
| 61 | + * Preferred language order for showing search results, overrides the value |
| 62 | + * specified in the "Accept-Language" HTTP header. Either use a standard RFC2616 |
| 63 | + * accept-language string or a simple comma-separated list of language codes. |
| 64 | + */ |
| 65 | + searchResultLanguage?: string; |
| 66 | +}; |
| 67 | + |
| 68 | +export const createNominatimSearchFunction = ({ |
| 69 | + addressDetails = 1, |
| 70 | + bounded = 1, |
| 71 | + countryCodes = 'de', |
| 72 | + limit = 10, |
| 73 | + nominatimBaseUrl = 'https://nominatim.openstreetmap.org/search?', |
| 74 | + searchResultLanguage, |
| 75 | + viewBox = '-180,90,180,-90' |
| 76 | +}: NominatimArgs = {}): SearchFunction<Geometry, NominatimPlace> => { |
| 77 | + return async (searchTerm) => { |
| 78 | + const baseParams = { |
| 79 | + format: 'json', |
| 80 | + viewbox: viewBox, |
| 81 | + bounded: bounded, |
| 82 | + // eslint-disable-next-line camelcase |
| 83 | + polygon_geojson: '1', |
| 84 | + addressdetails: addressDetails, |
| 85 | + limit: limit, |
| 86 | + countrycodes: countryCodes, |
| 87 | + q: searchTerm |
| 88 | + }; |
| 89 | + |
| 90 | + const getRequestParams = UrlUtil.objectToRequestString(baseParams); |
| 91 | + |
| 92 | + let fetchOpts: RequestInit = {}; |
| 93 | + if (searchResultLanguage) { |
| 94 | + fetchOpts = { |
| 95 | + headers: { |
| 96 | + 'accept-language': searchResultLanguage |
| 97 | + } |
| 98 | + }; |
| 99 | + } |
| 100 | + const response = await fetch(`${nominatimBaseUrl}${getRequestParams}`, fetchOpts); |
| 101 | + if (!response.ok) { |
| 102 | + throw new Error(`Return code: ${response.status}`); |
| 103 | + } |
| 104 | + const places = await response.json() as NominatimPlace[]; |
| 105 | + return { |
| 106 | + type: 'FeatureCollection', |
| 107 | + features: places.map(p => ({ |
| 108 | + type: 'Feature', |
| 109 | + geometry: p.geojson, |
| 110 | + properties: p |
| 111 | + })) |
| 112 | + }; |
| 113 | + }; |
| 114 | +}; |
| 115 | + |
| 116 | +export const createNominatimGetValueFunction = () => |
| 117 | + (feature: Feature<Geometry, NominatimPlace>) => feature.properties.display_name; |
| 118 | + |
| 119 | +export const createNominatimGetExtentFunction = () => |
| 120 | + (feature: Feature<Geometry, NominatimPlace>) => { |
| 121 | + const bbox: number[] = feature.properties.boundingbox.map(parseFloat); |
| 122 | + return [ |
| 123 | + bbox[2], |
| 124 | + bbox[0], |
| 125 | + bbox[3], |
| 126 | + bbox[1] |
| 127 | + ] as Extent; |
| 128 | + }; |
0 commit comments