diff --git a/src/Logic/Search/OpenStreetMapIdSearch.ts b/src/Logic/Search/OpenStreetMapIdSearch.ts index cc026ae28..48e9d9b43 100644 --- a/src/Logic/Search/OpenStreetMapIdSearch.ts +++ b/src/Logic/Search/OpenStreetMapIdSearch.ts @@ -2,14 +2,15 @@ import { Store, UIEventSource } from "../UIEventSource" import GeocodingProvider, { GeocodingOptions, GeocodeResult } from "./GeocodingProvider" import { OsmId } from "../../Models/OsmFeature" import { SpecialVisualizationState } from "../../UI/SpecialVisualization" +import { Utils } from "../../Utils" export default class OpenStreetMapIdSearch implements GeocodingProvider { private static readonly regex = /((https?:\/\/)?(www.)?(osm|openstreetmap).org\/)?(n|node|w|way|r|relation)[/ ]?([0-9]+)/ private static readonly types: Readonly> = { - "n":"node", - "w":"way", - "r":"relation", + "n": "node", + "w": "way", + "r": "relation", } private readonly _state: SpecialVisualizationState @@ -37,41 +38,55 @@ export default class OpenStreetMapIdSearch implements GeocodingProvider { if (match) { let type = match.at(-2) const id = match.at(-1) - if(type.length === 1){ - type = OpenStreetMapIdSearch.types[type] + if (type.length === 1) { + type = OpenStreetMapIdSearch.types[type] } return (type + "/" + id) } return undefined } - async search(query: string, options?: GeocodingOptions): Promise { - const id = OpenStreetMapIdSearch.extractId(query) - if (!id) { - return [] - } + private async getInfoAbout(id: OsmId): Promise { const [osm_type, osm_id] = id.split("/") const obj = await this._state.osmObjectDownloader.DownloadObjectAsync(id) if (obj === "deleted") { - return [{ + return { display_name: id + " was deleted", category: "coordinate", osm_type: <"node" | "way" | "relation">osm_type, osm_id, lat: 0, lon: 0, - source: "osmid" + source: "osmid", - }] + } } const [lat, lon] = obj.centerpoint() - return [{ + return { lat, lon, display_name: obj.tags.name ?? obj.tags.alt_name ?? obj.tags.local_name ?? obj.tags.ref ?? id, + description: osm_type, osm_type: <"node" | "way" | "relation">osm_type, osm_id, - source: "osmid" + source: "osmid", + } + } - }] + async search(query: string, options?: GeocodingOptions): Promise { + + if (!isNaN(Number(query))) { + const n = Number(query) + return Utils.NoNullInplace(await Promise.all([ + this.getInfoAbout(`node/${n}`).catch(x => undefined), + this.getInfoAbout(`way/${n}`).catch(x => undefined), + this.getInfoAbout(`relation/${n}`).catch(() => undefined), + ])) + } + + const id = OpenStreetMapIdSearch.extractId(query) + if (!id) { + return [] + } + return [await this.getInfoAbout(id)] } suggest?(query: string, options?: GeocodingOptions): Store {