forked from MapComplete/MapComplete
		
	Search: OSM-ID search now also supports typing in a number
This commit is contained in:
		
							parent
							
								
									b92b74df69
								
							
						
					
					
						commit
						cbad65b569
					
				
					 1 changed files with 31 additions and 16 deletions
				
			
		|  | @ -2,6 +2,7 @@ import { Store, UIEventSource } from "../UIEventSource" | ||||||
| import GeocodingProvider, { GeocodingOptions, GeocodeResult } from "./GeocodingProvider" | import GeocodingProvider, { GeocodingOptions, GeocodeResult } from "./GeocodingProvider" | ||||||
| import { OsmId } from "../../Models/OsmFeature" | import { OsmId } from "../../Models/OsmFeature" | ||||||
| import { SpecialVisualizationState } from "../../UI/SpecialVisualization" | import { SpecialVisualizationState } from "../../UI/SpecialVisualization" | ||||||
|  | import { Utils } from "../../Utils" | ||||||
| 
 | 
 | ||||||
| export default class OpenStreetMapIdSearch implements GeocodingProvider { | 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 regex = /((https?:\/\/)?(www.)?(osm|openstreetmap).org\/)?(n|node|w|way|r|relation)[/ ]?([0-9]+)/ | ||||||
|  | @ -45,33 +46,47 @@ export default class OpenStreetMapIdSearch implements GeocodingProvider { | ||||||
|         return undefined |         return undefined | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     async search(query: string, options?: GeocodingOptions): Promise<GeocodeResult[]> { |     private async getInfoAbout(id: OsmId): Promise<GeocodeResult> { | ||||||
|         const id = OpenStreetMapIdSearch.extractId(query) |  | ||||||
|         if (!id) { |  | ||||||
|             return [] |  | ||||||
|         } |  | ||||||
|         const [osm_type, osm_id] = id.split("/") |         const [osm_type, osm_id] = id.split("/") | ||||||
|         const obj = await this._state.osmObjectDownloader.DownloadObjectAsync(id) |         const obj = await this._state.osmObjectDownloader.DownloadObjectAsync(id) | ||||||
|         if (obj === "deleted") { |         if (obj === "deleted") { | ||||||
|             return [{ |             return { | ||||||
|                 display_name: id + " was deleted", |                 display_name: id + " was deleted", | ||||||
|                 category: "coordinate", |                 category: "coordinate", | ||||||
|                 osm_type: <"node" | "way" | "relation">osm_type, |                 osm_type: <"node" | "way" | "relation">osm_type, | ||||||
|                 osm_id, |                 osm_id, | ||||||
|                 lat: 0, lon: 0, |                 lat: 0, lon: 0, | ||||||
|                 source: "osmid" |                 source: "osmid", | ||||||
| 
 | 
 | ||||||
|             }] |             } | ||||||
|         } |         } | ||||||
|         const [lat, lon] = obj.centerpoint() |         const [lat, lon] = obj.centerpoint() | ||||||
|         return [{ |         return { | ||||||
|             lat, lon, |             lat, lon, | ||||||
|             display_name: obj.tags.name ?? obj.tags.alt_name ?? obj.tags.local_name ?? obj.tags.ref ?? id, |             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_type: <"node" | "way" | "relation">osm_type, | ||||||
|             osm_id, |             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[]> { |     suggest?(query: string, options?: GeocodingOptions): Store<GeocodeResult[]> { | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue