-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathuseMap.ts
More file actions
410 lines (352 loc) · 11.9 KB
/
Copy pathuseMap.ts
File metadata and controls
410 lines (352 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import { ref, shallowRef, watch, computed } from 'vue'
import type TileSource from 'ol/source/Tile'
import type Map from 'ol/Map'
import VectorSource from 'ol/source/Vector'
import VectorLayer from 'ol/layer/Vector'
import VectorTileLayer from 'ol/layer/VectorTile'
import GlTileLayer from 'ol/layer/WebGLTile.js'
import TileLayer from 'ol/layer/Tile'
import type XYZ from 'ol/source/XYZ'
import GeoJSON from 'ol/format/GeoJSON'
import { transformExtent } from 'ol/proj'
import type { Extent } from 'ol/extent'
import { type FeatureCollection } from 'geojson'
import useNotifier from './useNotifier'
import useSettings from './useSettings'
import createCloudlessLayer from '../layers/S2-Cloudless-Layer'
import createS2GridLayer from '../layers/S2-Grid-Layer'
import {
createGlobalPredictionsLayer,
updateGlobalPredictionsLayer,
} from '../layers/Global-Predictions-Layer'
import { Fill, Stroke, Style } from 'ol/style'
import { type FeatureLike } from 'ol/Feature'
import {
createGlobalOverviewLayer,
updateGlobalOverviewLayer,
} from '../layers/Global-Overview-Layers'
import { inferenceStyle } from '../layers/color-scales'
let featureId = 0
const loadingCount = ref(0)
export const isLayerLoading = computed(() => loadingCount.value > 0)
export function trackTileSource(source: TileSource): () => void {
const onStart = () => {
loadingCount.value++
}
const onEnd = () => {
loadingCount.value = Math.max(0, loadingCount.value - 1)
}
source.on('tileloadstart', onStart)
source.on(['tileloadend', 'tileloaderror'], onEnd)
return () => {
source.un('tileloadstart', onStart)
source.un(['tileloadend', 'tileloaderror'], onEnd)
}
}
export interface AreaValues {
min_area_km2: number
max_area_km2: number
default?: boolean
}
const { settings } = useSettings()
export const map = shallowRef<Map | null>(null)
const areaValues = ref<AreaValues>({
min_area_km2: 100,
max_area_km2: 500,
default: true,
})
const vectorLayer = shallowRef<VectorLayer<VectorSource> | null>(null)
// Properties display state
const selectedFeature = shallowRef<FeatureLike | null>(null)
watch(selectedFeature, () => vectorLayer.value?.changed())
const propertiesBoxPosition = ref<{ x: number; y: number } | null>(null)
const originalClickPosition = ref<{ x: number; y: number } | null>(null)
const showPropertiesBox = ref(false)
export const geoJsonResults = shallowRef<any[]>([])
// Cloudless layer management
const cloudlessLayer = shallowRef<TileLayer<XYZ> | null>(null)
let untrackCloudless: (() => void) | null = null
watch(cloudlessLayer, (newLayer) => {
untrackCloudless?.()
const src = newLayer?.getSource() as TileSource | null
untrackCloudless = src ? trackTileSource(src) : null
})
// Watch for year changes and update the cloudless layer
watch(
() => settings.value.year,
(newYear) => {
if (!map.value) {
return
}
// Remove the old cloudless layer if it exists
if (cloudlessLayer.value) {
map.value.removeLayer(cloudlessLayer.value)
}
// Create and add the new cloudless layer with the updated year
cloudlessLayer.value = createCloudlessLayer(newYear)
// Insert at index 0 to keep it as the base layer
map.value.getLayers().insertAt(0, cloudlessLayer.value)
if (settings.value.mode === 'global') {
if (globalPredictionsLayer.value) {
map.value.removeLayer(globalPredictionsLayer.value)
globalPredictionsLayer.value = null
}
globalPredictionsLayer.value = createGlobalPredictionsLayer(settings.value)
map.value.addLayer(globalPredictionsLayer.value)
}
},
)
const initCloudlessLayer = () => {
if (!map.value) {
return
}
cloudlessLayer.value = createCloudlessLayer(settings.value.year)
map.value.getLayers().insertAt(0, cloudlessLayer.value)
}
// Global predictions and S2 grid layer management
const s2GridLayer = shallowRef<VectorLayer<VectorSource> | null>(null)
const globalPredictionsLayer = shallowRef<VectorTileLayer | null>(null)
const globalOverviewLayer = shallowRef<GlTileLayer | null>(null)
let untrackGlobalPredictions: (() => void) | null = null
watch(globalPredictionsLayer, (newLayer) => {
untrackGlobalPredictions?.()
const src = newLayer?.getSource() as TileSource | null
untrackGlobalPredictions = src ? trackTileSource(src) : null
})
let untrackGlobalOverview: (() => void) | null = null
watch(globalOverviewLayer, (newLayer) => {
untrackGlobalOverview?.()
const src = newLayer?.getSource() as TileSource | null
untrackGlobalOverview = src ? trackTileSource(src) : null
})
watch(
() => settings.value.threshold,
() => {
if (globalOverviewLayer.value) {
updateGlobalOverviewLayer(globalOverviewLayer.value, settings.value)
}
if (globalPredictionsLayer.value) {
updateGlobalPredictionsLayer(globalPredictionsLayer.value, settings.value)
}
},
)
const updateAggregateLayer = () => {
if (!map.value) {
return
}
if (globalOverviewLayer.value) {
map.value.removeLayer(globalOverviewLayer.value)
globalOverviewLayer.value = null
}
if (settings.value.aggregate) {
globalOverviewLayer.value = createGlobalOverviewLayer(settings.value)
map.value.addLayer(globalOverviewLayer.value)
}
}
watch(() => settings.value.aggregate, updateAggregateLayer)
const updateLayers = () => {
if (!map.value) {
return
}
if (settings.value.mode === 'global') {
if (s2GridLayer.value) {
map.value.removeLayer(s2GridLayer.value)
s2GridLayer.value = null
}
// Initialize with global predictions layers
if (!globalPredictionsLayer.value) {
// Only handle first initialization here, year changes are handled by a watcher on year above
globalPredictionsLayer.value = createGlobalPredictionsLayer(settings.value)
map.value.addLayer(globalPredictionsLayer.value)
}
if (settings.value.aggregate) {
if (!globalOverviewLayer.value) {
globalOverviewLayer.value = createGlobalOverviewLayer(settings.value)
map.value.addLayer(globalOverviewLayer.value)
}
} else {
if (globalOverviewLayer.value) {
map.value.removeLayer(globalOverviewLayer.value)
globalOverviewLayer.value = null
}
}
} else {
// Initialize with S2 grid layer
if (!s2GridLayer.value) {
s2GridLayer.value = createS2GridLayer()
map.value.addLayer(s2GridLayer.value)
}
// Remove global predictions layers if they exist
if (globalPredictionsLayer.value) {
map.value.removeLayer(globalPredictionsLayer.value)
globalPredictionsLayer.value = null
}
if (globalOverviewLayer.value) {
map.value.removeLayer(globalOverviewLayer.value)
globalOverviewLayer.value = null
}
}
}
watch(() => settings.value.mode, updateLayers)
const featureStyle = new Style({
fill: new Fill({
color: inferenceStyle.fill,
}),
stroke: new Stroke({
color: inferenceStyle.stroke,
width: 2,
}),
})
const highlightStyle = [
featureStyle,
new Style({
stroke: new Stroke({
color: 'rgba(255, 0, 0, 1)',
width: 1.5,
}),
}),
]
export default function useMap() {
const { showWarning } = useNotifier()
const handleMapClick = (event: any) => {
// Check if click is on a feature from our vector layer
const pixel = event.pixel
// Check if we clicked on a feature from our results layer
const [clickedFeature] = map.value!.getFeaturesAtPixel(pixel, {
layerFilter: (layer) => layer === vectorLayer.value,
})
if (clickedFeature) {
// Clicked on a feature from our results layer
selectedFeature.value = clickedFeature
// Store original click position for arrow indicator
originalClickPosition.value = { x: pixel[0], y: pixel[1] }
// Calculate optimal position for the properties box to avoid screen edges
const optimalPosition = calculateOptimalPosition(pixel[0], pixel[1])
propertiesBoxPosition.value = optimalPosition
showPropertiesBox.value = true
} else {
// Clicked outside our results layer features, hide properties box
hidePropertiesBox()
}
}
const hidePropertiesBox = () => {
showPropertiesBox.value = false
selectedFeature.value = null
propertiesBoxPosition.value = null
originalClickPosition.value = null
}
const calculateOptimalPosition = (clickX: number, clickY: number) => {
const boxWidth = 300 // Approximate width of properties box
const boxHeight = 200 // Approximate height of properties box
const margin = 20 // Minimum margin from screen edges
let optimalX = clickX
let optimalY = clickY
// Get viewport dimensions
const viewportWidth = window.innerWidth
const viewportHeight = window.innerHeight
// Adjust X position if too close to right edge
if (clickX + boxWidth + margin > viewportWidth) {
optimalX = clickX - boxWidth - margin
}
// Adjust X position if too close to left edge
if (optimalX < margin) {
optimalX = margin
}
// Adjust Y position if too close to bottom edge
if (clickY + boxHeight + margin > viewportHeight) {
optimalY = clickY - boxHeight - margin
}
// Adjust Y position if too close to top edge
if (optimalY < margin) {
optimalY = margin
}
return { x: optimalX, y: optimalY }
}
const fitMapToBbox = (bbox: number[]) => {
// Validate bbox before processing
if (!bbox || bbox.length !== 4 || bbox.some((coord) => isNaN(coord) || coord === 0)) {
console.warn('Invalid bbox provided to fitMapToBbox:', bbox)
return
}
const extent: Extent = transformExtent(bbox, 'EPSG:4326', 'EPSG:3857')
// Validate transformed extent
if (!extent || extent.some((coord) => isNaN(coord))) {
console.warn('Invalid transformed extent:', extent)
return
}
// TODO: FIX ISSUE WITH SCROLLING AND CHANGE LAYER COLOR
map.value!.getView().fit(extent, {
padding: [50, 50, 50, 50],
duration: 500,
})
}
const displayGeoJSON = (
geojson: FeatureCollection & { crs: { properties: { name: string } } },
) => {
// Remove existing vector layer if it exists
if (vectorLayer.value) {
map.value!.removeLayer(vectorLayer.value)
}
for (const feature of geojson.features) {
if (feature.id === undefined) {
feature.id =
feature.properties?.id !== undefined ? feature.properties.id : `feature-${featureId++}`
}
}
// Create new vector source and layer
const source = new VectorSource({
features: new GeoJSON({
dataProjection: geojson.crs.properties.name,
featureProjection: 'EPSG:3857',
}).readFeatures(geojson),
})
// Check if we have valid features
if (source.getFeatures().length === 0) {
showWarning(
'No valid features found in the processing results. Please try again with a different area or settings.',
)
return null
}
vectorLayer.value = new VectorLayer({
source: source,
style: (feature) => {
if (feature === selectedFeature.value) {
return highlightStyle
}
return featureStyle
},
zIndex: 1001, // Higher than S2-grid-layer (1000)
})
// Ensure the results layer is on top by setting a high z-index
map.value!.addLayer(vectorLayer.value)
geoJsonResults.value = geojson.features
// Get the extent and validate it
const extent = source.getExtent()
if (!extent || extent.every((coord) => coord === 0) || extent.some((coord) => isNaN(coord))) {
showWarning(
'Invalid extent generated from processing results. Please try again with a different area or settings.',
)
return null
}
return transformExtent(extent, 'EPSG:3857', 'EPSG:4326')
}
return {
map,
areaValues,
vectorLayer,
maxArea: 3000,
handleMapClick,
hidePropertiesBox,
calculateOptimalPosition,
selectedFeature,
propertiesBoxPosition,
originalClickPosition,
showPropertiesBox,
fitMapToBbox,
displayGeoJSON,
geoJsonResults,
initCloudlessLayer,
updateLayers,
isLayerLoading,
}
}