| 
									
										
										
										
											2022-10-27 01:50:41 +02:00
										 |  |  | import { Utils } from "../../Utils" | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import * as polygon_features from "../../assets/polygon-features.json" | 
					
						
							| 
									
										
										
										
											2022-10-27 01:50:41 +02:00
										 |  |  | import { Store, UIEventSource } from "../UIEventSource" | 
					
						
							|  |  |  | import { BBox } from "../BBox" | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import * as OsmToGeoJson from "osmtogeojson" | 
					
						
							| 
									
										
										
										
											2022-10-27 01:50:41 +02:00
										 |  |  | import { NodeId, OsmFeature, OsmId, OsmTags, RelationId, WayId } from "../../Models/OsmFeature" | 
					
						
							|  |  |  | import { Feature, LineString, Polygon } from "geojson" | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export abstract class OsmObject { | 
					
						
							| 
									
										
										
										
											2021-07-10 15:52:52 +02:00
										 |  |  |     private static defaultBackend = "https://www.openstreetmap.org/" | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     protected static backendURL = OsmObject.defaultBackend | 
					
						
							| 
									
										
										
										
											2021-06-30 18:48:23 +02:00
										 |  |  |     private static polygonFeatures = OsmObject.constructPolygonFeatures() | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     private static objectCache = new Map<string, UIEventSource<OsmObject>>() | 
					
						
							|  |  |  |     private static historyCache = new Map<string, UIEventSource<OsmObject[]>>() | 
					
						
							|  |  |  |     type: "node" | "way" | "relation" | 
					
						
							|  |  |  |     id: number | 
					
						
							| 
									
										
										
										
											2021-09-22 05:02:09 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * The OSM tags as simple object | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-10-27 01:50:41 +02:00
										 |  |  |     tags: OsmTags & { id: OsmId } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     version: number | 
					
						
							|  |  |  |     public changed: boolean = false | 
					
						
							|  |  |  |     timestamp: Date | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     protected constructor(type: string, id: number) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         this.id = id | 
					
						
							| 
									
										
										
										
											2021-10-04 03:12:42 +02:00
										 |  |  |         // @ts-ignore
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         this.type = type | 
					
						
							| 
									
										
										
										
											2021-05-06 01:33:09 +02:00
										 |  |  |         this.tags = { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             id: `${this.type}/${id}`, | 
					
						
							| 
									
										
										
										
											2021-05-06 01:33:09 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-30 18:48:23 +02:00
										 |  |  |     public static SetBackendUrl(url: string) { | 
					
						
							|  |  |  |         if (!url.endsWith("/")) { | 
					
						
							|  |  |  |             throw "Backend URL must end with a '/'" | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (!url.startsWith("http")) { | 
					
						
							|  |  |  |             throw "Backend URL must begin with http" | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         this.backendURL = url | 
					
						
							| 
									
										
										
										
											2021-06-30 18:48:23 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-27 01:50:41 +02:00
										 |  |  |     public static DownloadObject(id: NodeId, forceRefresh?: boolean): Store<OsmNode> | 
					
						
							|  |  |  |     public static DownloadObject(id: RelationId, forceRefresh?: boolean): Store<OsmRelation> | 
					
						
							|  |  |  |     public static DownloadObject(id: WayId, forceRefresh?: boolean): Store<OsmWay> | 
					
						
							| 
									
										
										
										
											2022-08-24 02:28:41 +02:00
										 |  |  |     public static DownloadObject(id: string, forceRefresh: boolean = false): Store<OsmObject> { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         let src: UIEventSource<OsmObject> | 
					
						
							| 
									
										
										
										
											2021-06-30 18:48:23 +02:00
										 |  |  |         if (OsmObject.objectCache.has(id)) { | 
					
						
							| 
									
										
										
										
											2021-07-09 19:56:00 +02:00
										 |  |  |             src = OsmObject.objectCache.get(id) | 
					
						
							| 
									
										
										
										
											2021-07-10 15:52:52 +02:00
										 |  |  |             if (forceRefresh) { | 
					
						
							| 
									
										
										
										
											2021-07-09 19:56:00 +02:00
										 |  |  |                 src.setData(undefined) | 
					
						
							| 
									
										
										
										
											2021-07-10 15:52:52 +02:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 return src | 
					
						
							| 
									
										
										
										
											2021-07-09 19:56:00 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-07-10 15:52:52 +02:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2022-08-24 02:28:41 +02:00
										 |  |  |             src = UIEventSource.FromPromise(OsmObject.DownloadObjectAsync(id)) | 
					
						
							| 
									
										
										
										
											2021-06-30 18:48:23 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-22 05:02:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         OsmObject.objectCache.set(id, src) | 
					
						
							|  |  |  |         return src | 
					
						
							| 
									
										
										
										
											2021-09-22 05:02:09 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-16 01:09:18 +01:00
										 |  |  |     static async DownloadPropertiesOf(id: string): Promise<OsmTags | "deleted"> { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const splitted = id.split("/") | 
					
						
							|  |  |  |         const idN = Number(splitted[1]) | 
					
						
							| 
									
										
										
										
											2021-10-10 23:38:09 +02:00
										 |  |  |         if (idN < 0) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return undefined | 
					
						
							| 
									
										
										
										
											2021-10-10 23:38:09 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const url = `${OsmObject.backendURL}api/0.6/${id}` | 
					
						
							| 
									
										
										
										
											2022-12-16 01:09:18 +01:00
										 |  |  |         const rawData = await Utils.downloadJsonCachedAdvanced(url, 1000) | 
					
						
							| 
									
										
										
										
											2022-12-16 13:45:07 +01:00
										 |  |  |         if (rawData["error"] !== undefined && rawData["statuscode"] === 410) { | 
					
						
							| 
									
										
										
										
											2022-12-16 01:09:18 +01:00
										 |  |  |             return "deleted" | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-01-03 23:45:04 +01:00
										 |  |  |         return rawData["content"].elements[0].tags | 
					
						
							| 
									
										
										
										
											2021-10-10 23:38:09 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-22 05:02:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-27 01:50:41 +02:00
										 |  |  |     static async DownloadObjectAsync( | 
					
						
							|  |  |  |         id: NodeId, | 
					
						
							|  |  |  |         maxCacheAgeInSecs?: number | 
					
						
							|  |  |  |     ): Promise<OsmNode | undefined> | 
					
						
							|  |  |  |     static async DownloadObjectAsync( | 
					
						
							|  |  |  |         id: WayId, | 
					
						
							|  |  |  |         maxCacheAgeInSecs?: number | 
					
						
							|  |  |  |     ): Promise<OsmWay | undefined> | 
					
						
							|  |  |  |     static async DownloadObjectAsync( | 
					
						
							|  |  |  |         id: RelationId, | 
					
						
							|  |  |  |         maxCacheAgeInSecs?: number | 
					
						
							|  |  |  |     ): Promise<OsmRelation | undefined> | 
					
						
							|  |  |  |     static async DownloadObjectAsync( | 
					
						
							|  |  |  |         id: OsmId, | 
					
						
							|  |  |  |         maxCacheAgeInSecs?: number | 
					
						
							|  |  |  |     ): Promise<OsmObject | undefined> | 
					
						
							|  |  |  |     static async DownloadObjectAsync( | 
					
						
							|  |  |  |         id: string, | 
					
						
							|  |  |  |         maxCacheAgeInSecs?: number | 
					
						
							|  |  |  |     ): Promise<OsmObject | undefined> | 
					
						
							|  |  |  |     static async DownloadObjectAsync( | 
					
						
							|  |  |  |         id: string, | 
					
						
							|  |  |  |         maxCacheAgeInSecs?: number | 
					
						
							|  |  |  |     ): Promise<OsmObject | undefined> { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const splitted = id.split("/") | 
					
						
							|  |  |  |         const type = splitted[0] | 
					
						
							|  |  |  |         const idN = Number(splitted[1]) | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |         if (idN < 0) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return undefined | 
					
						
							| 
									
										
										
										
											2021-07-18 14:52:09 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const full = !id.startsWith("node") ? "/full" : "" | 
					
						
							|  |  |  |         const url = `${OsmObject.backendURL}api/0.6/${id}${full}` | 
					
						
							| 
									
										
										
										
											2022-09-21 02:11:10 +02:00
										 |  |  |         const rawData = await Utils.downloadJsonCached(url, (maxCacheAgeInSecs ?? 10) * 1000) | 
					
						
							| 
									
										
										
										
											2022-06-24 18:12:39 +02:00
										 |  |  |         if (rawData === undefined) { | 
					
						
							| 
									
										
										
										
											2022-04-28 00:29:51 +02:00
										 |  |  |             return undefined | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-22 16:07:56 +02:00
										 |  |  |         // A full query might contain more then just the requested object (e.g. nodes that are part of a way, where we only want the way)
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const parsed = OsmObject.ParseObjects(rawData.elements) | 
					
						
							| 
									
										
										
										
											2021-09-22 16:07:56 +02:00
										 |  |  |         // Lets fetch the object we need
 | 
					
						
							|  |  |  |         for (const osmObject of parsed) { | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |             if (osmObject.type !== type) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2021-09-22 16:07:56 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |             if (osmObject.id !== idN) { | 
					
						
							| 
									
										
										
										
											2021-09-22 16:07:56 +02:00
										 |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             // Found the one!
 | 
					
						
							|  |  |  |             return osmObject | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-22 16:07:56 +02:00
										 |  |  |         throw "PANIC: requested object is not part of the response" | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-28 18:06:54 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Downloads the ways that are using this node. | 
					
						
							|  |  |  |      * Beware: their geometry will be incomplete! | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-09-22 05:02:09 +02:00
										 |  |  |     public static DownloadReferencingWays(id: string): Promise<OsmWay[]> { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return Utils.downloadJsonCached( | 
					
						
							|  |  |  |             `${OsmObject.backendURL}api/0.6/${id}/ways`, | 
					
						
							|  |  |  |             60 * 1000 | 
					
						
							|  |  |  |         ).then((data) => { | 
					
						
							|  |  |  |             return data.elements.map((wayInfo) => { | 
					
						
							|  |  |  |                 const way = new OsmWay(wayInfo.id) | 
					
						
							|  |  |  |                 way.LoadData(wayInfo) | 
					
						
							|  |  |  |                 return way | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2021-06-28 18:06:54 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-30 18:48:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-28 18:06:54 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Downloads the relations that are using this feature. | 
					
						
							|  |  |  |      * Beware: their geometry will be incomplete! | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-09-22 05:02:09 +02:00
										 |  |  |     public static async DownloadReferencingRelations(id: string): Promise<OsmRelation[]> { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const data = await Utils.downloadJsonCached( | 
					
						
							|  |  |  |             `${OsmObject.backendURL}api/0.6/${id}/relations`, | 
					
						
							|  |  |  |             60 * 1000 | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         return data.elements.map((wayInfo) => { | 
					
						
							| 
									
										
										
										
											2021-09-22 05:02:09 +02:00
										 |  |  |             const rel = new OsmRelation(wayInfo.id) | 
					
						
							|  |  |  |             rel.LoadData(wayInfo) | 
					
						
							| 
									
										
										
										
											2022-06-24 18:12:39 +02:00
										 |  |  |             rel.SaveExtraData(wayInfo, undefined) | 
					
						
							| 
									
										
										
										
											2021-09-22 05:02:09 +02:00
										 |  |  |             return rel | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2021-06-28 18:06:54 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-30 18:48:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public static DownloadHistory(id: string): UIEventSource<OsmObject[]> { | 
					
						
							| 
									
										
										
										
											2021-06-30 18:48:23 +02:00
										 |  |  |         if (OsmObject.historyCache.has(id)) { | 
					
						
							|  |  |  |             return OsmObject.historyCache.get(id) | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const splitted = id.split("/") | 
					
						
							|  |  |  |         const type = splitted[0] | 
					
						
							|  |  |  |         const idN = Number(splitted[1]) | 
					
						
							|  |  |  |         const src = new UIEventSource<OsmObject[]>([]) | 
					
						
							|  |  |  |         OsmObject.historyCache.set(id, src) | 
					
						
							|  |  |  |         Utils.downloadJsonCached( | 
					
						
							|  |  |  |             `${OsmObject.backendURL}api/0.6/${type}/${idN}/history`, | 
					
						
							|  |  |  |             10 * 60 * 1000 | 
					
						
							|  |  |  |         ).then((data) => { | 
					
						
							|  |  |  |             const elements: any[] = data.elements | 
					
						
							| 
									
										
										
										
											2021-05-06 03:03:54 +02:00
										 |  |  |             const osmObjects: OsmObject[] = [] | 
					
						
							|  |  |  |             for (const element of elements) { | 
					
						
							|  |  |  |                 let osmObject: OsmObject = null | 
					
						
							|  |  |  |                 switch (type) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     case "node": | 
					
						
							|  |  |  |                         osmObject = new OsmNode(idN) | 
					
						
							|  |  |  |                         break | 
					
						
							|  |  |  |                     case "way": | 
					
						
							|  |  |  |                         osmObject = new OsmWay(idN) | 
					
						
							|  |  |  |                         break | 
					
						
							|  |  |  |                     case "relation": | 
					
						
							|  |  |  |                         osmObject = new OsmRelation(idN) | 
					
						
							|  |  |  |                         break | 
					
						
							| 
									
										
										
										
											2021-05-06 03:03:54 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 osmObject?.LoadData(element) | 
					
						
							|  |  |  |                 osmObject?.SaveExtraData(element, []) | 
					
						
							| 
									
										
										
										
											2021-05-06 03:03:54 +02:00
										 |  |  |                 osmObjects.push(osmObject) | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-06-30 18:48:23 +02:00
										 |  |  |             src.setData(osmObjects) | 
					
						
							| 
									
										
										
										
											2021-05-06 03:03:54 +02:00
										 |  |  |         }) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return src | 
					
						
							| 
									
										
										
										
											2021-05-06 03:03:54 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-30 18:48:23 +02:00
										 |  |  |     // bounds should be: [[maxlat, minlon], [minlat, maxlon]] (same as Utils.tile_bounds)
 | 
					
						
							| 
									
										
										
										
											2021-09-22 05:02:09 +02:00
										 |  |  |     public static async LoadArea(bbox: BBox): Promise<OsmObject[]> { | 
					
						
							|  |  |  |         const url = `${OsmObject.backendURL}api/0.6/map.json?bbox=${bbox.minLon},${bbox.minLat},${bbox.maxLon},${bbox.maxLat}` | 
					
						
							|  |  |  |         const data = await Utils.downloadJson(url) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const elements: any[] = data.elements | 
					
						
							|  |  |  |         return OsmObject.ParseObjects(elements) | 
					
						
							| 
									
										
										
										
											2021-06-30 18:48:23 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public static ParseObjects(elements: any[]): OsmObject[] { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const objects: OsmObject[] = [] | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |         const allNodes: Map<number, OsmNode> = new Map<number, OsmNode>() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (const element of elements) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             const type = element.type | 
					
						
							|  |  |  |             const idN = element.id | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |             let osmObject: OsmObject = null | 
					
						
							|  |  |  |             switch (type) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 case "node": | 
					
						
							|  |  |  |                     const node = new OsmNode(idN) | 
					
						
							|  |  |  |                     allNodes.set(idN, node) | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |                     osmObject = node | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     node.SaveExtraData(element) | 
					
						
							|  |  |  |                     break | 
					
						
							|  |  |  |                 case "way": | 
					
						
							|  |  |  |                     osmObject = new OsmWay(idN) | 
					
						
							|  |  |  |                     const nodes = element.nodes.map((i) => allNodes.get(i)) | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |                     osmObject.SaveExtraData(element, nodes) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     break | 
					
						
							|  |  |  |                 case "relation": | 
					
						
							|  |  |  |                     osmObject = new OsmRelation(idN) | 
					
						
							|  |  |  |                     const allGeojsons = OsmToGeoJson.default( | 
					
						
							| 
									
										
										
										
											2022-10-27 01:50:41 +02:00
										 |  |  |                         { elements }, | 
					
						
							| 
									
										
										
										
											2022-06-24 18:12:39 +02:00
										 |  |  |                         // @ts-ignore
 | 
					
						
							|  |  |  |                         { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                             flatProperties: true, | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     ) | 
					
						
							|  |  |  |                     const feature = allGeojsons.features.find( | 
					
						
							|  |  |  |                         (f) => f.id === osmObject.type + "/" + osmObject.id | 
					
						
							|  |  |  |                     ) | 
					
						
							| 
									
										
										
										
											2022-06-24 18:12:39 +02:00
										 |  |  |                     osmObject.SaveExtraData(element, feature) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     break | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (osmObject !== undefined && OsmObject.backendURL !== OsmObject.defaultBackend) { | 
					
						
							|  |  |  |                 osmObject.tags["_backend"] = OsmObject.backendURL | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             osmObject?.LoadData(element) | 
					
						
							|  |  |  |             objects.push(osmObject) | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return objects | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-22 14:13:41 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Uses the list of polygon features to determine if the given tags are a polygon or not. | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-07-29 20:04:36 +02:00
										 |  |  |      * OsmObject.isPolygon({"building":"yes"}) // => true
 | 
					
						
							|  |  |  |      * OsmObject.isPolygon({"highway":"residential"}) // => false
 | 
					
						
							| 
									
										
										
										
											2022-02-22 14:13:41 +01:00
										 |  |  |      * */ | 
					
						
							| 
									
										
										
										
											2021-05-31 20:47:08 +02:00
										 |  |  |     protected static isPolygon(tags: any): boolean { | 
					
						
							|  |  |  |         for (const tagsKey in tags) { | 
					
						
							|  |  |  |             if (!tags.hasOwnProperty(tagsKey)) { | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             const polyGuide: { values: Set<string>; blacklist: boolean } = | 
					
						
							|  |  |  |                 OsmObject.polygonFeatures.get(tagsKey) | 
					
						
							| 
									
										
										
										
											2021-05-31 20:47:08 +02:00
										 |  |  |             if (polyGuide === undefined) { | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             if (polyGuide.values === null) { | 
					
						
							| 
									
										
										
										
											2022-02-22 14:13:41 +01:00
										 |  |  |                 // .values is null, thus merely _having_ this key is enough to be a polygon (or if blacklist, being a line)
 | 
					
						
							| 
									
										
										
										
											2021-05-31 20:47:08 +02:00
										 |  |  |                 return !polyGuide.blacklist | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-02-22 14:13:41 +01:00
										 |  |  |             // is the key contained? Then we have a match if the value is contained
 | 
					
						
							|  |  |  |             const doesMatch = polyGuide.values.has(tags[tagsKey]) | 
					
						
							| 
									
										
										
										
											2022-06-24 18:12:39 +02:00
										 |  |  |             if (polyGuide.blacklist) { | 
					
						
							| 
									
										
										
										
											2022-02-22 14:13:41 +01:00
										 |  |  |                 return !doesMatch | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             return doesMatch | 
					
						
							| 
									
										
										
										
											2021-05-31 20:47:08 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-06-24 18:12:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return false | 
					
						
							| 
									
										
										
										
											2021-05-31 20:47:08 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-27 01:50:41 +02:00
										 |  |  |     private static constructPolygonFeatures(): Map< | 
					
						
							|  |  |  |         string, | 
					
						
							|  |  |  |         { values: Set<string>; blacklist: boolean } | 
					
						
							|  |  |  |     > { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const result = new Map<string, { values: Set<string>; blacklist: boolean }>() | 
					
						
							|  |  |  |         for (const polygonFeature of polygon_features["default"] ?? polygon_features) { | 
					
						
							|  |  |  |             const key = polygonFeature.key | 
					
						
							| 
									
										
										
										
											2021-05-06 03:03:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-30 18:48:23 +02:00
										 |  |  |             if (polygonFeature.polygon === "all") { | 
					
						
							| 
									
										
										
										
											2022-10-27 01:50:41 +02:00
										 |  |  |                 result.set(key, { values: null, blacklist: false }) | 
					
						
							| 
									
										
										
										
											2021-06-30 18:48:23 +02:00
										 |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-05-10 23:51:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-30 18:48:23 +02:00
										 |  |  |             const blacklist = polygonFeature.polygon === "blacklist" | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             result.set(key, { | 
					
						
							|  |  |  |                 values: new Set<string>(polygonFeature.values), | 
					
						
							|  |  |  |                 blacklist: blacklist, | 
					
						
							|  |  |  |             }) | 
					
						
							| 
									
										
										
										
											2021-04-21 01:23:28 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return result | 
					
						
							| 
									
										
										
										
											2021-04-21 01:23:28 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-06 01:33:09 +02:00
										 |  |  |     // The centerpoint of the feature, as [lat, lon]
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public abstract centerpoint(): [number, number] | 
					
						
							| 
									
										
										
										
											2021-05-06 01:33:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public abstract asGeoJson(): any | 
					
						
							| 
									
										
										
										
											2021-05-06 01:33:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     abstract SaveExtraData(element: any, allElements: OsmObject[] | any) | 
					
						
							| 
									
										
										
										
											2020-07-05 18:59:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Generates the changeset-XML for tags | 
					
						
							|  |  |  |      * @constructor | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     TagsXML(): string { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         let tags = "" | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |         for (const key in this.tags) { | 
					
						
							| 
									
										
										
										
											2021-05-10 23:51:03 +02:00
										 |  |  |             if (key.startsWith("_")) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2021-05-09 18:56:51 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-05-10 23:51:03 +02:00
										 |  |  |             if (key === "id") { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2021-05-10 16:03:11 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             const v = this.tags[key] | 
					
						
							| 
									
										
										
										
											2021-10-20 00:09:40 +02:00
										 |  |  |             if (v !== "" && v !== undefined) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 tags += | 
					
						
							|  |  |  |                     '        <tag k="' + | 
					
						
							|  |  |  |                     Utils.EncodeXmlValue(key) + | 
					
						
							|  |  |  |                     '" v="' + | 
					
						
							|  |  |  |                     Utils.EncodeXmlValue(this.tags[key]) + | 
					
						
							|  |  |  |                     '"/>\n' | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return tags | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     abstract ChangesetXML(changesetId: string): string | 
					
						
							| 
									
										
										
										
											2020-08-27 18:44:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-21 01:23:28 +02:00
										 |  |  |     protected VersionXML() { | 
					
						
							|  |  |  |         if (this.version === undefined) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return "" | 
					
						
							| 
									
										
										
										
											2020-08-27 18:44:16 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return 'version="' + this.version + '"' | 
					
						
							| 
									
										
										
										
											2020-08-27 18:44:16 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-05-06 03:03:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private LoadData(element: any): void { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         this.tags = element.tags ?? this.tags | 
					
						
							|  |  |  |         this.version = element.version | 
					
						
							|  |  |  |         this.timestamp = element.timestamp | 
					
						
							|  |  |  |         const tgs = this.tags | 
					
						
							| 
									
										
										
										
											2021-05-10 23:51:03 +02:00
										 |  |  |         if (element.tags === undefined) { | 
					
						
							| 
									
										
										
										
											2021-05-06 03:03:54 +02:00
										 |  |  |             // Simple node which is part of a way - not important
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return | 
					
						
							| 
									
										
										
										
											2021-05-06 03:03:54 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         tgs["_last_edit:contributor"] = element.user | 
					
						
							|  |  |  |         tgs["_last_edit:contributor:uid"] = element.uid | 
					
						
							|  |  |  |         tgs["_last_edit:changeset"] = element.changeset | 
					
						
							|  |  |  |         tgs["_last_edit:timestamp"] = element.timestamp | 
					
						
							|  |  |  |         tgs["_version_number"] = element.version | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         tgs["id"] = <OsmId>(this.type + "/" + this.id) | 
					
						
							| 
									
										
										
										
											2021-05-06 03:03:54 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class OsmNode extends OsmObject { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     lat: number | 
					
						
							|  |  |  |     lon: number | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-15 00:39:11 +02:00
										 |  |  |     constructor(id: number) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         super("node", id) | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ChangesetXML(changesetId: string): string { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         let tags = this.TagsXML() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return ( | 
					
						
							|  |  |  |             '    <node id="' + | 
					
						
							|  |  |  |             this.id + | 
					
						
							|  |  |  |             '" changeset="' + | 
					
						
							|  |  |  |             changesetId + | 
					
						
							|  |  |  |             '" ' + | 
					
						
							|  |  |  |             this.VersionXML() + | 
					
						
							|  |  |  |             ' lat="' + | 
					
						
							|  |  |  |             this.lat + | 
					
						
							|  |  |  |             '" lon="' + | 
					
						
							|  |  |  |             this.lon + | 
					
						
							|  |  |  |             '">\n' + | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |             tags + | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             "    </node>\n" | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     SaveExtraData(element) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         this.lat = element.lat | 
					
						
							|  |  |  |         this.lon = element.lon | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-05-06 01:33:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     centerpoint(): [number, number] { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return [this.lat, this.lon] | 
					
						
							| 
									
										
										
										
											2021-05-06 01:33:09 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     asGeoJson(): OsmFeature { | 
					
						
							| 
									
										
										
										
											2021-05-06 01:33:09 +02:00
										 |  |  |         return { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             type: "Feature", | 
					
						
							|  |  |  |             properties: this.tags, | 
					
						
							|  |  |  |             geometry: { | 
					
						
							|  |  |  |                 type: "Point", | 
					
						
							|  |  |  |                 coordinates: [this.lon, this.lat], | 
					
						
							|  |  |  |             }, | 
					
						
							| 
									
										
										
										
											2021-05-06 01:33:09 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class OsmWay extends OsmObject { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     nodes: number[] = [] | 
					
						
							| 
									
										
										
										
											2021-09-22 05:02:09 +02:00
										 |  |  |     // The coordinates of the way, [lat, lon][]
 | 
					
						
							| 
									
										
										
										
											2021-05-06 01:33:09 +02:00
										 |  |  |     coordinates: [number, number][] = [] | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     lat: number | 
					
						
							|  |  |  |     lon: number | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-15 00:39:11 +02:00
										 |  |  |     constructor(id: number) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         super("way", id) | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-06 01:33:09 +02:00
										 |  |  |     centerpoint(): [number, number] { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return [this.lat, this.lon] | 
					
						
							| 
									
										
										
										
											2021-05-06 01:33:09 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     ChangesetXML(changesetId: string): string { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         let tags = this.TagsXML() | 
					
						
							|  |  |  |         let nds = "" | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |         for (const node in this.nodes) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             nds += '      <nd ref="' + this.nodes[node] + '"/>\n' | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return ( | 
					
						
							|  |  |  |             '    <way id="' + | 
					
						
							|  |  |  |             this.id + | 
					
						
							|  |  |  |             '" changeset="' + | 
					
						
							|  |  |  |             changesetId + | 
					
						
							|  |  |  |             '" ' + | 
					
						
							|  |  |  |             this.VersionXML() + | 
					
						
							|  |  |  |             ">\n" + | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |             nds + | 
					
						
							|  |  |  |             tags + | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             "    </way>\n" | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-06 03:03:54 +02:00
										 |  |  |     SaveExtraData(element, allNodes: OsmNode[]) { | 
					
						
							|  |  |  |         let latSum = 0 | 
					
						
							|  |  |  |         let lonSum = 0 | 
					
						
							| 
									
										
										
										
											2021-05-06 01:33:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-27 18:55:12 +02:00
										 |  |  |         const nodeDict = new Map<number, OsmNode>() | 
					
						
							| 
									
										
										
										
											2021-05-06 01:33:09 +02:00
										 |  |  |         for (const node of allNodes) { | 
					
						
							| 
									
										
										
										
											2021-05-27 18:55:12 +02:00
										 |  |  |             nodeDict.set(node.id, node) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-22 16:07:56 +02:00
										 |  |  |         if (element.nodes === undefined) { | 
					
						
							| 
									
										
										
										
											2022-02-22 14:13:41 +01:00
										 |  |  |             console.error("PANIC: no nodes!") | 
					
						
							| 
									
										
										
										
											2021-09-22 16:07:56 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-27 18:55:12 +02:00
										 |  |  |         for (const nodeId of element.nodes) { | 
					
						
							|  |  |  |             const node = nodeDict.get(nodeId) | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |             if (node === undefined) { | 
					
						
							| 
									
										
										
										
											2021-07-28 15:14:13 +02:00
										 |  |  |                 console.error("Error: node ", nodeId, "not found in ", nodeDict) | 
					
						
							| 
									
										
										
										
											2021-07-28 15:50:33 +02:00
										 |  |  |                 // This is probably part of a relation which hasn't been fully downloaded
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2021-07-28 15:14:13 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             this.coordinates.push(node.centerpoint()) | 
					
						
							| 
									
										
										
										
											2022-01-05 16:36:08 +01:00
										 |  |  |             latSum += node.lat | 
					
						
							|  |  |  |             lonSum += node.lon | 
					
						
							| 
									
										
										
										
											2021-05-06 01:33:09 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         let count = this.coordinates.length | 
					
						
							|  |  |  |         this.lat = latSum / count | 
					
						
							|  |  |  |         this.lon = lonSum / count | 
					
						
							|  |  |  |         this.nodes = element.nodes | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-05-06 01:33:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-21 02:11:10 +02:00
										 |  |  |     public asGeoJson(): Feature<Polygon | LineString> & { properties: { id: WayId } } { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         let coordinates: [number, number][] | [number, number][][] = this.coordinates.map( | 
					
						
							|  |  |  |             ([lat, lon]) => [lon, lat] | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2022-09-21 02:11:10 +02:00
										 |  |  |         let geometry: LineString | Polygon | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-22 16:07:56 +02:00
										 |  |  |         if (this.isPolygon()) { | 
					
						
							| 
									
										
										
										
											2022-09-21 02:11:10 +02:00
										 |  |  |             geometry = { | 
					
						
							|  |  |  |                 type: "Polygon", | 
					
						
							|  |  |  |                 coordinates: [coordinates], | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             geometry = { | 
					
						
							|  |  |  |                 type: "LineString", | 
					
						
							|  |  |  |                 coordinates: coordinates, | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-09-22 05:02:09 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-05-06 01:33:09 +02:00
										 |  |  |         return { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             type: "Feature", | 
					
						
							| 
									
										
										
										
											2022-09-21 02:11:10 +02:00
										 |  |  |             properties: <any>this.tags, | 
					
						
							|  |  |  |             geometry, | 
					
						
							| 
									
										
										
										
											2021-05-06 01:33:09 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-05-27 18:55:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private isPolygon(): boolean { | 
					
						
							| 
									
										
										
										
											2022-02-22 14:13:41 +01:00
										 |  |  |         // Compare lat and lon seperately, as the coordinate array might not be a reference to the same object
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         if ( | 
					
						
							|  |  |  |             this.coordinates[0][0] !== this.coordinates[this.coordinates.length - 1][0] || | 
					
						
							|  |  |  |             this.coordinates[0][1] !== this.coordinates[this.coordinates.length - 1][1] | 
					
						
							|  |  |  |         ) { | 
					
						
							|  |  |  |             return false // Not closed
 | 
					
						
							| 
									
										
										
										
											2021-05-27 18:55:12 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-05-31 20:47:08 +02:00
										 |  |  |         return OsmObject.isPolygon(this.tags) | 
					
						
							| 
									
										
										
										
											2021-05-27 18:55:12 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class OsmRelation extends OsmObject { | 
					
						
							| 
									
										
										
										
											2021-07-15 20:47:28 +02:00
										 |  |  |     public members: { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         type: "node" | "way" | "relation" | 
					
						
							|  |  |  |         ref: number | 
					
						
							| 
									
										
										
										
											2021-07-15 20:47:28 +02:00
										 |  |  |         role: string | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     }[] | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-24 18:12:39 +02:00
										 |  |  |     private geojson = undefined | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-15 00:39:11 +02:00
										 |  |  |     constructor(id: number) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         super("relation", id) | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-06 01:33:09 +02:00
										 |  |  |     centerpoint(): [number, number] { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return [0, 0] // TODO
 | 
					
						
							| 
									
										
										
										
											2021-05-06 01:33:09 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     ChangesetXML(changesetId: string): string { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         let members = "" | 
					
						
							| 
									
										
										
										
											2021-05-06 01:33:09 +02:00
										 |  |  |         for (const member of this.members) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             members += | 
					
						
							|  |  |  |                 '      <member type="' + | 
					
						
							|  |  |  |                 member.type + | 
					
						
							|  |  |  |                 '" ref="' + | 
					
						
							|  |  |  |                 member.ref + | 
					
						
							|  |  |  |                 '" role="' + | 
					
						
							|  |  |  |                 member.role + | 
					
						
							|  |  |  |                 '"/>\n' | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         let tags = this.TagsXML() | 
					
						
							| 
									
										
										
										
											2021-08-24 12:13:47 +02:00
										 |  |  |         let cs = "" | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |         if (changesetId !== undefined) { | 
					
						
							| 
									
										
										
										
											2021-08-24 12:13:47 +02:00
										 |  |  |             cs = `changeset="${changesetId}"` | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return `    <relation id="${this.id}" ${cs} ${this.VersionXML()}>
 | 
					
						
							|  |  |  | ${members}${tags}        </relation> | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | `
 | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-24 18:12:39 +02:00
										 |  |  |     SaveExtraData(element, geojson) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         this.members = element.members | 
					
						
							| 
									
										
										
										
											2022-06-24 18:12:39 +02:00
										 |  |  |         this.geojson = geojson | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-05-06 01:33:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-22 05:02:09 +02:00
										 |  |  |     asGeoJson(): any { | 
					
						
							| 
									
										
										
										
											2022-06-24 18:12:39 +02:00
										 |  |  |         if (this.geojson !== undefined) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return this.geojson | 
					
						
							| 
									
										
										
										
											2022-06-24 18:12:39 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-05-06 01:33:09 +02:00
										 |  |  |         throw "Not Implemented" | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | } |