mapcomplete/Logic/FeatureSource/PerLayerFeatureSourceSplitter.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

116 lines
4.7 KiB
TypeScript
Raw Normal View History

2023-04-26 18:04:42 +02:00
import { FeatureSource, IndexedFeatureSource } from "./FeatureSource"
import FilteredLayer from "../../Models/FilteredLayer"
import SimpleFeatureSource from "./Sources/SimpleFeatureSource"
import { Feature } from "geojson"
2023-03-28 05:13:48 +02:00
import { UIEventSource } from "../UIEventSource"
/**
* 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
*/
2023-04-26 18:04:42 +02:00
export default class PerLayerFeatureSourceSplitter<T extends FeatureSource = FeatureSource> {
2023-03-28 05:13:48 +02:00
public readonly perLayer: ReadonlyMap<string, T>
constructor(
2023-03-28 05:13:48 +02:00
layers: FilteredLayer[],
upstream: FeatureSource,
2021-11-07 16:34:51 +01:00
options?: {
2023-03-28 05:13:48 +02:00
constructStore?: (features: UIEventSource<Feature[]>, layer: FilteredLayer) => T
handleLeftovers?: (featuresWithoutLayer: Feature[]) => void
}
) {
2023-03-28 05:13:48 +02:00
const knownLayers = new Map<string, T>()
2023-04-26 18:04:42 +02:00
/**
* Keeps track of the ids that are included per layer.
* Used to know if the downstream feature source needs to be pinged
*/
let layerIndexes: ReadonlySet<string>[] = layers.map((_) => new Set<string>())
2023-03-28 05:13:48 +02:00
this.perLayer = knownLayers
const layerSources = new Map<string, UIEventSource<Feature[]>>()
const constructStore =
options?.constructStore ?? ((store, layer) => new SimpleFeatureSource(layer, store))
for (const layer of layers) {
const src = new UIEventSource<Feature[]>([])
layerSources.set(layer.layerDef.id, src)
knownLayers.set(layer.layerDef.id, <T>constructStore(src, layer))
}
upstream.features.addCallbackAndRunD((features) => {
if (layers === undefined) {
return
}
// We try to figure out (for each feature) in which feature store it should be saved.
const featuresPerLayer = new Map<string, Feature[]>()
2023-04-26 18:04:42 +02:00
/**
* Indexed on layer-position
* Will be true if a new id pops up
*/
const hasChanged: boolean[] = layers.map((_) => false)
const newIndices: Set<string>[] = layers.map((_) => new Set<string>())
2023-03-28 05:13:48 +02:00
const noLayerFound: Feature[] = []
2021-11-07 16:34:51 +01:00
2023-03-28 05:13:48 +02:00
for (const layer of layers) {
featuresPerLayer.set(layer.layerDef.id, [])
}
for (const f of features) {
let foundALayer = false
2023-04-26 18:04:42 +02:00
for (let i = 0; i < layers.length; i++) {
const layer = layers[i]
if (layer.layerDef.source.osmTags.matchesProperties(f.properties)) {
2023-04-26 18:04:42 +02:00
const id = f.properties.id
// We have found our matching layer!
2023-04-26 18:04:42 +02:00
const previousIndex = layerIndexes[i]
hasChanged[i] = hasChanged[i] || !previousIndex.has(id)
newIndices[i].add(id)
featuresPerLayer.get(layer.layerDef.id).push(f)
foundALayer = true
if (!layer.layerDef.passAllFeatures) {
// If not 'passAllFeatures', we are done for this feature
break
}
}
}
if (!foundALayer) {
noLayerFound.push(f)
}
}
// At this point, we have our features per layer as a list
// We assign them to the correct featureSources
2023-04-26 18:04:42 +02:00
for (let i = 0; i < layers.length; i++) {
const layer = layers[i]
const id = layer.layerDef.id
const features = featuresPerLayer.get(id)
if (features === undefined) {
// No such features for this layer
continue
}
2023-04-26 18:04:42 +02:00
if (!hasChanged[i] && layerIndexes[i].size === newIndices[i].size) {
// No new id has been added and the sizes are the same (thus: nothing has been removed as well)
// We can safely assume that no changes were made
continue
}
2023-04-26 18:04:42 +02:00
layerSources.get(id).setData(features)
}
2021-11-07 16:34:51 +01:00
2023-04-26 18:04:42 +02:00
layerIndexes = newIndices
// AT last, the leftovers are handled
2021-11-07 16:34:51 +01:00
if (options?.handleLeftovers !== undefined && noLayerFound.length > 0) {
options.handleLeftovers(noLayerFound)
}
2023-03-28 05:13:48 +02:00
})
}
public forEach(f: (featureSource: T) => void) {
2023-03-28 05:13:48 +02:00
for (const fs of this.perLayer.values()) {
f(fs)
}
}
}