MapComplete/Logic/FilteredLayer.ts

163 lines
5.8 KiB
TypeScript
Raw Normal View History

import {TagsFilter, TagUtils} from "./Tags";
import {UIEventSource} from "./UIEventSource";
import * as L from "leaflet"
import {Layer} from "leaflet"
import {GeoOperations} from "./GeoOperations";
import {UIElement} from "../UI/UIElement";
import State from "../State";
2020-10-27 01:01:34 +01:00
import LayerConfig from "../Customizations/JSON/LayerConfig";
2020-11-17 02:22:48 +01:00
import Hash from "./Web/Hash";
2020-12-05 03:22:17 +01:00
import LazyElement from "../UI/Base/LazyElement";
2020-07-30 09:59:30 +02:00
2020-06-24 00:35:19 +02:00
/***
*
2020-06-24 00:35:19 +02:00
*/
export class FilteredLayer {
2020-07-22 00:50:30 +02:00
public readonly name: string | UIElement;
public readonly isDisplayed: UIEventSource<boolean> = new UIEventSource(true);
2020-10-27 01:01:34 +01:00
public readonly layerDef: LayerConfig;
private readonly filters: TagsFilter;
2020-06-28 23:33:48 +02:00
private readonly _maxAllowedOverlap: number;
2020-06-24 00:35:19 +02:00
/** The featurecollection from overpass
*/
private _dataFromOverpass: any[];
2020-06-24 00:35:19 +02:00
/**
* The leaflet layer object which should be removed on rerendering
*/
private _geolayer;
2021-01-03 00:19:42 +01:00
private _showOnPopup: (tags: UIEventSource<any>, feature: any) => UIElement;
2020-06-24 00:35:19 +02:00
2021-01-03 00:19:42 +01:00
2020-06-24 00:35:19 +02:00
constructor(
2020-10-27 01:01:34 +01:00
layerDef: LayerConfig,
showOnPopup: ((tags: UIEventSource<any>, feature: any) => UIElement)
2020-06-29 16:21:36 +02:00
) {
2020-07-22 11:01:25 +02:00
this.layerDef = layerDef;
2020-07-22 11:05:04 +02:00
2020-06-29 16:21:36 +02:00
this._showOnPopup = showOnPopup;
2020-06-24 00:35:19 +02:00
this.name = name;
2020-10-27 01:01:34 +01:00
this.filters = layerDef.overpassTags;
this._maxAllowedOverlap = layerDef.hideUnderlayingFeaturesMinPercentage;
2020-06-24 00:35:19 +02:00
}
2021-01-03 00:19:42 +01:00
2020-06-24 00:35:19 +02:00
/**
* The main function to load data into this layer.
* The data that is NOT used by this layer, is returned as a geojson object; the other data is rendered
*/
2021-01-03 00:19:42 +01:00
public SetApplicableData(features: any[]): any[] {
2020-06-24 00:35:19 +02:00
const leftoverFeatures = [];
const selfFeatures = [];
2021-01-03 00:19:42 +01:00
for (let feature of features) {
2020-10-19 13:23:09 +02:00
const tags = TagUtils.proprtiesToKV(feature.properties);
2020-11-17 02:22:48 +01:00
const matches = this.filters.matches(tags);
if (matches) {
2021-01-03 00:19:42 +01:00
selfFeatures.push(feature);
2020-11-17 02:22:48 +01:00
}
if (!matches || this.layerDef.passAllFeatures) {
2020-10-27 14:46:40 +01:00
leftoverFeatures.push(feature);
}
2020-06-24 00:35:19 +02:00
}
2021-01-03 00:19:42 +01:00
this.RenderLayer(selfFeatures)
return leftoverFeatures;
2020-06-24 00:35:19 +02:00
}
private RenderLayer(features: any[]) {
2020-06-24 00:35:19 +02:00
if (this._geolayer !== undefined && this._geolayer !== null) {
// Remove the old geojson layer from the map - we'll reshow all the elements later on anyway
2021-01-02 21:03:40 +01:00
State.state.leafletMap.data.removeLayer(this._geolayer);
2020-06-24 00:35:19 +02:00
}
2020-11-16 01:59:30 +01:00
// We fetch all the data we have to show:
const data = {
2020-06-24 00:35:19 +02:00
type: "FeatureCollection",
features: features
2020-06-24 00:35:19 +02:00
}
2020-11-16 01:59:30 +01:00
let self = this;
2020-06-24 00:35:19 +02:00
this._geolayer = L.geoJSON(data, {
2020-11-27 01:39:54 +01:00
style: feature => {
2020-12-05 03:22:17 +01:00
const tagsSource = State.state.allElements.getEventSourceFor(feature);
2020-11-27 01:39:54 +01:00
return self.layerDef.GenerateLeafletStyle(tagsSource, self._showOnPopup !== undefined);
},
2020-11-17 16:29:51 +01:00
pointToLayer: function (feature, latLng) {
// Point to layer converts the 'point' to a layer object - as the geojson layer natively cannot handle points
// Click handling is done in the next step
2020-12-05 03:22:17 +01:00
const tagSource = State.state.allElements.getEventSourceFor(feature);
2020-11-17 16:29:51 +01:00
2020-11-27 01:39:54 +01:00
const style = self.layerDef.GenerateLeafletStyle(tagSource, self._showOnPopup !== undefined);
2020-11-17 16:29:51 +01:00
let marker;
if (style.icon === undefined) {
marker = L.circle(latLng, {
radius: 25,
color: style.color
2020-11-17 02:22:48 +01:00
});
2020-11-17 16:29:51 +01:00
} else {
marker = L.marker(latLng, {
icon: L.divIcon({
html: style.icon.html.Render(),
className: style.icon.className,
iconAnchor: style.icon.iconAnchor,
iconUrl: style.icon.iconUrl,
popupAnchor: style.icon.popupAnchor,
iconSize: style.icon.iconSize
})
2020-11-17 16:29:51 +01:00
});
}
return marker;
},
onEachFeature: function (feature, layer: Layer) {
2020-11-17 02:22:48 +01:00
2020-11-17 16:29:51 +01:00
if (self._showOnPopup === undefined) {
// No popup contents defined -> don't do anything
return;
}
const popup = L.popup({
autoPan: true,
closeOnEscapeKey: true,
}, layer);
2020-12-05 03:22:17 +01:00
const eventSource = State.state.allElements.getEventSourceFor(feature);
let uiElement: LazyElement = new LazyElement(() => self._showOnPopup(eventSource, feature));
2020-11-17 16:29:51 +01:00
popup.setContent(uiElement.Render());
layer.bindPopup(popup);
// We first render the UIelement (which'll still need an update later on...)
// But at least it'll be visible already
layer.on("click", (e) => {
// We set the element as selected...
2020-12-05 03:22:17 +01:00
uiElement.Activate();
2020-11-17 16:29:51 +01:00
State.state.selectedElement.setData(feature);
});
if (feature.properties.id.replace(/\//g, "_") === Hash.Get().data) {
2020-12-05 03:22:17 +01:00
// This element is in the URL, so this is a share link
// We already open it
uiElement.Activate();
popup.setContent(uiElement.Render());
2021-01-03 00:19:42 +01:00
2020-11-17 16:29:51 +01:00
const center = GeoOperations.centerpoint(feature).geometry.coordinates;
popup.setLatLng({lat: center[1], lng: center[0]});
2021-01-02 21:03:40 +01:00
popup.openOn(State.state.leafletMap.data);
State.state.selectedElement.setData(feature);
uiElement.Update();
2020-11-17 02:22:48 +01:00
}
2020-11-17 16:29:51 +01:00
}
2020-11-17 02:22:48 +01:00
});
2020-06-24 00:35:19 +02:00
this._geolayer.addTo(State.state.leafletMap.data);
2020-11-16 01:59:30 +01:00
}
2020-06-24 00:35:19 +02:00
}