The overpassfeaturesource now only fetches layers that must be updated, fix #599

This commit is contained in:
Pieter Vander Vennet 2022-01-15 02:44:11 +01:00
parent 9b88478804
commit 00e5ce0b02
7 changed files with 56 additions and 10 deletions

View file

@ -11,6 +11,8 @@ import {BBox} from "../BBox";
import Loc from "../../Models/Loc";
import LayerConfig from "../../Models/ThemeConfig/LayerConfig";
import Constants from "../../Models/Constants";
import TileFreshnessCalculator from "../FeatureSource/TileFreshnessCalculator";
import {Tiles} from "../../Models/TileRange";
export default class OverpassFeatureSource implements FeatureSource {
@ -38,9 +40,18 @@ export default class OverpassFeatureSource implements FeatureSource {
readonly overpassTimeout: UIEventSource<number>;
readonly currentBounds: UIEventSource<BBox>
}
private readonly _isActive: UIEventSource<boolean>;
private readonly _isActive: UIEventSource<boolean>
/**
* Callback to handle all the data
*/
private readonly onBboxLoaded: (bbox: BBox, date: Date, layers: LayerConfig[], zoomlevel: number) => void;
/**
* Keeps track of how fresh the data is
* @private
*/
private readonly freshnesses: Map<string, TileFreshnessCalculator>;
constructor(
state: {
readonly locationControl: UIEventSource<Loc>,
@ -54,13 +65,15 @@ export default class OverpassFeatureSource implements FeatureSource {
padToTiles: UIEventSource<number>,
isActive?: UIEventSource<boolean>,
relationTracker: RelationsTracker,
onBboxLoaded?: (bbox: BBox, date: Date, layers: LayerConfig[], zoomlevel: number) => void
onBboxLoaded?: (bbox: BBox, date: Date, layers: LayerConfig[], zoomlevel: number) => void,
freshnesses?: Map<string, TileFreshnessCalculator>
}) {
this.state = state
this._isActive = options.isActive;
this.onBboxLoaded = options.onBboxLoaded
this.relationsTracker = options.relationTracker
this.freshnesses = options.freshnesses
const self = this;
state.currentBounds.addCallback(_ => {
self.update(options.padToTiles.data)
@ -117,12 +130,13 @@ export default class OverpassFeatureSource implements FeatureSource {
const layersToDownload = []
const neededTiles = this.state.currentBounds.data.expandToTileBounds(padToZoomLevel).containingTileRange(padToZoomLevel)
for (const layer of this.state.layoutToUse.layers) {
if (typeof (layer) === "string") {
throw "A layer was not expanded!"
}
if(Constants.priviliged_layers.indexOf(layer.id) >= 0){
if (Constants.priviliged_layers.indexOf(layer.id) >= 0) {
continue
}
if (this.state.locationControl.data.zoom < layer.minzoom) {
@ -135,9 +149,32 @@ export default class OverpassFeatureSource implements FeatureSource {
// Not our responsibility to download this layer!
continue;
}
const freshness = this.freshnesses?.get(layer.id)
if (freshness !== undefined) {
const oldestDataDate = Math.min(...Tiles.MapRange(neededTiles, (x, y) => {
const date = freshness.freshnessFor(padToZoomLevel, x, y);
if (date === undefined) {
return 0
}
return date.getTime()
})) / 1000;
const now = new Date().getTime()
const minRequiredAge = (now / 1000) - layer.maxAgeOfCache
if (oldestDataDate >= minRequiredAge) {
// still fresh enough - not updating
continue
}
}
layersToDownload.push(layer)
}
if (layersToDownload.length == 0) {
console.debug("Not updating - no layers needed")
return;
}
const self = this;
const overpassUrls = self.state.overpassUrl.data
let bounds: BBox