Add summary layer

This commit is contained in:
Pieter Vander Vennet 2024-02-15 17:39:59 +01:00
parent 5b318236bf
commit 74fb4bd5d1
19 changed files with 533 additions and 88 deletions

View file

@ -11,16 +11,15 @@ import { OsmTags } from "../../../Models/OsmFeature"
* Highly specialized feature source.
* Based on a lon/lat UIEVentSource, will generate the corresponding feature with the correct properties
*/
export class LastClickFeatureSource implements WritableFeatureSource {
public readonly features: UIEventSource<Feature[]> = new UIEventSource<Feature[]>([])
public readonly hasNoteLayer: boolean
export class LastClickFeatureSource {
public readonly renderings: string[]
public readonly hasPresets: boolean
private i: number = 0
private readonly hasPresets: boolean
private readonly hasNoteLayer: boolean
constructor(location: Store<{ lon: number; lat: number }>, layout: LayoutConfig) {
this.hasNoteLayer = layout.layers.some((l) => l.id === "note")
this.hasPresets = layout.layers.some((l) => l.presets?.length > 0)
constructor(layout: LayoutConfig) {
this.hasNoteLayer = layout.hasNoteLayer()
this.hasPresets = layout.hasPresets()
const allPresets: BaseUIElement[] = []
for (const layer of layout.layers)
for (let i = 0; i < (layer.presets ?? []).length; i++) {
@ -43,16 +42,11 @@ export class LastClickFeatureSource implements WritableFeatureSource {
Utils.runningFromConsole ? "" : uiElem.ConstructElement().innerHTML
)
)
location.addCallbackAndRunD(({ lon, lat }) => {
this.features.setData([this.createFeature(lon, lat)])
})
}
public createFeature(lon: number, lat: number): Feature<Point, OsmTags> {
const properties: OsmTags = {
lastclick: "yes",
id: "last_click_" + this.i,
id: "new_point_dialog",
has_note_layer: this.hasNoteLayer ? "yes" : "no",
has_presets: this.hasPresets ? "yes" : "no",
renderings: this.renderings.join(""),

View file

@ -0,0 +1,85 @@
import DynamicTileSource from "./DynamicTileSource"
import { Store, UIEventSource } from "../../UIEventSource"
import { BBox } from "../../BBox"
import StaticFeatureSource from "../Sources/StaticFeatureSource"
import { Feature, Point } from "geojson"
import { Utils } from "../../../Utils"
import { Tiles } from "../../../Models/TileRange"
/**
* Provides features summarizing the total amount of features at a given location
*/
export class SummaryTileSource extends DynamicTileSource {
private static readonly empty = []
constructor(
cacheserver: string,
layers: string[],
zoomRounded: Store<number>,
mapProperties: {
bounds: Store<BBox>
zoom: Store<number>
},
options?: {
isActive?: Store<boolean>
}
) {
const layersSummed = layers.join("+")
super(
zoomRounded,
0, // minzoom
(tileIndex) => {
const [z, x, y] = Tiles.tile_from_index(tileIndex)
const coordinates = Tiles.centerPointOf(z, x, y)
const count = UIEventSource.FromPromiseWithErr(
Utils.downloadJson(`${cacheserver}/${layersSummed}/${z}/${x}/${y}.json`)
)
const features: Store<Feature<Point>[]> = count.mapD((count) => {
if (count["error"] !== undefined) {
console.error(
"Could not download count for tile",
z,
x,
y,
"due to",
count["error"]
)
return SummaryTileSource.empty
}
const counts = count["success"]
if (counts === undefined || counts["total"] === 0) {
return SummaryTileSource.empty
}
return [
{
type: "Feature",
properties: {
id: "summary_" + tileIndex,
summary: "yes",
...counts,
layers: layersSummed,
},
geometry: {
type: "Point",
coordinates,
},
},
]
})
return new StaticFeatureSource(
features.map(
(f) => {
if (z !== zoomRounded.data) {
return SummaryTileSource.empty
}
return f
},
[zoomRounded]
)
)
},
mapProperties,
options
)
}
}