From cbc4e6eb15a2a6d7f9693a24f35839886b4f3544 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Tue, 23 May 2023 01:16:45 +0200 Subject: [PATCH] Fix: actually load geojson layers; formatting --- Logic/FeatureSource/Sources/GeoJsonSource.ts | 30 +++++++++++++------- Logic/FeatureSource/Sources/LayoutSource.ts | 4 +-- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/Logic/FeatureSource/Sources/GeoJsonSource.ts b/Logic/FeatureSource/Sources/GeoJsonSource.ts index 96ed0b1f4..79c54c7d1 100644 --- a/Logic/FeatureSource/Sources/GeoJsonSource.ts +++ b/Logic/FeatureSource/Sources/GeoJsonSource.ts @@ -1,14 +1,14 @@ /** * Fetches a geojson file somewhere and passes it along */ -import { Store, UIEventSource } from "../../UIEventSource" -import { Utils } from "../../../Utils" -import { FeatureSource } from "../FeatureSource" -import { BBox } from "../../BBox" -import { GeoOperations } from "../../GeoOperations" -import { Feature } from "geojson" +import {Store, UIEventSource} from "../../UIEventSource" +import {Utils} from "../../../Utils" +import {FeatureSource} from "../FeatureSource" +import {BBox} from "../../BBox" +import {GeoOperations} from "../../GeoOperations" +import {Feature} from "geojson" import LayerConfig from "../../../Models/ThemeConfig/LayerConfig" -import { Tiles } from "../../../Models/TileRange" +import {Tiles} from "../../../Models/TileRange" export default class GeoJsonSource implements FeatureSource { public readonly features: Store @@ -67,7 +67,7 @@ export default class GeoJsonSource implements FeatureSource { this.LoadJSONFrom(url, eventsource, layer) .then((_) => console.log("Loaded geojson " + url)) .catch((err) => console.error("Could not load ", url, "due to", err)) - return true + return true // data is loaded, we can safely unregister }) } else { this.LoadJSONFrom(url, eventsource, layer) @@ -77,13 +77,20 @@ export default class GeoJsonSource implements FeatureSource { this.features = eventsource } + /** + * 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, - layer: LayerConfig - ): Promise { + layer: LayerConfig, + options?: { + maxCacheAgeSec?: number | 300 + } + ): Promise { const self = this - let json = await Utils.downloadJsonCached(url, 60 * 60) + let json = await Utils.downloadJsonCached(url, (options?.maxCacheAgeSec ?? 300) * 1000) if (json.features === undefined || json.features === null) { json.features = [] @@ -134,5 +141,6 @@ export default class GeoJsonSource implements FeatureSource { } eventSource.setData(newFeatures) + return newFeatures } } diff --git a/Logic/FeatureSource/Sources/LayoutSource.ts b/Logic/FeatureSource/Sources/LayoutSource.ts index bfd7b8641..79406084d 100644 --- a/Logic/FeatureSource/Sources/LayoutSource.ts +++ b/Logic/FeatureSource/Sources/LayoutSource.ts @@ -63,8 +63,6 @@ export default class LayoutSource extends FeatureSourceMerger { LayoutSource.setupGeojsonSource(l, mapProperties, isDisplayed(l.id)) ) - const expiryInSeconds = Math.min(...(layers?.map((l) => l.maxAgeOfCache) ?? [])) - super(overpassSource, osmApiSource, ...geojsonSources, ...fromCache) const self = this @@ -84,7 +82,7 @@ export default class LayoutSource extends FeatureSourceMerger { ): FeatureSource { const source = layer.source isActive = mapProperties.zoom.map( - (z) => (isActive?.data ?? true) && z >= layer.maxzoom, + (z) => (isActive?.data ?? true) && z >= layer.minzoom, [isActive] ) if (source.geojsonZoomLevel === undefined) {