| 
									
										
										
										
											2021-10-15 14:52:11 +02:00
										 |  |  | import BaseLayer from "../../Models/BaseLayer"; | 
					
						
							|  |  |  | import {UIEventSource} from "../UIEventSource"; | 
					
						
							|  |  |  | import Loc from "../../Models/Loc"; | 
					
						
							|  |  |  | import {GeoOperations} from "../GeoOperations"; | 
					
						
							|  |  |  | import * as editorlayerindex from "../../assets/editor-layer-index.json"; | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | import * as L from "leaflet"; | 
					
						
							| 
									
										
										
										
											2021-10-15 14:52:11 +02:00
										 |  |  | import {TileLayer} from "leaflet"; | 
					
						
							|  |  |  | import * as X from "leaflet-providers"; | 
					
						
							|  |  |  | import {Utils} from "../../Utils"; | 
					
						
							|  |  |  | import {AvailableBaseLayersObj} from "./AvailableBaseLayers"; | 
					
						
							| 
									
										
										
										
											2022-02-11 15:11:50 +01:00
										 |  |  | import {BBox} from "../BBox"; | 
					
						
							| 
									
										
										
										
											2021-10-15 14:52:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | export default class AvailableBaseLayersImplementation implements AvailableBaseLayersObj { | 
					
						
							| 
									
										
										
										
											2021-10-15 14:52:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public readonly osmCarto: BaseLayer = | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             id: "osm", | 
					
						
							|  |  |  |             name: "OpenStreetMap", | 
					
						
							|  |  |  |             layer: () => AvailableBaseLayersImplementation.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, | 
					
						
							| 
									
										
										
										
											2021-12-05 02:30:03 +01:00
										 |  |  |             isBest: true, // Of course, OpenStreetMap is the best map!
 | 
					
						
							| 
									
										
										
										
											2021-10-15 14:52:11 +02:00
										 |  |  |             category: "osmbasedmap" | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-11 15:11:50 +01:00
										 |  |  |     public readonly layerOverview = AvailableBaseLayersImplementation.LoadRasterIndex().concat(AvailableBaseLayersImplementation.LoadProviderIndex()); | 
					
						
							|  |  |  |     public readonly globalLayers = this.layerOverview.filter(layer => layer.feature?.geometry === undefined || layer.feature?.geometry === null) | 
					
						
							|  |  |  |     public readonly localLayers = this.layerOverview.filter(layer => layer.feature?.geometry !== undefined && layer.featuer?.geometry !== null) | 
					
						
							| 
									
										
										
										
											2021-10-15 14:52:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private static LoadRasterIndex(): BaseLayer[] { | 
					
						
							|  |  |  |         const layers: BaseLayer[] = [] | 
					
						
							|  |  |  |         // @ts-ignore
 | 
					
						
							|  |  |  |         const features = editorlayerindex.features; | 
					
						
							|  |  |  |         for (const i in features) { | 
					
						
							|  |  |  |             const layer = features[i]; | 
					
						
							|  |  |  |             const props = layer.properties; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-08 00:34:07 +01:00
										 |  |  |             if (props.type === "bing") { | 
					
						
							|  |  |  |                 // A lot of work to implement - see https://github.com/pietervdvn/MapComplete/issues/648
 | 
					
						
							| 
									
										
										
										
											2021-10-15 14:52:11 +02:00
										 |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (props.id === "MAPNIK") { | 
					
						
							|  |  |  |                 // Already added by default
 | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (props.overlay) { | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (props.url.toLowerCase().indexOf("apikey") > 0) { | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (props.max_zoom < 19) { | 
					
						
							|  |  |  |                 // 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
 | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (props.name === undefined) { | 
					
						
							|  |  |  |                 console.warn("Editor layer index: name not defined on ", props) | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-01-26 21:40:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-15 14:52:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             const leafletLayer: () => TileLayer = () => AvailableBaseLayersImplementation.CreateBackgroundLayer( | 
					
						
							|  |  |  |                 props.id, | 
					
						
							|  |  |  |                 props.name, | 
					
						
							|  |  |  |                 props.url, | 
					
						
							|  |  |  |                 props.name, | 
					
						
							|  |  |  |                 props.license_url, | 
					
						
							|  |  |  |                 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, | 
					
						
							| 
									
										
										
										
											2021-10-21 21:42:28 +02:00
										 |  |  |                 max_zoom: props.max_zoom ?? 19, | 
					
						
							| 
									
										
										
										
											2021-10-15 14:52:11 +02:00
										 |  |  |                 min_zoom: props.min_zoom ?? 1, | 
					
						
							|  |  |  |                 name: props.name, | 
					
						
							|  |  |  |                 layer: leafletLayer, | 
					
						
							| 
									
										
										
										
											2021-11-21 02:44:35 +01:00
										 |  |  |                 feature: layer.geometry !== null ? layer : null, | 
					
						
							| 
									
										
										
										
											2021-10-15 14:52:11 +02:00
										 |  |  |                 isBest: props.best ?? false, | 
					
						
							|  |  |  |                 category: props.category | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return layers; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private static LoadProviderIndex(): BaseLayer[] { | 
					
						
							|  |  |  |         // @ts-ignore
 | 
					
						
							|  |  |  |         X; // Import X to make sure the namespace is not optimized away
 | 
					
						
							|  |  |  |         function l(id: string, name: string): BaseLayer { | 
					
						
							|  |  |  |             try { | 
					
						
							| 
									
										
										
										
											2021-11-21 02:44:35 +01:00
										 |  |  |                 const layer: any = L.tileLayer.provider(id, undefined); | 
					
						
							| 
									
										
										
										
											2021-10-15 14:52:11 +02:00
										 |  |  |                 return { | 
					
						
							|  |  |  |                     feature: null, | 
					
						
							|  |  |  |                     id: id, | 
					
						
							|  |  |  |                     name: name, | 
					
						
							| 
									
										
										
										
											2022-02-04 02:48:31 +01:00
										 |  |  |                     layer: () => L.tileLayer.provider(id, { | 
					
						
							| 
									
										
										
										
											2022-02-04 14:36:26 +01:00
										 |  |  |                         maxNativeZoom: layer.options?.maxZoom, | 
					
						
							|  |  |  |                        maxZoom: Math.max(layer.options?.maxZoom ?? 19, 21) | 
					
						
							| 
									
										
										
										
											2022-02-04 02:48:31 +01:00
										 |  |  |                     }), | 
					
						
							| 
									
										
										
										
											2021-11-21 02:44:35 +01:00
										 |  |  |                     min_zoom: 1, | 
					
						
							|  |  |  |                     max_zoom: layer.options.maxZoom, | 
					
						
							| 
									
										
										
										
											2021-10-15 14:52:11 +02:00
										 |  |  |                     category: "osmbasedmap", | 
					
						
							|  |  |  |                     isBest: false | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } catch (e) { | 
					
						
							|  |  |  |                 console.error("Could not find provided layer", name, e); | 
					
						
							|  |  |  |                 return null; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const layers = [ | 
					
						
							|  |  |  |             l("Stamen.TonerLite", "Toner Lite (by Stamen)"), | 
					
						
							|  |  |  |             l("Stamen.TonerBackground", "Toner Background - no labels (by Stamen)"), | 
					
						
							|  |  |  |             l("Stamen.Watercolor", "Watercolor (by Stamen)"), | 
					
						
							|  |  |  |             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)"), | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         return Utils.NoNull(layers); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 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 = { | 
					
						
							| 
									
										
										
										
											2022-01-08 20:02:07 +01:00
										 |  |  |                 maxZoom: Math.max(maxZoom ?? 19, 21), | 
					
						
							|  |  |  |                 maxNativeZoom: maxZoom ?? 19, | 
					
						
							| 
									
										
										
										
											2021-10-15 14:52:11 +02:00
										 |  |  |                 attribution: attribution + " | ", | 
					
						
							|  |  |  |                 subdomains: domains, | 
					
						
							|  |  |  |                 uppercase: isUpper, | 
					
						
							| 
									
										
										
										
											2022-01-08 20:02:07 +01:00
										 |  |  |                 transparent: false, | 
					
						
							| 
									
										
										
										
											2021-10-15 14:52:11 +02:00
										 |  |  |             }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             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, | 
					
						
							| 
									
										
										
										
											2022-01-08 20:02:07 +01:00
										 |  |  |                 maxZoom: Math.max(21, maxZoom ?? 19), | 
					
						
							|  |  |  |                 maxNativeZoom: maxZoom ?? 19, | 
					
						
							| 
									
										
										
										
											2022-01-26 21:40:38 +01:00
										 |  |  |                 minZoom: 1, | 
					
						
							| 
									
										
										
										
											2021-10-15 14:52:11 +02:00
										 |  |  |                 // @ts-ignore
 | 
					
						
							|  |  |  |                 wmts: isWMTS ?? false, | 
					
						
							|  |  |  |                 subdomains: domains | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-01-26 21:40:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |     public AvailableLayersAt(location: UIEventSource<Loc>): UIEventSource<BaseLayer[]> { | 
					
						
							| 
									
										
										
										
											2021-11-21 02:44:35 +01:00
										 |  |  |         return UIEventSource.ListStabilized(location.map( | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |             (currentLocation) => { | 
					
						
							|  |  |  |                 if (currentLocation === undefined) { | 
					
						
							|  |  |  |                     return this.layerOverview; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-11-21 02:44:35 +01:00
										 |  |  |                 return this.CalculateAvailableLayersAt(currentLocation?.lon, currentLocation?.lat); | 
					
						
							|  |  |  |             })); | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public SelectBestLayerAccordingTo(location: UIEventSource<Loc>, preferedCategory: UIEventSource<string | string[]>): UIEventSource<BaseLayer> { | 
					
						
							| 
									
										
										
										
											2021-11-21 02:44:35 +01:00
										 |  |  |         return this.AvailableLayersAt(location) | 
					
						
							|  |  |  |             .map(available => { | 
					
						
							| 
									
										
										
										
											2022-01-26 21:40:38 +01:00
										 |  |  |                 // First float all 'best layers' to the top
 | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |                 available.sort((a, b) => { | 
					
						
							| 
									
										
										
										
											2022-01-26 21:40:38 +01:00
										 |  |  |                         if (a.isBest && b.isBest) { | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |                             return 0; | 
					
						
							|  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2022-01-26 21:40:38 +01:00
										 |  |  |                         if (!a.isBest) { | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |                             return 1 | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                         return -1; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2022-01-26 21:40:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 if (preferedCategory.data === undefined) { | 
					
						
							|  |  |  |                     return available[0] | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 let prefered: string [] | 
					
						
							|  |  |  |                 if (typeof preferedCategory.data === "string") { | 
					
						
							|  |  |  |                     prefered = [preferedCategory.data] | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     prefered = preferedCategory.data; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 prefered.reverse(); | 
					
						
							|  |  |  |                 for (const category of prefered) { | 
					
						
							|  |  |  |                     //Then sort all 'photo'-layers to the top. Stability of the sorting will force a 'best' photo layer on top
 | 
					
						
							|  |  |  |                     available.sort((a, b) => { | 
					
						
							|  |  |  |                             if (a.category === category && b.category === category) { | 
					
						
							|  |  |  |                                 return 0; | 
					
						
							|  |  |  |                             } | 
					
						
							|  |  |  |                             if (a.category !== category) { | 
					
						
							|  |  |  |                                 return 1 | 
					
						
							|  |  |  |                             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                             return -1; | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     ) | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 return available[0] | 
					
						
							|  |  |  |             }, [preferedCategory]) | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-26 21:40:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |     private CalculateAvailableLayersAt(lon: number, lat: number): BaseLayer[] { | 
					
						
							|  |  |  |         const availableLayers = [this.osmCarto] | 
					
						
							| 
									
										
										
										
											2022-02-11 15:11:50 +01:00
										 |  |  |         if (lon === undefined || lat === undefined) { | 
					
						
							|  |  |  |             return availableLayers.concat(this.globalLayers); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         const lonlat = [lon, lat]; | 
					
						
							|  |  |  |         for (const layerOverviewItem of this.localLayers) { | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |             const layer = layerOverviewItem; | 
					
						
							| 
									
										
										
										
											2022-02-11 15:11:50 +01:00
										 |  |  |             const bbox = BBox.get(layer.feature) | 
					
						
							|  |  |  |              | 
					
						
							|  |  |  |             if(!bbox.contains(lonlat)){ | 
					
						
							|  |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-11 15:11:50 +01:00
										 |  |  |             if (GeoOperations.inside(lonlat, layer.feature)) { | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |                 availableLayers.push(layer); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-11 15:11:50 +01:00
										 |  |  |         return availableLayers.concat(this.globalLayers); | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-10-15 14:52:11 +02:00
										 |  |  | } |