2020-08-30 01:13:18 +02:00
|
|
|
import {TagsFilter, TagUtils} from "./Tags";
|
2020-08-17 17:23:15 +02:00
|
|
|
import {UIEventSource} from "./UIEventSource";
|
2020-06-24 00:35:19 +02:00
|
|
|
import L from "leaflet"
|
2020-07-26 02:01:34 +02:00
|
|
|
import {GeoOperations} from "./GeoOperations";
|
|
|
|
import {UIElement} from "../UI/UIElement";
|
|
|
|
import {LayerDefinition} from "../Customizations/LayerDefinition";
|
2020-07-26 19:13:52 +02:00
|
|
|
import codegrid from "codegrid-js";
|
2020-07-31 01:45:54 +02:00
|
|
|
import {State} from "../State";
|
2020-07-30 09:59:30 +02:00
|
|
|
|
2020-06-24 00:35:19 +02:00
|
|
|
/***
|
|
|
|
* A filtered layer is a layer which offers a 'set-data' function
|
|
|
|
* It is initialized with a tagfilter.
|
|
|
|
*
|
|
|
|
* When geojson-data is given to 'setData', all the geojson matching the filter, is rendered on this layer.
|
|
|
|
* If it is not rendered, it is returned in a 'leftOver'-geojson; which can be consumed by the next layer.
|
|
|
|
*
|
|
|
|
* This also makes sure that no objects are rendered twice if they are applicable on two layers
|
|
|
|
*/
|
|
|
|
export class FilteredLayer {
|
|
|
|
|
2020-07-22 00:50:30 +02:00
|
|
|
public readonly name: string | UIElement;
|
2020-06-24 00:35:19 +02:00
|
|
|
public readonly filters: TagsFilter;
|
2020-07-15 15:55:08 +02:00
|
|
|
public readonly isDisplayed: UIEventSource<boolean> = new UIEventSource(true);
|
2020-08-30 01:13:18 +02:00
|
|
|
private readonly combinedIsDisplayed : UIEventSource<boolean>;
|
2020-07-22 11:01:25 +02:00
|
|
|
public readonly layerDef: LayerDefinition;
|
2020-06-28 23:33:48 +02:00
|
|
|
private readonly _maxAllowedOverlap: number;
|
2020-06-24 00:35:19 +02:00
|
|
|
|
2020-07-30 16:34:06 +02:00
|
|
|
private readonly _style: (properties) => { color: string, weight?: number, icon: { iconUrl: string, iconSize? : number[], popupAnchor?: number[], iconAnchor?:number[] } };
|
2020-06-24 00:35:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
/** The featurecollection from overpass
|
|
|
|
*/
|
2020-08-30 01:13:18 +02:00
|
|
|
private _dataFromOverpass: any[];
|
|
|
|
private readonly _wayHandling: number;
|
2020-06-24 00:35:19 +02:00
|
|
|
/** List of new elements, geojson features
|
|
|
|
*/
|
|
|
|
private _newElements = [];
|
|
|
|
/**
|
|
|
|
* The leaflet layer object which should be removed on rerendering
|
|
|
|
*/
|
|
|
|
private _geolayer;
|
2020-09-14 20:16:03 +02:00
|
|
|
|
2020-07-22 01:07:32 +02:00
|
|
|
private _showOnPopup: (tags: UIEventSource<any>, feature: any) => UIElement;
|
2020-06-24 00:35:19 +02:00
|
|
|
|
2020-09-26 01:11:17 +02:00
|
|
|
private static readonly grid = codegrid.CodeGrid("./tiles/");
|
2020-07-26 19:13:52 +02:00
|
|
|
|
2020-06-24 00:35:19 +02:00
|
|
|
constructor(
|
2020-07-22 11:01:25 +02:00
|
|
|
layerDef: LayerDefinition,
|
2020-07-24 15:52:21 +02:00
|
|
|
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
|
|
|
|
|
|
|
this._wayHandling = layerDef.wayHandling;
|
2020-06-29 16:21:36 +02:00
|
|
|
this._showOnPopup = showOnPopup;
|
2020-09-25 23:10:40 +02:00
|
|
|
this._style = (tags) => {
|
|
|
|
if(layerDef.style === undefined){
|
2020-08-22 02:12:46 +02:00
|
|
|
return {icon: {iconUrl: "./assets/bug.svg"}, color: "#000"};
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
2020-09-25 23:10:40 +02:00
|
|
|
|
|
|
|
const obj = layerDef.style(tags);
|
|
|
|
if(obj.weight && typeof (obj.weight) === "string"){
|
|
|
|
obj.weight = Number(obj.weight);// Weight MUST be a number, otherwise leaflet does weird things. see https://github.com/Leaflet/Leaflet/issues/6075
|
|
|
|
if(isNaN(obj.weight)){
|
|
|
|
obj.weight = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
};
|
2020-06-24 00:35:19 +02:00
|
|
|
this.name = name;
|
2020-07-22 11:01:25 +02:00
|
|
|
this.filters = layerDef.overpassFilter;
|
|
|
|
this._maxAllowedOverlap = layerDef.maxAllowedOverlapPercentage;
|
2020-07-15 15:55:08 +02:00
|
|
|
const self = this;
|
2020-08-30 01:13:18 +02:00
|
|
|
this.combinedIsDisplayed = this.isDisplayed.map<boolean>(isDisplayed => {
|
|
|
|
return isDisplayed && State.state.locationControl.data.zoom >= self.layerDef.minzoom
|
|
|
|
},
|
|
|
|
[State.state.locationControl]
|
|
|
|
);
|
|
|
|
this.combinedIsDisplayed.addCallback(function (isDisplayed) {
|
2020-07-31 01:45:54 +02:00
|
|
|
const map = State.state.bm.map;
|
2020-07-15 15:55:08 +02:00
|
|
|
if (self._geolayer !== undefined && self._geolayer !== null) {
|
|
|
|
if (isDisplayed) {
|
2020-07-31 01:45:54 +02:00
|
|
|
self._geolayer.addTo(map);
|
2020-07-15 15:55:08 +02:00
|
|
|
} else {
|
2020-07-31 01:45:54 +02:00
|
|
|
map.removeLayer(self._geolayer);
|
2020-07-15 15:55:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
2020-07-26 02:01:34 +02:00
|
|
|
|
|
|
|
static fromDefinition(
|
2020-07-31 01:45:54 +02:00
|
|
|
definition,
|
2020-07-26 02:01:34 +02:00
|
|
|
showOnPopup: (tags: UIEventSource<any>, feature: any) => UIElement):
|
|
|
|
FilteredLayer {
|
|
|
|
return new FilteredLayer(
|
2020-07-31 01:45:54 +02:00
|
|
|
definition, showOnPopup);
|
2020-07-26 02:01:34 +02: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
|
|
|
|
*/
|
|
|
|
public SetApplicableData(geojson: any): any {
|
|
|
|
const leftoverFeatures = [];
|
|
|
|
const selfFeatures = [];
|
2020-07-22 00:50:30 +02:00
|
|
|
for (let feature of geojson.features) {
|
2020-06-24 00:35:19 +02:00
|
|
|
// feature.properties contains all the properties
|
2020-08-30 01:13:18 +02:00
|
|
|
const tags = TagUtils.proprtiesToKV(feature.properties);
|
|
|
|
|
2020-06-24 00:35:19 +02:00
|
|
|
if (this.filters.matches(tags)) {
|
2020-07-26 19:13:52 +02:00
|
|
|
const centerPoint = GeoOperations.centerpoint(feature);
|
2020-09-26 01:43:20 +02:00
|
|
|
feature.properties["_surface"] = "" + GeoOperations.surfaceAreaInSqMeters(feature);
|
|
|
|
const lat = centerPoint.geometry.coordinates[1];
|
|
|
|
const lon = centerPoint.geometry.coordinates[0]
|
|
|
|
feature.properties["_lon"] = "" + lat; // We expect a string here for lat/lon
|
|
|
|
feature.properties["_lat"] = "" + lon;
|
|
|
|
// But the codegrid SHOULD be a number!
|
2020-07-26 19:13:52 +02:00
|
|
|
FilteredLayer.grid.getCode(lat, lon, (error, code) => {
|
|
|
|
if (error === null) {
|
|
|
|
feature.properties["_country"] = code;
|
2020-09-26 01:43:20 +02:00
|
|
|
} else {
|
|
|
|
console.warn("Could not determine country for", feature.properties.id, error);
|
2020-07-26 19:13:52 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-07-22 11:05:04 +02:00
|
|
|
if (feature.geometry.type !== "Point") {
|
|
|
|
if (this._wayHandling === LayerDefinition.WAYHANDLING_CENTER_AND_WAY) {
|
2020-07-26 19:13:52 +02:00
|
|
|
selfFeatures.push(centerPoint);
|
2020-07-22 11:05:04 +02:00
|
|
|
} else if (this._wayHandling === LayerDefinition.WAYHANDLING_CENTER_ONLY) {
|
2020-07-26 19:13:52 +02:00
|
|
|
feature = centerPoint;
|
2020-07-22 00:50:30 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
selfFeatures.push(feature);
|
|
|
|
} else {
|
|
|
|
leftoverFeatures.push(feature);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.RenderLayer({
|
|
|
|
type: "FeatureCollection",
|
|
|
|
features: selfFeatures
|
|
|
|
})
|
|
|
|
|
|
|
|
const notShadowed = [];
|
|
|
|
for (const feature of leftoverFeatures) {
|
2020-07-18 20:40:51 +02:00
|
|
|
if (this._maxAllowedOverlap !== undefined && this._maxAllowedOverlap > 0) {
|
2020-06-28 23:33:48 +02:00
|
|
|
if (GeoOperations.featureIsContainedInAny(feature, selfFeatures, this._maxAllowedOverlap)) {
|
2020-06-24 00:35:19 +02:00
|
|
|
// This feature is filtered away
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
notShadowed.push(feature);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: "FeatureCollection",
|
|
|
|
features: notShadowed
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public AddNewElement(element) {
|
|
|
|
this._newElements.push(element);
|
|
|
|
console.log("Element added");
|
2020-08-28 03:16:21 +02:00
|
|
|
this.RenderLayer({features:this._dataFromOverpass}); // Update the layer
|
2020-06-24 00:35:19 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private RenderLayer(data) {
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
if (this._geolayer !== undefined && this._geolayer !== null) {
|
2020-08-28 03:16:21 +02:00
|
|
|
// Remove the old geojson layer from the map - we'll reshow all the elements later on anyway
|
2020-07-31 01:45:54 +02:00
|
|
|
State.state.bm.map.removeLayer(this._geolayer);
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
2020-08-28 03:16:21 +02:00
|
|
|
|
|
|
|
const oldData = this._dataFromOverpass ?? [];
|
|
|
|
|
|
|
|
// We keep track of all the ids that are freshly loaded in order to avoid adding duplicates
|
|
|
|
const idsFromOverpass: Set<number> = new Set<number>();
|
|
|
|
// A list of all the features to show
|
2020-06-24 00:35:19 +02:00
|
|
|
const fusedFeatures = [];
|
2020-08-28 03:16:21 +02:00
|
|
|
// First, we add all the fresh data:
|
2020-06-24 00:35:19 +02:00
|
|
|
for (const feature of data.features) {
|
2020-08-28 03:16:21 +02:00
|
|
|
idsFromOverpass.add(feature.properties.id);
|
|
|
|
fusedFeatures.push(feature);
|
|
|
|
}
|
|
|
|
// Now we add all the stale data
|
|
|
|
for (const feature of oldData) {
|
|
|
|
if (idsFromOverpass.has(feature.properties.id)) {
|
|
|
|
continue; // Feature already loaded and a fresher version is available
|
|
|
|
}
|
|
|
|
idsFromOverpass.add(feature.properties.id);
|
2020-06-24 00:35:19 +02:00
|
|
|
fusedFeatures.push(feature);
|
|
|
|
}
|
2020-09-02 11:37:34 +02:00
|
|
|
this._dataFromOverpass = fusedFeatures;
|
2020-06-24 00:35:19 +02:00
|
|
|
|
|
|
|
for (const feature of this._newElements) {
|
2020-09-02 11:37:34 +02:00
|
|
|
if (!idsFromOverpass.has(feature.properties.id)) {
|
2020-06-24 00:35:19 +02:00
|
|
|
// This element is not yet uploaded or not yet visible in overpass
|
|
|
|
// We include it in the layer
|
|
|
|
fusedFeatures.push(feature);
|
|
|
|
}
|
|
|
|
}
|
2020-08-28 03:16:21 +02:00
|
|
|
|
2020-06-24 00:35:19 +02:00
|
|
|
|
|
|
|
// We use a new, fused dataset
|
|
|
|
data = {
|
|
|
|
type: "FeatureCollection",
|
|
|
|
features: fusedFeatures
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// The data is split in two parts: the poinst and the rest
|
|
|
|
// The points get a special treatment in order to render them properly
|
|
|
|
// Note that some features might get a point representation as well
|
|
|
|
|
|
|
|
|
|
|
|
this._geolayer = L.geoJSON(data, {
|
|
|
|
style: function (feature) {
|
|
|
|
return self._style(feature.properties);
|
|
|
|
},
|
|
|
|
pointToLayer: function (feature, latLng) {
|
|
|
|
const style = self._style(feature.properties);
|
|
|
|
let marker;
|
2020-09-25 23:10:40 +02:00
|
|
|
if (style.icon === undefined) {
|
|
|
|
marker = L.circle(latLng, {
|
|
|
|
radius: 25,
|
|
|
|
color: style.color
|
|
|
|
});
|
|
|
|
|
|
|
|
} else if (style.icon.iconUrl.startsWith("$circle")) {
|
|
|
|
marker = L.circle(latLng, {
|
|
|
|
radius: 25,
|
|
|
|
color: style.color
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (style.icon.iconSize === undefined) {
|
|
|
|
style.icon.iconSize = [50, 50]
|
|
|
|
}
|
|
|
|
|
|
|
|
marker = L.marker(latLng, {
|
|
|
|
icon: new L.icon(style.icon),
|
|
|
|
});
|
|
|
|
}
|
2020-07-31 01:45:54 +02:00
|
|
|
let eventSource = State.state.allElements.addOrGetElement(feature);
|
2020-09-10 18:02:41 +02:00
|
|
|
const popup = L.popup({}, marker);
|
|
|
|
let uiElement: UIElement;
|
|
|
|
let content = undefined;
|
2020-07-27 00:14:34 +02:00
|
|
|
marker.bindPopup(popup)
|
2020-08-30 01:13:18 +02:00
|
|
|
.on("popupopen", () => {
|
2020-09-10 18:02:41 +02:00
|
|
|
if (content === undefined) {
|
|
|
|
uiElement = self._showOnPopup(eventSource, feature);
|
|
|
|
// Lazily create the content
|
|
|
|
content = uiElement.Render();
|
|
|
|
}
|
|
|
|
popup.setContent(content);
|
2020-07-27 00:14:34 +02:00
|
|
|
uiElement.Update();
|
|
|
|
});
|
2020-06-24 00:35:19 +02:00
|
|
|
return marker;
|
|
|
|
},
|
|
|
|
|
|
|
|
onEachFeature: function (feature, layer) {
|
2020-07-30 16:34:06 +02:00
|
|
|
|
2020-08-07 16:01:18 +02:00
|
|
|
// We monky-patch the feature element with an update-style
|
2020-07-30 16:34:06 +02:00
|
|
|
feature.updateStyle = () => {
|
2020-07-16 17:24:18 +02:00
|
|
|
if (layer.setIcon) {
|
2020-09-18 12:00:38 +02:00
|
|
|
const style = self._style(feature.properties);
|
|
|
|
const icon = style.icon;
|
2020-08-23 16:59:06 +02:00
|
|
|
if (icon.iconUrl) {
|
2020-09-18 12:00:38 +02:00
|
|
|
if (icon.iconUrl.startsWith("$circle")) {
|
|
|
|
// pass
|
|
|
|
} else {
|
|
|
|
layer.setIcon(L.icon(icon))
|
|
|
|
}
|
2020-08-23 16:59:06 +02:00
|
|
|
}
|
2020-07-16 17:24:18 +02:00
|
|
|
} else {
|
2020-08-07 16:01:18 +02:00
|
|
|
self._geolayer.setStyle(function (featureX) {
|
2020-09-09 18:42:13 +02:00
|
|
|
return self._style(featureX.properties);
|
2020-07-16 17:24:18 +02:00
|
|
|
});
|
|
|
|
}
|
2020-07-30 16:34:06 +02:00
|
|
|
}
|
|
|
|
|
2020-07-31 01:45:54 +02:00
|
|
|
let eventSource = State.state.allElements.addOrGetElement(feature);
|
2020-07-30 16:34:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
eventSource.addCallback(feature.updateStyle);
|
2020-06-29 16:21:36 +02:00
|
|
|
|
2020-07-01 16:32:17 +02:00
|
|
|
layer.on("click", function (e) {
|
2020-08-30 01:13:18 +02:00
|
|
|
State.state.selectedElement.data?.feature.updateStyle();
|
2020-07-31 01:45:54 +02:00
|
|
|
State.state.selectedElement.setData({feature: feature});
|
2020-08-07 16:01:18 +02:00
|
|
|
feature.updateStyle()
|
2020-07-26 23:28:31 +02:00
|
|
|
if (feature.geometry.type === "Point") {
|
|
|
|
return; // Points bind there own popups
|
|
|
|
}
|
|
|
|
|
2020-07-22 01:07:32 +02:00
|
|
|
const uiElement = self._showOnPopup(eventSource, feature);
|
2020-08-30 01:13:18 +02:00
|
|
|
|
|
|
|
L.popup({
|
2020-07-26 02:01:34 +02:00
|
|
|
autoPan: true,
|
2020-08-30 01:13:18 +02:00
|
|
|
}).setContent(uiElement.Render())
|
2020-07-07 15:08:52 +02:00
|
|
|
.setLatLng(e.latlng)
|
2020-07-31 01:45:54 +02:00
|
|
|
.openOn(State.state.bm.map);
|
2020-09-14 20:16:03 +02:00
|
|
|
uiElement.Update();
|
2020-07-01 16:32:17 +02:00
|
|
|
L.DomEvent.stop(e); // Marks the event as consumed
|
2020-06-24 00:35:19 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-08-30 01:13:18 +02:00
|
|
|
if (this.combinedIsDisplayed.data) {
|
2020-07-31 01:45:54 +02:00
|
|
|
this._geolayer.addTo(State.state.bm.map);
|
2020-07-15 15:55:08 +02:00
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|