| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  | import { Store, Stores } from "../../UIEventSource" | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import { Tiles } from "../../../Models/TileRange" | 
					
						
							|  |  |  | import { BBox } from "../../BBox" | 
					
						
							| 
									
										
										
										
											2023-04-06 01:33:08 +02:00
										 |  |  | import { FeatureSource } from "../FeatureSource" | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  | import FeatureSourceMerger from "../Sources/FeatureSourceMerger" | 
					
						
							| 
									
										
										
										
											2021-09-20 17:14:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  | /*** | 
					
						
							| 
									
										
										
										
											2023-03-28 05:13:48 +02:00
										 |  |  |  * A tiled source which dynamically loads the required tiles at a fixed zoom level. | 
					
						
							|  |  |  |  * A single featureSource will be initiliased for every tile in view; which will alter be merged into this featureSource | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  | export default class DynamicTileSource extends FeatureSourceMerger { | 
					
						
							| 
									
										
										
										
											2021-09-20 17:14:55 +02:00
										 |  |  |     constructor( | 
					
						
							|  |  |  |         zoomlevel: number, | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |         constructSource: (tileIndex) => FeatureSource, | 
					
						
							|  |  |  |         mapProperties: { | 
					
						
							|  |  |  |             bounds: Store<BBox> | 
					
						
							|  |  |  |             zoom: Store<number> | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         options?: { | 
					
						
							|  |  |  |             isActive?: Store<boolean> | 
					
						
							| 
									
										
										
										
											2021-09-20 17:14:55 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |         super() | 
					
						
							|  |  |  |         const loadedTiles = new Set<number>() | 
					
						
							|  |  |  |         const neededTiles: Store<number[]> = Stores.ListStabilized( | 
					
						
							|  |  |  |             mapProperties.bounds | 
					
						
							|  |  |  |                 .mapD( | 
					
						
							|  |  |  |                     (bounds) => { | 
					
						
							|  |  |  |                         const tileRange = Tiles.TileRangeBetween( | 
					
						
							|  |  |  |                             zoomlevel, | 
					
						
							|  |  |  |                             bounds.getNorth(), | 
					
						
							|  |  |  |                             bounds.getEast(), | 
					
						
							|  |  |  |                             bounds.getSouth(), | 
					
						
							|  |  |  |                             bounds.getWest() | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                         ) | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |                         if (tileRange.total > 10000) { | 
					
						
							|  |  |  |                             console.error( | 
					
						
							|  |  |  |                                 "Got a really big tilerange, bounds and location might be out of sync" | 
					
						
							|  |  |  |                             ) | 
					
						
							|  |  |  |                             return undefined | 
					
						
							|  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |                         const needed = Tiles.MapRange(tileRange, (x, y) => | 
					
						
							|  |  |  |                             Tiles.tile_index(zoomlevel, x, y) | 
					
						
							|  |  |  |                         ).filter((i) => !loadedTiles.has(i)) | 
					
						
							|  |  |  |                         if (needed.length === 0) { | 
					
						
							|  |  |  |                             return undefined | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         return needed | 
					
						
							|  |  |  |                     }, | 
					
						
							|  |  |  |                     [options?.isActive, mapProperties.zoom] | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |                 .stabilized(250) | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         neededTiles.addCallbackAndRunD((neededIndexes) => { | 
					
						
							| 
									
										
										
										
											2021-09-20 17:14:55 +02:00
										 |  |  |             for (const neededIndex of neededIndexes) { | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |                 loadedTiles.add(neededIndex) | 
					
						
							|  |  |  |                 super.addSource(constructSource(neededIndex)) | 
					
						
							| 
									
										
										
										
											2021-09-20 17:14:55 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  | } |