forked from MapComplete/MapComplete
Optimizing trees theme and clustering
This commit is contained in:
parent
5ac035be20
commit
8fca373437
11 changed files with 125 additions and 67 deletions
|
@ -38,7 +38,8 @@ export default class ShowDataLayer {
|
|||
ShowDataLayer.dataLayerIds++
|
||||
this._enablePopups = options.enablePopups ?? true;
|
||||
if (options.features === undefined) {
|
||||
throw "Invalid ShowDataLayer invocation"
|
||||
console.error("Invalid ShowDataLayer invocation: options.features is undefed")
|
||||
throw "Invalid ShowDataLayer invocation: options.features is undefed"
|
||||
}
|
||||
const features = options.features.features.map(featFreshes => featFreshes.map(ff => ff.feature));
|
||||
this._features = features;
|
||||
|
@ -166,7 +167,7 @@ export default class ShowDataLayer {
|
|||
|
||||
|
||||
private createStyleFor(feature) {
|
||||
const tagsSource = this.allElements?.addOrGetElement(feature) ?? new UIEventSource<any>(feature.properties.id);
|
||||
const tagsSource = this.allElements?.addOrGetElement(feature) ?? new UIEventSource<any>(feature.properties);
|
||||
// Every object is tied to exactly one layer
|
||||
const layer = this._layerToShow
|
||||
return layer?.GenerateLeafletStyle(tagsSource, true);
|
||||
|
|
|
@ -4,7 +4,11 @@ import {UIEventSource} from "../../Logic/UIEventSource";
|
|||
import {Tiles} from "../../Models/TileRange";
|
||||
import StaticFeatureSource from "../../Logic/FeatureSource/Sources/StaticFeatureSource";
|
||||
import {BBox} from "../../Logic/BBox";
|
||||
import FilteredLayer from "../../Models/FilteredLayer";
|
||||
|
||||
/**
|
||||
* A feature source containing but a single feature, which keeps stats about a tile
|
||||
*/
|
||||
export class TileHierarchyAggregator implements FeatureSource {
|
||||
private _parent: TileHierarchyAggregator;
|
||||
private _root: TileHierarchyAggregator;
|
||||
|
@ -16,28 +20,39 @@ export class TileHierarchyAggregator implements FeatureSource {
|
|||
|
||||
private _subtiles: [TileHierarchyAggregator, TileHierarchyAggregator, TileHierarchyAggregator, TileHierarchyAggregator] = [undefined, undefined, undefined, undefined]
|
||||
public totalValue: number = 0
|
||||
public showCount: number = 0
|
||||
public hiddenCount: number = 0
|
||||
|
||||
private static readonly empty = []
|
||||
public readonly features = new UIEventSource<{ feature: any, freshness: Date }[]>(TileHierarchyAggregator.empty)
|
||||
public readonly name;
|
||||
|
||||
private readonly featuresStatic = []
|
||||
private readonly featureProperties: { count: string, kilocount: string, tileId: string, id: string };
|
||||
|
||||
private constructor(parent: TileHierarchyAggregator, z: number, x: number, y: number) {
|
||||
private readonly featureProperties: { count: string, kilocount: string, tileId: string, id: string, showCount: string, totalCount: string };
|
||||
private readonly _state: { filteredLayers: UIEventSource<FilteredLayer[]> };
|
||||
private readonly updateSignal = new UIEventSource<any>(undefined)
|
||||
|
||||
private constructor(parent: TileHierarchyAggregator,
|
||||
state: {
|
||||
filteredLayers: UIEventSource<FilteredLayer[]>
|
||||
},
|
||||
z: number, x: number, y: number) {
|
||||
this._parent = parent;
|
||||
this._state = state;
|
||||
this._root = parent?._root ?? this
|
||||
this._z = z;
|
||||
this._x = x;
|
||||
this._y = y;
|
||||
this._tileIndex = Tiles.tile_index(z, x, y)
|
||||
this.name = "Count(" + this._tileIndex + ")"
|
||||
|
||||
|
||||
const totals = {
|
||||
id: ""+this._tileIndex,
|
||||
tileId: ""+this._tileIndex,
|
||||
id: "" + this._tileIndex,
|
||||
tileId: "" + this._tileIndex,
|
||||
count: `0`,
|
||||
kilocount: "0"
|
||||
kilocount: "0",
|
||||
showCount: "0",
|
||||
totalCount: "0"
|
||||
}
|
||||
this.featureProperties = totals
|
||||
|
||||
|
@ -91,25 +106,57 @@ export class TileHierarchyAggregator implements FeatureSource {
|
|||
private update() {
|
||||
const newMap = new Map<string, number>()
|
||||
let total = 0
|
||||
let hiddenCount = 0
|
||||
let showCount = 0
|
||||
let isShown: Map<string, FilteredLayer> = new Map<string, FilteredLayer>()
|
||||
for (const filteredLayer of this._state.filteredLayers.data) {
|
||||
isShown.set(filteredLayer.layerDef.id, filteredLayer)
|
||||
}
|
||||
this?._counter?.countsPerLayer?.data?.forEach((count, layerId) => {
|
||||
newMap.set(layerId, count)
|
||||
newMap.set("layer:" + layerId, count)
|
||||
total += count
|
||||
this.featureProperties["direct_layer:" + layerId] = count
|
||||
const flayer = isShown.get(layerId)
|
||||
if (flayer.isDisplayed.data && this._z >= flayer.layerDef.minzoom) {
|
||||
showCount += count
|
||||
} else {
|
||||
hiddenCount += count;
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
for (const tile of this._subtiles) {
|
||||
if (tile === undefined) {
|
||||
continue;
|
||||
}
|
||||
total += tile.totalValue
|
||||
|
||||
showCount += tile.showCount
|
||||
hiddenCount += tile.hiddenCount
|
||||
|
||||
for (const key in tile.featureProperties) {
|
||||
if (key.startsWith("layer:")) {
|
||||
newMap.set(key, (newMap.get(key) ?? 0) + Number(tile.featureProperties[key] ?? 0))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.totalValue = total
|
||||
this.showCount = showCount
|
||||
this.hiddenCount = hiddenCount
|
||||
this._parent?.update()
|
||||
|
||||
|
||||
if (total === 0) {
|
||||
this.features.setData(TileHierarchyAggregator.empty)
|
||||
} else {
|
||||
this.featureProperties.count = "" + total;
|
||||
this.featureProperties.kilocount = "" +Math.floor(total/1000);
|
||||
this.featureProperties.kilocount = "" + Math.floor(total / 1000);
|
||||
this.featureProperties.showCount = "" + showCount
|
||||
this.featureProperties.totalCount = "" + total
|
||||
newMap.forEach((value, key) => {
|
||||
this.featureProperties[key] = "" + value
|
||||
})
|
||||
|
||||
this.features.data = this.featuresStatic
|
||||
this.features.ping()
|
||||
}
|
||||
|
@ -137,49 +184,48 @@ export class TileHierarchyAggregator implements FeatureSource {
|
|||
|
||||
const subtileIndex = yDiff * 2 + xDiff;
|
||||
if (this._subtiles[subtileIndex] === undefined) {
|
||||
this._subtiles[subtileIndex] = new TileHierarchyAggregator(this, tileZ, tileX, tileY)
|
||||
this._subtiles[subtileIndex] = new TileHierarchyAggregator(this, this._state, tileZ, tileX, tileY)
|
||||
}
|
||||
this._subtiles[subtileIndex].addTile(source)
|
||||
}
|
||||
|
||||
this.updateSignal.setData(source)
|
||||
}
|
||||
|
||||
public static createHierarchy() {
|
||||
return new TileHierarchyAggregator(undefined, 0, 0, 0)
|
||||
public static createHierarchy(state: { filteredLayers: UIEventSource<FilteredLayer[]> }) {
|
||||
return new TileHierarchyAggregator(undefined, state, 0, 0, 0)
|
||||
}
|
||||
|
||||
private visitSubTiles(f : (aggr: TileHierarchyAggregator) => boolean){
|
||||
private visitSubTiles(f: (aggr: TileHierarchyAggregator) => boolean) {
|
||||
const visitFurther = f(this)
|
||||
if(visitFurther){
|
||||
if (visitFurther) {
|
||||
this._subtiles.forEach(tile => tile?.visitSubTiles(f))
|
||||
}
|
||||
}
|
||||
|
||||
getCountsForZoom(clusteringConfig: {maxZoom: number}, locationControl: UIEventSource<{ zoom : number }>, cutoff: number = 0) : FeatureSource{
|
||||
|
||||
getCountsForZoom(clusteringConfig: { maxZoom: number }, locationControl: UIEventSource<{ zoom: number }>, cutoff: number = 0): FeatureSource {
|
||||
const self = this
|
||||
const empty = []
|
||||
return new StaticFeatureSource(
|
||||
locationControl.map(loc => loc.zoom).map(targetZoom => {
|
||||
if(targetZoom-1 > clusteringConfig.maxZoom){
|
||||
return empty
|
||||
const features = locationControl.map(loc => loc.zoom).map(targetZoom => {
|
||||
if (targetZoom - 1 > clusteringConfig.maxZoom) {
|
||||
return empty
|
||||
}
|
||||
|
||||
const features = []
|
||||
self.visitSubTiles(aggr => {
|
||||
if (aggr.showCount < cutoff) {
|
||||
return false
|
||||
}
|
||||
|
||||
const features = []
|
||||
self.visitSubTiles(aggr => {
|
||||
if(aggr.totalValue < cutoff) {
|
||||
return false
|
||||
}
|
||||
if(aggr._z === targetZoom){
|
||||
features.push(...aggr.features.data)
|
||||
return false
|
||||
}
|
||||
return aggr._z <= targetZoom;
|
||||
|
||||
})
|
||||
|
||||
return features
|
||||
if (aggr._z === targetZoom) {
|
||||
features.push(...aggr.features.data)
|
||||
return false
|
||||
}
|
||||
return aggr._z <= targetZoom;
|
||||
})
|
||||
, true);
|
||||
|
||||
return features
|
||||
}, [this.updateSignal.stabilized(500)])
|
||||
|
||||
return new StaticFeatureSource(features, true);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -209,14 +255,11 @@ class SingleTileCounter implements Tiled {
|
|||
const layer = source.layer.layerDef
|
||||
this.registeredLayers.set(layer.id, layer)
|
||||
const self = this
|
||||
|
||||
source.features.map(f => {
|
||||
const isDisplayed = source.layer.isDisplayed.data
|
||||
self.countsPerLayer.data.set(layer.id, isDisplayed ? f.length : 0)
|
||||
self.countsPerLayer.ping()
|
||||
}, [source.layer.isDisplayed])
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue