forked from MapComplete/MapComplete
		
	reset to previous commit
This commit is contained in:
		
							parent
							
								
									d5b614fc44
								
							
						
					
					
						commit
						196d40084d
					
				
					 90 changed files with 4953 additions and 1922 deletions
				
			
		|  | @ -1,11 +1,12 @@ | |||
| import * as editorlayerindex from "../../assets/editor-layer-index.json" | ||||
| import BaseLayer from "../../Models/BaseLayer"; | ||||
| import * as L from "leaflet"; | ||||
| import {TileLayer} from "leaflet"; | ||||
| import * as X from "leaflet-providers"; | ||||
| import {UIEventSource} from "../UIEventSource"; | ||||
| import {GeoOperations} from "../GeoOperations"; | ||||
| import {TileLayer} from "leaflet"; | ||||
| import {Utils} from "../../Utils"; | ||||
| import Loc from "../../Models/Loc"; | ||||
| 
 | ||||
| /** | ||||
|  * Calculates which layers are available at the current location | ||||
|  | @ -24,45 +25,87 @@ export default class AvailableBaseLayers { | |||
|                 false, false), | ||||
|             feature: null, | ||||
|             max_zoom: 19, | ||||
|             min_zoom: 0 | ||||
|             min_zoom: 0, | ||||
|             isBest: false, // This is a lie! Of course OSM is the best map! (But not in this context)
 | ||||
|             category: "osmbasedmap" | ||||
|         } | ||||
| 
 | ||||
| 
 | ||||
|     public static layerOverview = AvailableBaseLayers.LoadRasterIndex().concat(AvailableBaseLayers.LoadProviderIndex()); | ||||
|     public availableEditorLayers: UIEventSource<BaseLayer[]>; | ||||
| 
 | ||||
|     constructor(location: UIEventSource<{ lat: number, lon: number, zoom: number }>) { | ||||
|         const self = this; | ||||
|         this.availableEditorLayers = | ||||
|             location.map( | ||||
|                 (currentLocation) => { | ||||
|     public static AvailableLayersAt(location: UIEventSource<Loc>): UIEventSource<BaseLayer[]> { | ||||
|         const source = location.map( | ||||
|             (currentLocation) => { | ||||
| 
 | ||||
|                     if (currentLocation === undefined) { | ||||
|                         return AvailableBaseLayers.layerOverview; | ||||
|                     } | ||||
|                 if (currentLocation === undefined) { | ||||
|                     return AvailableBaseLayers.layerOverview; | ||||
|                 } | ||||
| 
 | ||||
|                     const currentLayers = self.availableEditorLayers?.data; | ||||
|                     const newLayers = AvailableBaseLayers.AvailableLayersAt(currentLocation?.lon, currentLocation?.lat); | ||||
|                 const currentLayers = source?.data; // A bit unorthodox - I know
 | ||||
|                 const newLayers = AvailableBaseLayers.CalculateAvailableLayersAt(currentLocation?.lon, currentLocation?.lat); | ||||
| 
 | ||||
|                     if (currentLayers === undefined) { | ||||
|                 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; | ||||
|                     } | ||||
|                     if (newLayers.length !== currentLayers.length) { | ||||
|                         return newLayers; | ||||
|                     } | ||||
|                     for (let i = 0; i < newLayers.length; i++) { | ||||
|                         if (newLayers[i].name !== currentLayers[i].name) { | ||||
|                             return newLayers; | ||||
|                         } | ||||
|                     } | ||||
| 
 | ||||
|                     return currentLayers; | ||||
|                 }); | ||||
| 
 | ||||
|                 } | ||||
| 
 | ||||
|                 return currentLayers; | ||||
|             }); | ||||
|         return source; | ||||
|     } | ||||
| 
 | ||||
|     private static AvailableLayersAt(lon: number, lat: number): BaseLayer[] { | ||||
|     public static SelectBestLayerAccordingTo(location: UIEventSource<Loc>, preferedCategory: UIEventSource<string | string[]>): UIEventSource<BaseLayer> { | ||||
|         return AvailableBaseLayers.AvailableLayersAt(location).map(available => { | ||||
|             // First float all 'best layers' to the top
 | ||||
|             available.sort((a, b) => { | ||||
|                     if (a.isBest && b.isBest) { | ||||
|                         return 0; | ||||
|                     } | ||||
|                     if (!a.isBest) { | ||||
|                         return 1 | ||||
|                     } | ||||
| 
 | ||||
|                     return -1; | ||||
|                 } | ||||
|             ) | ||||
| 
 | ||||
|             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] | ||||
|         }) | ||||
|     } | ||||
| 
 | ||||
|     private static CalculateAvailableLayersAt(lon: number, lat: number): BaseLayer[] { | ||||
|         const availableLayers = [AvailableBaseLayers.osmCarto] | ||||
|         const globalLayers = []; | ||||
|         for (const layerOverviewItem of AvailableBaseLayers.layerOverview) { | ||||
|  | @ -140,7 +183,9 @@ export default class AvailableBaseLayers { | |||
|                 min_zoom: props.min_zoom ?? 1, | ||||
|                 name: props.name, | ||||
|                 layer: leafletLayer, | ||||
|                 feature: layer | ||||
|                 feature: layer, | ||||
|                 isBest: props.best ?? false, | ||||
|                 category: props.category | ||||
|             }); | ||||
|         } | ||||
|         return layers; | ||||
|  | @ -152,15 +197,16 @@ export default class AvailableBaseLayers { | |||
|         function l(id: string, name: string): BaseLayer { | ||||
|             try { | ||||
|                 const layer: any = () => L.tileLayer.provider(id, undefined); | ||||
|                 const baseLayer: BaseLayer = { | ||||
|                 return { | ||||
|                     feature: null, | ||||
|                     id: id, | ||||
|                     name: name, | ||||
|                     layer: layer, | ||||
|                     min_zoom: layer.minzoom, | ||||
|                     max_zoom: layer.maxzoom | ||||
|                     max_zoom: layer.maxzoom, | ||||
|                     category: "osmbasedmap", | ||||
|                     isBest: false | ||||
|                 } | ||||
|                 return baseLayer | ||||
|             } catch (e) { | ||||
|                 console.error("Could not find provided layer", name, e); | ||||
|                 return null; | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue