| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import { TagsFilter } from "../Tags/TagsFilter" | 
					
						
							|  |  |  | import RelationsTracker from "./RelationsTracker" | 
					
						
							|  |  |  | import { Utils } from "../../Utils" | 
					
						
							|  |  |  | import { ImmutableStore, Store } from "../UIEventSource" | 
					
						
							|  |  |  | import { BBox } from "../BBox" | 
					
						
							| 
									
										
										
										
											2023-01-17 02:56:34 +01:00
										 |  |  | import osmtogeojson from "osmtogeojson" | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import { FeatureCollection } from "@turf/turf" | 
					
						
							| 
									
										
										
										
											2022-04-28 00:37:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-30 01:13:18 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Interfaces overpass to get all the latest data | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | export class Overpass { | 
					
						
							| 
									
										
										
										
											2021-03-25 15:19:44 +01:00
										 |  |  |     private _filter: TagsFilter | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     private readonly _interpreterUrl: string | 
					
						
							|  |  |  |     private readonly _timeout: Store<number> | 
					
						
							|  |  |  |     private readonly _extraScripts: string[] | 
					
						
							| 
									
										
										
										
											2022-09-18 12:45:02 +02:00
										 |  |  |     private readonly _includeMeta: boolean | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     private _relationTracker: RelationsTracker | 
					
						
							| 
									
										
										
										
											2021-09-28 17:30:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     constructor( | 
					
						
							|  |  |  |         filter: TagsFilter, | 
					
						
							|  |  |  |         extraScripts: string[], | 
					
						
							|  |  |  |         interpreterUrl: string, | 
					
						
							|  |  |  |         timeout?: Store<number>, | 
					
						
							|  |  |  |         relationTracker?: RelationsTracker, | 
					
						
							|  |  |  |         includeMeta = true | 
					
						
							|  |  |  |     ) { | 
					
						
							|  |  |  |         this._timeout = timeout ?? new ImmutableStore<number>(90) | 
					
						
							|  |  |  |         this._interpreterUrl = interpreterUrl | 
					
						
							| 
									
										
										
										
											2022-03-13 01:27:19 +01:00
										 |  |  |         const optimized = filter.optimize() | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         if (optimized === true || optimized === false) { | 
					
						
							| 
									
										
										
										
											2022-03-13 01:27:19 +01:00
										 |  |  |             throw "Invalid filter: optimizes to true of false" | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         this._filter = optimized | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         this._extraScripts = extraScripts | 
					
						
							|  |  |  |         this._includeMeta = includeMeta | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |         this._relationTracker = relationTracker | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-08 03:14:55 +02:00
										 |  |  |     public async queryGeoJson(bounds: BBox): Promise<[FeatureCollection, Date]> { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const bbox = | 
					
						
							|  |  |  |             "[bbox:" + | 
					
						
							|  |  |  |             bounds.getSouth() + | 
					
						
							|  |  |  |             "," + | 
					
						
							|  |  |  |             bounds.getWest() + | 
					
						
							|  |  |  |             "," + | 
					
						
							|  |  |  |             bounds.getNorth() + | 
					
						
							|  |  |  |             "," + | 
					
						
							|  |  |  |             bounds.getEast() + | 
					
						
							|  |  |  |             "]" | 
					
						
							| 
									
										
										
										
											2022-05-21 01:02:03 +02:00
										 |  |  |         const query = this.buildScript(bbox) | 
					
						
							| 
									
										
										
										
											2022-12-16 01:02:46 +01:00
										 |  |  |         return await this.ExecuteQuery(query) | 
					
						
							| 
									
										
										
										
											2022-05-21 01:02:03 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public buildUrl(query: string) { | 
					
						
							| 
									
										
										
										
											2022-05-21 01:02:03 +02:00
										 |  |  |         return `${this._interpreterUrl}?data=${encodeURIComponent(query)}` | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public async ExecuteQuery(query: string): Promise<[FeatureCollection, Date]> { | 
					
						
							|  |  |  |         const self = this | 
					
						
							| 
									
										
										
										
											2022-05-21 01:02:03 +02:00
										 |  |  |         const json = await Utils.downloadJson(this.buildUrl(query)) | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-28 17:30:48 +02:00
										 |  |  |         if (json.elements.length === 0 && json.remark !== undefined) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             console.warn("Timeout or other runtime error while querying overpass", json.remark) | 
					
						
							| 
									
										
										
										
											2021-09-28 17:30:48 +02:00
										 |  |  |             throw `Runtime error (timeout or similar)${json.remark}` | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |         if (json.elements.length === 0) { | 
					
						
							|  |  |  |             console.warn("No features for", json) | 
					
						
							| 
									
										
										
										
											2021-09-22 16:07:56 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-05-10 23:51:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-24 19:59:46 +01:00
										 |  |  |         self._relationTracker?.RegisterRelations(json) | 
					
						
							| 
									
										
										
										
											2023-01-17 02:56:34 +01:00
										 |  |  |         const geojson = osmtogeojson(json) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const osmTime = new Date(json.osm3s.timestamp_osm_base) | 
					
						
							|  |  |  |         return [<any>geojson, osmTime] | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-03-25 15:19:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-24 19:59:46 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-05-21 01:02:03 +02:00
										 |  |  |      * Constructs the actual script to execute on Overpass | 
					
						
							|  |  |  |      * 'PostCall' can be used to set an extra range, see 'AsOverpassTurboLink' | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-04-28 00:37:30 +02:00
										 |  |  |      * import {Tag} from "../Tags/Tag"; | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-03-24 19:59:46 +01:00
										 |  |  |      * new Overpass(new Tag("key","value"), [], "").buildScript("{{bbox}}") // => `[out:json][timeout:90]{{bbox}};(nwr["key"="value"];);out body;out meta;>;out skel qt;`
 | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public buildScript(bbox: string, postCall: string = "", pretty = false): string { | 
					
						
							| 
									
										
										
										
											2021-03-25 15:19:44 +01:00
										 |  |  |         const filters = this._filter.asOverpass() | 
					
						
							|  |  |  |         let filter = "" | 
					
						
							|  |  |  |         for (const filterOr of filters) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             if (pretty) { | 
					
						
							| 
									
										
										
										
											2022-03-24 19:59:46 +01:00
										 |  |  |                 filter += "    " | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             filter += "nwr" + filterOr + postCall + ";" | 
					
						
							|  |  |  |             if (pretty) { | 
					
						
							|  |  |  |                 filter += "\n" | 
					
						
							| 
									
										
										
										
											2022-03-24 19:59:46 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-03-25 15:19:44 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         for (const extraScript of this._extraScripts) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             filter += "(" + extraScript + ");" | 
					
						
							| 
									
										
										
										
											2021-03-25 15:19:44 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return `[out:json][timeout:${this._timeout.data}]${bbox};(${filter});out body;${ | 
					
						
							|  |  |  |             this._includeMeta ? "out meta;" : "" | 
					
						
							|  |  |  |         }>;out skel qt;`
 | 
					
						
							| 
									
										
										
										
											2022-03-24 19:59:46 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-05-21 01:02:03 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Constructs the actual script to execute on Overpass with geocoding | 
					
						
							|  |  |  |      * 'PostCall' can be used to set an extra range, see 'AsOverpassTurboLink' | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public buildScriptInArea( | 
					
						
							|  |  |  |         area: { osm_type: "way" | "relation"; osm_id: number }, | 
					
						
							|  |  |  |         pretty = false | 
					
						
							|  |  |  |     ): string { | 
					
						
							| 
									
										
										
										
											2022-05-21 01:02:03 +02:00
										 |  |  |         const filters = this._filter.asOverpass() | 
					
						
							|  |  |  |         let filter = "" | 
					
						
							|  |  |  |         for (const filterOr of filters) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             if (pretty) { | 
					
						
							| 
									
										
										
										
											2022-05-21 01:02:03 +02:00
										 |  |  |                 filter += "    " | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             filter += "nwr" + filterOr + "(area.searchArea);" | 
					
						
							|  |  |  |             if (pretty) { | 
					
						
							|  |  |  |                 filter += "\n" | 
					
						
							| 
									
										
										
										
											2022-05-21 01:02:03 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         for (const extraScript of this._extraScripts) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             filter += "(" + extraScript + ");" | 
					
						
							| 
									
										
										
										
											2022-05-21 01:02:03 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         let id = area.osm_id | 
					
						
							|  |  |  |         if (area.osm_type === "relation") { | 
					
						
							| 
									
										
										
										
											2022-05-21 01:02:03 +02:00
										 |  |  |             id += 3600000000 | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return `[out:json][timeout:${this._timeout.data}];
 | 
					
						
							| 
									
										
										
										
											2022-05-21 01:02:03 +02:00
										 |  |  |         area(id:${id})->.searchArea; | 
					
						
							|  |  |  |         (${filter}); | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         out body;${this._includeMeta ? "out meta;" : ""}>;out skel qt;`
 | 
					
						
							| 
									
										
										
										
											2022-05-21 01:02:03 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-21 01:02:03 +02:00
										 |  |  |     public buildQuery(bbox: string) { | 
					
						
							|  |  |  |         return this.buildUrl(this.buildScript(bbox)) | 
					
						
							| 
									
										
										
										
											2021-03-25 15:19:44 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-24 19:59:46 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Little helper method to quickly open overpass-turbo in the browser | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public static AsOverpassTurboLink(tags: TagsFilter) { | 
					
						
							| 
									
										
										
										
											2022-03-24 19:59:46 +01:00
										 |  |  |         const overpass = new Overpass(tags, [], "", undefined, undefined, false) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const script = overpass.buildScript("", "({{bbox}})", true) | 
					
						
							| 
									
										
										
										
											2022-03-24 19:59:46 +01:00
										 |  |  |         const url = "http://overpass-turbo.eu/?Q=" | 
					
						
							|  |  |  |         return url + encodeURIComponent(script) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-29 13:16:21 +02:00
										 |  |  | } |