Search: OSM-ID search now also supports typing in a number

This commit is contained in:
Pieter Vander Vennet 2024-09-15 02:26:17 +02:00
parent b92b74df69
commit cbad65b569

View file

@ -2,14 +2,15 @@ import { Store, UIEventSource } from "../UIEventSource"
import GeocodingProvider, { GeocodingOptions, GeocodeResult } from "./GeocodingProvider" import GeocodingProvider, { GeocodingOptions, GeocodeResult } from "./GeocodingProvider"
import { OsmId } from "../../Models/OsmFeature" import { OsmId } from "../../Models/OsmFeature"
import { SpecialVisualizationState } from "../../UI/SpecialVisualization" import { SpecialVisualizationState } from "../../UI/SpecialVisualization"
import { Utils } from "../../Utils"
export default class OpenStreetMapIdSearch implements GeocodingProvider { 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 regex = /((https?:\/\/)?(www.)?(osm|openstreetmap).org\/)?(n|node|w|way|r|relation)[/ ]?([0-9]+)/
private static readonly types: Readonly<Record<string, "node" | "way" | "relation">> = { private static readonly types: Readonly<Record<string, "node" | "way" | "relation">> = {
"n":"node", "n": "node",
"w":"way", "w": "way",
"r":"relation", "r": "relation",
} }
private readonly _state: SpecialVisualizationState private readonly _state: SpecialVisualizationState
@ -37,41 +38,55 @@ export default class OpenStreetMapIdSearch implements GeocodingProvider {
if (match) { if (match) {
let type = match.at(-2) let type = match.at(-2)
const id = match.at(-1) const id = match.at(-1)
if(type.length === 1){ if (type.length === 1) {
type = OpenStreetMapIdSearch.types[type] type = OpenStreetMapIdSearch.types[type]
} }
return <OsmId>(type + "/" + id) return <OsmId>(type + "/" + id)
} }
return undefined return undefined
} }
async search(query: string, options?: GeocodingOptions): Promise<GeocodeResult[]> { private async getInfoAbout(id: OsmId): Promise<GeocodeResult> {
const id = OpenStreetMapIdSearch.extractId(query)
if (!id) {
return []
}
const [osm_type, osm_id] = id.split("/") const [osm_type, osm_id] = id.split("/")
const obj = await this._state.osmObjectDownloader.DownloadObjectAsync(id) const obj = await this._state.osmObjectDownloader.DownloadObjectAsync(id)
if (obj === "deleted") { if (obj === "deleted") {
return [{ return {
display_name: id + " was deleted", display_name: id + " was deleted",
category: "coordinate", category: "coordinate",
osm_type: <"node" | "way" | "relation">osm_type, osm_type: <"node" | "way" | "relation">osm_type,
osm_id, osm_id,
lat: 0, lon: 0, lat: 0, lon: 0,
source: "osmid" source: "osmid",
}] }
} }
const [lat, lon] = obj.centerpoint() const [lat, lon] = obj.centerpoint()
return [{ return {
lat, lon, lat, lon,
display_name: obj.tags.name ?? obj.tags.alt_name ?? obj.tags.local_name ?? obj.tags.ref ?? id, 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_type: <"node" | "way" | "relation">osm_type,
osm_id, osm_id,
source: "osmid" source: "osmid",
}
}
}] async search(query: string, options?: GeocodingOptions): Promise<GeocodeResult[]> {
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<GeocodeResult[]> { suggest?(query: string, options?: GeocodingOptions): Store<GeocodeResult[]> {