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 { 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<Record<string, "node" | "way" | "relation">> = {
"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 <OsmId>(type + "/" + id)
}
return undefined
}
async search(query: string, options?: GeocodingOptions): Promise<GeocodeResult[]> {
const id = OpenStreetMapIdSearch.extractId(query)
if (!id) {
return []
}
private async getInfoAbout(id: OsmId): Promise<GeocodeResult> {
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<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[]> {