2021-01-02 16:04:16 +01:00
|
|
|
import * as editorlayerindex from "../../assets/editor-layer-index.json"
|
|
|
|
import {BaseLayer} from "../../Models/BaseLayer";
|
|
|
|
import * as L from "leaflet";
|
2021-01-02 21:03:40 +01:00
|
|
|
import * as X from "leaflet-providers";
|
|
|
|
import {UIEventSource} from "../UIEventSource";
|
|
|
|
import {GeoOperations} from "../GeoOperations";
|
2021-01-02 16:04:16 +01:00
|
|
|
import {TileLayer} from "leaflet";
|
|
|
|
import {Utils} from "../../Utils";
|
2020-09-25 21:58:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates which layers are available at the current location
|
2021-01-02 16:04:16 +01:00
|
|
|
* Changes the basemap
|
2020-09-25 21:58:29 +02:00
|
|
|
*/
|
|
|
|
export default class AvailableBaseLayers {
|
|
|
|
|
2021-01-02 16:04:16 +01:00
|
|
|
|
|
|
|
public static osmCarto: BaseLayer =
|
|
|
|
{
|
|
|
|
id: "osm",
|
|
|
|
name: "OpenStreetMap",
|
|
|
|
layer: AvailableBaseLayers.CreateBackgroundLayer("osm", "OpenStreetMap",
|
|
|
|
"https://tile.openstreetmap.org/{z}/{x}/{y}.png", "OpenStreetMap", "https://openStreetMap.org/copyright",
|
|
|
|
19,
|
|
|
|
false, false),
|
|
|
|
feature: null,
|
|
|
|
max_zoom: 19,
|
|
|
|
min_zoom: 0
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:00:20 +02:00
|
|
|
|
2020-09-27 23:37:47 +02:00
|
|
|
public static layerOverview = AvailableBaseLayers.LoadRasterIndex().concat(AvailableBaseLayers.LoadProviderIndex());
|
2020-09-27 01:38:51 +02:00
|
|
|
public availableEditorLayers: UIEventSource<BaseLayer[]>;
|
2020-09-25 21:58:29 +02:00
|
|
|
|
2021-01-02 21:03:40 +01:00
|
|
|
constructor(location: UIEventSource<{ lat: number, lon: number, zoom: number }>) {
|
2020-09-25 21:58:29 +02:00
|
|
|
const self = this;
|
|
|
|
this.availableEditorLayers =
|
2021-01-02 16:04:16 +01:00
|
|
|
location.map(
|
2020-09-25 21:58:29 +02:00
|
|
|
(currentLocation) => {
|
2021-01-02 21:03:40 +01:00
|
|
|
|
|
|
|
if(currentLocation === undefined){
|
|
|
|
return AvailableBaseLayers.layerOverview;
|
|
|
|
}
|
|
|
|
|
2020-09-25 21:58:29 +02:00
|
|
|
const currentLayers = self.availableEditorLayers?.data;
|
|
|
|
const newLayers = AvailableBaseLayers.AvailableLayersAt(currentLocation?.lon, currentLocation?.lat);
|
|
|
|
|
|
|
|
if (currentLayers === undefined) {
|
|
|
|
return newLayers;
|
|
|
|
}
|
|
|
|
if (newLayers.length !== currentLayers.length) {
|
|
|
|
return newLayers;
|
|
|
|
}
|
|
|
|
for (let i = 0; i < newLayers.length; i++) {
|
|
|
|
if (newLayers[i].name !== currentLayers[i].name) {
|
|
|
|
return newLayers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-27 01:38:51 +02:00
|
|
|
return currentLayers;
|
2020-09-25 21:58:29 +02:00
|
|
|
});
|
|
|
|
|
2020-09-27 01:38:51 +02:00
|
|
|
|
2021-01-02 21:03:40 +01:00
|
|
|
|
2020-09-25 21:58:29 +02:00
|
|
|
}
|
|
|
|
|
2021-01-02 16:04:16 +01:00
|
|
|
private static AvailableLayersAt(lon: number, lat: number): BaseLayer[] {
|
|
|
|
const availableLayers = [AvailableBaseLayers.osmCarto]
|
2020-09-25 21:58:29 +02:00
|
|
|
const globalLayers = [];
|
2021-01-02 21:03:40 +01:00
|
|
|
for (const layerOverviewItem of AvailableBaseLayers.layerOverview) {
|
|
|
|
const layer = layerOverviewItem;
|
|
|
|
|
2020-09-27 23:37:47 +02:00
|
|
|
if (layer.feature?.geometry === undefined || layer.feature?.geometry === null) {
|
2020-09-25 21:58:29 +02:00
|
|
|
globalLayers.push(layer);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lon === undefined || lat === undefined) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GeoOperations.inside([lon, lat], layer.feature)) {
|
|
|
|
availableLayers.push(layer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return availableLayers.concat(globalLayers);
|
|
|
|
}
|
|
|
|
|
2020-09-27 01:38:51 +02:00
|
|
|
private static LoadRasterIndex(): BaseLayer[] {
|
|
|
|
const layers: BaseLayer[] = []
|
2020-09-25 21:58:29 +02:00
|
|
|
// @ts-ignore
|
|
|
|
const features = editorlayerindex.features;
|
|
|
|
for (const i in features) {
|
|
|
|
const layer = features[i];
|
|
|
|
const props = layer.properties;
|
|
|
|
|
2020-09-27 01:38:51 +02:00
|
|
|
if (props.id === "Bing") {
|
2020-09-25 21:58:29 +02:00
|
|
|
// Doesnt work
|
|
|
|
continue;
|
|
|
|
}
|
2020-09-27 01:38:51 +02:00
|
|
|
|
2020-09-27 18:45:37 +02:00
|
|
|
if (props.id === "MAPNIK") {
|
2020-09-27 01:38:51 +02:00
|
|
|
// Already added by default
|
2020-09-25 21:58:29 +02:00
|
|
|
continue;
|
|
|
|
}
|
2020-09-27 01:38:51 +02:00
|
|
|
|
|
|
|
if (props.overlay) {
|
2020-09-25 21:58:29 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (props.url.toLowerCase().indexOf("apikey") > 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-01-02 16:04:16 +01:00
|
|
|
|
|
|
|
if (props.max_zoom < 19) {
|
2020-09-27 18:45:37 +02:00
|
|
|
// We want users to zoom to level 19 when adding a point
|
|
|
|
// If they are on a layer which hasn't enough precision, they can not zoom far enough. This is confusing, so we don't use this layer
|
2020-09-25 21:58:29 +02:00
|
|
|
continue;
|
|
|
|
}
|
2020-09-27 18:45:37 +02:00
|
|
|
|
2021-01-02 16:04:16 +01:00
|
|
|
if (props.name === undefined) {
|
2020-09-27 01:38:51 +02:00
|
|
|
console.warn("Editor layer index: name not defined on ", props)
|
2020-09-25 23:00:20 +02:00
|
|
|
continue
|
|
|
|
}
|
2020-09-25 21:58:29 +02:00
|
|
|
|
2021-01-02 16:04:16 +01:00
|
|
|
const leafletLayer = AvailableBaseLayers.CreateBackgroundLayer(
|
2020-09-25 21:58:29 +02:00
|
|
|
props.id,
|
|
|
|
props.name,
|
|
|
|
props.url,
|
|
|
|
props.name,
|
2020-09-27 18:45:37 +02:00
|
|
|
props.license_url,
|
2020-09-25 21:58:29 +02:00
|
|
|
props.max_zoom,
|
|
|
|
props.type.toLowerCase() === "wms",
|
|
|
|
props.type.toLowerCase() === "wmts"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Note: if layer.geometry is null, there is global coverage for this layer
|
|
|
|
layers.push({
|
|
|
|
id: props.id,
|
2020-09-27 01:38:51 +02:00
|
|
|
max_zoom: props.max_zoom ?? 25,
|
|
|
|
min_zoom: props.min_zoom ?? 1,
|
2020-09-25 21:58:29 +02:00
|
|
|
name: props.name,
|
2020-09-27 01:38:51 +02:00
|
|
|
layer: leafletLayer,
|
|
|
|
feature: layer
|
2020-09-25 21:58:29 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return layers;
|
2020-09-27 22:48:43 +02:00
|
|
|
}
|
2020-09-27 23:37:47 +02:00
|
|
|
|
|
|
|
private static LoadProviderIndex(): BaseLayer[] {
|
2021-01-02 16:04:16 +01:00
|
|
|
// @ts-ignore
|
|
|
|
X; // Import X to make sure the namespace is not optimized away
|
|
|
|
function l(id: string, name: string) {
|
|
|
|
try {
|
|
|
|
const layer: any = L.tileLayer.provider(id, undefined);
|
|
|
|
return {
|
|
|
|
feature: null,
|
|
|
|
id: id,
|
|
|
|
name: name,
|
|
|
|
layer: layer,
|
|
|
|
min_zoom: layer.minzoom,
|
|
|
|
max_zoom: layer.maxzoom
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Could not find provided layer", name, e);
|
|
|
|
return null;
|
2020-09-27 22:48:43 +02:00
|
|
|
}
|
2020-09-27 23:37:47 +02:00
|
|
|
}
|
2021-01-02 16:04:16 +01:00
|
|
|
|
|
|
|
const layers = [
|
2020-09-30 22:56:04 +02:00
|
|
|
l("CyclOSM", "CyclOSM - A bicycle oriented map"),
|
2020-09-27 23:37:47 +02:00
|
|
|
l("Stamen.TonerLite", "Toner Lite (by Stamen)"),
|
|
|
|
l("Stamen.TonerBackground", "Toner Background - no labels (by Stamen)"),
|
|
|
|
l("Stamen.Watercolor", "Watercolor (by Stamen)"),
|
|
|
|
l("Stadia.AlidadeSmooth", "Alidade Smooth (by Stadia)"),
|
|
|
|
l("Stadia.AlidadeSmoothDark", "Alidade Smooth Dark (by Stadia)"),
|
|
|
|
l("Stadia.OSMBright", "Osm Bright (by Stadia)"),
|
|
|
|
l("CartoDB.Positron", "Positron (by CartoDB)"),
|
|
|
|
l("CartoDB.PositronNoLabels", "Positron - no labels (by CartoDB)"),
|
|
|
|
l("CartoDB.Voyager", "Voyager (by CartoDB)"),
|
|
|
|
l("CartoDB.VoyagerNoLabels", "Voyager - no labels (by CartoDB)"),
|
|
|
|
];
|
2021-01-02 16:04:16 +01:00
|
|
|
return Utils.NoNull(layers);
|
2020-09-25 21:58:29 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-02 16:04:16 +01:00
|
|
|
/**
|
|
|
|
* Converts a layer from the editor-layer-index into a tilelayer usable by leaflet
|
|
|
|
*/
|
|
|
|
private static CreateBackgroundLayer(id: string, name: string, url: string, attribution: string, attributionUrl: string,
|
|
|
|
maxZoom: number, isWms: boolean, isWMTS?: boolean): TileLayer {
|
|
|
|
|
|
|
|
url = url.replace("{zoom}", "{z}")
|
|
|
|
.replace("&BBOX={bbox}", "")
|
|
|
|
.replace("&bbox={bbox}", "");
|
|
|
|
|
|
|
|
const subdomainsMatch = url.match(/{switch:[^}]*}/)
|
|
|
|
let domains: string[] = [];
|
|
|
|
if (subdomainsMatch !== null) {
|
|
|
|
let domainsStr = subdomainsMatch[0].substr("{switch:".length);
|
|
|
|
domainsStr = domainsStr.substr(0, domainsStr.length - 1);
|
|
|
|
domains = domainsStr.split(",");
|
|
|
|
url = url.replace(/{switch:[^}]*}/, "{s}")
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (isWms) {
|
|
|
|
url = url.replace("&SRS={proj}", "");
|
|
|
|
url = url.replace("&srs={proj}", "");
|
|
|
|
const paramaters = ["format", "layers", "version", "service", "request", "styles", "transparent", "version"];
|
|
|
|
const urlObj = new URL(url);
|
|
|
|
|
|
|
|
const isUpper = urlObj.searchParams["LAYERS"] !== null;
|
|
|
|
const options = {
|
|
|
|
maxZoom: maxZoom ?? 19,
|
|
|
|
attribution: attribution + " | ",
|
|
|
|
subdomains: domains,
|
|
|
|
uppercase: isUpper,
|
|
|
|
transparent: false
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const paramater of paramaters) {
|
|
|
|
let p = paramater;
|
|
|
|
if (isUpper) {
|
|
|
|
p = paramater.toUpperCase();
|
|
|
|
}
|
|
|
|
options[paramater] = urlObj.searchParams.get(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.transparent === null) {
|
|
|
|
options.transparent = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return L.tileLayer.wms(urlObj.protocol + "//" + urlObj.host + urlObj.pathname, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attributionUrl) {
|
|
|
|
attribution = `<a href='${attributionUrl}' target='_blank'>${attribution}</a>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return L.tileLayer(url,
|
|
|
|
{
|
|
|
|
attribution: attribution,
|
|
|
|
maxZoom: maxZoom,
|
|
|
|
minZoom: 1,
|
|
|
|
// @ts-ignore
|
|
|
|
wmts: isWMTS ?? false,
|
|
|
|
subdomains: domains
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-25 21:58:29 +02:00
|
|
|
}
|