MapComplete/src/Logic/Osm/Geocoding.ts

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

43 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-07-03 22:24:12 +02:00
import { Utils } from "../../Utils"
2022-04-28 00:28:04 +02:00
import { BBox } from "../BBox"
import Constants from "../../Models/Constants"
2023-12-19 22:21:34 +01:00
import { FeatureCollection } from "geojson"
2022-04-28 00:28:04 +02:00
export interface GeoCodeResult {
display_name: string
lat: number
lon: number
2023-03-28 05:13:48 +02:00
/**
* Format:
* [lat, lat, lon, lon]
*/
2022-04-28 00:28:04 +02:00
boundingbox: number[]
2022-05-21 01:02:03 +02:00
osm_type: "node" | "way" | "relation"
osm_id: string
2022-04-28 00:28:04 +02:00
}
2021-07-03 22:24:12 +02:00
2020-07-01 02:12:33 +02:00
export class Geocoding {
public static readonly host = Constants.nominatimEndpoint
2020-07-01 02:12:33 +02:00
2023-03-24 19:21:15 +01:00
static async Search(query: string, bbox: BBox): Promise<GeoCodeResult[]> {
const b = bbox ?? BBox.global
2023-12-19 22:21:34 +01:00
const url = `${
Geocoding.host
}search?format=json&limit=1&viewbox=${b.getEast()},${b.getNorth()},${b.getWest()},${b.getSouth()}&accept-language=nl&q=${query}`
return Utils.downloadJson(url)
}
static async reverse(
coordinate: { lon: number; lat: number },
zoom: number = 17,
language?: string
2023-12-19 22:21:34 +01:00
): 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 = `${Geocoding.host}reverse?format=geojson&lat=${coordinate.lat}&lon=${
coordinate.lon
}&zoom=${Math.ceil(zoom) + 1}&accept-language=${language}`
2022-04-28 00:28:04 +02:00
return Utils.downloadJson(url)
2020-07-01 02:12:33 +02:00
}
}