mapcomplete/Logic/UpdateFromOverpass.ts

182 lines
6.2 KiB
TypeScript
Raw Normal View History

import {Or, TagsFilter} from "./Tags";
import {UIEventSource} from "./UIEventSource";
2021-01-03 00:19:42 +01:00
import Bounds from "../Models/Bounds";
2020-07-30 00:59:08 +02:00
import {Overpass} from "./Osm/Overpass";
2021-01-03 00:19:42 +01:00
import Loc from "../Models/Loc";
import LayoutConfig from "../Customizations/JSON/LayoutConfig";
import FeatureSource from "./Actors/FeatureSource";
2020-06-24 00:35:19 +02:00
2021-01-03 00:19:42 +01:00
export default class UpdateFromOverpass implements FeatureSource{
/**
* The last loaded features of the geojson
*/
public readonly features: UIEventSource<any[]> = new UIEventSource<any[]>(undefined);
/**
* The time of updating according to Overpass
*/
public readonly freshness:UIEventSource<Date> = new UIEventSource<Date>(undefined);
2020-06-24 00:35:19 +02:00
public readonly sufficientlyZoomed: UIEventSource<boolean>;
2020-06-24 00:35:19 +02:00
public readonly runningQuery: UIEventSource<boolean> = new UIEventSource<boolean>(false);
public readonly retries: UIEventSource<number> = new UIEventSource<number>(0);
2021-01-03 00:19:42 +01:00
/**
* The previous bounds for which the query has been run at the given zoom level
*
* Note that some layers only activate on a certain zoom level.
* If the map location changes, we check for each layer if it is loaded:
* we start checking the bounds at the first zoom level the layer might operate. If in bounds - no reload needed, otherwise we continue walking down
2020-06-24 00:35:19 +02:00
*/
2021-01-03 00:19:42 +01:00
private readonly _previousBounds: Map<number, Bounds[]> = new Map<number, Bounds[]>();
private readonly _location: UIEventSource<Loc>;
private readonly _layoutToUse: UIEventSource<LayoutConfig>;
private readonly _leafletMap: UIEventSource<L.Map>;
2020-06-24 00:35:19 +02:00
/**
* The most important layer should go first, as that one gets first pick for the questions
*/
2021-01-03 00:19:42 +01:00
constructor(
location: UIEventSource<Loc>,
layoutToUse: UIEventSource<LayoutConfig>,
leafletMap: UIEventSource<L.Map>) {
this._location = location;
this._layoutToUse = layoutToUse;
this._leafletMap = leafletMap;
2020-06-24 00:35:19 +02:00
const self = this;
2021-01-03 00:19:42 +01:00
this.sufficientlyZoomed = location.map(location => {
if(location?.zoom === undefined){
return false;
}
2021-01-03 00:19:42 +01:00
let minzoom = Math.min(...layoutToUse.data.layers.map(layer => layer.minzoom ?? 18));
return location.zoom >= minzoom;
2021-01-03 00:19:42 +01:00
}, [layoutToUse]
);
for (let i = 0; i < 25; i++) {
// This update removes all data on all layers -> erase the map on lower levels too
2021-01-03 00:19:42 +01:00
this._previousBounds.set(i, []);
}
2021-01-03 00:19:42 +01:00
layoutToUse.addCallback(() => {
self.update()
2020-06-24 00:35:19 +02:00
});
2021-01-03 00:19:42 +01:00
location.addCallbackAndRun(() => {
self.update()
2020-07-31 16:17:16 +02:00
});
}
public ForceRefresh() {
for (let i = 0; i < 25; i++) {
2021-01-03 00:19:42 +01:00
this._previousBounds.set(i, []);
}
2021-01-03 00:19:42 +01:00
this.update();
}
2020-06-24 00:35:19 +02:00
2021-01-03 00:19:42 +01:00
private GetFilter() {
const filters: TagsFilter[] = [];
2021-01-03 00:19:42 +01:00
for (const layer of this._layoutToUse.data.layers) {
2020-09-02 11:37:34 +02:00
if(typeof(layer) === "string"){
continue;
}
2021-01-03 00:19:42 +01:00
if (this._location.data.zoom < layer.minzoom) {
continue;
}
2020-11-17 02:22:48 +01:00
if(layer.doNotDownload){
continue;
}
// Check if data for this layer has already been loaded
let previouslyLoaded = false;
for (let z = layer.minzoom; z < 25 && !previouslyLoaded; z++) {
2021-01-03 00:19:42 +01:00
const previousLoadedBounds = this._previousBounds.get(z);
if (previousLoadedBounds === undefined) {
continue;
}
for (const previousLoadedBound of previousLoadedBounds) {
2021-01-03 00:19:42 +01:00
previouslyLoaded = previouslyLoaded || this.IsInBounds(previousLoadedBound);
if(previouslyLoaded){
break;
}
}
}
if (previouslyLoaded) {
continue;
2020-07-31 16:17:16 +02:00
}
2020-10-27 01:01:34 +01:00
filters.push(layer.overpassTags);
2020-07-31 16:17:16 +02:00
}
if (filters.length === 0) {
return undefined;
}
return new Or(filters);
2020-06-24 00:35:19 +02:00
}
2021-01-03 00:19:42 +01:00
private update(): void {
const filter = this.GetFilter();
2020-07-31 16:17:16 +02:00
if (filter === undefined) {
2020-06-24 00:35:19 +02:00
return;
}
if (this.runningQuery.data) {
console.log("Still running a query, skip");
2020-07-31 16:17:16 +02:00
return;
2020-06-24 00:35:19 +02:00
}
2020-07-30 00:59:08 +02:00
2021-01-03 00:19:42 +01:00
const bounds = this._leafletMap.data.getBounds();
2020-07-31 16:17:16 +02:00
2021-01-03 00:19:42 +01:00
const diff = this._layoutToUse.data.widenFactor;
2020-07-30 00:59:08 +02:00
const n = Math.min(90, bounds.getNorth() + diff);
const e = Math.min(180, bounds.getEast() + diff);
const s = Math.max(-90, bounds.getSouth() - diff);
const w = Math.max(-180, bounds.getWest() - diff);
const queryBounds = {north: n, east: e, south: s, west: w};
2020-07-30 00:59:08 +02:00
2021-01-03 00:19:42 +01:00
const z = Math.floor(this._location.data.zoom);
2020-07-31 16:17:16 +02:00
2020-06-24 00:35:19 +02:00
this.runningQuery.setData(true);
const self = this;
2020-07-31 16:17:16 +02:00
const overpass = new Overpass(filter);
overpass.queryGeoJson(queryBounds,
2021-01-03 00:19:42 +01:00
function (data, date) {
self._previousBounds.get(z).push(queryBounds);
self.retries.setData(0);
self.freshness.setData(date);
self.features.setData(data.features);
self.runningQuery.setData(false);
2020-06-24 00:35:19 +02:00
},
function (reason) {
2021-01-03 00:19:42 +01:00
self.retries.data++;
self.ForceRefresh();
console.log(`QUERY FAILED (retrying in ${5 * self.retries.data} sec)`, undefined);
self.retries.ping();
self.runningQuery.setData(false)
window?.setTimeout(
function () {
self.update()
}, self.retries.data * 5000
)
2020-06-24 00:35:19 +02:00
}
);
2020-10-12 01:47:46 +02:00
2020-06-24 00:35:19 +02:00
}
2021-01-03 00:19:42 +01:00
private IsInBounds(bounds: Bounds): boolean {
if (this._previousBounds === undefined) {
2020-06-24 00:35:19 +02:00
return false;
}
2021-01-03 00:19:42 +01:00
const b = this._leafletMap.data.getBounds();
return b.getSouth() >= bounds.south &&
b.getNorth() <= bounds.north &&
b.getEast() <= bounds.east &&
b.getWest() >= bounds.west;
2020-06-24 00:35:19 +02:00
}
2020-07-31 16:17:16 +02:00
2020-06-24 00:35:19 +02:00
}