2021-09-22 05:02:09 +02:00
|
|
|
import FeatureSource, {FeatureSourceForLayer, Tiled} from "./FeatureSource";
|
2021-09-20 17:14:55 +02:00
|
|
|
import {UIEventSource} from "../UIEventSource";
|
|
|
|
import FilteredLayer from "../../Models/FilteredLayer";
|
2021-09-21 02:10:42 +02:00
|
|
|
import SimpleFeatureSource from "./Sources/SimpleFeatureSource";
|
2021-09-20 17:14:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* In some rare cases, some elements are shown on multiple layers (when 'passthrough' is enabled)
|
|
|
|
* If this is the case, multiple objects with a different _matching_layer_id are generated.
|
|
|
|
* In any case, this featureSource marks the objects with _matching_layer_id
|
|
|
|
*/
|
|
|
|
export default class PerLayerFeatureSourceSplitter {
|
|
|
|
|
|
|
|
constructor(layers: UIEventSource<FilteredLayer[]>,
|
2021-09-22 05:02:09 +02:00
|
|
|
handleLayerData: (source: FeatureSourceForLayer & Tiled) => void,
|
|
|
|
upstream: FeatureSource,
|
|
|
|
options?:{
|
|
|
|
handleLeftovers?: (featuresWithoutLayer: any[]) => void
|
|
|
|
}) {
|
2021-09-20 17:14:55 +02:00
|
|
|
|
2021-09-22 05:02:09 +02:00
|
|
|
const knownLayers = new Map<string, FeatureSourceForLayer & Tiled>()
|
2021-09-20 17:14:55 +02:00
|
|
|
|
|
|
|
function update() {
|
|
|
|
const features = upstream.features.data;
|
|
|
|
if (features === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-21 02:10:42 +02:00
|
|
|
if (layers.data === undefined) {
|
2021-09-20 17:14:55 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We try to figure out (for each feature) in which feature store it should be saved.
|
|
|
|
// Note that this splitter is only run when it is invoked by the overpass feature source, so we can't be sure in which layer it should go
|
|
|
|
|
|
|
|
const featuresPerLayer = new Map<string, { feature, freshness } []>();
|
2021-09-22 05:02:09 +02:00
|
|
|
const noLayerFound = []
|
2021-09-20 17:14:55 +02:00
|
|
|
function addTo(layer: FilteredLayer, feature: { feature, freshness }) {
|
|
|
|
const id = layer.layerDef.id
|
|
|
|
const list = featuresPerLayer.get(id)
|
|
|
|
if (list !== undefined) {
|
|
|
|
list.push(feature)
|
|
|
|
} else {
|
|
|
|
featuresPerLayer.set(id, [feature])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const f of features) {
|
|
|
|
for (const layer of layers.data) {
|
|
|
|
if (layer.layerDef.source.osmTags.matchesProperties(f.feature.properties)) {
|
|
|
|
// We have found our matching layer!
|
|
|
|
addTo(layer, f)
|
|
|
|
if (!layer.layerDef.passAllFeatures) {
|
|
|
|
// If not 'passAllFeatures', we are done for this feature
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-09-22 05:02:09 +02:00
|
|
|
noLayerFound.push(f)
|
2021-09-20 17:14:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, we have our features per layer as a list
|
|
|
|
// We assign them to the correct featureSources
|
|
|
|
for (const layer of layers.data) {
|
|
|
|
const id = layer.layerDef.id;
|
|
|
|
const features = featuresPerLayer.get(id)
|
|
|
|
if (features === undefined) {
|
|
|
|
// No such features for this layer
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let featureSource = knownLayers.get(id)
|
|
|
|
if (featureSource === undefined) {
|
|
|
|
// Not yet initialized - now is a good time
|
|
|
|
featureSource = new SimpleFeatureSource(layer)
|
2021-09-21 02:10:42 +02:00
|
|
|
featureSource.features.setData(features)
|
2021-09-20 17:14:55 +02:00
|
|
|
knownLayers.set(id, featureSource)
|
|
|
|
handleLayerData(featureSource)
|
2021-09-21 02:10:42 +02:00
|
|
|
} else {
|
|
|
|
featureSource.features.setData(features)
|
2021-09-20 17:14:55 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-22 05:02:09 +02:00
|
|
|
|
|
|
|
// AT last, the leftovers are handled
|
|
|
|
if(options?.handleLeftovers !== undefined && noLayerFound.length > 0){
|
|
|
|
options.handleLeftovers(noLayerFound)
|
|
|
|
}
|
2021-09-20 17:14:55 +02:00
|
|
|
}
|
2021-09-21 02:10:42 +02:00
|
|
|
|
|
|
|
layers.addCallback(_ => update())
|
2021-09-20 17:14:55 +02:00
|
|
|
upstream.features.addCallbackAndRunD(_ => update())
|
|
|
|
}
|
|
|
|
}
|