MapComplete/src/Logic/Search/NominatimGeocoding.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

41 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-08-15 01:51:33 +02:00
import { Utils } from "../../Utils"
import { BBox } from "../BBox"
import Constants from "../../Models/Constants"
import { FeatureCollection } from "geojson"
import Locale from "../../UI/i18n/Locale"
import GeocodingProvider, { GeocodingOptions, SearchResult } from "./GeocodingProvider"
2024-08-15 01:51:33 +02:00
2024-08-22 22:50:37 +02:00
export class NominatimGeocoding implements GeocodingProvider {
2024-10-19 14:44:55 +02:00
private readonly _host
private readonly limit: number
public readonly name = "Nominatim"
2024-08-15 01:51:33 +02:00
2024-10-19 14:44:55 +02:00
constructor(limit: number = 3, host: string = Constants.nominatimEndpoint) {
this.limit = limit
2024-08-15 01:51:33 +02:00
this._host = host
}
2024-10-19 14:44:55 +02:00
public search(query: string, options?: GeocodingOptions): Promise<SearchResult[]> {
2024-08-15 01:51:33 +02:00
const b = options?.bbox ?? BBox.global
2024-10-19 14:44:55 +02:00
const url = `${this._host}search?format=json&limit=${
this.limit
}&viewbox=${b.getEast()},${b.getNorth()},${b.getWest()},${b.getSouth()}&accept-language=${
2024-08-15 01:51:33 +02:00
Locale.language.data
}&q=${query}`
2024-08-22 22:50:37 +02:00
return Utils.downloadJson(url)
2024-08-15 01:51:33 +02:00
}
async reverseSearch(
coordinate: { lon: number; lat: number },
zoom: number = 17,
language?: string
): Promise<FeatureCollection> {
// https://nominatim.org/release-docs/develop/api/Reverse/
// IF the zoom is low, it'll only return a country instead of an address
const url = `${this._host}reverse?format=geojson&lat=${coordinate.lat}&lon=${
coordinate.lon
}&zoom=${Math.ceil(zoom) + 1}&accept-language=${language}`
return Utils.downloadJson(url)
}
}