MapComplete/src/Logic/Geocoding/CoordinateSearch.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

86 lines
3.9 KiB
TypeScript
Raw Normal View History

2024-08-26 13:09:46 +02:00
import GeocodingProvider, { SearchResult } from "./GeocodingProvider"
2024-08-15 01:51:33 +02:00
import { Utils } from "../../Utils"
2024-08-22 22:50:37 +02:00
import { ImmutableStore, Store } from "../UIEventSource"
2024-08-15 01:51:33 +02:00
/**
* A simple search-class which interprets possible locations
*/
export default class CoordinateSearch implements GeocodingProvider {
private static readonly latLonRegexes: ReadonlyArray<RegExp> = [
2024-08-24 01:53:06 +02:00
/(-?[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]+)['"]?/,
/https:\/\/www.openstreetmap.org\/.*#map=[0-9]+\/(-?[0-9]+\.[0-9]+)\/(-?[0-9]+\.[0-9]+)/,
/https:\/\/www.google.com\/maps\/@(-?[0-9]+.[0-9]+),(-?[0-9]+.[0-9]+).*/
2024-08-15 01:51:33 +02:00
]
private static readonly lonLatRegexes: ReadonlyArray<RegExp> = [
2024-08-24 01:53:06 +02:00
/(-?[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]+)['"]?/,
2024-08-15 01:51:33 +02:00
]
/**
*
* const ls = new CoordinateSearch()
2024-08-22 22:50:37 +02:00
* const results = ls.directSearch("https://www.openstreetmap.org/search?query=Brugge#map=11/51.2611/3.2217")
2024-08-15 01:51:33 +02:00
* results.length // => 1
* results[0] // => {lat: 51.2611, lon: 3.2217, display_name: "lon: 3.2217, lat: 51.2611", "category": "coordinate","source": "coordinate:latlon"}
2024-08-15 01:51:33 +02:00
*
* const ls = new CoordinateSearch()
2024-08-22 22:50:37 +02:00
* const results = ls.directSearch("https://www.openstreetmap.org/#map=11/51.2611/3.2217")
2024-08-15 01:51:33 +02:00
* results.length // => 1
* results[0] // => {lat: 51.2611, lon: 3.2217, display_name: "lon: 3.2217, lat: 51.2611", "category": "coordinate","source": "coordinate:latlon"}
2024-08-15 01:51:33 +02:00
*
* const ls = new CoordinateSearch()
2024-08-22 22:50:37 +02:00
* const results = ls.directSearch("51.2611 3.2217")
2024-08-15 01:51:33 +02:00
* 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"}
2024-08-15 01:51:33 +02:00
*
2024-08-24 01:53:06 +02:00
* // test OSM-XML format
* const ls = new CoordinateSearch()
* const results = ls.directSearch(' lat="57.5802905" lon="12.7202538"')
* results.length // => 1
* results[0] // => {lat: 57.5802905, lon: 12.7202538, display_name: "lon: 12.7202538, lat: 57.5802905", "category": "coordinate", "source": "coordinate:latlon"}
2024-08-24 01:53:06 +02:00
*
* // should work with negative coordinates
* const ls = new CoordinateSearch()
* const results = ls.directSearch(' lat="-57.5802905" lon="-12.7202538"')
* results.length // => 1
* results[0] // => {lat: -57.5802905, lon: -12.7202538, display_name: "lon: -12.7202538, lat: -57.5802905", "category": "coordinate", "source": "coordinate:latlon"}
2024-08-15 01:51:33 +02:00
*/
2024-08-26 13:09:46 +02:00
private directSearch(query: string): SearchResult[] {
2024-08-15 01:51:33 +02:00
2024-08-26 13:09:46 +02:00
const matches = Utils.NoNull(CoordinateSearch.latLonRegexes.map(r => query.match(r))).map(m => <SearchResult>{
2024-08-15 01:51:33 +02:00
lat: Number(m[1]),
lon: Number(m[2]),
display_name: "lon: " + m[2] + ", lat: " + m[1],
source: "coordinate:latlon",
2024-08-21 14:06:42 +02:00
category: "coordinate"
2024-08-15 01:51:33 +02:00
})
2024-08-22 22:50:37 +02:00
const matchesLonLat = Utils.NoNull(CoordinateSearch.lonLatRegexes.map(r => query.match(r)))
2024-08-26 13:09:46 +02:00
.map(m => <SearchResult>{
2024-08-15 01:51:33 +02:00
lat: Number(m[2]),
lon: Number(m[1]),
display_name: "lon: " + m[1] + ", lat: " + m[2],
category: "coordinate",
source: "coordinate:lonlat"
2024-08-15 01:51:33 +02:00
})
return matches.concat(matchesLonLat)
}
2024-08-26 13:09:46 +02:00
suggest(query: string): Store<SearchResult[]> {
2024-08-22 22:50:37 +02:00
return new ImmutableStore(this.directSearch(query))
}
2024-08-26 13:09:46 +02:00
async search (query: string): Promise<SearchResult[]> {
2024-08-22 22:50:37 +02:00
return this.directSearch(query)
2024-08-15 01:51:33 +02:00
}
}