| 
									
										
										
										
											2021-10-31 02:08:39 +01:00
										 |  |  | import TileHierarchy from "./TileHierarchy"; | 
					
						
							|  |  |  | import FeatureSource, {FeatureSourceForLayer, Tiled} from "../FeatureSource"; | 
					
						
							|  |  |  | import {OsmNode, OsmObject, OsmWay} from "../../Osm/OsmObject"; | 
					
						
							|  |  |  | import SimpleFeatureSource from "../Sources/SimpleFeatureSource"; | 
					
						
							|  |  |  | import FilteredLayer from "../../../Models/FilteredLayer"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default class FullNodeDatabaseSource implements TileHierarchy<FeatureSource & Tiled> { | 
					
						
							|  |  |  |     public readonly loadedTiles = new Map<number, FeatureSource & Tiled>() | 
					
						
							|  |  |  |     private readonly onTileLoaded: (tile: (Tiled & FeatureSourceForLayer)) => void; | 
					
						
							| 
									
										
										
										
											2021-11-03 00:44:53 +01:00
										 |  |  |     private readonly layer: FilteredLayer | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-31 02:08:39 +01:00
										 |  |  |     constructor( | 
					
						
							| 
									
										
										
										
											2021-11-03 00:44:53 +01:00
										 |  |  |         layer: FilteredLayer, | 
					
						
							| 
									
										
										
										
											2021-10-31 02:08:39 +01:00
										 |  |  |         onTileLoaded: ((tile: Tiled & FeatureSourceForLayer) => void)) { | 
					
						
							|  |  |  |         this.onTileLoaded = onTileLoaded | 
					
						
							| 
									
										
										
										
											2021-11-03 00:44:53 +01:00
										 |  |  |         this.layer = layer; | 
					
						
							|  |  |  |         if (this.layer === undefined) { | 
					
						
							|  |  |  |             throw "Layer is undefined" | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-04 02:16:07 +01:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											2021-11-03 00:44:53 +01:00
										 |  |  |     public handleOsmJson(osmJson: any, tileId: number) { | 
					
						
							| 
									
										
										
										
											2021-10-31 02:08:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const allObjects = OsmObject.ParseObjects(osmJson.elements) | 
					
						
							|  |  |  |         const nodesById = new Map<number, OsmNode>() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (const osmObj of allObjects) { | 
					
						
							|  |  |  |             if (osmObj.type !== "node") { | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             const osmNode = <OsmNode>osmObj; | 
					
						
							|  |  |  |             nodesById.set(osmNode.id, osmNode) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const parentWaysByNodeId = new Map<number, OsmWay[]>() | 
					
						
							|  |  |  |         for (const osmObj of allObjects) { | 
					
						
							|  |  |  |             if (osmObj.type !== "way") { | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             const osmWay = <OsmWay>osmObj; | 
					
						
							|  |  |  |             for (const nodeId of osmWay.nodes) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (!parentWaysByNodeId.has(nodeId)) { | 
					
						
							|  |  |  |                     parentWaysByNodeId.set(nodeId, []) | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 parentWaysByNodeId.get(nodeId).push(osmWay) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         parentWaysByNodeId.forEach((allWays, nodeId) => { | 
					
						
							|  |  |  |             nodesById.get(nodeId).tags["parent_ways"] = JSON.stringify(allWays.map(w => w.tags)) | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         const now = new Date() | 
					
						
							|  |  |  |         const asGeojsonFeatures = Array.from(nodesById.values()).map(osmNode => ({ | 
					
						
							| 
									
										
										
										
											2021-11-03 00:44:53 +01:00
										 |  |  |             feature: osmNode.asGeoJson(), freshness: now | 
					
						
							| 
									
										
										
										
											2021-10-31 02:08:39 +01:00
										 |  |  |         })) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const featureSource = new SimpleFeatureSource(this.layer, tileId) | 
					
						
							|  |  |  |         featureSource.features.setData(asGeojsonFeatures) | 
					
						
							|  |  |  |         this.loadedTiles.set(tileId, featureSource) | 
					
						
							|  |  |  |         this.onTileLoaded(featureSource) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-03 00:44:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 |