| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  | import { Store } from "../../UIEventSource" | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import DynamicTileSource from "./DynamicTileSource" | 
					
						
							|  |  |  | import { Utils } from "../../../Utils" | 
					
						
							|  |  |  | import GeoJsonSource from "../Sources/GeoJsonSource" | 
					
						
							|  |  |  | import { BBox } from "../../BBox" | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  | import LayerConfig from "../../../Models/ThemeConfig/LayerConfig" | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default class DynamicGeoJsonTileSource extends DynamicTileSource { | 
					
						
							| 
									
										
										
										
											2021-12-13 13:22:23 +01:00
										 |  |  |     private static whitelistCache = new Map<string, any>() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     constructor( | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |         layer: LayerConfig, | 
					
						
							|  |  |  |         mapProperties: { | 
					
						
							|  |  |  |             zoom: Store<number> | 
					
						
							|  |  |  |             bounds: Store<BBox> | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         options?: { | 
					
						
							|  |  |  |             isActive?: Store<boolean> | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |         const source = layer.source | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |         if (source.geojsonZoomLevel === undefined) { | 
					
						
							|  |  |  |             throw "Invalid layer: geojsonZoomLevel expected" | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (source.geojsonSource === undefined) { | 
					
						
							|  |  |  |             throw "Invalid layer: geojsonSource expected" | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-05-24 03:26:33 +02:00
										 |  |  |         console.log("Creating a dynamic geojson source for", layer.source.geojsonSource) | 
					
						
							| 
									
										
										
										
											2021-10-27 03:52:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |         let whitelist = undefined | 
					
						
							| 
									
										
										
										
											2021-10-27 03:52:19 +02:00
										 |  |  |         if (source.geojsonSource.indexOf("{x}_{y}.geojson") > 0) { | 
					
						
							|  |  |  |             const whitelistUrl = source.geojsonSource | 
					
						
							|  |  |  |                 .replace("{z}", "" + source.geojsonZoomLevel) | 
					
						
							|  |  |  |                 .replace("{x}_{y}.geojson", "overview.json") | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |                 .replace("{layer}", layer.id) | 
					
						
							| 
									
										
										
										
											2021-10-27 03:52:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-13 13:22:23 +01:00
										 |  |  |             if (DynamicGeoJsonTileSource.whitelistCache.has(whitelistUrl)) { | 
					
						
							|  |  |  |                 whitelist = DynamicGeoJsonTileSource.whitelistCache.get(whitelistUrl) | 
					
						
							|  |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 Utils.downloadJsonCached(whitelistUrl, 1000 * 60 * 60) | 
					
						
							|  |  |  |                     .then((json) => { | 
					
						
							|  |  |  |                         const data = new Map<number, Set<number>>() | 
					
						
							| 
									
										
										
										
											2021-12-13 13:22:23 +01:00
										 |  |  |                         for (const x in json) { | 
					
						
							| 
									
										
										
										
											2022-01-26 21:40:38 +01:00
										 |  |  |                             if (x === "zoom") { | 
					
						
							| 
									
										
										
										
											2021-12-13 13:22:23 +01:00
										 |  |  |                                 continue | 
					
						
							|  |  |  |                             } | 
					
						
							|  |  |  |                             data.set(Number(x), new Set(json[x])) | 
					
						
							|  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                         console.log( | 
					
						
							|  |  |  |                             "The whitelist is", | 
					
						
							|  |  |  |                             data, | 
					
						
							|  |  |  |                             "based on ", | 
					
						
							|  |  |  |                             json, | 
					
						
							|  |  |  |                             "from", | 
					
						
							|  |  |  |                             whitelistUrl | 
					
						
							|  |  |  |                         ) | 
					
						
							| 
									
										
										
										
											2021-12-13 13:22:23 +01:00
										 |  |  |                         whitelist = data | 
					
						
							|  |  |  |                         DynamicGeoJsonTileSource.whitelistCache.set(whitelistUrl, whitelist) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     }) | 
					
						
							|  |  |  |                     .catch((err) => { | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |                         console.warn("No whitelist found for ", layer.id, err) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     }) | 
					
						
							| 
									
										
										
										
											2021-12-13 13:22:23 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-10-27 03:52:19 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const blackList = new Set<string>() | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |         super( | 
					
						
							|  |  |  |             source.geojsonZoomLevel, | 
					
						
							|  |  |  |             (zxy) => { | 
					
						
							| 
									
										
										
										
											2021-10-27 03:52:19 +02:00
										 |  |  |                 if (whitelist !== undefined) { | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |                     const isWhiteListed = whitelist.get(zxy[1])?.has(zxy[2]) | 
					
						
							| 
									
										
										
										
											2021-10-27 03:52:19 +02:00
										 |  |  |                     if (!isWhiteListed) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                         console.debug( | 
					
						
							|  |  |  |                             "Not downloading tile", | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  |                             zxy, | 
					
						
							|  |  |  |                             "for layer", | 
					
						
							|  |  |  |                             layer.id, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                             "as it is not on the whitelist" | 
					
						
							|  |  |  |                         ) | 
					
						
							|  |  |  |                         return undefined | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-10-27 03:52:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |                 return new GeoJsonSource(layer, { | 
					
						
							|  |  |  |                     zxy, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     featureIdBlacklist: blackList, | 
					
						
							| 
									
										
										
										
											2023-04-06 01:33:08 +02:00
										 |  |  |                     isActive: options?.isActive, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 }) | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |             }, | 
					
						
							| 
									
										
										
										
											2023-03-26 05:58:28 +02:00
										 |  |  |             mapProperties, | 
					
						
							| 
									
										
										
										
											2023-03-28 05:13:48 +02:00
										 |  |  |             { | 
					
						
							|  |  |  |                 isActive: options?.isActive, | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | } |