Skip to content

Commit

Permalink
Search: OSM-ID search now also supports typing in a number
Browse files Browse the repository at this point in the history
  • Loading branch information
pietervdvn committed Sep 15, 2024
1 parent b92b74d commit cbad65b
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions src/Logic/Search/OpenStreetMapIdSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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[]> {
Expand Down

0 comments on commit cbad65b

Please sign in to comment.