2023-03-11 02:37:07 +01:00
|
|
|
import { Store, UIEventSource } from "../UIEventSource"
|
2021-10-20 00:09:40 +02:00
|
|
|
import { Utils } from "../../Utils"
|
2023-03-11 02:37:07 +01:00
|
|
|
import {
|
|
|
|
AvailableRasterLayers,
|
|
|
|
RasterLayerPolygon,
|
|
|
|
RasterLayerUtils,
|
|
|
|
} from "../../Models/RasterLayers"
|
2021-01-02 21:03:40 +01:00
|
|
|
|
|
|
|
/**
|
2023-03-11 02:37:07 +01:00
|
|
|
* When a user pans around on the map, they might pan out of the range of the current background raster layer.
|
|
|
|
* This actor will then quickly select a (best) raster layer of the same category which is available
|
2021-01-02 21:03:40 +01:00
|
|
|
*/
|
2021-09-21 02:10:42 +02:00
|
|
|
export default class BackgroundLayerResetter {
|
2021-09-09 00:05:51 +02:00
|
|
|
constructor(
|
2023-03-11 02:37:07 +01:00
|
|
|
currentBackgroundLayer: UIEventSource<RasterLayerPolygon>,
|
|
|
|
availableLayers: Store<RasterLayerPolygon[]>
|
2021-09-28 18:00:44 +02:00
|
|
|
) {
|
2021-11-07 16:34:51 +01:00
|
|
|
if (Utils.runningFromConsole) {
|
2021-10-20 00:09:40 +02:00
|
|
|
return
|
|
|
|
}
|
2021-11-07 16:34:51 +01:00
|
|
|
|
2023-03-11 02:37:07 +01:00
|
|
|
// Change the baseLayer back to OSM if we go out of the current range of the layer
|
|
|
|
availableLayers.addCallbackAndRunD((availableLayers) => {
|
|
|
|
// We only check on move/on change of the availableLayers
|
|
|
|
const currentBgPolygon: RasterLayerPolygon | undefined = currentBackgroundLayer.data
|
2021-09-09 00:05:51 +02:00
|
|
|
|
2023-03-11 02:37:07 +01:00
|
|
|
if (availableLayers.findIndex((available) => currentBgPolygon == available) >= 0) {
|
|
|
|
// Still available!
|
|
|
|
return
|
2021-01-02 21:03:40 +01:00
|
|
|
}
|
2023-03-11 02:37:07 +01:00
|
|
|
|
2021-01-02 21:03:40 +01:00
|
|
|
// Oops, we panned out of range for this layer!
|
2023-03-11 02:37:07 +01:00
|
|
|
// What is the 'best' map of the same category which is available?
|
|
|
|
const availableInSameCat = RasterLayerUtils.SelectBestLayerAccordingTo(
|
|
|
|
availableLayers,
|
|
|
|
currentBgPolygon?.properties?.category ?? "osmbasedmap"
|
2021-01-02 21:03:40 +01:00
|
|
|
)
|
2023-03-11 02:37:07 +01:00
|
|
|
console.log("Selecting a different layer:", availableInSameCat.properties.id)
|
|
|
|
currentBackgroundLayer.setData(availableInSameCat ?? AvailableRasterLayers.osmCarto)
|
2021-01-02 21:03:40 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|