forked from MapComplete/MapComplete
Search: add 'osmId' to coordinate search
This commit is contained in:
parent
8239820d04
commit
3e2027a366
1 changed files with 32 additions and 26 deletions
|
@ -12,7 +12,7 @@ export default class CoordinateSearch implements GeocodingProvider {
|
||||||
/lat[:=]? *['"]?(-?[0-9]+\.[0-9]+)['"]?[ ,;&]+lng[:=]? *['"]?(-?[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.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]+).*/
|
/https:\/\/www.google.com\/maps\/@(-?[0-9]+.[0-9]+),(-?[0-9]+.[0-9]+).*/,
|
||||||
]
|
]
|
||||||
|
|
||||||
private static readonly lonLatRegexes: ReadonlyArray<RegExp> = [
|
private static readonly lonLatRegexes: ReadonlyArray<RegExp> = [
|
||||||
|
@ -27,60 +27,66 @@ export default class CoordinateSearch implements GeocodingProvider {
|
||||||
* const ls = new CoordinateSearch()
|
* const ls = new CoordinateSearch()
|
||||||
* const results = ls.directSearch("https://www.openstreetmap.org/search?query=Brugge#map=11/51.2611/3.2217")
|
* const results = ls.directSearch("https://www.openstreetmap.org/search?query=Brugge#map=11/51.2611/3.2217")
|
||||||
* results.length // => 1
|
* results.length // => 1
|
||||||
* results[0] // => {lat: 51.2611, lon: 3.2217, display_name: "lon: 3.2217, lat: 51.2611", "category": "coordinate","source": "coordinate:latlon"}
|
* results[0] // => {lat: 51.2611, lon: 3.2217, display_name: "lon: 3.2217, lat: 51.2611", "category": "coordinate", "osm_id": "3.2217/51.2611","source": "coordinate:latlon"}
|
||||||
*
|
*
|
||||||
* const ls = new CoordinateSearch()
|
* const ls = new CoordinateSearch()
|
||||||
* const results = ls.directSearch("https://www.openstreetmap.org/#map=11/51.2611/3.2217")
|
* const results = ls.directSearch("https://www.openstreetmap.org/#map=11/51.2611/3.2217")
|
||||||
* results.length // => 1
|
* results.length // => 1
|
||||||
* results[0] // => {lat: 51.2611, lon: 3.2217, display_name: "lon: 3.2217, lat: 51.2611", "category": "coordinate","source": "coordinate:latlon"}
|
* results[0] // => {lat: 51.2611, lon: 3.2217, display_name: "lon: 3.2217, lat: 51.2611", "category": "coordinate","osm_id": "3.2217/51.2611","source": "coordinate:latlon"}
|
||||||
*
|
*
|
||||||
* const ls = new CoordinateSearch()
|
* const ls = new CoordinateSearch()
|
||||||
* const results = ls.directSearch("51.2611 3.2217")
|
* const results = ls.directSearch("51.2611 3.2217")
|
||||||
* results.length // => 2
|
* 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[0] // => {lat: 51.2611, lon: 3.2217, display_name: "lon: 3.2217, lat: 51.2611", "category": "coordinate", "osm_id": "3.2217/51.2611","source": "coordinate:latlon"}
|
||||||
* results[1] // => {lon: 51.2611, lat: 3.2217, display_name: "lon: 51.2611, lat: 3.2217", "category": "coordinate", "source": "coordinate:lonlat"}
|
* results[1] // => {lon: 51.2611, lat: 3.2217, display_name: "lon: 51.2611, lat: 3.2217", "category": "coordinate", "osm_id": "51.2611/3.2217","source": "coordinate:lonlat"}
|
||||||
*
|
*
|
||||||
* // Test format mentioned in 1599
|
* // Test format mentioned in 1599
|
||||||
* const ls = new CoordinateSearch()
|
* const ls = new CoordinateSearch()
|
||||||
* const results = ls.directSearch("51.2611/3.2217")
|
* const results = ls.directSearch("51.2611/3.2217")
|
||||||
* results.length // => 2
|
* 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[0] // => {lat: 51.2611, lon: 3.2217, display_name: "lon: 3.2217, lat: 51.2611", "category": "coordinate", "source": "coordinate:latlon", "osm_id": "3.2217/51.2611",}
|
||||||
* results[1] // => {lon: 51.2611, lat: 3.2217, display_name: "lon: 51.2611, lat: 3.2217", "category": "coordinate", "source": "coordinate:lonlat"}
|
* results[1] // => {lon: 51.2611, lat: 3.2217, display_name: "lon: 51.2611, lat: 3.2217", "category": "coordinate", "osm_id": "51.2611/3.2217","source": "coordinate:lonlat"}
|
||||||
*
|
*
|
||||||
* // test OSM-XML format
|
* // test OSM-XML format
|
||||||
* const ls = new CoordinateSearch()
|
* const ls = new CoordinateSearch()
|
||||||
* const results = ls.directSearch(' lat="57.5802905" lon="12.7202538"')
|
* const results = ls.directSearch(' lat="57.5802905" lon="12.7202538"')
|
||||||
* results.length // => 1
|
* results.length // => 1
|
||||||
* results[0] // => {lat: 57.5802905, lon: 12.7202538, display_name: "lon: 12.7202538, lat: 57.5802905", "category": "coordinate", "source": "coordinate:latlon"}
|
* results[0] // => {lat: 57.5802905, lon: 12.7202538, "display_name": "lon: 12.720254, lat: 57.580291", "category": "coordinate", "osm_id": "12.720254/57.580291","source": "coordinate:latlon"}
|
||||||
*
|
*
|
||||||
* // should work with negative coordinates
|
* // should work with negative coordinates
|
||||||
* const ls = new CoordinateSearch()
|
* const ls = new CoordinateSearch()
|
||||||
* const results = ls.directSearch(' lat="-57.5802905" lon="-12.7202538"')
|
* const results = ls.directSearch(' lat="-57.5802905" lon="-12.7202538"')
|
||||||
* results.length // => 1
|
* results.length // => 1
|
||||||
* results[0] // => {lat: -57.5802905, lon: -12.7202538, display_name: "lon: -12.7202538, lat: -57.5802905", "category": "coordinate", "source": "coordinate:latlon"}
|
* 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"}
|
||||||
*/
|
*/
|
||||||
private directSearch(query: string): GeocodeResult[] {
|
private directSearch(query: string): GeocodeResult[] {
|
||||||
|
const matches = Utils.NoNull(CoordinateSearch.latLonRegexes.map(r => query.match(r)))
|
||||||
const matches = Utils.NoNull(CoordinateSearch.latLonRegexes.map(r => query.match(r))).map(m => <GeocodeResult>{
|
.map(m => CoordinateSearch.asResult(m[2], m[1], "latlon") )
|
||||||
lat: Number(m[1]),
|
|
||||||
lon: Number(m[2]),
|
|
||||||
display_name: "lon: " + m[2] + ", lat: " + m[1],
|
|
||||||
source: "coordinate:latlon",
|
|
||||||
category: "coordinate"
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
const matchesLonLat = Utils.NoNull(CoordinateSearch.lonLatRegexes.map(r => query.match(r)))
|
const matchesLonLat = Utils.NoNull(CoordinateSearch.lonLatRegexes.map(r => query.match(r)))
|
||||||
.map(m => <GeocodeResult>{
|
.map(m => CoordinateSearch.asResult(m[1], m[2], "lonlat"))
|
||||||
lat: Number(m[2]),
|
|
||||||
lon: Number(m[1]),
|
|
||||||
display_name: "lon: " + m[1] + ", lat: " + m[2],
|
|
||||||
category: "coordinate",
|
|
||||||
source: "coordinate:lonlat"
|
|
||||||
})
|
|
||||||
return matches.concat(matchesLonLat)
|
return matches.concat(matchesLonLat)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static round6(n: number): string {
|
||||||
|
return "" + (Math.round(n * 1000000) / 1000000)
|
||||||
|
}
|
||||||
|
|
||||||
|
private static asResult(lonIn: string, latIn: string, source: string): GeocodeResult {
|
||||||
|
const lon = Number(lonIn)
|
||||||
|
const lat = Number(latIn)
|
||||||
|
const lonStr = CoordinateSearch.round6(lon)
|
||||||
|
const latStr = CoordinateSearch.round6(lat)
|
||||||
|
return {
|
||||||
|
lat,
|
||||||
|
lon,
|
||||||
|
display_name: "lon: " + lonStr + ", lat: " + latStr,
|
||||||
|
category: "coordinate",
|
||||||
|
source: "coordinate:"+source,
|
||||||
|
osm_id: lonStr + "/" + latStr,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
suggest(query: string): Store<GeocodeResult[]> {
|
suggest(query: string): Store<GeocodeResult[]> {
|
||||||
return new ImmutableStore(this.directSearch(query))
|
return new ImmutableStore(this.directSearch(query))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue