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 DynamicTileSource from "./DynamicTileSource"
|
|
|
|
import { Utils } from "../../../Utils"
|
|
|
|
import GeoJsonSource from "../Sources/GeoJsonSource"
|
2021-12-13 02:05:34 +01:00
|
|
|
import { BBox } from "../../BBox"
|
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>()
|
|
|
|
|
2021-09-21 02:10:42 +02:00
|
|
|
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: {
|
2022-03-24 03:11:29 +01:00
|
|
|
locationControl?: UIEventSource<{ zoom?: number }>
|
2021-12-13 02:05:34 +01:00
|
|
|
currentBounds: UIEventSource<BBox>
|
2021-09-21 02:10:42 +02:00
|
|
|
}
|
|
|
|
) {
|
|
|
|
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)
|
|
|
|
|
2021-12-13 13:22:23 +01:00
|
|
|
if (DynamicGeoJsonTileSource.whitelistCache.has(whitelistUrl)) {
|
|
|
|
whitelist = DynamicGeoJsonTileSource.whitelistCache.get(whitelistUrl)
|
|
|
|
} else {
|
|
|
|
Utils.downloadJsonCached(whitelistUrl, 1000 * 60 * 60)
|
|
|
|
.then((json) => {
|
|
|
|
const data = new Map<number, Set<number>>()
|
|
|
|
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]))
|
|
|
|
}
|
|
|
|
console.log(
|
|
|
|
"The whitelist is",
|
|
|
|
data,
|
|
|
|
"based on ",
|
|
|
|
json,
|
|
|
|
"from",
|
|
|
|
whitelistUrl
|
|
|
|
)
|
|
|
|
whitelist = data
|
|
|
|
DynamicGeoJsonTileSource.whitelistCache.set(whitelistUrl, whitelist)
|
2021-10-27 03:52:19 +02:00
|
|
|
})
|
2021-12-13 13:22:23 +01:00
|
|
|
.catch((err) => {
|
|
|
|
console.warn("No whitelist found for ", layer.layerDef.id, err)
|
|
|
|
})
|
|
|
|
}
|
2021-10-27 03:52:19 +02:00
|
|
|
}
|
2021-09-21 02:10:42 +02:00
|
|
|
|
2022-02-11 03:57:39 +01:00
|
|
|
const blackList = new Set<string>()
|
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-12-13 13:22:23 +01:00
|
|
|
console.debug(
|
|
|
|
"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, zxy, {
|
2021-09-29 17:48:15 +02:00
|
|
|
featureIdBlacklist: blackList,
|
|
|
|
})
|
2022-09-08 21:40:48 +02:00
|
|
|
|
2021-09-21 02:10:42 +02:00
|
|
|
registerLayer(src)
|
|
|
|
return src
|
|
|
|
},
|
|
|
|
state
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-13 13:22:23 +01:00
|
|
|
public static RegisterWhitelist(url: string, json: any) {
|
|
|
|
const data = new Map<number, Set<number>>()
|
|
|
|
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]))
|
|
|
|
}
|
|
|
|
DynamicGeoJsonTileSource.whitelistCache.set(url, data)
|
|
|
|
}
|
2021-09-21 02:10:42 +02:00
|
|
|
}
|