Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/components/GlobalDataPanel.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script setup lang="ts">
import useSettings from '../composables/useSettings'
import { computed, onUnmounted, ref, watch } from 'vue'
import useSettings, { GLOBAL_DATA_MAP_FIELD_START_ZOOM_LEVEL } from '../composables/useSettings'
import useMap from '../composables/useMap'
import useAreaOfInterest from '../composables/useAreaOfInterest'
import type { PlaceResult } from '../composables/useAreaOfInterest'
Expand All @@ -13,6 +14,34 @@ const { map } = useMap()
const { fitToExtent } = useAreaOfInterest()
const { showError } = useNotifier()

const zoom = ref(0)
const onZoomChange = () => {
zoom.value = map.value?.getView()?.getZoom() ?? 0
}

// Watch map and attach zoom listener when map becomes available
watch(
map,
(newMap, oldMap) => {
// Clean up previous listener
if (oldMap) {
oldMap.getView()?.un('change:resolution', onZoomChange)
}

// Attach new listener if map is available
if (newMap) {
onZoomChange()
newMap.getView().on('change:resolution', onZoomChange)
}
},
{ immediate: true },
)

onUnmounted(() => {
map.value?.getView()?.un('change:resolution', onZoomChange)
})
const fieldBoundariesDisabled = computed(() => zoom.value < GLOBAL_DATA_MAP_FIELD_START_ZOOM_LEVEL)

const handleLocationSelected = (place: PlaceResult) => {
Comment thread
m-mohr marked this conversation as resolved.
if (!map.value) return
if (!place.boundingbox) {
Expand Down Expand Up @@ -52,6 +81,15 @@ const handleLocationSelected = (place: PlaceResult) => {
thumb-color="teal"
hide-details
/>
<v-checkbox
v-model="settings.showFieldBoundaries"
label="Show field boundaries"
density="compact"
hide-details
color="teal"
class="mt-2"
:disabled="fieldBoundariesDisabled"
/>
<h3 class="group legend">Legend</h3>
<MapLegend />
</v-col>
Expand Down
21 changes: 18 additions & 3 deletions src/composables/useMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,27 @@ watch(
// Insert at index 0 to keep it as the base layer
map.value.getLayers().insertAt(0, cloudlessLayer.value)

if (settings.value.mode === 'global' && globalPredictionsController.value) {
removeGlobalPredictionsLayer()
updateLayers()
if (settings.value.mode === 'global') {
if (globalPredictionsController.value) {
map.value.removeLayer(globalPredictionsController.value.layer)
globalPredictionsController.value.dispose()
globalPredictionsController.value = null
}

globalPredictionsController.value = createGlobalPredictionsLayer(settings.value)
globalPredictionsController.value.layer.setVisible(settings.value.showFieldBoundaries)
map.value.addLayer(globalPredictionsController.value.layer)
}
},
)

watch(
() => settings.value.showFieldBoundaries,
(visible) => {
globalPredictionsController.value?.layer.setVisible(visible)
},
)

const initCloudlessLayer = () => {
if (!map.value) {
return
Expand Down Expand Up @@ -143,6 +157,7 @@ const updateLayers = () => {
if (!globalPredictionsController.value) {
// Only handle first initialization here, year changes are handled by a watcher on year above
globalPredictionsController.value = createGlobalPredictionsLayer(settings.value)
globalPredictionsController.value.layer.setVisible(settings.value.showFieldBoundaries)
map.value.addLayer(globalPredictionsController.value.layer)
}
if (!globalOverviewLayer.value) {
Expand Down
9 changes: 9 additions & 0 deletions src/composables/usePermalink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface PermalinkStateInference extends PermalinkState {

export interface PermalinkStateGlobal extends PermalinkState {
threshold: number
showFieldBoundaries: boolean
}

export default function usePermalink() {
Expand Down Expand Up @@ -58,6 +59,7 @@ export default function usePermalink() {
center: [0, 0],
year: 2025,
threshold: 0.4,
showFieldBoundaries: true,
}

const getDefaultState = (mode: string): PermalinkStateInference | PermalinkStateGlobal => {
Expand Down Expand Up @@ -173,6 +175,7 @@ export default function usePermalink() {
zoom,
center,
threshold: defaultGlobalState.threshold,
showFieldBoundaries: defaultGlobalState.showFieldBoundaries,
}

for (const part of keyValueParts) {
Expand All @@ -182,6 +185,8 @@ export default function usePermalink() {
} else if (part.startsWith('year:')) {
const year = parseInt(part.substring(5), 10)
if (!isNaN(year)) result.year = year
} else if (part.startsWith('field_boundaries:')) {
result.showFieldBoundaries = part.substring(17) === '1'
}
}

Expand Down Expand Up @@ -284,13 +289,15 @@ export default function usePermalink() {
if (settings.value.year) {
hashParts.push(`year:${settings.value.year}`)
}
hashParts.push(`field_boundaries:${settings.value.showFieldBoundaries ? 1 : 0}`)

state = {
mode,
zoom,
center,
threshold: settings.value.threshold,
year: settings.value.year,
showFieldBoundaries: settings.value.showFieldBoundaries,
}
}

Expand Down Expand Up @@ -327,6 +334,7 @@ export default function usePermalink() {

function restoreGlobalState(state: PermalinkStateGlobal) {
settings.value.threshold = state.threshold
settings.value.showFieldBoundaries = state.showFieldBoundaries
if (state.year) {
settings.value.year = state.year
}
Expand Down Expand Up @@ -358,6 +366,7 @@ export default function usePermalink() {
settings.value.areaCoverage,
settings.value.buffer,
settings.value.threshold,
settings.value.showFieldBoundaries,
],
() => {
if (!map.value) {
Expand Down
2 changes: 2 additions & 0 deletions src/composables/useSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface Settings {
expertMode: boolean
mode: string
threshold: number
showFieldBoundaries: boolean
}

export interface ModelInfo {
Expand Down Expand Up @@ -57,6 +58,7 @@ const defaultSettings: Settings = {
expertMode: false,
mode: defaultMode,
threshold: 0.4,
showFieldBoundaries: true,
}

const defaultModel = computed(() => {
Expand Down
Loading