| 
									
										
										
										
											2023-03-23 01:42:47 +01:00
										 |  |  | import { Feature } from "geojson" | 
					
						
							| 
									
										
										
										
											2023-04-06 01:33:08 +02:00
										 |  |  | import { FeatureSource } from "../FeatureSource" | 
					
						
							| 
									
										
										
										
											2023-03-28 05:13:48 +02:00
										 |  |  | import { ImmutableStore, Store, UIEventSource } from "../../UIEventSource" | 
					
						
							|  |  |  | import LayerConfig from "../../../Models/ThemeConfig/LayerConfig" | 
					
						
							|  |  |  | import { Or } from "../../Tags/Or" | 
					
						
							|  |  |  | import { Overpass } from "../../Osm/Overpass" | 
					
						
							|  |  |  | import { Utils } from "../../../Utils" | 
					
						
							|  |  |  | import { TagsFilter } from "../../Tags/TagsFilter" | 
					
						
							|  |  |  | import { BBox } from "../../BBox" | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * A wrapper around the 'Overpass'-object. | 
					
						
							|  |  |  |  * It has more logic and will automatically fetch the data for the right bbox and the active layers | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-09-28 17:30:48 +02:00
										 |  |  | export default class OverpassFeatureSource implements FeatureSource { | 
					
						
							| 
									
										
										
										
											2021-01-03 00:19:42 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-03-23 01:42:47 +01:00
										 |  |  |      * The last loaded features, as geojson | 
					
						
							| 
									
										
										
										
											2021-01-03 00:19:42 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-03-23 01:42:47 +01:00
										 |  |  |     public readonly features: UIEventSource<Feature[]> = new UIEventSource(undefined) | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public readonly runningQuery: UIEventSource<boolean> = new UIEventSource<boolean>(false) | 
					
						
							|  |  |  |     public readonly timeout: UIEventSource<number> = new UIEventSource<number>(0) | 
					
						
							| 
									
										
										
										
											2021-09-26 17:36:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     private readonly retries: UIEventSource<number> = new UIEventSource<number>(0) | 
					
						
							| 
									
										
										
										
											2021-10-20 01:26:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |     private readonly state: { | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |         readonly zoom: Store<number> | 
					
						
							| 
									
										
										
										
											2023-03-30 04:51:56 +02:00
										 |  |  |         readonly layers: LayerConfig[] | 
					
						
							|  |  |  |         readonly widenFactor: number | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         readonly overpassUrl: Store<string[]> | 
					
						
							|  |  |  |         readonly overpassTimeout: Store<number> | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |         readonly bounds: Store<BBox> | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-06-05 02:24:14 +02:00
										 |  |  |     private readonly _isActive: Store<boolean> | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |     private readonly padToZoomLevel?: Store<number> | 
					
						
							|  |  |  |     private _lastQueryBBox: BBox | 
					
						
							| 
									
										
										
										
											2022-01-15 02:44:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-03 00:19:42 +01:00
										 |  |  |     constructor( | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |         state: { | 
					
						
							| 
									
										
										
										
											2023-03-30 04:51:56 +02:00
										 |  |  |             readonly layers: LayerConfig[] | 
					
						
							|  |  |  |             readonly widenFactor: number | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |             readonly zoom: Store<number> | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             readonly overpassUrl: Store<string[]> | 
					
						
							|  |  |  |             readonly overpassTimeout: Store<number> | 
					
						
							|  |  |  |             readonly overpassMaxZoom: Store<number> | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |             readonly bounds: Store<BBox> | 
					
						
							| 
									
										
										
										
											2021-09-29 16:55:05 +02:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |         options?: { | 
					
						
							|  |  |  |             padToTiles?: Store<number> | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             isActive?: Store<boolean> | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |         this.state = state | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |         this._isActive = options?.isActive ?? new ImmutableStore(true) | 
					
						
							|  |  |  |         this.padToZoomLevel = options?.padToTiles | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const self = this | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |         state.bounds.addCallbackD((_) => { | 
					
						
							|  |  |  |             self.updateAsyncIfNeeded() | 
					
						
							| 
									
										
										
										
											2021-09-03 13:48:04 +02:00
										 |  |  |         }) | 
					
						
							| 
									
										
										
										
											2021-01-02 16:04:16 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Creates the 'Overpass'-object for the given layers | 
					
						
							|  |  |  |      * @param interpreterUrl | 
					
						
							|  |  |  |      * @param layersToDownload | 
					
						
							|  |  |  |      * @constructor | 
					
						
							|  |  |  |      * @private | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-09-30 04:13:23 +02:00
										 |  |  |     private GetFilter(interpreterUrl: string, layersToDownload: LayerConfig[]): Overpass { | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |         let filters: TagsFilter[] = layersToDownload.map((layer) => layer.source.osmTags) | 
					
						
							| 
									
										
										
										
											2021-03-20 23:45:52 +01:00
										 |  |  |         filters = Utils.NoNull(filters) | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |         if (filters.length === 0) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return undefined | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |         return new Overpass(new Or(filters), [], interpreterUrl, this.state.overpassTimeout) | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-02-20 01:45:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @private | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private async updateAsyncIfNeeded(): Promise<void> { | 
					
						
							|  |  |  |         if (!this._isActive?.data) { | 
					
						
							|  |  |  |             console.log("OverpassFeatureSource: not triggering as not active") | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return | 
					
						
							| 
									
										
										
										
											2021-09-28 17:30:48 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |         if (this.runningQuery.data) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             console.log("Still running a query, not updating") | 
					
						
							|  |  |  |             return undefined | 
					
						
							| 
									
										
										
										
											2021-04-21 01:25:00 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-04-23 12:55:38 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (this.timeout.data > 0) { | 
					
						
							| 
									
										
										
										
											2021-04-21 01:25:00 +02:00
										 |  |  |             console.log("Still in timeout - not updating") | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return undefined | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |         const requestedBounds = this.state.bounds.data | 
					
						
							|  |  |  |         if ( | 
					
						
							|  |  |  |             this._lastQueryBBox !== undefined && | 
					
						
							|  |  |  |             requestedBounds.isContainedIn(this._lastQueryBBox) | 
					
						
							|  |  |  |         ) { | 
					
						
							|  |  |  |             return undefined | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-03-28 05:13:48 +02:00
										 |  |  |         const result = await this.updateAsync() | 
					
						
							|  |  |  |         if (!result) { | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         const [bounds, date, updatedLayers] = result | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |         this._lastQueryBBox = bounds | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-30 00:59:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Download the relevant data from overpass. Attempt to use a different server; only downloads the relevant layers | 
					
						
							|  |  |  |      * @private | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private async updateAsync(): Promise<[BBox, Date, LayerConfig[]]> { | 
					
						
							| 
									
										
										
										
											2021-10-20 01:26:39 +02:00
										 |  |  |         let data: any = undefined | 
					
						
							|  |  |  |         let date: Date = undefined | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         let lastUsed = 0 | 
					
						
							| 
									
										
										
										
											2021-10-20 01:26:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-30 04:13:23 +02:00
										 |  |  |         const layersToDownload = [] | 
					
						
							| 
									
										
										
										
											2023-03-30 04:51:56 +02:00
										 |  |  |         for (const layer of this.state.layers) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             if (typeof layer === "string") { | 
					
						
							| 
									
										
										
										
											2021-10-20 01:26:39 +02:00
										 |  |  |                 throw "A layer was not expanded!" | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-03-25 02:48:24 +01:00
										 |  |  |             if (layer.source === undefined) { | 
					
						
							| 
									
										
										
										
											2021-11-16 02:57:26 +01:00
										 |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |             if (this.state.zoom.data < layer.minzoom) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2021-10-20 01:26:39 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |             if (layer.doNotDownload) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2021-10-20 01:26:39 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-03-30 04:51:56 +02:00
										 |  |  |             if (layer.source === null) { | 
					
						
							|  |  |  |                 // This is a special layer. Should not have been here
 | 
					
						
							|  |  |  |                 console.warn( | 
					
						
							|  |  |  |                     "OverpassFeatureSource received a layer for which the source is null:", | 
					
						
							|  |  |  |                     layer.id | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-10-20 01:26:39 +02:00
										 |  |  |             if (layer.source.geojsonSource !== undefined) { | 
					
						
							|  |  |  |                 // Not our responsibility to download this layer!
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2021-10-20 01:26:39 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |             layersToDownload.push(layer) | 
					
						
							| 
									
										
										
										
											2021-09-30 04:13:23 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-26 17:36:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-15 02:44:11 +01:00
										 |  |  |         if (layersToDownload.length == 0) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return | 
					
						
							| 
									
										
										
										
											2022-01-15 02:44:11 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const self = this | 
					
						
							| 
									
										
										
										
											2021-09-29 16:55:05 +02:00
										 |  |  |         const overpassUrls = self.state.overpassUrl.data | 
					
						
							| 
									
										
										
										
											2022-10-27 01:50:41 +02:00
										 |  |  |         if (overpassUrls === undefined || overpassUrls.length === 0) { | 
					
						
							| 
									
										
										
										
											2022-09-18 12:45:02 +02:00
										 |  |  |             throw "Panic: overpassFeatureSource didn't receive any overpassUrls" | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |         // Note: the bounds are updated between attempts, in case that the user zoomed around
 | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |         let bounds: BBox | 
					
						
							| 
									
										
										
										
											2021-09-26 17:36:39 +02:00
										 |  |  |         do { | 
					
						
							|  |  |  |             try { | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |                 bounds = this.state.bounds.data | 
					
						
							| 
									
										
										
										
											2023-03-30 04:51:56 +02:00
										 |  |  |                     ?.pad(this.state.widenFactor) | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |                     ?.expandToTileBounds(this.padToZoomLevel?.data) | 
					
						
							| 
									
										
										
										
											2021-10-20 01:26:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 if (bounds === undefined) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     return undefined | 
					
						
							| 
									
										
										
										
											2021-10-20 01:26:39 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 const overpass = this.GetFilter(overpassUrls[lastUsed], layersToDownload) | 
					
						
							| 
									
										
										
										
											2021-09-29 16:55:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 if (overpass === undefined) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     return undefined | 
					
						
							| 
									
										
										
										
											2021-09-29 16:55:05 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-12-16 13:45:07 +01:00
										 |  |  |                 this.runningQuery.setData(true) | 
					
						
							|  |  |  |                 ;[data, date] = await overpass.queryGeoJson(bounds) | 
					
						
							| 
									
										
										
										
											2021-09-26 17:36:39 +02:00
										 |  |  |             } catch (e) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 self.retries.data++ | 
					
						
							|  |  |  |                 self.retries.ping() | 
					
						
							|  |  |  |                 console.error(`QUERY FAILED due to`, e) | 
					
						
							| 
									
										
										
										
											2021-09-29 16:55:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 await Utils.waitFor(1000) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (lastUsed + 1 < overpassUrls.length) { | 
					
						
							|  |  |  |                     lastUsed++ | 
					
						
							|  |  |  |                     console.log("Trying next time with", overpassUrls[lastUsed]) | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     lastUsed = 0 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     self.timeout.setData(self.retries.data * 5) | 
					
						
							| 
									
										
										
										
											2021-09-29 16:55:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                     while (self.timeout.data > 0) { | 
					
						
							|  |  |  |                         await Utils.waitFor(1000) | 
					
						
							|  |  |  |                         self.timeout.data-- | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                         self.timeout.ping() | 
					
						
							| 
									
										
										
										
											2021-09-29 16:55:05 +02:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2021-02-20 01:45:51 +01:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-09-26 17:36:39 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         } while (data === undefined && this._isActive.data) | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-26 17:36:39 +02:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |             if (data === undefined) { | 
					
						
							| 
									
										
										
										
											2021-10-20 19:12:28 +02:00
										 |  |  |                 return undefined | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-03-28 05:13:48 +02:00
										 |  |  |             // Some metatags are delivered by overpass _without_ underscore-prefix; we fix them below
 | 
					
						
							|  |  |  |             // TODO FIXME re-enable this data.features.forEach((f) => SimpleMetaTaggers.objectMetaInfo.applyMetaTagsOnFeature(f))
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-30 04:51:56 +02:00
										 |  |  |             console.log("Overpass returned", data.features.length, "features") | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |             self.features.setData(data.features) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return [bounds, date, layersToDownload] | 
					
						
							| 
									
										
										
										
											2021-09-26 17:36:39 +02:00
										 |  |  |         } catch (e) { | 
					
						
							|  |  |  |             console.error("Got the overpass response, but could not process it: ", e, e.stack) | 
					
						
							| 
									
										
										
										
											2021-10-20 19:12:28 +02:00
										 |  |  |             return undefined | 
					
						
							| 
									
										
										
										
											2021-09-29 16:55:05 +02:00
										 |  |  |         } finally { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             self.retries.setData(0) | 
					
						
							|  |  |  |             self.runningQuery.setData(false) | 
					
						
							| 
									
										
										
										
											2021-09-26 17:36:39 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | } |