2022-06-05 02:24:14 +02:00
|
|
|
import {Store, UIEventSource} from "../UIEventSource";
|
2021-03-29 02:04:42 +02:00
|
|
|
import {Or} from "../Tags/Or";
|
2021-01-03 03:09:52 +01:00
|
|
|
import {Overpass} from "../Osm/Overpass";
|
2021-09-28 18:00:44 +02:00
|
|
|
import FeatureSource from "../FeatureSource/FeatureSource";
|
2021-03-20 23:45:52 +01:00
|
|
|
import {Utils} from "../../Utils";
|
2021-03-29 02:04:42 +02:00
|
|
|
import {TagsFilter} from "../Tags/TagsFilter";
|
2021-05-10 23:51:03 +02:00
|
|
|
import SimpleMetaTagger from "../SimpleMetaTagger";
|
2021-08-07 23:11:34 +02:00
|
|
|
import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig";
|
2021-09-21 02:10:42 +02:00
|
|
|
import RelationsTracker from "../Osm/RelationsTracker";
|
2021-09-28 17:30:48 +02:00
|
|
|
import {BBox} from "../BBox";
|
2021-09-30 04:13:23 +02:00
|
|
|
import Loc from "../../Models/Loc";
|
|
|
|
import LayerConfig from "../../Models/ThemeConfig/LayerConfig";
|
2021-12-21 18:35:31 +01:00
|
|
|
import Constants from "../../Models/Constants";
|
2022-01-15 02:44:11 +01:00
|
|
|
import TileFreshnessCalculator from "../FeatureSource/TileFreshnessCalculator";
|
|
|
|
import {Tiles} from "../../Models/TileRange";
|
2021-01-03 03:09:52 +01:00
|
|
|
|
2020-06-24 00:35:19 +02:00
|
|
|
|
2021-09-28 17:30:48 +02:00
|
|
|
export default class OverpassFeatureSource implements FeatureSource {
|
2021-01-03 00:19:42 +01:00
|
|
|
|
2021-05-14 02:25:30 +02:00
|
|
|
public readonly name = "OverpassFeatureSource"
|
2021-09-09 00:05:51 +02:00
|
|
|
|
2021-01-03 00:19:42 +01:00
|
|
|
/**
|
|
|
|
* The last loaded features of the geojson
|
|
|
|
*/
|
2021-02-20 01:45:51 +01:00
|
|
|
public readonly features: UIEventSource<{ feature: any, freshness: Date }[]> = new UIEventSource<any[]>(undefined);
|
2021-01-03 00:19:42 +01:00
|
|
|
|
2020-06-24 00:35:19 +02:00
|
|
|
|
|
|
|
public readonly runningQuery: UIEventSource<boolean> = new UIEventSource<boolean>(false);
|
2021-02-20 01:45:51 +01:00
|
|
|
public readonly timeout: UIEventSource<number> = new UIEventSource<number>(0);
|
2021-09-26 17:36:39 +02:00
|
|
|
|
2021-09-21 02:10:42 +02:00
|
|
|
public readonly relationsTracker: RelationsTracker;
|
2021-09-26 17:36:39 +02:00
|
|
|
|
2021-09-21 02:10:42 +02:00
|
|
|
|
2021-02-20 01:45:51 +01:00
|
|
|
private readonly retries: UIEventSource<number> = new UIEventSource<number>(0);
|
2021-10-20 01:26:39 +02:00
|
|
|
|
2021-09-21 02:10:42 +02:00
|
|
|
private readonly state: {
|
2022-06-05 02:24:14 +02:00
|
|
|
readonly locationControl: Store<Loc>,
|
2021-09-28 18:00:44 +02:00
|
|
|
readonly layoutToUse: LayoutConfig,
|
2022-06-05 02:24:14 +02:00
|
|
|
readonly overpassUrl: Store<string[]>;
|
|
|
|
readonly overpassTimeout: Store<number>;
|
|
|
|
readonly currentBounds: Store<BBox>
|
2021-09-21 02:10:42 +02:00
|
|
|
}
|
2022-06-05 02:24:14 +02:00
|
|
|
private readonly _isActive: Store<boolean>
|
2022-01-15 02:44:11 +01:00
|
|
|
/**
|
|
|
|
* Callback to handle all the data
|
|
|
|
*/
|
2021-10-11 22:30:22 +02:00
|
|
|
private readonly onBboxLoaded: (bbox: BBox, date: Date, layers: LayerConfig[], zoomlevel: number) => void;
|
2021-10-20 01:26:39 +02:00
|
|
|
|
2022-01-15 02:44:11 +01:00
|
|
|
/**
|
|
|
|
* Keeps track of how fresh the data is
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
private readonly freshnesses: Map<string, TileFreshnessCalculator>;
|
|
|
|
|
2021-01-03 00:19:42 +01:00
|
|
|
constructor(
|
2021-09-21 02:10:42 +02:00
|
|
|
state: {
|
2022-06-05 02:24:14 +02:00
|
|
|
readonly locationControl: Store<Loc>,
|
2021-09-28 18:00:44 +02:00
|
|
|
readonly layoutToUse: LayoutConfig,
|
2022-06-05 02:24:14 +02:00
|
|
|
readonly overpassUrl: Store<string[]>;
|
|
|
|
readonly overpassTimeout: Store<number>;
|
|
|
|
readonly overpassMaxZoom: Store<number>,
|
|
|
|
readonly currentBounds: Store<BBox>
|
2021-09-29 16:55:05 +02:00
|
|
|
},
|
2021-10-11 21:23:14 +02:00
|
|
|
options: {
|
2022-06-05 02:24:14 +02:00
|
|
|
padToTiles: Store<number>,
|
|
|
|
isActive?: Store<boolean>,
|
2021-09-30 04:13:23 +02:00
|
|
|
relationTracker: RelationsTracker,
|
2022-01-15 02:44:11 +01:00
|
|
|
onBboxLoaded?: (bbox: BBox, date: Date, layers: LayerConfig[], zoomlevel: number) => void,
|
|
|
|
freshnesses?: Map<string, TileFreshnessCalculator>
|
2021-09-29 16:55:05 +02:00
|
|
|
}) {
|
2021-09-21 02:10:42 +02:00
|
|
|
|
|
|
|
this.state = state
|
2021-09-28 17:30:48 +02:00
|
|
|
this._isActive = options.isActive;
|
2021-09-30 04:13:23 +02:00
|
|
|
this.onBboxLoaded = options.onBboxLoaded
|
2021-09-28 17:30:48 +02:00
|
|
|
this.relationsTracker = options.relationTracker
|
2022-01-15 02:44:11 +01:00
|
|
|
this.freshnesses = options.freshnesses
|
2020-06-24 00:35:19 +02:00
|
|
|
const self = this;
|
2021-09-28 17:30:48 +02:00
|
|
|
state.currentBounds.addCallback(_ => {
|
2021-10-11 22:30:22 +02:00
|
|
|
self.update(options.padToTiles.data)
|
2021-09-03 13:48:04 +02:00
|
|
|
})
|
2021-09-29 16:55:05 +02:00
|
|
|
|
2021-01-02 16:04:16 +01:00
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
|
2021-09-30 04:13:23 +02:00
|
|
|
private GetFilter(interpreterUrl: string, layersToDownload: LayerConfig[]): Overpass {
|
2021-03-20 23:45:52 +01:00
|
|
|
let filters: TagsFilter[] = [];
|
|
|
|
let extraScripts: string[] = [];
|
2021-09-30 04:13:23 +02:00
|
|
|
for (const layer of layersToDownload) {
|
2021-03-20 23:45:52 +01:00
|
|
|
if (layer.source.overpassScript !== undefined) {
|
|
|
|
extraScripts.push(layer.source.overpassScript)
|
|
|
|
} else {
|
|
|
|
filters.push(layer.source.osmTags);
|
|
|
|
}
|
2020-07-31 16:17:16 +02:00
|
|
|
}
|
2021-03-20 23:45:52 +01:00
|
|
|
filters = Utils.NoNull(filters)
|
|
|
|
extraScripts = Utils.NoNull(extraScripts)
|
|
|
|
if (filters.length + extraScripts.length === 0) {
|
2020-07-31 16:17:16 +02:00
|
|
|
return undefined;
|
|
|
|
}
|
2021-09-29 16:55:05 +02:00
|
|
|
return new Overpass(new Or(filters), extraScripts, interpreterUrl, this.state.overpassTimeout, this.relationsTracker);
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
2021-02-20 01:45:51 +01:00
|
|
|
|
2021-10-11 22:30:22 +02:00
|
|
|
private update(paddedZoomLevel: number) {
|
2021-09-29 16:55:05 +02:00
|
|
|
if (!this._isActive.data) {
|
2021-09-28 17:30:48 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-09-30 04:13:23 +02:00
|
|
|
const self = this;
|
2021-10-11 22:30:22 +02:00
|
|
|
this.updateAsync(paddedZoomLevel).then(bboxDate => {
|
2021-10-20 01:26:39 +02:00
|
|
|
if (bboxDate === undefined || self.onBboxLoaded === undefined) {
|
2021-09-28 17:30:48 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-09-30 04:13:23 +02:00
|
|
|
const [bbox, date, layers] = bboxDate
|
2021-10-11 22:30:22 +02:00
|
|
|
self.onBboxLoaded(bbox, date, layers, paddedZoomLevel)
|
2021-09-26 17:36:39 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-11 22:30:22 +02:00
|
|
|
private async updateAsync(padToZoomLevel: number): Promise<[BBox, Date, LayerConfig[]]> {
|
2020-06-24 00:35:19 +02:00
|
|
|
if (this.runningQuery.data) {
|
2021-04-21 01:25:00 +02:00
|
|
|
console.log("Still running a query, not updating");
|
2021-09-28 17:30:48 +02:00
|
|
|
return undefined;
|
2021-04-21 01:25:00 +02:00
|
|
|
}
|
2021-04-23 12:55:38 +02:00
|
|
|
|
|
|
|
if (this.timeout.data > 0) {
|
2021-04-21 01:25:00 +02:00
|
|
|
console.log("Still in timeout - not updating")
|
2021-09-28 17:30:48 +02:00
|
|
|
return undefined;
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
2020-07-30 00:59:08 +02:00
|
|
|
|
2021-10-20 01:26:39 +02:00
|
|
|
let data: any = undefined
|
|
|
|
let date: Date = undefined
|
|
|
|
let lastUsed = 0;
|
2021-11-07 16:34:51 +01:00
|
|
|
|
2021-10-20 01:26:39 +02:00
|
|
|
|
2021-09-30 04:13:23 +02:00
|
|
|
const layersToDownload = []
|
2022-01-15 02:44:11 +01:00
|
|
|
const neededTiles = this.state.currentBounds.data.expandToTileBounds(padToZoomLevel).containingTileRange(padToZoomLevel)
|
2021-09-30 04:13:23 +02:00
|
|
|
for (const layer of this.state.layoutToUse.layers) {
|
2021-10-20 01:26:39 +02:00
|
|
|
|
|
|
|
if (typeof (layer) === "string") {
|
|
|
|
throw "A layer was not expanded!"
|
|
|
|
}
|
2022-01-15 02:44:11 +01:00
|
|
|
if (Constants.priviliged_layers.indexOf(layer.id) >= 0) {
|
2021-11-16 02:57:26 +01:00
|
|
|
continue
|
|
|
|
}
|
2021-10-20 01:26:39 +02:00
|
|
|
if (this.state.locationControl.data.zoom < layer.minzoom) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (layer.doNotDownload) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (layer.source.geojsonSource !== undefined) {
|
|
|
|
// Not our responsibility to download this layer!
|
|
|
|
continue;
|
|
|
|
}
|
2022-01-15 02:44:11 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-10-20 01:26:39 +02:00
|
|
|
layersToDownload.push(layer)
|
2021-09-30 04:13:23 +02:00
|
|
|
}
|
2021-09-26 17:36:39 +02:00
|
|
|
|
2022-01-15 02:44:11 +01:00
|
|
|
if (layersToDownload.length == 0) {
|
|
|
|
console.debug("Not updating - no layers needed")
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-20 01:26:39 +02:00
|
|
|
const self = this;
|
2021-09-29 16:55:05 +02:00
|
|
|
const overpassUrls = self.state.overpassUrl.data
|
2021-11-07 16:34:51 +01:00
|
|
|
let bounds: BBox
|
2021-09-26 17:36:39 +02:00
|
|
|
do {
|
|
|
|
try {
|
2021-10-20 01:26:39 +02:00
|
|
|
|
|
|
|
bounds = this.state.currentBounds.data?.pad(this.state.layoutToUse.widenFactor)?.expandToTileBounds(padToZoomLevel);
|
|
|
|
|
|
|
|
if (bounds === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2021-09-30 04:13:23 +02:00
|
|
|
const overpass = this.GetFilter(overpassUrls[lastUsed], layersToDownload);
|
2021-09-29 16:55:05 +02:00
|
|
|
|
|
|
|
if (overpass === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
this.runningQuery.setData(true);
|
|
|
|
|
|
|
|
[data, date] = await overpass.queryGeoJson(bounds)
|
2021-09-28 17:30:48 +02:00
|
|
|
console.log("Querying overpass is done", data)
|
2021-09-26 17:36:39 +02:00
|
|
|
} catch (e) {
|
2021-01-03 00:19:42 +01:00
|
|
|
self.retries.data++;
|
|
|
|
self.retries.ping();
|
2021-09-29 16:55:05 +02:00
|
|
|
console.error(`QUERY FAILED due to`, e);
|
|
|
|
|
|
|
|
await Utils.waitFor(1000)
|
|
|
|
|
|
|
|
if (lastUsed + 1 < overpassUrls.length) {
|
|
|
|
lastUsed++
|
|
|
|
console.log("Trying next time with", overpassUrls[lastUsed])
|
|
|
|
} else {
|
|
|
|
lastUsed = 0
|
|
|
|
self.timeout.setData(self.retries.data * 5);
|
|
|
|
|
|
|
|
while (self.timeout.data > 0) {
|
|
|
|
await Utils.waitFor(1000)
|
|
|
|
console.log(self.timeout.data)
|
|
|
|
self.timeout.data--
|
|
|
|
self.timeout.ping();
|
|
|
|
}
|
2021-02-20 01:45:51 +01:00
|
|
|
}
|
2021-09-26 17:36:39 +02:00
|
|
|
}
|
2021-10-20 01:26:39 +02:00
|
|
|
} while (data === undefined && this._isActive.data);
|
2021-03-20 23:45:52 +01:00
|
|
|
|
2021-11-07 16:34:51 +01:00
|
|
|
|
2021-09-26 17:36:39 +02:00
|
|
|
try {
|
2021-11-07 16:34:51 +01:00
|
|
|
if (data === undefined) {
|
2021-10-20 19:12:28 +02:00
|
|
|
return undefined
|
|
|
|
}
|
2021-12-12 17:35:08 +01:00
|
|
|
data.features.forEach(feature => SimpleMetaTagger.objectMetaInfo.applyMetaTagsOnFeature(feature, date, undefined, this.state));
|
2021-09-26 17:36:39 +02:00
|
|
|
self.features.setData(data.features.map(f => ({feature: f, freshness: date})));
|
2021-09-30 04:13:23 +02:00
|
|
|
return [bounds, date, layersToDownload];
|
2021-09-26 17:36:39 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.error("Got the overpass response, but could not process it: ", e, e.stack)
|
2021-10-20 19:12:28 +02:00
|
|
|
return undefined
|
2021-09-29 16:55:05 +02:00
|
|
|
} finally {
|
2021-10-20 19:12:28 +02:00
|
|
|
self.retries.setData(0);
|
2021-09-28 17:30:48 +02:00
|
|
|
self.runningQuery.setData(false);
|
2021-09-26 17:36:39 +02:00
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|