2021-10-15 05:20:02 +02:00
|
|
|
import UserRelatedState from "./UserRelatedState";
|
|
|
|
import {UIEventSource} from "../UIEventSource";
|
|
|
|
import BaseLayer from "../../Models/BaseLayer";
|
|
|
|
import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig";
|
|
|
|
import AvailableBaseLayers from "../Actors/AvailableBaseLayers";
|
|
|
|
import Attribution from "../../UI/BigComponents/Attribution";
|
|
|
|
import Minimap, {MinimapObj} from "../../UI/Base/Minimap";
|
|
|
|
import {Tiles} from "../../Models/TileRange";
|
|
|
|
import BaseUIElement from "../../UI/BaseUIElement";
|
|
|
|
import FilteredLayer from "../../Models/FilteredLayer";
|
|
|
|
import TilesourceConfig from "../../Models/ThemeConfig/TilesourceConfig";
|
|
|
|
import {QueryParameters} from "../Web/QueryParameters";
|
|
|
|
import * as personal from "../../assets/themes/personal/personal.json";
|
|
|
|
import FilterConfig from "../../Models/ThemeConfig/FilterConfig";
|
|
|
|
import ShowOverlayLayer from "../../UI/ShowDataLayer/ShowOverlayLayer";
|
2021-11-09 01:49:07 +01:00
|
|
|
import {FeatureSourceForLayer, Tiled} from "../FeatureSource/FeatureSource";
|
2021-11-08 02:36:01 +01:00
|
|
|
import SimpleFeatureSource from "../FeatureSource/Sources/SimpleFeatureSource";
|
2021-11-12 04:11:53 +01:00
|
|
|
import {LocalStorageSource} from "../Web/LocalStorageSource";
|
|
|
|
import {GeoOperations} from "../GeoOperations";
|
2021-12-10 15:51:08 +01:00
|
|
|
import StaticFeatureSource from "../FeatureSource/Sources/StaticFeatureSource";
|
2021-10-15 05:20:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Contains all the leaflet-map related state
|
|
|
|
*/
|
|
|
|
export default class MapState extends UserRelatedState {
|
|
|
|
|
|
|
|
/**
|
|
|
|
The leaflet instance of the big basemap
|
|
|
|
*/
|
2021-10-15 14:52:11 +02:00
|
|
|
public leafletMap = new UIEventSource<any /*L.Map*/>(undefined, "leafletmap");
|
2021-10-15 05:20:02 +02:00
|
|
|
/**
|
|
|
|
* A list of currently available background layers
|
|
|
|
*/
|
|
|
|
public availableBackgroundLayers: UIEventSource<BaseLayer[]>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The current background layer
|
|
|
|
*/
|
|
|
|
public backgroundLayer: UIEventSource<BaseLayer>;
|
|
|
|
/**
|
|
|
|
* Last location where a click was registered
|
|
|
|
*/
|
|
|
|
public readonly LastClickLocation: UIEventSource<{
|
|
|
|
lat: number;
|
|
|
|
lon: number;
|
|
|
|
}> = new UIEventSource<{ lat: number; lon: number }>(undefined);
|
|
|
|
|
2021-12-10 17:30:50 +01:00
|
|
|
/**
|
|
|
|
* The bounds of the current map view
|
|
|
|
*/
|
|
|
|
public currentView: FeatureSourceForLayer & Tiled;
|
2021-10-15 05:20:02 +02:00
|
|
|
/**
|
|
|
|
* The location as delivered by the GPS
|
|
|
|
*/
|
2021-11-08 02:36:01 +01:00
|
|
|
public currentUserLocation: FeatureSourceForLayer & Tiled;
|
2021-12-10 15:51:08 +01:00
|
|
|
|
2021-11-08 02:36:01 +01:00
|
|
|
/**
|
|
|
|
* All previously visited points
|
|
|
|
*/
|
|
|
|
public historicalUserLocations: FeatureSourceForLayer & Tiled;
|
2021-11-12 04:11:53 +01:00
|
|
|
/**
|
2021-11-14 16:57:14 +01:00
|
|
|
* The number of seconds that the GPS-locations are stored in memory.
|
|
|
|
* Time in seconds
|
2021-11-12 04:11:53 +01:00
|
|
|
*/
|
2021-11-12 18:39:38 +01:00
|
|
|
public gpsLocationHistoryRetentionTime = new UIEventSource(7 * 24 * 60 * 60, "gps_location_retention")
|
2021-11-12 04:11:53 +01:00
|
|
|
public historicalUserLocationsTrack: FeatureSourceForLayer & Tiled;
|
2021-11-08 02:36:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A feature source containing the current home location of the user
|
|
|
|
*/
|
|
|
|
public homeLocation: FeatureSourceForLayer & Tiled
|
2021-10-15 05:20:02 +02:00
|
|
|
|
|
|
|
public readonly mainMapObject: BaseUIElement & MinimapObj;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WHich layers are enabled in the current theme
|
|
|
|
*/
|
|
|
|
public filteredLayers: UIEventSource<FilteredLayer[]> = new UIEventSource<FilteredLayer[]>([], "filteredLayers");
|
|
|
|
/**
|
|
|
|
* Which overlays are shown
|
|
|
|
*/
|
|
|
|
public overlayToggles: { config: TilesourceConfig, isDisplayed: UIEventSource<boolean> }[]
|
|
|
|
|
2021-11-12 18:39:38 +01:00
|
|
|
|
2021-10-15 05:20:02 +02:00
|
|
|
constructor(layoutToUse: LayoutConfig) {
|
|
|
|
super(layoutToUse);
|
|
|
|
|
|
|
|
this.availableBackgroundLayers = AvailableBaseLayers.AvailableLayersAt(this.locationControl);
|
|
|
|
|
2021-11-21 02:44:35 +01:00
|
|
|
let defaultLayer = AvailableBaseLayers.osmCarto
|
|
|
|
const available = this.availableBackgroundLayers.data;
|
|
|
|
for (const layer of available) {
|
|
|
|
if (this.backgroundLayerId.data === layer.id) {
|
|
|
|
defaultLayer = layer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const self = this
|
|
|
|
this.backgroundLayer = new UIEventSource<BaseLayer>(defaultLayer)
|
|
|
|
this.backgroundLayer.addCallbackAndRunD(layer => self.backgroundLayerId.setData(layer.id))
|
|
|
|
|
2021-10-15 05:20:02 +02:00
|
|
|
const attr = new Attribution(
|
|
|
|
this.locationControl,
|
|
|
|
this.osmConnection.userDetails,
|
|
|
|
this.layoutToUse,
|
|
|
|
this.currentBounds
|
|
|
|
);
|
|
|
|
|
|
|
|
// Will write into this.leafletMap
|
|
|
|
this.mainMapObject = Minimap.createMiniMap({
|
|
|
|
background: this.backgroundLayer,
|
|
|
|
location: this.locationControl,
|
|
|
|
leafletMap: this.leafletMap,
|
|
|
|
bounds: this.currentBounds,
|
|
|
|
attribution: attr,
|
|
|
|
lastClickLocation: this.LastClickLocation
|
|
|
|
})
|
|
|
|
|
2021-10-15 14:52:11 +02:00
|
|
|
|
2021-10-15 05:20:02 +02:00
|
|
|
this.overlayToggles = this.layoutToUse.tileLayerSources.filter(c => c.name !== undefined).map(c => ({
|
|
|
|
config: c,
|
2021-10-23 02:46:37 +02:00
|
|
|
isDisplayed: QueryParameters.GetBooleanQueryParameter("overlay-" + c.id, "" + c.defaultState, "Wether or not the overlay " + c.id + " is shown")
|
2021-10-15 05:20:02 +02:00
|
|
|
}))
|
|
|
|
this.filteredLayers = this.InitializeFilteredLayers()
|
|
|
|
|
|
|
|
|
2021-10-15 14:52:11 +02:00
|
|
|
this.lockBounds()
|
|
|
|
this.AddAllOverlaysToMap(this.leafletMap)
|
2021-11-12 18:39:38 +01:00
|
|
|
|
2021-11-08 02:36:01 +01:00
|
|
|
this.initHomeLocation()
|
|
|
|
this.initGpsLocation()
|
2021-11-08 14:18:45 +01:00
|
|
|
this.initUserLocationTrail()
|
2021-12-10 15:51:08 +01:00
|
|
|
this.initCurrentView()
|
2021-10-15 05:20:02 +02:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:34:51 +01:00
|
|
|
public AddAllOverlaysToMap(leafletMap: UIEventSource<any>) {
|
|
|
|
const initialized = new Set()
|
|
|
|
for (const overlayToggle of this.overlayToggles) {
|
|
|
|
new ShowOverlayLayer(overlayToggle.config, leafletMap, overlayToggle.isDisplayed)
|
|
|
|
initialized.add(overlayToggle.config)
|
|
|
|
}
|
2021-10-15 05:20:02 +02:00
|
|
|
|
2021-11-07 16:34:51 +01:00
|
|
|
for (const tileLayerSource of this.layoutToUse.tileLayerSources) {
|
|
|
|
if (initialized.has(tileLayerSource)) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
new ShowOverlayLayer(tileLayerSource, leafletMap)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-10-15 05:20:02 +02:00
|
|
|
|
|
|
|
private lockBounds() {
|
|
|
|
const layout = this.layoutToUse;
|
|
|
|
if (layout.lockLocation) {
|
|
|
|
if (layout.lockLocation === true) {
|
|
|
|
const tile = Tiles.embedded_tile(
|
|
|
|
layout.startLat,
|
|
|
|
layout.startLon,
|
|
|
|
layout.startZoom - 1
|
|
|
|
);
|
|
|
|
const bounds = Tiles.tile_bounds(tile.z, tile.x, tile.y);
|
|
|
|
// We use the bounds to get a sense of distance for this zoom level
|
|
|
|
const latDiff = bounds[0][0] - bounds[1][0];
|
|
|
|
const lonDiff = bounds[0][1] - bounds[1][1];
|
|
|
|
layout.lockLocation = [
|
|
|
|
[layout.startLat - latDiff, layout.startLon - lonDiff],
|
|
|
|
[layout.startLat + latDiff, layout.startLon + lonDiff],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
console.warn("Locking the bounds to ", layout.lockLocation);
|
|
|
|
this.leafletMap.addCallbackAndRunD(map => {
|
|
|
|
// @ts-ignore
|
|
|
|
map.setMaxBounds(layout.lockLocation);
|
|
|
|
map.setMinZoom(layout.startZoom);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-12-10 15:51:08 +01:00
|
|
|
|
|
|
|
private initCurrentView(){
|
2021-12-10 17:30:50 +01:00
|
|
|
let currentViewLayer: FilteredLayer = this.filteredLayers.data.filter(l => l.layerDef.id === "current_view")[0]
|
|
|
|
|
|
|
|
if(currentViewLayer === undefined){
|
|
|
|
// This layer is not needed by the theme and thus unloaded
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let i = 0
|
2021-12-10 15:51:08 +01:00
|
|
|
const features : UIEventSource<{ feature: any, freshness: Date }[]>= this.currentBounds.map(bounds => {
|
2021-12-10 17:30:50 +01:00
|
|
|
if(bounds === undefined){
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
i++
|
2021-12-10 15:51:08 +01:00
|
|
|
const feature = {
|
|
|
|
freshness: new Date(),
|
|
|
|
feature: {
|
2021-12-10 17:30:50 +01:00
|
|
|
type: "Feature",
|
2021-12-10 15:51:08 +01:00
|
|
|
properties:{
|
2021-12-10 17:30:50 +01:00
|
|
|
id:"current_view-"+i,
|
|
|
|
"current_view":"yes"
|
2021-12-10 15:51:08 +01:00
|
|
|
},
|
|
|
|
geometry:{
|
|
|
|
type:"Polygon",
|
2021-12-10 17:30:50 +01:00
|
|
|
coordinates:[[
|
2021-12-10 15:51:08 +01:00
|
|
|
[bounds.maxLon, bounds.maxLat],
|
|
|
|
[bounds.minLon, bounds.maxLat],
|
|
|
|
[bounds.minLon, bounds.minLat],
|
|
|
|
[bounds.maxLon, bounds.minLat],
|
|
|
|
[bounds.maxLon, bounds.maxLat],
|
2021-12-10 17:30:50 +01:00
|
|
|
]]
|
2021-12-10 15:51:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [feature]
|
|
|
|
})
|
2021-12-10 17:30:50 +01:00
|
|
|
|
2021-12-10 15:51:08 +01:00
|
|
|
this.currentView = new SimpleFeatureSource(currentViewLayer,0,features)
|
|
|
|
}
|
2021-11-12 18:39:38 +01:00
|
|
|
|
|
|
|
private initGpsLocation() {
|
2021-11-08 02:36:01 +01:00
|
|
|
// Initialize the gps layer data. This is emtpy for now, the actual writing happens in the Geolocationhandler
|
|
|
|
let gpsLayerDef: FilteredLayer = this.filteredLayers.data.filter(l => l.layerDef.id === "gps_location")[0]
|
|
|
|
this.currentUserLocation = new SimpleFeatureSource(gpsLayerDef, Tiles.tile_index(0, 0, 0));
|
|
|
|
}
|
2021-11-12 18:39:38 +01:00
|
|
|
|
|
|
|
private initUserLocationTrail() {
|
|
|
|
const features = LocalStorageSource.GetParsed<{ feature: any, freshness: Date }[]>("gps_location_history", [])
|
2021-11-12 04:11:53 +01:00
|
|
|
const now = new Date().getTime()
|
|
|
|
features.data = features.data
|
|
|
|
.map(ff => ({feature: ff.feature, freshness: new Date(ff.freshness)}))
|
2021-11-14 16:57:14 +01:00
|
|
|
.filter(ff => (now - ff.freshness.getTime()) < 1000 * this.gpsLocationHistoryRetentionTime.data)
|
2021-11-12 04:11:53 +01:00
|
|
|
features.ping()
|
|
|
|
const self = this;
|
2021-11-08 14:18:45 +01:00
|
|
|
let i = 0
|
|
|
|
this.currentUserLocation.features.addCallbackAndRunD(([location]) => {
|
2021-11-12 18:39:38 +01:00
|
|
|
if (location === undefined) {
|
2021-11-08 14:18:45 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-11-12 18:39:38 +01:00
|
|
|
|
2021-11-12 04:11:53 +01:00
|
|
|
const previousLocation = features.data[features.data.length - 1]
|
2021-11-12 18:39:38 +01:00
|
|
|
if (previousLocation !== undefined) {
|
2021-11-12 04:11:53 +01:00
|
|
|
const d = GeoOperations.distanceBetween(
|
|
|
|
previousLocation.feature.geometry.coordinates,
|
|
|
|
location.feature.geometry.coordinates
|
|
|
|
)
|
2021-11-12 18:39:38 +01:00
|
|
|
let timeDiff = Number.MAX_VALUE // in seconds
|
|
|
|
const olderLocation = features.data[features.data.length - 2]
|
|
|
|
if (olderLocation !== undefined) {
|
2021-11-15 11:51:32 +01:00
|
|
|
timeDiff = (new Date(previousLocation.freshness).getTime() - new Date(olderLocation.freshness).getTime()) / 1000
|
2021-11-12 18:39:38 +01:00
|
|
|
}
|
|
|
|
if (d < 20 && timeDiff < 60) {
|
2021-11-12 04:11:53 +01:00
|
|
|
// Do not append changes less then 20m - it's probably noise anyway
|
|
|
|
return;
|
|
|
|
}
|
2021-11-08 14:18:45 +01:00
|
|
|
}
|
2021-11-12 18:39:38 +01:00
|
|
|
|
2021-11-12 04:11:53 +01:00
|
|
|
const feature = JSON.parse(JSON.stringify(location.feature))
|
2021-11-12 18:39:38 +01:00
|
|
|
feature.properties.id = "gps/" + features.data.length
|
2021-11-12 04:11:53 +01:00
|
|
|
i++
|
|
|
|
features.data.push({feature, freshness: new Date()})
|
2021-11-08 14:18:45 +01:00
|
|
|
features.ping()
|
|
|
|
})
|
2021-11-12 18:39:38 +01:00
|
|
|
|
|
|
|
|
2021-11-12 04:11:53 +01:00
|
|
|
let gpsLayerDef: FilteredLayer = this.filteredLayers.data.filter(l => l.layerDef.id === "gps_location_history")[0]
|
2021-11-08 14:18:45 +01:00
|
|
|
this.historicalUserLocations = new SimpleFeatureSource(gpsLayerDef, Tiles.tile_index(0, 0, 0), features);
|
2021-11-09 01:49:07 +01:00
|
|
|
this.changes.useLocationHistory(this)
|
2021-11-12 04:11:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
const asLine = features.map(allPoints => {
|
2021-11-12 18:39:38 +01:00
|
|
|
if (allPoints === undefined || allPoints.length < 2) {
|
2021-11-12 04:11:53 +01:00
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
const feature = {
|
|
|
|
type: "Feature",
|
2021-11-12 18:39:38 +01:00
|
|
|
properties: {
|
|
|
|
"id": "location_track",
|
2021-11-12 04:11:53 +01:00
|
|
|
"_date:now": new Date().toISOString(),
|
|
|
|
},
|
2021-11-12 18:39:38 +01:00
|
|
|
geometry: {
|
2021-11-12 04:11:53 +01:00
|
|
|
type: "LineString",
|
|
|
|
coordinates: allPoints.map(ff => ff.feature.geometry.coordinates)
|
|
|
|
}
|
|
|
|
}
|
2021-11-12 18:39:38 +01:00
|
|
|
|
2021-11-12 04:11:53 +01:00
|
|
|
self.allElements.ContainingFeatures.set(feature.properties.id, feature)
|
2021-11-12 18:39:38 +01:00
|
|
|
|
2021-11-12 04:11:53 +01:00
|
|
|
return [{
|
|
|
|
feature,
|
|
|
|
freshness: new Date()
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
let gpsLineLayerDef: FilteredLayer = this.filteredLayers.data.filter(l => l.layerDef.id === "gps_track")[0]
|
|
|
|
this.historicalUserLocationsTrack = new SimpleFeatureSource(gpsLineLayerDef, Tiles.tile_index(0, 0, 0), asLine);
|
2021-11-08 14:18:45 +01:00
|
|
|
}
|
2021-11-08 02:36:01 +01:00
|
|
|
|
|
|
|
private initHomeLocation() {
|
|
|
|
const empty = []
|
|
|
|
const feature = UIEventSource.ListStabilized(this.osmConnection.userDetails.map(userDetails => {
|
|
|
|
|
|
|
|
if (userDetails === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const home = userDetails.home;
|
|
|
|
if (home === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return [home.lon, home.lat]
|
|
|
|
})).map(homeLonLat => {
|
|
|
|
if (homeLonLat === undefined) {
|
|
|
|
return empty
|
|
|
|
}
|
|
|
|
return [{
|
|
|
|
feature: {
|
|
|
|
"type": "Feature",
|
|
|
|
"properties": {
|
2021-11-12 18:39:38 +01:00
|
|
|
"id": "home",
|
2021-11-08 02:36:01 +01:00
|
|
|
"user:home": "yes",
|
|
|
|
"_lon": homeLonLat[0],
|
|
|
|
"_lat": homeLonLat[1]
|
|
|
|
},
|
|
|
|
"geometry": {
|
|
|
|
"type": "Point",
|
|
|
|
"coordinates": homeLonLat
|
|
|
|
}
|
|
|
|
}, freshness: new Date()
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
|
|
|
|
const flayer = this.filteredLayers.data.filter(l => l.layerDef.id === "home_location")[0]
|
|
|
|
this.homeLocation = new SimpleFeatureSource(flayer, Tiles.tile_index(0, 0, 0), feature)
|
|
|
|
|
|
|
|
}
|
2021-10-15 14:52:11 +02:00
|
|
|
|
2021-10-15 05:20:02 +02:00
|
|
|
private InitializeFilteredLayers() {
|
|
|
|
|
|
|
|
const layoutToUse = this.layoutToUse;
|
|
|
|
const empty = []
|
|
|
|
const flayers: FilteredLayer[] = [];
|
|
|
|
for (const layer of layoutToUse.layers) {
|
|
|
|
let isDisplayed: UIEventSource<boolean>
|
|
|
|
if (layoutToUse.id === personal.id) {
|
|
|
|
isDisplayed = this.osmConnection.GetPreference("personal-theme-layer-" + layer.id + "-enabled")
|
|
|
|
.map(value => value === "yes", [], enabled => {
|
|
|
|
return enabled ? "yes" : "";
|
|
|
|
})
|
|
|
|
} else {
|
2021-10-23 02:46:37 +02:00
|
|
|
isDisplayed = QueryParameters.GetBooleanQueryParameter(
|
2021-10-15 05:20:02 +02:00
|
|
|
"layer-" + layer.id,
|
2021-12-03 02:29:25 +01:00
|
|
|
""+layer.shownByDefault,
|
2021-10-15 05:20:02 +02:00
|
|
|
"Wether or not layer " + layer.id + " is shown"
|
2021-10-23 02:46:37 +02:00
|
|
|
)
|
2021-10-15 05:20:02 +02:00
|
|
|
}
|
|
|
|
const flayer = {
|
|
|
|
isDisplayed: isDisplayed,
|
|
|
|
layerDef: layer,
|
|
|
|
appliedFilters: new UIEventSource<{ filter: FilterConfig, selected: number }[]>([]),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (layer.filters.length > 0) {
|
|
|
|
const filtersPerName = new Map<string, FilterConfig>()
|
|
|
|
layer.filters.forEach(f => filtersPerName.set(f.id, f))
|
|
|
|
const qp = QueryParameters.GetQueryParameter("filter-" + layer.id, "", "Filtering state for a layer")
|
2021-11-21 02:44:35 +01:00
|
|
|
flayer.appliedFilters.map(filters => (filters ?? []).map(f => f.filter.id + "." + f.selected).join(","), [], textual => {
|
2021-10-15 05:20:02 +02:00
|
|
|
if (textual.length === 0) {
|
|
|
|
return empty
|
|
|
|
}
|
|
|
|
return textual.split(",").map(part => {
|
|
|
|
const [filterId, selected] = part.split(".");
|
|
|
|
return {filter: filtersPerName.get(filterId), selected: Number(selected)}
|
|
|
|
}).filter(f => f.filter !== undefined && !isNaN(f.selected))
|
|
|
|
}).syncWith(qp, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
flayers.push(flayer);
|
|
|
|
}
|
|
|
|
return new UIEventSource<FilteredLayer[]>(flayers);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|