mapcomplete/UI/Map/ShowDataLayer.ts

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

524 lines
19 KiB
TypeScript
Raw Normal View History

import { ImmutableStore, Store, UIEventSource } from "../../Logic/UIEventSource"
import type { Map as MlMap } from "maplibre-gl"
import { GeoJSONSource, Marker } from "maplibre-gl"
import { ShowDataLayerOptions } from "./ShowDataLayerOptions"
import { GeoOperations } from "../../Logic/GeoOperations"
import LayerConfig from "../../Models/ThemeConfig/LayerConfig"
import PointRenderingConfig from "../../Models/ThemeConfig/PointRenderingConfig"
import { OsmTags } from "../../Models/OsmFeature"
import { FeatureSource, FeatureSourceForLayer } from "../../Logic/FeatureSource/FeatureSource"
import { BBox } from "../../Logic/BBox"
import { Feature, Point } from "geojson"
2023-03-24 19:21:15 +01:00
import LineRenderingConfig from "../../Models/ThemeConfig/LineRenderingConfig"
import { Utils } from "../../Utils"
2023-03-24 19:21:15 +01:00
import * as range_layer from "../../assets/layers/range/range.json"
import { LayerConfigJson } from "../../Models/ThemeConfig/Json/LayerConfigJson"
2023-03-28 05:13:48 +02:00
import PerLayerFeatureSourceSplitter from "../../Logic/FeatureSource/PerLayerFeatureSourceSplitter"
import FilteredLayer from "../../Models/FilteredLayer"
import SimpleFeatureSource from "../../Logic/FeatureSource/Sources/SimpleFeatureSource"
2023-03-25 02:48:24 +01:00
class PointRenderingLayer {
private readonly _config: PointRenderingConfig
private readonly _visibility?: Store<boolean>
2023-03-28 05:13:48 +02:00
private readonly _fetchStore?: (id: string) => Store<Record<string, string>>
private readonly _map: MlMap
private readonly _onClick: (feature: Feature) => void
2023-03-24 19:21:15 +01:00
private readonly _allMarkers: Map<string, Marker> = new Map<string, Marker>()
private readonly _selectedElement: Store<{ properties: { id?: string } }>
private readonly _markedAsSelected: HTMLElement[] = []
private _dirty = false
constructor(
map: MlMap,
features: FeatureSource,
config: PointRenderingConfig,
2023-03-24 19:21:15 +01:00
visibility?: Store<boolean>,
2023-03-28 05:13:48 +02:00
fetchStore?: (id: string) => Store<Record<string, string>>,
onClick?: (feature: Feature) => void,
selectedElement?: Store<{ properties: { id?: string } }>
) {
this._visibility = visibility
this._config = config
this._map = map
this._fetchStore = fetchStore
2023-03-24 19:21:15 +01:00
this._onClick = onClick
this._selectedElement = selectedElement
const self = this
2023-03-24 19:21:15 +01:00
features.features.addCallbackAndRunD((features) => self.updateFeatures(features))
visibility?.addCallbackAndRunD((visible) => {
if (visible === true && self._dirty) {
self.updateFeatures(features.features.data)
}
self.setVisibility(visible)
})
selectedElement?.addCallbackAndRun((selected) => {
this._markedAsSelected.forEach((el) => el.classList.remove("selected"))
this._markedAsSelected.splice(0, this._markedAsSelected.length)
if (selected === undefined) {
return
}
PointRenderingConfig.allowed_location_codes.forEach((code) => {
const marker = this._allMarkers
.get(selected.properties?.id + "-" + code)
?.getElement()
if (marker === undefined) {
return
}
marker?.classList?.add("selected")
this._markedAsSelected.push(marker)
})
})
2023-03-24 19:21:15 +01:00
}
private updateFeatures(features: Feature[]) {
if (this._visibility?.data === false) {
this._dirty = true
return
}
this._dirty = false
2023-03-24 19:21:15 +01:00
const cache = this._allMarkers
const unseenKeys = new Set(cache.keys())
for (const location of this._config.location) {
for (const feature of features) {
2023-03-25 02:48:24 +01:00
if (feature?.geometry === undefined) {
console.warn(
"Got an invalid feature:",
features,
" while rendering",
location,
"of",
this._config
)
}
const id = feature.properties.id + "-" + location
unseenKeys.delete(id)
2023-03-24 19:21:15 +01:00
const loc = GeoOperations.featureToCoordinateWithRenderingType(
<any>feature,
location
)
if (loc === undefined) {
continue
}
if (cache.has(id)) {
const cached = cache.get(id)
const oldLoc = cached.getLngLat()
if (loc[0] !== oldLoc.lng && loc[1] !== oldLoc.lat) {
cached.setLngLat(loc)
}
continue
}
2023-03-24 19:21:15 +01:00
const marker = this.addPoint(feature, loc)
if (this._selectedElement?.data === feature.properties.id) {
marker.getElement().classList.add("selected")
this._markedAsSelected.push(marker.getElement())
}
cache.set(id, marker)
}
2023-03-24 19:21:15 +01:00
}
2023-03-24 19:21:15 +01:00
for (const unseenKey of unseenKeys) {
cache.get(unseenKey).remove()
cache.delete(unseenKey)
}
}
private setVisibility(visible: boolean) {
for (const marker of this._allMarkers.values()) {
if (visible) {
marker.getElement().classList.remove("hidden")
} else {
marker.getElement().classList.add("hidden")
}
2023-03-24 19:21:15 +01:00
}
}
2023-03-24 19:21:15 +01:00
private addPoint(feature: Feature, loc: [number, number]): Marker {
2023-03-28 05:13:48 +02:00
let store: Store<Record<string, string>>
if (this._fetchStore) {
store = this._fetchStore(feature.properties.id)
} else {
2023-03-24 19:21:15 +01:00
store = new ImmutableStore(<OsmTags>feature.properties)
}
const { html, iconAnchor } = this._config.RenderIcon(store, true)
html.SetClass("marker")
if (this._onClick !== undefined) {
html.SetClass("cursor-pointer")
}
const el = html.ConstructElement()
2023-03-24 19:21:15 +01:00
if (this._onClick) {
const self = this
el.addEventListener("click", function (ev) {
ev.preventDefault()
self._onClick(feature)
console.log("Got click:", feature)
// Workaround to signal the MapLibreAdaptor to ignore this click
ev["consumed"] = true
2023-03-24 19:21:15 +01:00
})
}
const marker = new Marker({ element: el}).setLngLat(loc).setOffset(iconAnchor).addTo(this._map)
2023-03-25 02:48:24 +01:00
store
.map((tags) => this._config.pitchAlignment.GetRenderValue(tags).Subs(tags).txt)
.addCallbackAndRun((pitchAligment) => marker.setPitchAlignment(<any> pitchAligment))
2023-03-25 02:48:24 +01:00
store
.map((tags) => this._config.rotationAlignment.GetRenderValue(tags).Subs(tags).txt)
.addCallbackAndRun((pitchAligment) => marker.setRotationAlignment(<any> pitchAligment))
if (feature.geometry.type === "Point") {
// When the tags get 'pinged', check that the location didn't change
store.addCallbackAndRunD(() => {
// Check if the location is still the same
const oldLoc = marker.getLngLat()
const newloc = (<Point>feature.geometry).coordinates
if (newloc[0] === oldLoc.lng && newloc[1] === oldLoc.lat) {
return
}
marker.setLngLat({ lon: newloc[0], lat: newloc[1] })
})
}
2023-03-25 02:48:24 +01:00
return marker
2023-03-24 19:21:15 +01:00
}
}
class LineRenderingLayer {
/**
* These are dynamic properties
* @private
*/
private static readonly lineConfigKeys = [
"color",
"width",
"lineCap",
"offset",
"fill",
"fillColor",
2023-03-25 02:48:24 +01:00
] as const
private static readonly lineConfigKeysColor = ["color", "fillColor"] as const
private static readonly lineConfigKeysNumber = ["width", "offset"] as const
private static missingIdTriggered = false
2023-03-24 19:21:15 +01:00
private readonly _map: MlMap
private readonly _config: LineRenderingConfig
private readonly _visibility?: Store<boolean>
2023-03-28 05:13:48 +02:00
private readonly _fetchStore?: (id: string) => Store<Record<string, string>>
private readonly _onClick?: (feature: Feature) => void
2023-03-24 19:21:15 +01:00
private readonly _layername: string
2023-03-25 02:48:24 +01:00
private readonly _listenerInstalledOn: Set<string> = new Set<string>()
private currentSourceData
2023-03-24 19:21:15 +01:00
constructor(
map: MlMap,
features: FeatureSource,
layername: string,
config: LineRenderingConfig,
visibility?: Store<boolean>,
2023-03-28 05:13:48 +02:00
fetchStore?: (id: string) => Store<Record<string, string>>,
onClick?: (feature: Feature) => void
2023-03-24 19:21:15 +01:00
) {
this._layername = layername
this._map = map
this._config = config
this._visibility = visibility
this._fetchStore = fetchStore
this._onClick = onClick
const self = this
features.features.addCallbackAndRunD(() => self.update(features.features))
2023-03-24 19:21:15 +01:00
}
2023-05-19 01:37:31 +02:00
public destruct(): void {
this._map.removeLayer(this._layername + "_polygon")
}
/**
* Calculate the feature-state for maplibre
* @param properties
* @private
*/
2023-03-25 02:48:24 +01:00
private calculatePropsFor(
properties: Record<string, string>
2023-06-14 20:44:01 +02:00
): Partial<Record<(typeof LineRenderingLayer.lineConfigKeys)[number], string>> {
2023-03-25 02:48:24 +01:00
const config = this._config
2023-05-19 01:37:31 +02:00
const calculatedProps: Record<string, string | number> = {}
2023-03-25 02:48:24 +01:00
for (const key of LineRenderingLayer.lineConfigKeys) {
calculatedProps[key] = config[key]?.GetRenderValue(properties)?.Subs(properties).txt
2023-03-25 02:48:24 +01:00
}
calculatedProps.fillColor = calculatedProps.fillColor ?? calculatedProps.color
2023-05-19 01:37:31 +02:00
2023-03-25 02:48:24 +01:00
for (const key of LineRenderingLayer.lineConfigKeysColor) {
2023-05-19 01:37:31 +02:00
let v = <string>calculatedProps[key]
2023-03-25 02:48:24 +01:00
if (v === undefined) {
continue
}
if (v.length == 9 && v.startsWith("#")) {
// This includes opacity
2023-05-19 01:37:31 +02:00
calculatedProps[`${key}-opacity`] = parseInt(v.substring(7), 16) / 256
calculatedProps[key] = v.substring(0, 7)
2023-03-25 02:48:24 +01:00
}
}
2023-05-19 01:37:31 +02:00
calculatedProps["fillColor-opacity"] = calculatedProps["fillColor-opacity"] ?? 0.1
2023-03-25 02:48:24 +01:00
for (const key of LineRenderingLayer.lineConfigKeysNumber) {
2023-05-19 01:37:31 +02:00
calculatedProps[key] = Number(calculatedProps[key])
2023-03-25 02:48:24 +01:00
}
return calculatedProps
}
private async update(featureSource: Store<Feature[]>) {
2023-03-24 19:21:15 +01:00
const map = this._map
while (!map.isStyleLoaded()) {
await Utils.waitFor(100)
}
// After waiting 'till the map has loaded, the data might have changed already
// As such, we only now read the features from the featureSource and compare with the previously set data
const features = featureSource.data
const src = <GeoJSONSource>map.getSource(this._layername)
if (this.currentSourceData === features) {
// Already up to date
return
}
if (src === undefined) {
this.currentSourceData = features
map.addSource(this._layername, {
type: "geojson",
data: {
type: "FeatureCollection",
features,
},
promoteId: "id",
})
// @ts-ignore
2023-03-28 05:13:48 +02:00
const linelayer = this._layername + "_line"
map.addLayer({
source: this._layername,
2023-03-28 05:13:48 +02:00
id: linelayer,
type: "line",
paint: {
"line-color": ["feature-state", "color"],
"line-opacity": ["feature-state", "color-opacity"],
"line-width": ["feature-state", "width"],
"line-offset": ["feature-state", "offset"],
},
layout: {
"line-cap": "round",
},
})
map.on("click", linelayer, (e) => {
// line-layer-listener
e.originalEvent["consumed"] = true
this._onClick(e.features[0])
})
2023-03-28 05:13:48 +02:00
const polylayer = this._layername + "_polygon"
2023-05-19 01:37:31 +02:00
map.addLayer({
source: this._layername,
2023-03-28 05:13:48 +02:00
id: polylayer,
type: "fill",
filter: ["in", ["geometry-type"], ["literal", ["Polygon", "MultiPolygon"]]],
layout: {},
paint: {
"fill-color": ["feature-state", "fillColor"],
2023-05-19 01:37:31 +02:00
"fill-opacity": ["feature-state", "fillColor-opacity"],
},
})
2023-05-19 01:37:31 +02:00
if (this._onClick) {
map.on("click", polylayer, (e) => {
2023-06-01 02:52:21 +02:00
console.log("Got polylayer click:", e)
// polygon-layer-listener
if (e.originalEvent["consumed"]) {
// This is a polygon beneath a marker, we can ignore it
return
}
2023-05-19 01:37:31 +02:00
e.originalEvent["consumed"] = true
console.log("Got features:", e.features, e)
2023-05-19 01:37:31 +02:00
this._onClick(e.features[0])
})
}
2023-03-28 05:13:48 +02:00
2023-03-29 17:21:20 +02:00
this._visibility?.addCallbackAndRunD((visible) => {
try {
map.setLayoutProperty(linelayer, "visibility", visible ? "visible" : "none")
map.setLayoutProperty(polylayer, "visibility", visible ? "visible" : "none")
} catch (e) {
console.warn(
"Error while setting visiblity of layers ",
linelayer,
polylayer,
e
)
}
2023-03-28 05:13:48 +02:00
})
} else {
this.currentSourceData = features
src.setData({
2023-03-24 19:21:15 +01:00
type: "FeatureCollection",
features: this.currentSourceData,
})
}
2023-03-25 02:48:24 +01:00
for (let i = 0; i < features.length; i++) {
const feature = features[i]
const id = feature.properties.id ?? feature.id
if (id === undefined) {
if (!LineRenderingLayer.missingIdTriggered) {
console.trace(
"Got a feature without ID; this causes rendering bugs:",
feature,
"from"
)
LineRenderingLayer.missingIdTriggered = true
}
2023-03-25 02:48:24 +01:00
continue
}
if (this._listenerInstalledOn.has(id)) {
continue
}
if (!map.getSource(this._layername)) {
continue
}
2023-03-25 02:48:24 +01:00
if (this._fetchStore === undefined) {
map.setFeatureState(
{ source: this._layername, id },
2023-03-25 02:48:24 +01:00
this.calculatePropsFor(feature.properties)
)
} else {
const tags = this._fetchStore(id)
this._listenerInstalledOn.add(id)
tags.addCallbackAndRunD((properties) => {
map.setFeatureState(
{ source: this._layername, id },
2023-03-25 02:48:24 +01:00
this.calculatePropsFor(properties)
)
})
}
}
}
}
2023-03-24 19:21:15 +01:00
export default class ShowDataLayer {
2023-03-25 02:48:24 +01:00
private static rangeLayer = new LayerConfig(
<LayerConfigJson>range_layer,
"ShowDataLayer.ts:range.json"
)
private readonly _map: Store<MlMap>
private readonly _options: ShowDataLayerOptions & {
layer: LayerConfig
drawMarkers?: true | boolean
drawLines?: true | boolean
}
2023-05-19 01:37:31 +02:00
private onDestroy: (() => void)[] = []
constructor(
map: Store<MlMap>,
options: ShowDataLayerOptions & {
layer: LayerConfig
drawMarkers?: true | boolean
drawLines?: true | boolean
}
) {
this._map = map
this._options = options
const self = this
2023-05-19 01:37:31 +02:00
this.onDestroy.push(map.addCallbackAndRunD((map) => self.initDrawFeatures(map)))
}
2023-03-28 05:13:48 +02:00
public static showMultipleLayers(
mlmap: UIEventSource<MlMap>,
features: FeatureSource,
layers: LayerConfig[],
options?: Partial<ShowDataLayerOptions>
) {
const perLayer: PerLayerFeatureSourceSplitter<FeatureSourceForLayer> =
new PerLayerFeatureSourceSplitter(
layers.filter((l) => l.source !== null).map((l) => new FilteredLayer(l)),
features,
{
constructStore: (features, layer) => new SimpleFeatureSource(layer, features),
}
)
2023-03-28 05:13:48 +02:00
perLayer.forEach((fs) => {
new ShowDataLayer(mlmap, {
layer: fs.layer.layerDef,
features: fs,
...(options ?? {}),
})
})
}
2023-03-24 19:21:15 +01:00
public static showRange(
map: Store<MlMap>,
features: FeatureSource,
doShowLayer?: Store<boolean>
): ShowDataLayer {
2023-07-16 02:08:43 +02:00
2023-03-24 19:21:15 +01:00
return new ShowDataLayer(map, {
layer: ShowDataLayer.rangeLayer,
features,
doShowLayer,
})
}
public destruct() {}
2023-05-19 01:37:31 +02:00
2023-03-24 19:21:15 +01:00
private zoomToCurrentFeatures(map: MlMap) {
if (this._options.zoomToFeatures) {
const features = this._options.features.features.data
2023-03-24 19:21:15 +01:00
const bbox = BBox.bboxAroundAll(features.map(BBox.get))
2023-06-01 02:52:21 +02:00
map.resize()
map.fitBounds(bbox.toLngLat(), {
padding: { top: 10, bottom: 10, left: 10, right: 10 },
animate: false,
})
}
}
2023-03-24 19:21:15 +01:00
private initDrawFeatures(map: MlMap) {
let { features, doShowLayer, fetchStore, selectedElement, selectedLayer } = this._options
const onClick =
this._options.onClick ??
(this._options.layer.title === undefined
? undefined
: (feature: Feature) => {
selectedElement?.setData(feature)
selectedLayer?.setData(this._options.layer)
})
if (this._options.drawLines !== false) {
for (let i = 0; i < this._options.layer.lineRendering.length; i++) {
const lineRenderingConfig = this._options.layer.lineRendering[i]
2023-05-19 01:37:31 +02:00
const l = new LineRenderingLayer(
map,
features,
this._options.layer.id + "_linerendering_" + i,
lineRenderingConfig,
doShowLayer,
fetchStore,
onClick
)
2023-05-19 01:37:31 +02:00
this.onDestroy.push(l.destruct)
}
2023-03-24 19:21:15 +01:00
}
if (this._options.drawMarkers !== false) {
for (const pointRenderingConfig of this._options.layer.mapRendering) {
new PointRenderingLayer(
map,
features,
pointRenderingConfig,
doShowLayer,
fetchStore,
onClick,
selectedElement
)
}
2023-03-24 19:21:15 +01:00
}
features.features.addCallbackAndRunD((_) => this.zoomToCurrentFeatures(map))
}
}