1- import { ref , shallowRef , watch , computed } from 'vue'
2- import type TileSource from 'ol/source/Tile'
1+ import { ref , shallowRef , watch } from 'vue'
32import type Map from 'ol/Map'
43import VectorSource from 'ol/source/Vector'
54import VectorLayer from 'ol/layer/Vector'
6- import VectorTileLayer from 'ol/layer/VectorTile'
75import GlTileLayer from 'ol/layer/WebGLTile.js'
86import TileLayer from 'ol/layer/Tile'
97import type XYZ from 'ol/source/XYZ'
@@ -17,7 +15,7 @@ import createCloudlessLayer from '../layers/S2-Cloudless-Layer'
1715import createS2GridLayer from '../layers/S2-Grid-Layer'
1816import {
1917 createGlobalPredictionsLayer ,
20- updateGlobalPredictionsLayer ,
18+ type GlobalPredictionsController ,
2119} from '../layers/Global-Predictions-Layer'
2220import { Fill , Stroke , Style } from 'ol/style'
2321import { type FeatureLike } from 'ol/Feature'
@@ -29,23 +27,8 @@ import { inferenceStyle } from '../layers/color-scales'
2927
3028let featureId = 0
3129
32- const loadingCount = ref ( 0 )
33- export const isLayerLoading = computed ( ( ) => loadingCount . value > 0 )
34-
35- export function trackTileSource ( source : TileSource ) : ( ) => void {
36- const onStart = ( ) => {
37- loadingCount . value ++
38- }
39- const onEnd = ( ) => {
40- loadingCount . value = Math . max ( 0 , loadingCount . value - 1 )
41- }
42- source . on ( 'tileloadstart' , onStart )
43- source . on ( [ 'tileloadend' , 'tileloaderror' ] , onEnd )
44- return ( ) => {
45- source . un ( 'tileloadstart' , onStart )
46- source . un ( [ 'tileloadend' , 'tileloaderror' ] , onEnd )
47- }
48- }
30+ const isLayerLoading = ref ( false )
31+ export { isLayerLoading }
4932
5033export interface AreaValues {
5134 min_area_km2 : number
@@ -56,6 +39,15 @@ export interface AreaValues {
5639const { settings } = useSettings ( )
5740
5841export const map = shallowRef < Map | null > ( null )
42+ watch ( map , ( newMap ) => {
43+ if ( ! newMap ) return
44+ newMap . on ( 'loadstart' , ( ) => {
45+ isLayerLoading . value = true
46+ } )
47+ newMap . on ( 'loadend' , ( ) => {
48+ isLayerLoading . value = false
49+ } )
50+ } )
5951const areaValues = ref < AreaValues > ( {
6052 min_area_km2 : 100 ,
6153 max_area_km2 : 500 ,
@@ -75,13 +67,6 @@ export const geoJsonResults = shallowRef<any[]>([])
7567// Cloudless layer management
7668const cloudlessLayer = shallowRef < TileLayer < XYZ > | null > ( null )
7769
78- let untrackCloudless : ( ( ) => void ) | null = null
79- watch ( cloudlessLayer , ( newLayer ) => {
80- untrackCloudless ?.( )
81- const src = newLayer ?. getSource ( ) as TileSource | null
82- untrackCloudless = src ? trackTileSource ( src ) : null
83- } )
84-
8570// Watch for year changes and update the cloudless layer
8671watch (
8772 ( ) => settings . value . year ,
@@ -100,14 +85,9 @@ watch(
10085 // Insert at index 0 to keep it as the base layer
10186 map . value . getLayers ( ) . insertAt ( 0 , cloudlessLayer . value )
10287
103- if ( settings . value . mode === 'global' ) {
104- if ( globalPredictionsLayer . value ) {
105- map . value . removeLayer ( globalPredictionsLayer . value )
106- globalPredictionsLayer . value = null
107- }
108-
109- globalPredictionsLayer . value = createGlobalPredictionsLayer ( settings . value )
110- map . value . addLayer ( globalPredictionsLayer . value )
88+ if ( settings . value . mode === 'global' && globalPredictionsController . value ) {
89+ removeGlobalPredictionsLayer ( )
90+ updateLayers ( )
11191 }
11292 } ,
11393)
@@ -122,23 +102,9 @@ const initCloudlessLayer = () => {
122102
123103// Global predictions and S2 grid layer management
124104const s2GridLayer = shallowRef < VectorLayer < VectorSource > | null > ( null )
125- const globalPredictionsLayer = shallowRef < VectorTileLayer | null > ( null )
105+ const globalPredictionsController = shallowRef < GlobalPredictionsController | null > ( null )
126106const globalOverviewLayer = shallowRef < GlTileLayer | null > ( null )
127107
128- let untrackGlobalPredictions : ( ( ) => void ) | null = null
129- watch ( globalPredictionsLayer , ( newLayer ) => {
130- untrackGlobalPredictions ?.( )
131- const src = newLayer ?. getSource ( ) as TileSource | null
132- untrackGlobalPredictions = src ? trackTileSource ( src ) : null
133- } )
134-
135- let untrackGlobalOverview : ( ( ) => void ) | null = null
136- watch ( globalOverviewLayer , ( newLayer ) => {
137- untrackGlobalOverview ?.( )
138- const src = newLayer ?. getSource ( ) as TileSource | null
139- untrackGlobalOverview = src ? trackTileSource ( src ) : null
140- } )
141-
142108let thresholdDebounce : ReturnType < typeof setTimeout > | null = null
143109watch (
144110 ( ) => settings . value . threshold ,
@@ -148,13 +114,20 @@ watch(
148114 if ( globalOverviewLayer . value ) {
149115 updateGlobalOverviewLayer ( globalOverviewLayer . value , settings . value )
150116 }
151- if ( globalPredictionsLayer . value ) {
152- updateGlobalPredictionsLayer ( globalPredictionsLayer . value , settings . value )
117+ if ( globalPredictionsController . value ) {
118+ globalPredictionsController . value . update ( settings . value )
153119 }
154120 } , 80 )
155121 } ,
156122)
157123
124+ const removeGlobalPredictionsLayer = ( ) => {
125+ if ( ! map . value || ! globalPredictionsController . value ) return
126+ map . value . removeLayer ( globalPredictionsController . value . layer )
127+ globalPredictionsController . value . dispose ( )
128+ globalPredictionsController . value = null
129+ }
130+
158131const updateLayers = ( ) => {
159132 if ( ! map . value ) {
160133 return
@@ -167,10 +140,10 @@ const updateLayers = () => {
167140 }
168141
169142 // Initialize with global predictions layers
170- if ( ! globalPredictionsLayer . value ) {
143+ if ( ! globalPredictionsController . value ) {
171144 // Only handle first initialization here, year changes are handled by a watcher on year above
172- globalPredictionsLayer . value = createGlobalPredictionsLayer ( settings . value )
173- map . value . addLayer ( globalPredictionsLayer . value )
145+ globalPredictionsController . value = createGlobalPredictionsLayer ( settings . value )
146+ map . value . addLayer ( globalPredictionsController . value . layer )
174147 }
175148 if ( ! globalOverviewLayer . value ) {
176149 globalOverviewLayer . value = createGlobalOverviewLayer ( settings . value )
@@ -184,10 +157,7 @@ const updateLayers = () => {
184157 }
185158
186159 // Remove global predictions layers if they exist
187- if ( globalPredictionsLayer . value ) {
188- map . value . removeLayer ( globalPredictionsLayer . value )
189- globalPredictionsLayer . value = null
190- }
160+ removeGlobalPredictionsLayer ( )
191161 if ( globalOverviewLayer . value ) {
192162 map . value . removeLayer ( globalOverviewLayer . value )
193163 globalOverviewLayer . value = null
0 commit comments