-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathProcessingResults.vue
More file actions
353 lines (310 loc) · 9.83 KB
/
ProcessingResults.vue
File metadata and controls
353 lines (310 loc) · 9.83 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
<template>
<v-card elevation="8" :class="{ closed: !isOpen, 'processing-results': true, sidebar: true }">
<v-card-title class="d-flex align-center justify-space-between pa-2">
<div class="collapse-action" @click="toggleCollapsible">
<v-icon
:class="{ 'rotate-180': isOpen }"
class="mr-1 text-white transition-transform"
:icon="mdiChevronDown"
>
</v-icon>
<span class="text-white title">Results ({{ geoJsonResults.length }})</span>
</div>
</v-card-title>
<v-card-text v-show="isOpen" class="content">
<div class="settings">
<v-expansion-panels>
<v-expansion-panel title="Statistics" class="panel statistics">
<v-expansion-panel-text>
<div v-for="field in statFields" class="group" :key="field">
<v-label class="text-capitalize mb-1">{{ field }}</v-label>
<PropertiesDisplay :properties="statistics[field]" :units="statUnits[field]" />
</div>
</v-expansion-panel-text>
</v-expansion-panel>
<v-expansion-panel title="Field Details" class="panel fields">
<v-expansion-panel-text>
<v-row>
<v-col cols="12" class="d-flex align-center">
<v-select
class="w-50"
label="Sort by"
hide-details
outlined
:items="['Id', 'Area', 'Perimeter']"
v-model="sortKey"
></v-select>
<v-select
class="w-50"
label="Sort order"
hide-details
outlined
:items="['ascending', 'descending']"
v-model="sortOrder"
></v-select>
</v-col>
</v-row>
<v-list density="compact" color="transparent" class="pa-0">
<v-list-item
v-for="result in sortedResults"
:key="result.id"
class="result-item"
@click.stop="fitMapToResult(result)"
>
<div class="result-properties">
<PropertiesDisplay :properties="result.properties" />
</div>
</v-list-item>
</v-list>
<v-btn v-if="hasMoreResults" @click="limit += 50" class="action-button mt-4"
>Show more
</v-btn>
</v-expansion-panel-text>
</v-expansion-panel>
<v-expansion-panel title="Processes" class="panel processes">
<v-expansion-panel-text>
<div>
<div v-for="project in projects" class="group" :key="project">
<v-label class="text-capitalize mb-1">{{ project }}</v-label>
</div>
</div>
</v-expansion-panel-text>
</v-expansion-panel>
</v-expansion-panels>
</div>
<div class="action-buttons">
<v-btn
icon
color="primary"
variant="flat"
@click.stop="returnToResultsHandler"
class="action-button return-to-results"
title="Zoom to results"
>
<v-icon :icon="mdiTarget"></v-icon>
</v-btn>
<v-btn class="action-button download-results" @click="downloadResults" density="comfortable"
>Download</v-btn
>
<v-btn
class="action-button clear-results"
@click="clearResultsHandler"
color="error"
density="compact"
>Clear</v-btn
>
</div>
</v-card-text>
</v-card>
</template>
<script setup lang="ts">
import { ref, onMounted, onUnmounted, onBeforeUnmount, computed } from 'vue'
import useMap from '../composables/useMap'
import useNotifier from '../composables/useNotifier'
import useAreaOfInterest from '../composables/useAreaOfInterest'
import useProcessing from '../composables/useProcessing'
import PropertiesDisplay from './PropertiesDisplay.vue'
import { mdiChevronDown, mdiTarget } from '@mdi/js'
import { type Feature } from 'geojson'
const props = defineProps<{
geoJsonResults: any[]
}>()
const emit = defineEmits<{
(e: 'clearResults'): void
}>()
const { map, handleMapClick, vectorLayer, selectedFeature, hidePropertiesBox } = useMap()
const { clearResults, returnToResults, fitToExtent } = useAreaOfInterest()
const { showInfo, showError } = useNotifier()
const { projects } = useProcessing()
const isOpen = ref(true)
const limit = ref(50)
const sortKey = ref<'Id' | 'Area' | 'Perimeter'>('Id')
const sortOrder = ref<'ascending' | 'descending'>('ascending')
const hasMoreResults = computed(() => {
return props.geoJsonResults.length > limit.value
})
const sortedResults = computed(() => {
return props.geoJsonResults
.slice(0)
.sort((a, b) => {
let compareA, compareB
switch (sortKey.value) {
case 'Id':
compareA = a.id
compareB = b.id
break
default:
const key = sortKey.value.toLowerCase()
compareA = a.properties[key]
compareB = b.properties[key]
}
let order = 0
if (typeof compareA === 'string' || typeof compareB === 'string') {
compareA = String(compareA)
compareB = String(compareB)
order = compareA.localeCompare(compareB)
} else if (compareA < compareB) {
order = -1
} else if (compareA > compareB) {
order = 1
}
if (sortOrder.value === 'descending') {
order *= -1
}
return order
})
.slice(0, limit.value)
})
interface ResaultStats {
count: number
min: number
mean: number | null
max: number
sum: number
}
const statFields: ('area' | 'perimeter')[] = ['area', 'perimeter']
const statUnits: Record<'area' | 'perimeter', (key: string | number) => string> = {
area: (key) => (key === 'count' ? '' : 'ha'),
perimeter: (key) => (key === 'count' ? '' : 'm'),
}
const statistics = computed(() => {
const template: ResaultStats = { count: 0, min: Infinity, mean: 0, max: -Infinity, sum: 0 }
const stats: Record<'area' | 'perimeter', ResaultStats> = {
area: Object.assign({}, template),
perimeter: Object.assign({}, template),
}
props.geoJsonResults.forEach((feature) => {
for (const type of statFields) {
const value = feature.properties[type]
if (typeof value === 'number') {
stats[type].count += 1
stats[type].sum += value
if (value < stats[type].min) stats[type].min = value
if (value > stats[type].max) stats[type].max = value
}
}
})
for (const type of statFields) {
const stat = stats[type]
stat.mean = stat.count > 0 ? stat.sum / stat.count : null
}
return stats
})
const toggleCollapsible = () => {
isOpen.value = !isOpen.value
}
const downloadResults = () => {
if (!props.geoJsonResults || props.geoJsonResults.length === 0) {
showInfo('No results to download.')
return
}
try {
// Create a GeoJSON FeatureCollection from the results
const geojson = {
type: 'FeatureCollection',
features: props.geoJsonResults,
}
// Convert to JSON string
const jsonString = JSON.stringify(geojson)
// Create blob and download
const blob = new Blob([jsonString], { type: 'application/geo+json' })
const url = URL.createObjectURL(blob)
// Create download link
const link = document.createElement('a')
link.href = url
link.download = `processing-results-${new Date().toISOString().split('T')[0]}.geojson`
// Trigger download
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
// Clean up
URL.revokeObjectURL(url)
} catch (error) {
console.error('Error downloading results:', error)
showError('Failed to download results.')
}
}
const fitMapToResult = (result: Feature) => {
if (!result || !result.id) {
showError('No valid geometry found for this result.')
return
}
const resultFeature = vectorLayer.value?.getSource()?.getFeatureById(result.id)
if (!resultFeature) {
showError('Feature not found on the map.')
return
}
hidePropertiesBox()
// Highlight the result feature
selectedFeature.value = resultFeature
// Get the extent of the feature
const extent = resultFeature.getGeometry()?.getExtent()
fitToExtent(map.value!, extent, null)
}
onMounted(() => {
// Add map click handler to detect feature clicks and show properties
if (map.value) {
map.value.on('singleclick', handleMapClick)
}
})
onBeforeUnmount(() => {
selectedFeature.value = null
})
// Clean up map click handler when component is unmounted
onUnmounted(() => {
if (map.value) {
map.value.un('singleclick', handleMapClick)
}
})
const clearResultsHandler = () => {
// Clear results and zoom back to S2 grid
clearResults(map.value!)
// Emit event to clear results in parent components
emit('clearResults')
}
const returnToResultsHandler = () => {
returnToResults(map.value!)
}
</script>
<style scoped>
.processing-results {
right: 1em;
max-height: calc(100vh - 3rem - 40px);
min-width: 250px;
width: 260px;
max-width: 45vw;
}
.processing-results .v-list {
background-color: transparent !important;
}
.result-item {
padding: 0.75rem;
background-color: rgba(255, 255, 255, 0.05);
border: 1px solid rgba(255, 255, 255, 0.1);
margin-top: 2px;
margin-bottom: 2px;
cursor: pointer;
transition: all 0.2s ease;
}
.result-item:hover {
background-color: rgba(255, 255, 255, 0.1);
border-color: rgba(0, 136, 136, 0.6);
}
.result-item:last-child {
border-bottom: none !important;
}
.result-properties {
display: flex;
flex-direction: column;
gap: 0.25rem;
}
:deep(.panel.fields .v-expansion-panel-text__wrapper) {
padding: 0;
}
.group {
margin-bottom: 1rem;
}
.group:last-child {
margin-bottom: 0;
}
</style>