Search: add formats as requested in #1671, #1599

This commit is contained in:
Pieter Vander Vennet 2024-08-29 16:14:12 +02:00
parent d313153f4c
commit c0d1cabac0
2 changed files with 26 additions and 4 deletions

View file

@ -7,7 +7,7 @@ import { ImmutableStore, Store } from "../UIEventSource"
*/
export default class CoordinateSearch implements GeocodingProvider {
private static readonly latLonRegexes: ReadonlyArray<RegExp> = [
/(-?[0-9]+\.[0-9]+)[ ,;]+(-?[0-9]+\.[0-9]+)/,
/^(-?[0-9]+\.[0-9]+)[ ,;/\\]+(-?[0-9]+\.[0-9]+)/,
/lat[:=]? *['"]?(-?[0-9]+\.[0-9]+)['"]?[ ,;&]+lon[:=]? *['"]?(-?[0-9]+\.[0-9]+)['"]?/,
/lat[:=]? *['"]?(-?[0-9]+\.[0-9]+)['"]?[ ,;&]+lng[:=]? *['"]?(-?[0-9]+\.[0-9]+)['"]?/,
@ -16,7 +16,7 @@ export default class CoordinateSearch implements GeocodingProvider {
]
private static readonly lonLatRegexes: ReadonlyArray<RegExp> = [
/(-?[0-9]+\.[0-9]+)[ ,;]+(-?[0-9]+\.[0-9]+)/,
/^(-?[0-9]+\.[0-9]+)[ ,;/\\]+(-?[0-9]+\.[0-9]+)/,
/lon[:=]? *['"]?(-?[0-9]+\.[0-9]+)['"]?[ ,;&]+lat[:=]? *['"]?(-?[0-9]+\.[0-9]+)['"]?/,
/lng[:=]? *['"]?(-?[0-9]+\.[0-9]+)['"]?[ ,;&]+lat[:=]? *['"]?(-?[0-9]+\.[0-9]+)['"]?/,
@ -40,6 +40,13 @@ export default class CoordinateSearch implements GeocodingProvider {
* results[0] // => {lat: 51.2611, lon: 3.2217, display_name: "lon: 3.2217, lat: 51.2611", "category": "coordinate", "source": "coordinate:latlon"}
* results[1] // => {lon: 51.2611, lat: 3.2217, display_name: "lon: 51.2611, lat: 3.2217", "category": "coordinate", "source": "coordinate:lonlat"}
*
* // Test format mentioned in 1599
* const ls = new CoordinateSearch()
* const results = ls.directSearch("51.2611/3.2217")
* results.length // => 2
* results[0] // => {lat: 51.2611, lon: 3.2217, display_name: "lon: 3.2217, lat: 51.2611", "category": "coordinate", "source": "coordinate:latlon"}
* results[1] // => {lon: 51.2611, lat: 3.2217, display_name: "lon: 51.2611, lat: 3.2217", "category": "coordinate", "source": "coordinate:lonlat"}
*
* // test OSM-XML format
* const ls = new CoordinateSearch()
* const results = ls.directSearch(' lat="57.5802905" lon="12.7202538"')

View file

@ -4,7 +4,13 @@ import { OsmId } from "../../Models/OsmFeature"
import { SpecialVisualizationState } from "../../UI/SpecialVisualization"
export default class OpenStreetMapIdSearch implements GeocodingProvider {
private static regex = /((https?:\/\/)?(www.)?(osm|openstreetmap).org\/)?(node|way|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">> = {
"n":"node",
"w":"way",
"r":"relation",
}
private readonly _state: SpecialVisualizationState
@ -18,13 +24,22 @@ export default class OpenStreetMapIdSearch implements GeocodingProvider {
* OpenStreetMapIdSearch.extractId("https://openstreetmap.org/node/42#map=19/51.204245/3.212731") // => "node/42"
* OpenStreetMapIdSearch.extractId("node/42") // => "node/42"
* OpenStreetMapIdSearch.extractId("way/42") // => "way/42"
* OpenStreetMapIdSearch.extractId("n123456789") // => "node/123456789"
* OpenStreetMapIdSearch.extractId("node123456789") // => "node/123456789"
* OpenStreetMapIdSearch.extractId("node 123456789") // => "node/123456789"
* OpenStreetMapIdSearch.extractId("w123456789") // => "way/123456789"
* OpenStreetMapIdSearch.extractId("way123456789") // => "way/123456789"
* OpenStreetMapIdSearch.extractId("way 123456789") // => "way/123456789"
* OpenStreetMapIdSearch.extractId("https://www.openstreetmap.org/node/5212733638") // => "node/5212733638"
*/
public static extractId(query: string): OsmId | undefined {
const match = query.match(OpenStreetMapIdSearch.regex)
if (match) {
const type = match.at(-2)
let type = match.at(-2)
const id = match.at(-1)
if(type.length === 1){
type = OpenStreetMapIdSearch.types[type]
}
return <OsmId>(type + "/" + id)
}
return undefined