| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  | import FilteredLayer from "../../../Models/FilteredLayer"; | 
					
						
							| 
									
										
										
										
											2021-09-26 17:36:39 +02:00
										 |  |  | import {FeatureSourceForLayer, Tiled} from "../FeatureSource"; | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  | import {UIEventSource} from "../../UIEventSource"; | 
					
						
							|  |  |  | import Loc from "../../../Models/Loc"; | 
					
						
							|  |  |  | import DynamicTileSource from "./DynamicTileSource"; | 
					
						
							|  |  |  | import {Utils} from "../../../Utils"; | 
					
						
							|  |  |  | import GeoJsonSource from "../Sources/GeoJsonSource"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default class DynamicGeoJsonTileSource extends DynamicTileSource { | 
					
						
							|  |  |  |     constructor(layer: FilteredLayer, | 
					
						
							| 
									
										
										
										
											2021-09-26 17:36:39 +02:00
										 |  |  |                 registerLayer: (layer: FeatureSourceForLayer & Tiled) => void, | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |                 state: { | 
					
						
							|  |  |  |                     locationControl: UIEventSource<Loc> | 
					
						
							|  |  |  |                     leafletMap: any | 
					
						
							|  |  |  |                 }) { | 
					
						
							|  |  |  |         const source = layer.layerDef.source | 
					
						
							|  |  |  |         if (source.geojsonZoomLevel === undefined) { | 
					
						
							|  |  |  |             throw "Invalid layer: geojsonZoomLevel expected" | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (source.geojsonSource === undefined) { | 
					
						
							|  |  |  |             throw "Invalid layer: geojsonSource expected" | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											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") | 
					
						
							|  |  |  |                 .replace("{layer}", layer.layerDef.id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             Utils.downloadJson(whitelistUrl).then( | 
					
						
							|  |  |  |                 json => { | 
					
						
							|  |  |  |                     const data = new Map<number, Set<number>>(); | 
					
						
							|  |  |  |                     for (const x in json) { | 
					
						
							|  |  |  |                         data.set(Number(x), new Set(json[x])) | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     console.log("The whitelist is", data, "based on ", json, "from", whitelistUrl) | 
					
						
							|  |  |  |                     whitelist = data | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-10-27 03:52:19 +02:00
										 |  |  |             ).catch(err => { | 
					
						
							|  |  |  |                 console.warn("No whitelist found for ", layer.layerDef.id, err) | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 17:48:15 +02:00
										 |  |  |         const seenIds = new Set<string>(); | 
					
						
							|  |  |  |         const blackList = new UIEventSource(seenIds) | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |         super( | 
					
						
							|  |  |  |             layer, | 
					
						
							|  |  |  |             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) { | 
					
						
							| 
									
										
										
										
											2021-10-16 02:54:22 +02:00
										 |  |  |                         console.log("Not downloading tile", ...zxy, "as it is not on the whitelist") | 
					
						
							| 
									
										
										
										
											2021-10-13 03:10:46 +02:00
										 |  |  |                         return undefined; | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-10-27 03:52:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |                 const src = new GeoJsonSource( | 
					
						
							|  |  |  |                     layer, | 
					
						
							| 
									
										
										
										
											2021-09-29 17:48:15 +02:00
										 |  |  |                     zxy, | 
					
						
							|  |  |  |                     { | 
					
						
							|  |  |  |                         featureIdBlacklist: blackList | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2021-09-29 17:48:15 +02:00
										 |  |  |                 src.features.addCallbackAndRunD(feats => { | 
					
						
							|  |  |  |                     feats.forEach(feat => seenIds.add(feat.feature.properties.id)) | 
					
						
							|  |  |  |                     blackList.ping(); | 
					
						
							|  |  |  |                 }) | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |                 registerLayer(src) | 
					
						
							|  |  |  |                 return src | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             state | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |