forked from MapComplete/MapComplete
		
	Fix: improve background layer switching code, might improve #1481
This commit is contained in:
		
							parent
							
								
									775886b2b9
								
							
						
					
					
						commit
						33fa11fc17
					
				
					 2 changed files with 57 additions and 33 deletions
				
			
		| 
						 | 
					@ -1,7 +1,7 @@
 | 
				
			||||||
import { Store, UIEventSource } from "../../Logic/UIEventSource"
 | 
					import { Store, UIEventSource } from "../../Logic/UIEventSource"
 | 
				
			||||||
import type { Map as MLMap } from "maplibre-gl"
 | 
					import type { Map as MLMap } from "maplibre-gl"
 | 
				
			||||||
import { Map as MlMap, SourceSpecification } from "maplibre-gl"
 | 
					import { Map as MlMap, SourceSpecification } from "maplibre-gl"
 | 
				
			||||||
import { RasterLayerPolygon } from "../../Models/RasterLayers"
 | 
					import { AvailableRasterLayers, RasterLayerPolygon } from "../../Models/RasterLayers"
 | 
				
			||||||
import { Utils } from "../../Utils"
 | 
					import { Utils } from "../../Utils"
 | 
				
			||||||
import { BBox } from "../../Logic/BBox"
 | 
					import { BBox } from "../../Logic/BBox"
 | 
				
			||||||
import { ExportableMap, MapProperties } from "../../Models/MapProperties"
 | 
					import { ExportableMap, MapProperties } from "../../Models/MapProperties"
 | 
				
			||||||
