forked from MapComplete/MapComplete
Search: add support for OpenLocationCodes and some other coordinate formats, see #2157
This commit is contained in:
parent
181219928c
commit
4d2b3c9cf7
5 changed files with 106 additions and 5 deletions
|
|
@ -1,13 +1,15 @@
|
|||
import GeocodingProvider, { GeocodeResult } from "./GeocodingProvider"
|
||||
import { Utils } from "../../Utils"
|
||||
import { ImmutableStore, Store } from "../UIEventSource"
|
||||
|
||||
import CoordinateParser from "coordinate-parser"
|
||||
/**
|
||||
* A simple search-class which interprets possible locations
|
||||
*/
|
||||
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]+)/,
|
||||
/^ *(-?[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]+)['"]?/,
|
||||
|
||||
|
|
@ -17,6 +19,8 @@ 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]+)['"]?/,
|
||||
|
||||
|
|
@ -58,14 +62,31 @@ export default class CoordinateSearch implements GeocodingProvider {
|
|||
* 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.720254, lat: -57.58029", "category": "coordinate","osm_id": "-12.720254/-57.58029", "source": "coordinate:latlon"}
|
||||
*
|
||||
* // Should work with commas
|
||||
* const ls = new CoordinateSearch()
|
||||
* const results = ls.directSearch('51,047977 3,51184')
|
||||
* results.length // => 2
|
||||
* results[0] // => {lat: 51.047977, lon: 3.51184, "display_name": "lon: 3.51184, lat: 51.047977", "category": "coordinate","osm_id": "3.51184/51.047977", "source": "coordinate:latlon"}
|
||||
*/
|
||||
private directSearch(query: string): GeocodeResult[] {
|
||||
const matches = Utils.NoNull(CoordinateSearch.latLonRegexes.map(r => query.match(r)))
|
||||
.map(m => CoordinateSearch.asResult(m[2], m[1], "latlon") )
|
||||
.map(m => CoordinateSearch.asResult(m[2], m[1], "latlon"))
|
||||
|
||||
const matchesLonLat = Utils.NoNull(CoordinateSearch.lonLatRegexes.map(r => query.match(r)))
|
||||
.map(m => CoordinateSearch.asResult(m[1], m[2], "lonlat"))
|
||||
return matches.concat(matchesLonLat)
|
||||
const init = matches.concat(matchesLonLat)
|
||||
if (init.length > 0) {
|
||||
return init
|
||||
}
|
||||
|
||||
try {
|
||||
const c = new CoordinateParser(query);
|
||||
return [CoordinateSearch.asResult(""+c.getLongitude(), ""+c.getLatitude(), "coordinateParser")]
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static round6(n: number): string {
|
||||
|
|
@ -73,6 +94,9 @@ export default class CoordinateSearch implements GeocodingProvider {
|
|||
}
|
||||
|
||||
private static asResult(lonIn: string, latIn: string, source: string): GeocodeResult {
|
||||
lonIn = lonIn.replaceAll(",", ".")
|
||||
latIn = latIn.replaceAll(",", ".")
|
||||
|
||||
const lon = Number(lonIn)
|
||||
const lat = Number(latIn)
|
||||
const lonStr = CoordinateSearch.round6(lon)
|
||||
|
|
@ -82,7 +106,7 @@ export default class CoordinateSearch implements GeocodingProvider {
|
|||
lon,
|
||||
display_name: "lon: " + lonStr + ", lat: " + latStr,
|
||||
category: "coordinate",
|
||||
source: "coordinate:"+source,
|
||||
source: "coordinate:" + source,
|
||||
osm_id: lonStr + "/" + latStr,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue