Download button: take advantage of MVT server, download button will now attempt to download everything

This commit is contained in:
Pieter Vander Vennet 2024-02-21 16:35:49 +01:00
parent bccda67e1c
commit e4eb8d6b52
21 changed files with 453 additions and 353 deletions

View file

@ -11,9 +11,14 @@ import LayerConfig from "../../../Models/ThemeConfig/LayerConfig"
import { Tiles } from "../../../Models/TileRange"
export default class GeoJsonSource implements FeatureSource {
public readonly features: Store<Feature[]>
private readonly _features: UIEventSource<Feature[]> = new UIEventSource<Feature[]>(undefined)
public readonly features: Store<Feature[]> = this._features
private readonly seenids: Set<string>
private readonly idKey?: string
private readonly url: string
private readonly layer: LayerConfig
private _isDownloaded = false
private currentlyRunning: Promise<any>
public constructor(
layer: LayerConfig,
@ -30,6 +35,7 @@ export default class GeoJsonSource implements FeatureSource {
this.idKey = layer.source.idKey
this.seenids = options?.featureIdBlacklist ?? new Set<string>()
let url = layer.source.geojsonSource.replace("{layer}", layer.id)
this.layer = layer
let zxy = options?.zxy
if (zxy !== undefined) {
let tile_bbox: BBox
@ -57,86 +63,88 @@ export default class GeoJsonSource implements FeatureSource {
.replace("{x_min}", "" + bounds.minLon)
.replace("{x_max}", "" + bounds.maxLon)
}
this.url = url
const eventsource = new UIEventSource<Feature[]>([])
if (options?.isActive !== undefined) {
options.isActive.addCallbackAndRunD(async (active) => {
if (!active) {
return
}
this.LoadJSONFrom(url, eventsource, layer)
.then((fs) => console.debug("Loaded", fs.length, "features from", url))
.catch((err) => console.warn("Could not load ", url, "due to", err))
this.updateAsync()
return true // data is loaded, we can safely unregister
})
} else {
this.LoadJSONFrom(url, eventsource, layer)
.then((fs) => console.debug("Loaded", fs.length, "features from", url))
.catch((err) => console.warn("Could not load ", url, "due to", err))
this.updateAsync()
}
this.features = eventsource
}
public async updateAsync(): Promise<void> {
if (!this.currentlyRunning) {
this.currentlyRunning = this.LoadJSONFrom()
}
await this.currentlyRunning
}
/**
* Init the download, write into the specified event source for the given layer.
* Note this method caches the requested geojson for five minutes
*/
private async LoadJSONFrom(
url: string,
eventSource: UIEventSource<Feature[]>,
layer: LayerConfig,
options?: {
maxCacheAgeSec?: number | 300
private async LoadJSONFrom(options?: { maxCacheAgeSec?: number | 300 }): Promise<Feature[]> {
if (this._isDownloaded) {
return
}
): Promise<Feature[]> {
const self = this
let json = await Utils.downloadJsonCached(url, (options?.maxCacheAgeSec ?? 300) * 1000)
const url = this.url
try {
let json = await Utils.downloadJsonCached(url, (options?.maxCacheAgeSec ?? 300) * 1000)
if (json.features === undefined || json.features === null) {
json.features = []
}
if (layer.source.mercatorCrs) {
json = GeoOperations.GeoJsonToWGS84(json)
}
const time = new Date()
const newFeatures: Feature[] = []
let i = 0
for (const feature of json.features) {
if (feature.geometry.type === "Point") {
// See https://github.com/maproulette/maproulette-backend/issues/242
feature.geometry.coordinates = feature.geometry.coordinates.map(Number)
if (json.features === undefined || json.features === null) {
json.features = []
}
const props = feature.properties
for (const key in props) {
if (props[key] === null) {
delete props[key]
if (this.layer.source.mercatorCrs) {
json = GeoOperations.GeoJsonToWGS84(json)
}
const newFeatures: Feature[] = []
let i = 0
for (const feature of json.features) {
if (feature.geometry.type === "Point") {
// See https://github.com/maproulette/maproulette-backend/issues/242
feature.geometry.coordinates = feature.geometry.coordinates.map(Number)
}
const props = feature.properties
for (const key in props) {
if (props[key] === null) {
delete props[key]
}
if (typeof props[key] !== "string") {
// Make sure all the values are string, it crashes stuff otherwise
props[key] = JSON.stringify(props[key])
}
}
if (typeof props[key] !== "string") {
// Make sure all the values are string, it crashes stuff otherwise
props[key] = JSON.stringify(props[key])
if (this.idKey !== undefined) {
props.id = props[this.idKey]
}
if (props.id === undefined) {
props.id = url + "/" + i
feature.id = url + "/" + i
i++
}
if (this.seenids.has(props.id)) {
continue
}
this.seenids.add(props.id)
newFeatures.push(feature)
}
if (self.idKey !== undefined) {
props.id = props[self.idKey]
}
if (props.id === undefined) {
props.id = url + "/" + i
feature.id = url + "/" + i
i++
}
if (self.seenids.has(props.id)) {
continue
}
self.seenids.add(props.id)
newFeatures.push(feature)
this._features.setData(newFeatures)
this._isDownloaded = true
return newFeatures
} catch (e) {
console.warn("Could not load ", url, "due to", e)
}
eventSource.setData(newFeatures)
return newFeatures
}
}