| 
						 | 
					@ -370,9 +370,18 @@ export class MapLibreAdaptor implements MapProperties, ExportableMap {
 | 
				
			||||||
    private removeCurrentLayer(map: MLMap): void {
 | 
					    private removeCurrentLayer(map: MLMap): void {
 | 
				
			||||||
        if (this._currentRasterLayer) {
 | 
					        if (this._currentRasterLayer) {
 | 
				
			||||||
            // hide the previous layer
 | 
					            // hide the previous layer
 | 
				
			||||||
 | 
					            try {
 | 
				
			||||||
 | 
					                if (map.getLayer(this._currentRasterLayer)) {
 | 
				
			||||||
                    map.removeLayer(this._currentRasterLayer)
 | 
					                    map.removeLayer(this._currentRasterLayer)
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                if (map.getSource(this._currentRasterLayer)) {
 | 
				
			||||||
                    map.removeSource(this._currentRasterLayer)
 | 
					                    map.removeSource(this._currentRasterLayer)
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					                this._currentRasterLayer = undefined
 | 
				
			||||||
 | 
					            } catch (e) {
 | 
				
			||||||
 | 
					                console.warn("Could not remove the previous layer")
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private async setBackground(): Promise<void> {
 | 
					    private async setBackground(): Promise<void> {
 | 
				
			||||||
| 
						 | 
					@ -381,47 +390,58 @@ export class MapLibreAdaptor implements MapProperties, ExportableMap {
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        const background: RasterLayerProperties = this.rasterLayer?.data?.properties
 | 
					        const background: RasterLayerProperties = this.rasterLayer?.data?.properties
 | 
				
			||||||
        if (background !== undefined && this._currentRasterLayer === background.id) {
 | 
					        console.log("Setting background to", background)
 | 
				
			||||||
 | 
					        if (!background) {
 | 
				
			||||||
 | 
					            console.error(
 | 
				
			||||||
 | 
					                "Attempting to 'setBackground', but the background is",
 | 
				
			||||||
 | 
					                background,
 | 
				
			||||||
 | 
					                "for",
 | 
				
			||||||
 | 
					                map.getCanvas()
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					            return
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        if (this._currentRasterLayer === background.id) {
 | 
				
			||||||
            // already the correct background layer, nothing to do
 | 
					            // already the correct background layer, nothing to do
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        // await this.awaitStyleIsLoaded()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (background !== this.rasterLayer?.data?.properties) {
 | 
					 | 
				
			||||||
            // User selected another background in the meantime... abort
 | 
					 | 
				
			||||||
            return
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (background !== undefined && this._currentRasterLayer === background.id) {
 | 
					 | 
				
			||||||
            // already the correct background layer, nothing to do
 | 
					 | 
				
			||||||
            return
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        if (!background?.url) {
 | 
					        if (!background?.url) {
 | 
				
			||||||
            // no background to set
 | 
					            // no background to set
 | 
				
			||||||
            this.removeCurrentLayer(map)
 | 
					            this.removeCurrentLayer(map)
 | 
				
			||||||
            this._currentRasterLayer = undefined
 | 
					 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (background.type === "vector") {
 | 
					        if (background.type === "vector") {
 | 
				
			||||||
            console.log("Background layer is vector")
 | 
					            console.log("Background layer is vector", background.id)
 | 
				
			||||||
 | 
					            this.removeCurrentLayer(map)
 | 
				
			||||||
            map.setStyle(background.url)
 | 
					            map.setStyle(background.url)
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        map.addSource(background.id, MapLibreAdaptor.prepareWmsSource(background))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        map.resize()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        let addLayerBeforeId = "aeroway_fill" // this is the first non-landuse item in the stylesheet, we add the raster layer before the roads but above the landuse
 | 
					        let addLayerBeforeId = "aeroway_fill" // this is the first non-landuse item in the stylesheet, we add the raster layer before the roads but above the landuse
 | 
				
			||||||
        if (background.category === "osmbasedmap" || background.category === "map") {
 | 
					        if (background.category === "osmbasedmap" || background.category === "map") {
 | 
				
			||||||
            // The background layer is already an OSM-based map or another map, so we don't want anything from the baselayer
 | 
					            // The background layer is already an OSM-based map or another map, so we don't want anything from the baselayer
 | 
				
			||||||
            let layers = map.getStyle().layers
 | 
					            addLayerBeforeId = undefined
 | 
				
			||||||
            // THe last index of the maptiler layers
 | 
					            this.removeCurrentLayer(map)
 | 
				
			||||||
            let lastIndex = layers.findIndex((layer) => layer.id === "housenumber")
 | 
					        } else {
 | 
				
			||||||
            addLayerBeforeId = layers[lastIndex + 1]?.id ?? "housenumber"
 | 
					            // Make sure that the default maptiler style is loaded as it gives an overlay with roads
 | 
				
			||||||
 | 
					            const maptiler = AvailableRasterLayers.maplibre.properties
 | 
				
			||||||
 | 
					            if (!map.getSource(maptiler.id)) {
 | 
				
			||||||
 | 
					                this.removeCurrentLayer(map)
 | 
				
			||||||
 | 
					                map.addSource(maptiler.id, MapLibreAdaptor.prepareWmsSource(maptiler))
 | 
				
			||||||
 | 
					                map.setStyle(maptiler.url)
 | 
				
			||||||
 | 
					                await this.awaitStyleIsLoaded()
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (!map.getLayer(addLayerBeforeId)) {
 | 
				
			||||||
 | 
					            addLayerBeforeId = undefined
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        if (!map.getSource(background.id)) {
 | 
				
			||||||
 | 
					            map.addSource(background.id, MapLibreAdaptor.prepareWmsSource(background))
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        map.resize()
 | 
				
			||||||
 | 
					        if (!map.getLayer(background.id)) {
 | 
				
			||||||
            map.addLayer(
 | 
					            map.addLayer(
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    id: background.id,
 | 
					                    id: background.id,
 | 
				
			||||||
| 
						 | 
					@ -431,6 +451,7 @@ export class MapLibreAdaptor implements MapProperties, ExportableMap {
 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
                addLayerBeforeId
 | 
					                addLayerBeforeId
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        await this.awaitStyleIsLoaded()
 | 
					        await this.awaitStyleIsLoaded()
 | 
				
			||||||
        this.removeCurrentLayer(map)
 | 
					        this.removeCurrentLayer(map)
 | 
				
			||||||
        this._currentRasterLayer = background?.id
 | 
					        this._currentRasterLayer = background?.id
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -380,6 +380,9 @@ class LineRenderingLayer {
 | 
				
			||||||
            if (this._listenerInstalledOn.has(id)) {
 | 
					            if (this._listenerInstalledOn.has(id)) {
 | 
				
			||||||
                continue
 | 
					                continue
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					            if (!map.getSource(this._layername)) {
 | 
				
			||||||
 | 
					                continue
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
            if (this._fetchStore === undefined) {
 | 
					            if (this._fetchStore === undefined) {
 | 
				
			||||||
                map.setFeatureState(
 | 
					                map.setFeatureState(
 | 
				
			||||||
                    { source: this._layername, id },
 | 
					                    { source: this._layername, id },
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue