Fix: actually load geojson layers; formatting
This commit is contained in:
parent
f0d9365e06
commit
cbc4e6eb15
2 changed files with 20 additions and 14 deletions
|
@ -1,14 +1,14 @@
|
||||||
/**
|
/**
|
||||||
* Fetches a geojson file somewhere and passes it along
|
* Fetches a geojson file somewhere and passes it along
|
||||||
*/
|
*/
|
||||||
import { Store, UIEventSource } from "../../UIEventSource"
|
import {Store, UIEventSource} from "../../UIEventSource"
|
||||||
import { Utils } from "../../../Utils"
|
import {Utils} from "../../../Utils"
|
||||||
import { FeatureSource } from "../FeatureSource"
|
import {FeatureSource} from "../FeatureSource"
|
||||||
import { BBox } from "../../BBox"
|
import {BBox} from "../../BBox"
|
||||||
import { GeoOperations } from "../../GeoOperations"
|
import {GeoOperations} from "../../GeoOperations"
|
||||||
import { Feature } from "geojson"
|
import {Feature} from "geojson"
|
||||||
import LayerConfig from "../../../Models/ThemeConfig/LayerConfig"
|
import LayerConfig from "../../../Models/ThemeConfig/LayerConfig"
|
||||||
import { Tiles } from "../../../Models/TileRange"
|
import {Tiles} from "../../../Models/TileRange"
|
||||||
|
|
||||||
export default class GeoJsonSource implements FeatureSource {
|
export default class GeoJsonSource implements FeatureSource {
|
||||||
public readonly features: Store<Feature[]>
|
public readonly features: Store<Feature[]>
|
||||||
|
@ -67,7 +67,7 @@ export default class GeoJsonSource implements FeatureSource {
|
||||||
this.LoadJSONFrom(url, eventsource, layer)
|
this.LoadJSONFrom(url, eventsource, layer)
|
||||||
.then((_) => console.log("Loaded geojson " + url))
|
.then((_) => console.log("Loaded geojson " + url))
|
||||||
.catch((err) => console.error("Could not load ", url, "due to", err))
|
.catch((err) => console.error("Could not load ", url, "due to", err))
|
||||||
return true
|
return true // data is loaded, we can safely unregister
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
this.LoadJSONFrom(url, eventsource, layer)
|
this.LoadJSONFrom(url, eventsource, layer)
|
||||||
|
@ -77,13 +77,20 @@ export default class GeoJsonSource implements FeatureSource {
|
||||||
this.features = eventsource
|
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(
|
private async LoadJSONFrom(
|
||||||
url: string,
|
url: string,
|
||||||
eventSource: UIEventSource<Feature[]>,
|
eventSource: UIEventSource<Feature[]>,
|
||||||
layer: LayerConfig
|
layer: LayerConfig,
|
||||||
): Promise<void> {
|
options?: {
|
||||||
|
maxCacheAgeSec?: number | 300
|
||||||
|
}
|
||||||
|
): Promise<Feature[]> {
|
||||||
const self = this
|
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) {
|
if (json.features === undefined || json.features === null) {
|
||||||
json.features = []
|
json.features = []
|
||||||
|
@ -134,5 +141,6 @@ export default class GeoJsonSource implements FeatureSource {
|
||||||
}
|
}
|
||||||
|
|
||||||
eventSource.setData(newFeatures)
|
eventSource.setData(newFeatures)
|
||||||
|
return newFeatures
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,8 +63,6 @@ export default class LayoutSource extends FeatureSourceMerger {
|
||||||
LayoutSource.setupGeojsonSource(l, mapProperties, isDisplayed(l.id))
|
LayoutSource.setupGeojsonSource(l, mapProperties, isDisplayed(l.id))
|
||||||
)
|
)
|
||||||
|
|
||||||
const expiryInSeconds = Math.min(...(layers?.map((l) => l.maxAgeOfCache) ?? []))
|
|
||||||
|
|
||||||
super(overpassSource, osmApiSource, ...geojsonSources, ...fromCache)
|
super(overpassSource, osmApiSource, ...geojsonSources, ...fromCache)
|
||||||
|
|
||||||
const self = this
|
const self = this
|
||||||
|
@ -84,7 +82,7 @@ export default class LayoutSource extends FeatureSourceMerger {
|
||||||
): FeatureSource {
|
): FeatureSource {
|
||||||
const source = layer.source
|
const source = layer.source
|
||||||
isActive = mapProperties.zoom.map(
|
isActive = mapProperties.zoom.map(
|
||||||
(z) => (isActive?.data ?? true) && z >= layer.maxzoom,
|
(z) => (isActive?.data ?? true) && z >= layer.minzoom,
|
||||||
[isActive]
|
[isActive]
|
||||||
)
|
)
|
||||||
if (source.geojsonZoomLevel === undefined) {
|
if (source.geojsonZoomLevel === undefined) {
|
||||||
|
|
Loading…
Reference in a new issue