Merge files from develop branch

This commit is contained in:
Pieter Vander Vennet 2021-05-07 14:36:12 +02:00
parent 2a03b699d5
commit 3f29632cf4
16 changed files with 484 additions and 135 deletions

View file

@ -18,27 +18,32 @@ export default class FeaturePipeline implements FeatureSource {
public features: UIEventSource<{ feature: any; freshness: Date }[]>;
public readonly name = "FeaturePipeline"
public readonly name = "FeaturePipeline"
constructor(flayers: UIEventSource<{ isDisplayed: UIEventSource<boolean>, layerDef: LayerConfig }[]>,
updater: FeatureSource,
fromOsmApi: FeatureSource,
layout: UIEventSource<LayoutConfig>,
newPoints: FeatureSource,
locationControl: UIEventSource<Loc>) {
locationControl: UIEventSource<Loc>,
selectedElement: UIEventSource<any>) {
// first we metatag, then we save to get the metatags into storage too
// Note that we need to register before we do metatagging (as it expects the event sources)
const amendedOverpassSource =
new RememberingSource(
new LocalStorageSaver(
new MetaTaggingFeatureSource( // first we metatag, then we save to get the metatags into storage too
new RegisteringFeatureSource(
new FeatureDuplicatorPerLayer(flayers,
new FeatureDuplicatorPerLayer(flayers,
new MetaTaggingFeatureSource(
new RegisteringFeatureSource(
updater)
)), layout));
const geojsonSources: FeatureSource [] = GeoJsonSource
.ConstructMultiSource(flayers.data, locationControl)
.map(geojsonSource => new RegisteringFeatureSource(new FeatureDuplicatorPerLayer(flayers, geojsonSource)));
.map(geojsonSource => new RegisteringFeatureSource(new FeatureDuplicatorPerLayer(flayers, geojsonSource)));
const amendedLocalStorageSource =
new RememberingSource(new RegisteringFeatureSource(new FeatureDuplicatorPerLayer(flayers, new LocalStorageSource(layout))
));
@ -46,10 +51,16 @@ export default class FeaturePipeline implements FeatureSource {
newPoints = new MetaTaggingFeatureSource(new FeatureDuplicatorPerLayer(flayers,
new RegisteringFeatureSource(newPoints)));
const amendedOsmApiSource = new RememberingSource(
new FeatureDuplicatorPerLayer(flayers,
new MetaTaggingFeatureSource(
new RegisteringFeatureSource(fromOsmApi))));
const merged =
new FeatureSourceMerger([
amendedOverpassSource,
amendedOsmApiSource,
amendedLocalStorageSource,
newPoints,
...geojsonSources
@ -60,6 +71,7 @@ export default class FeaturePipeline implements FeatureSource {
new FilteringFeatureSource(
flayers,
locationControl,
selectedElement,
merged
));
this.features = source.features;

View file

@ -21,27 +21,48 @@ export default class FeatureSourceMerger implements FeatureSource {
}
private Update() {
let all = {}; // Mapping 'id' -> {feature, freshness}
let somethingChanged = false;
const all: Map<string, { feature: any, freshness: Date }> = new Map<string, { feature: any; freshness: Date }>();
// We seed the dictionary with the previously loaded features
const oldValues = this.features.data ?? [];
for (const oldValue of oldValues) {
all.set(oldValue.feature.id, oldValue)
}
for (const source of this._sources) {
if (source?.features?.data === undefined) {
continue;
}
for (const f of source.features.data) {
const id = f.feature.properties.id;
const oldV = all[id];
if (oldV === undefined) {
all[id] = f;
} else {
if (oldV.freshness < f.freshness) {
all[id] = f;
}
if (!all.has(id)) {
// This is a new feature
somethingChanged = true;
all.set(id, f);
continue;
}
// This value has been seen already, either in a previous run or by a previous datasource
// Let's figure out if something changed
const oldV = all.get(id);
if (oldV.freshness < f.freshness) {
// Jup, this feature is fresher
all.set(id, f);
somethingChanged = true;
}
}
}
const newList = [];
for (const id in all) {
newList.push(all[id]);
if(!somethingChanged){
// We don't bother triggering an update
return;
}
const newList = [];
all.forEach((value, key) => {
newList.push(value)
})
this.features.setData(newList);
}

View file

@ -5,12 +5,14 @@ import Loc from "../../Models/Loc";
export default class FilteringFeatureSource implements FeatureSource {
public features: UIEventSource<{ feature: any; freshness: Date }[]> = new UIEventSource<{ feature: any; freshness: Date }[]>([]);
public readonly name = "FilteringFeatureSource"
public readonly name = "FilteringFeatureSource"
constructor(layers: UIEventSource<{
isDisplayed: UIEventSource<boolean>,
layerDef: LayerConfig
}[]>,
location: UIEventSource<Loc>,
selectedElement: UIEventSource<any>,
upstream: FeatureSource) {
const self = this;
@ -18,7 +20,7 @@ public readonly name = "FilteringFeatureSource"
function update() {
const layerDict = {};
if(layers.data.length == 0){
if (layers.data.length == 0) {
throw "No layers defined!"
}
for (const layer of layers.data) {
@ -32,6 +34,11 @@ public readonly name = "FilteringFeatureSource"
const newFeatures = features.filter(f => {
const layerId = f.feature._matching_layer_id;
if(selectedElement.data !== undefined && selectedElement.data?.id === f.feature.id){
// This is the selected object - it gets a free pass even if zoom is not sufficient
return true;
}
if (layerId !== undefined) {
const layer: {
isDisplayed: UIEventSource<boolean>,
@ -41,16 +48,16 @@ public readonly name = "FilteringFeatureSource"
missingLayers.add(layerId)
return true;
}
const isShown = layer.layerDef.isShown
const tags = f.feature.properties;
if(isShown.IsKnown(tags)){
if (isShown.IsKnown(tags)) {
const result = layer.layerDef.isShown.GetRenderValue(f.feature.properties).txt;
if(result !== "yes"){
if (result !== "yes") {
return false;
}
}
if (FilteringFeatureSource.showLayer(layer, location)) {
return true;
}
@ -69,7 +76,7 @@ public readonly name = "FilteringFeatureSource"
});
console.log("Filtering layer source: input: ", upstream.features.data?.length, "output:", newFeatures.length)
self.features.setData(newFeatures);
if(missingLayers.size > 0){
if (missingLayers.size > 0) {
console.error("Some layers were not found: ", Array.from(missingLayers))
}
}
@ -86,7 +93,7 @@ public readonly name = "FilteringFeatureSource"
if (l.zoom < layer.layerDef.minzoom) {
continue;
}
if(l.zoom > layer.layerDef.maxzoom){
if (l.zoom > layer.layerDef.maxzoom) {
continue;
}
if (!layer.isDisplayed.data) {
@ -98,13 +105,13 @@ public readonly name = "FilteringFeatureSource"
}).addCallback(() => {
update();
});
layers.addCallback(update);
const registered = new Set<UIEventSource<boolean>>();
layers.addCallbackAndRun(layers => {
for (const layer of layers) {
if(registered.has(layer.isDisplayed)){
if (registered.has(layer.isDisplayed)) {
continue;
}
registered.add(layer.isDisplayed);

View file

@ -20,9 +20,9 @@ export default class GeoJsonSource implements FeatureSource {
private readonly layerId: string;
private readonly seenids: Set<string> = new Set<string>()
private constructor(locationControl: UIEventSource<Loc>,
flayer: { isDisplayed: UIEventSource<boolean>, layerDef: LayerConfig },
onFail?: ((errorMsg: any) => void)) {
private constructor(locationControl: UIEventSource<Loc>,
flayer: { isDisplayed: UIEventSource<boolean>, layerDef: LayerConfig },
onFail?: ((errorMsg: any) => void)) {
this.layerId = flayer.layerDef.id;
let url = flayer.layerDef.source.geojsonSource.replace("{layer}", flayer.layerDef.id);
this.name = "GeoJsonSource of " + url;
@ -33,7 +33,7 @@ export default class GeoJsonSource implements FeatureSource {
if (zoomLevel === undefined) {
// This is a classic, static geojson layer
if (onFail === undefined) {
onFail = errorMsg => {
onFail = _ => {
}
}
this.onFail = onFail;
@ -43,57 +43,6 @@ export default class GeoJsonSource implements FeatureSource {
this.ConfigureDynamicLayer(url, zoomLevel, locationControl, flayer)
}
}
private ConfigureDynamicLayer(url: string, zoomLevel: number, locationControl: UIEventSource<Loc>, flayer: { isDisplayed: UIEventSource<boolean>, layerDef: LayerConfig }){
// This is a dynamic template with a fixed zoom level
url = url.replace("{z}", "" + zoomLevel)
const loadedTiles = new Set<string>();
const self = this;
this.onFail = (msg, url) => {
console.warn(`Could not load geojson layer from`, url, "due to", msg)
loadedTiles.add(url); // We add the url to the 'loadedTiles' in order to not reload it in the future
}
const neededTiles = locationControl.map(
location => {
// Yup, this is cheating to just get the bounds here
const bounds = State.state.leafletMap.data.getBounds()
const tileRange = Utils.TileRangeBetween(zoomLevel, bounds.getNorth(), bounds.getEast(), bounds.getSouth(), bounds.getWest())
const needed = new Set<string>();
for (let x = tileRange.xstart; x <= tileRange.xend; x++) {
for (let y = tileRange.ystart; y <= tileRange.yend; y++) {
let neededUrl = url.replace("{x}", "" + x).replace("{y}", "" + y);
needed.add(neededUrl)
}
}
return needed;
}
, [flayer.isDisplayed]);
neededTiles.stabilized(250).addCallback((needed: Set<string>) => {
if (needed === undefined) {
return;
}
if (!flayer.isDisplayed.data) {
// No need to download! - the layer is disabled
return;
}
if(locationControl.data.zoom < flayer.layerDef.minzoom){
return;
}
needed.forEach(neededTile => {
if (loadedTiles.has(neededTile)) {
return;
}
loadedTiles.add(neededTile)
self.LoadJSONFrom(neededTile)
})
})
}
/**
* Merges together the layers which have the same source
@ -152,6 +101,53 @@ export default class GeoJsonSource implements FeatureSource {
}
private ConfigureDynamicLayer(url: string, zoomLevel: number, locationControl: UIEventSource<Loc>, flayer: { isDisplayed: UIEventSource<boolean>, layerDef: LayerConfig }) {
// This is a dynamic template with a fixed zoom level
url = url.replace("{z}", "" + zoomLevel)
const loadedTiles = new Set<string>();
const self = this;
this.onFail = (msg, url) => {
console.warn(`Could not load geojson layer from`, url, "due to", msg)
loadedTiles.add(url); // We add the url to the 'loadedTiles' in order to not reload it in the future
}
const neededTiles = locationControl.map(
_ => {
// Yup, this is cheating to just get the bounds here
const bounds = State.state.leafletMap.data.getBounds()
const tileRange = Utils.TileRangeBetween(zoomLevel, bounds.getNorth(), bounds.getEast(), bounds.getSouth(), bounds.getWest())
const needed = Utils.MapRange(tileRange, (x, y) => {
return url.replace("{x}", "" + x).replace("{y}", "" + y);
})
return new Set<string>(needed);
}
, [flayer.isDisplayed]);
neededTiles.stabilized(250).addCallback((needed: Set<string>) => {
if (needed === undefined) {
return;
}
if (!flayer.isDisplayed.data) {
// No need to download! - the layer is disabled
return;
}
if (locationControl.data.zoom < flayer.layerDef.minzoom) {
return;
}
needed.forEach(neededTile => {
if (loadedTiles.has(neededTile)) {
return;
}
loadedTiles.add(neededTile)
self.LoadJSONFrom(neededTile)
})
})
}
private LoadJSONFrom(url: string) {
const eventSource = this.features;
const self = this;

View file

@ -0,0 +1,91 @@
import FeatureSource from "./FeatureSource";
import {UIEventSource} from "../UIEventSource";
import {OsmObject} from "../Osm/OsmObject";
import State from "../../State";
import {Utils} from "../../Utils";
import Loc from "../../Models/Loc";
export default class OsmApiFeatureSource implements FeatureSource {
public readonly features: UIEventSource<{ feature: any; freshness: Date }[]> = new UIEventSource<{ feature: any; freshness: Date }[]>([]);
public readonly name: string = "OsmApiFeatureSource";
private readonly loadedTiles: Set<string> = new Set<string>();
constructor(location: UIEventSource<Loc>) {
/* const self = this
location.addCallback(_ => {
self.loadArea()
})
*/
}
public load(id: string) {
OsmObject.DownloadObject(id, (element, meta) => {
const geojson = element.asGeoJson();
geojson.id = geojson.properties.id;
this.features.setData([{feature: geojson, freshness: meta["_last_edit:timestamp"]}])
})
}
/**
* Loads the current inview-area
*/
public loadArea(z: number = 16): boolean {
const layers = State.state.filteredLayers.data;
const disabledLayers = layers.filter(layer => layer.layerDef.source.overpassScript !== undefined || layer.layerDef.source.geojsonSource !== undefined)
if (disabledLayers.length > 0) {
return false;
}
const loc = State.state.locationControl.data;
if (loc.zoom < 16) {
return false;
}
if (State.state.leafletMap.data === undefined) {
return false; // Not yet inited
}
const bounds = State.state.leafletMap.data.getBounds()
const tileRange = Utils.TileRangeBetween(z, bounds.getNorth(), bounds.getEast(), bounds.getSouth(), bounds.getWest())
const self = this;
Utils.MapRange(tileRange, (x, y) => {
const key = x + "/" + y;
if (self.loadedTiles.has(key)) {
return;
}
self.loadedTiles.add(key);
const bounds = Utils.tile_bounds(z, x, y);
console.log("Loading OSM data tile", z, x, y, " with bounds", bounds)
OsmObject.LoadArea(bounds, objects => {
const keptGeoJson: {feature:any, freshness: Date}[] = []
// Which layer does the object match?
for (const object of objects) {
for (const flayer of layers) {
const layer = flayer.layerDef;
const tags = object.tags
const doesMatch = layer.source.osmTags.matchesProperties(tags);
if (doesMatch) {
const geoJson = object.asGeoJson();
geoJson._matching_layer_id = layer.id
keptGeoJson.push({feature: geoJson, freshness: object.timestamp})
break;
}
}
}
self.features.setData(keptGeoJson)
});
});
return true;
}
}