Feature: more or less working version of clustering, clusters of multiple layers are joined

This commit is contained in:
Pieter Vander Vennet 2025-07-24 15:57:30 +02:00
parent 5bc8f11d24
commit 0048c091d0
9 changed files with 143 additions and 65 deletions

View file

@ -11,17 +11,17 @@ import { UIEventSource } from "../UIEventSource"
* 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<T extends FeatureSource = FeatureSource> {
public readonly perLayer: ReadonlyMap<string, T>
export default class PerLayerFeatureSourceSplitter<T extends Feature, SRC extends FeatureSource<T>> {
public readonly perLayer: ReadonlyMap<string, SRC>
constructor(
layers: FilteredLayer[],
upstream: FeatureSource,
upstream: FeatureSource<T>,
options?: {
constructStore?: (features: UIEventSource<Feature[]>, layer: FilteredLayer) => T
handleLeftovers?: (featuresWithoutLayer: Feature[]) => void
constructStore?: (features: UIEventSource<T[]>, layer: FilteredLayer) => SRC
handleLeftovers?: (featuresWithoutLayer: T[]) => void
}
) {
const knownLayers = new Map<string, T>()
const knownLayers = new Map<string, SRC>()
/**
* Keeps track of the ids that are included per layer.
* Used to know if the downstream feature source needs to be pinged
@ -32,12 +32,12 @@ export default class PerLayerFeatureSourceSplitter<T extends FeatureSource = Fea
const constructStore =
options?.constructStore ?? ((store, layer) => new SimpleFeatureSource(layer, store))
for (const layer of layers) {
const src = new UIEventSource<Feature[]>([])
const src = new UIEventSource<T[]>([])
layerSources.set(layer.layerDef.id, src)
knownLayers.set(layer.layerDef.id, <T>constructStore(src, layer))
knownLayers.set(layer.layerDef.id, <SRC>constructStore(src, layer))
}
upstream.features.addCallbackAndRunD((features) => {
upstream.features.addCallbackAndRunD((features: T[]) => {
if (layers === undefined) {
return
}
@ -51,7 +51,7 @@ export default class PerLayerFeatureSourceSplitter<T extends FeatureSource = Fea
*/
const hasChanged: boolean[] = layers.map(() => false)
const newIndices: Set<string>[] = layers.map(() => new Set<string>())
const noLayerFound: Feature[] = []
const noLayerFound: T[] = []
for (const layer of layers) {
featuresPerLayer.set(layer.layerDef.id, [])
@ -115,7 +115,7 @@ export default class PerLayerFeatureSourceSplitter<T extends FeatureSource = Fea
})
}
public forEach(f: (featureSource: T) => void) {
public forEach(f: ((src: SRC) => void)) {
for (const fs of this.perLayer.values()) {
f(fs)
}