From bccda67e1c924e7dbf1e13653e6bdc47ac467a1b Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Tue, 20 Feb 2024 23:15:19 +0100 Subject: [PATCH] Update counts when selecting/unselecting a layer --- .../TiledFeatureSource/SummaryTileSource.ts | 43 +++++++++++++++++++ src/Models/ThemeViewState.ts | 8 +++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/Logic/FeatureSource/TiledFeatureSource/SummaryTileSource.ts b/src/Logic/FeatureSource/TiledFeatureSource/SummaryTileSource.ts index 91527bce3..96370e296 100644 --- a/src/Logic/FeatureSource/TiledFeatureSource/SummaryTileSource.ts +++ b/src/Logic/FeatureSource/TiledFeatureSource/SummaryTileSource.ts @@ -5,6 +5,49 @@ import StaticFeatureSource from "../Sources/StaticFeatureSource" import { Feature, Point } from "geojson" import { Utils } from "../../../Utils" import { Tiles } from "../../../Models/TileRange" +import { FeatureSource } from "../FeatureSource" +import FilteredLayer from "../../../Models/FilteredLayer" +import Constants from "../../../Models/Constants" + +export class SummaryTileSourceRewriter implements FeatureSource { + private readonly _features: UIEventSource = new UIEventSource([]) + private filteredLayers: FilteredLayer[] + public readonly features: Store = this._features + private readonly _summarySource: SummaryTileSource + constructor( + summarySource: SummaryTileSource, + filteredLayers: ReadonlyMap + ) { + this.filteredLayers = Array.from(filteredLayers.values()).filter( + (l) => + Constants.priviliged_layers.indexOf(l.layerDef.id) < 0 && + !l.layerDef.id.startsWith("note_import") + ) + this._summarySource = summarySource + filteredLayers.forEach((v, k) => { + v.isDisplayed.addCallback((_) => this.update()) + }) + this._summarySource.features.addCallbackAndRunD((_) => this.update()) + } + + private update() { + const newFeatures: Feature[] = [] + const layersToCount = this.filteredLayers.filter((fl) => fl.isDisplayed.data) + const bitmap = layersToCount.map((l) => (l.isDisplayed.data ? "1" : "0")).join("") + const ids = layersToCount.map((l) => l.layerDef.id) + for (const f of this._summarySource.features.data ?? []) { + let newTotal = 0 + for (const id of ids) { + newTotal += Number(f.properties[id] ?? 0) + } + newFeatures.push({ + ...f, + properties: { ...f.properties, id: f.properties.id + bitmap, total: newTotal }, + }) + } + this._features.setData(newFeatures) + } +} /** * Provides features summarizing the total amount of features at a given location diff --git a/src/Models/ThemeViewState.ts b/src/Models/ThemeViewState.ts index e5948b42c..a5d4fb06a 100644 --- a/src/Models/ThemeViewState.ts +++ b/src/Models/ThemeViewState.ts @@ -62,7 +62,10 @@ import FavouritesFeatureSource from "../Logic/FeatureSource/Sources/FavouritesFe import { ProvidedImage } from "../Logic/ImageProviders/ImageProvider" import { GeolocationControlState } from "../UI/BigComponents/GeolocationControl" import Zoomcontrol from "../UI/Zoomcontrol" -import { SummaryTileSource } from "../Logic/FeatureSource/TiledFeatureSource/SummaryTileSource" +import { + SummaryTileSource, + SummaryTileSourceRewriter, +} from "../Logic/FeatureSource/TiledFeatureSource/SummaryTileSource" import summaryLayer from "../assets/generated/layers/summary.json" import { LayerConfigJson } from "./ThemeConfig/Json/LayerConfigJson" import Locale from "../UI/i18n/Locale" @@ -664,7 +667,7 @@ export default class ThemeViewState implements SpecialVisualizationState { l.source.geojsonSource === undefined ) const url = new URL(Constants.VectorTileServer) - return new SummaryTileSource( + const summaryTileSource = new SummaryTileSource( url.protocol + "//" + url.host + "/summary", layers.map((l) => l.id), this.mapProperties.zoom.map((z) => Math.max(Math.ceil(z), 0)), @@ -673,6 +676,7 @@ export default class ThemeViewState implements SpecialVisualizationState { isActive: this.mapProperties.zoom.map((z) => z <= maxzoom), } ) + return new SummaryTileSourceRewriter(summaryTileSource, this.layerState.filteredLayers) } /** * Add the special layers to the map