| 
									
										
										
										
											2020-07-30 00:59:08 +02:00
										 |  |  | import * as OsmToGeoJson from "osmtogeojson"; | 
					
						
							| 
									
										
										
										
											2021-04-18 14:24:30 +02:00
										 |  |  | import {TagsFilter} from "../Tags/TagsFilter"; | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  | import RelationsTracker from "./RelationsTracker"; | 
					
						
							| 
									
										
										
										
											2021-07-03 22:24:12 +02:00
										 |  |  | import {Utils} from "../../Utils"; | 
					
						
							| 
									
										
										
										
											2021-08-23 15:48:42 +02:00
										 |  |  | import {UIEventSource} from "../UIEventSource"; | 
					
						
							| 
									
										
										
										
											2021-09-29 16:55:05 +02:00
										 |  |  | import {BBox} from "../BBox"; | 
					
						
							| 
									
										
										
										
											2020-07-30 00:59:08 +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 { | 
					
						
							| 
									
										
										
										
											2020-07-29 13:16:21 +02:00
										 |  |  |     public static testUrl: string = null | 
					
						
							| 
									
										
										
										
											2021-03-25 15:19:44 +01:00
										 |  |  |     private _filter: TagsFilter | 
					
						
							| 
									
										
										
										
											2021-09-29 16:55:05 +02:00
										 |  |  |     private readonly _interpreterUrl: string; | 
					
						
							| 
									
										
										
										
											2021-08-23 15:48:42 +02:00
										 |  |  |     private readonly _timeout: UIEventSource<number>; | 
					
						
							| 
									
										
										
										
											2021-03-25 15:19:44 +01:00
										 |  |  |     private readonly _extraScripts: string[]; | 
					
						
							| 
									
										
										
										
											2021-04-25 13:25:03 +02:00
										 |  |  |     private _includeMeta: boolean; | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |     private _relationTracker: RelationsTracker; | 
					
						
							| 
									
										
										
										
											2021-09-28 17:30:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-03 01:38:57 +02:00
										 |  |  |     constructor(filter: TagsFilter, | 
					
						
							|  |  |  |                 extraScripts: string[], | 
					
						
							| 
									
										
										
										
											2021-09-29 16:55:05 +02:00
										 |  |  |                 interpreterUrl: string, | 
					
						
							| 
									
										
										
										
											2021-08-23 15:48:42 +02:00
										 |  |  |                 timeout: UIEventSource<number>, | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |                 relationTracker: RelationsTracker, | 
					
						
							| 
									
										
										
										
											2021-08-23 15:48:42 +02:00
										 |  |  |                 includeMeta = true) { | 
					
						
							|  |  |  |         this._timeout = timeout; | 
					
						
							|  |  |  |         this._interpreterUrl = interpreterUrl; | 
					
						
							| 
									
										
										
										
											2020-07-29 13:16:21 +02:00
										 |  |  |         this._filter = filter | 
					
						
							| 
									
										
										
										
											2021-03-20 23:45:52 +01:00
										 |  |  |         this._extraScripts = extraScripts; | 
					
						
							| 
									
										
										
										
											2021-04-25 13:25:03 +02:00
										 |  |  |         this._includeMeta = includeMeta; | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |         this._relationTracker = relationTracker | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 16:55:05 +02:00
										 |  |  |     public async queryGeoJson(bounds: BBox): Promise<[any, Date]> { | 
					
						
							| 
									
										
										
										
											2020-07-20 20:15:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 16:55:05 +02:00
										 |  |  |         let query = this.buildQuery("[bbox:" + bounds.getSouth() + "," + bounds.getWest() + "," + bounds.getNorth() + "," + bounds.getEast() + "]") | 
					
						
							| 
									
										
										
										
											2020-10-12 01:25:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (Overpass.testUrl !== null) { | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |             console.log("Using testing URL") | 
					
						
							|  |  |  |             query = Overpass.testUrl; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |         const self = this; | 
					
						
							| 
									
										
										
										
											2021-09-22 16:07:56 +02:00
										 |  |  |         const json = await Utils.downloadJson(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) { | 
					
						
							|  |  |  |             console.warn("Timeout or other runtime error while querying overpass", json.remark); | 
					
						
							|  |  |  |             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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-22 16:07:56 +02:00
										 |  |  |         self._relationTracker.RegisterRelations(json) | 
					
						
							|  |  |  |         // @ts-ignore
 | 
					
						
							|  |  |  |         const geojson = OsmToGeoJson.default(json); | 
					
						
							|  |  |  |         const osmTime = new Date(json.osm3s.timestamp_osm_base); | 
					
						
							|  |  |  |         return [geojson, osmTime]; | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-03-25 15:19:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-22 03:30:46 +02:00
										 |  |  |     buildQuery(bbox: string): string { | 
					
						
							| 
									
										
										
										
											2021-03-25 15:19:44 +01:00
										 |  |  |         const filters = this._filter.asOverpass() | 
					
						
							|  |  |  |         let filter = "" | 
					
						
							|  |  |  |         for (const filterOr of filters) { | 
					
						
							|  |  |  |             filter += 'nwr' + filterOr + ';' | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         for (const extraScript of this._extraScripts) { | 
					
						
							|  |  |  |             filter += '(' + extraScript + ');'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         const query = | 
					
						
							| 
									
										
										
										
											2021-08-23 15:48:42 +02:00
										 |  |  |             `[out:json][timeout:${this._timeout.data}]${bbox};(${filter});out body;${this._includeMeta ? 'out meta;' : ''}>;out skel qt;` | 
					
						
							| 
									
										
										
										
											2021-09-29 16:55:05 +02:00
										 |  |  |         return `${this._interpreterUrl}?data=${encodeURIComponent(query)}` | 
					
						
							| 
									
										
										
										
											2021-03-25 15:19:44 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-29 13:16:21 +02:00
										 |  |  | } |