|
13 | 13 | :icon="showDataDrawer ? mdiChevronRight : mdiChevronLeft" size="x-small"> |
14 | 14 | </v-btn> |
15 | 15 | <v-col v-if="showFilterDrawer" :cols="3"> |
16 | | - <v-btn @click="downloadMapData()" class="w-100">Download Filtered Map Data</v-btn> |
17 | 16 | <FilterDrawer @onFilter="onFilter"/> |
18 | 17 | </v-col> |
19 | 18 | <v-divider vertical></v-divider> |
@@ -48,31 +47,14 @@ import DataViewDrawer from '@/components/DataViewDrawer.vue'; |
48 | 47 | import TheLeafletMap from '@/components/TheLeafletMap.vue'; |
49 | 48 | import { mdiChevronRight, mdiChevronLeft } from '@mdi/js' |
50 | 49 | import { useMapStore } from '@/stores/map'; |
51 | | -import { useDisplay } from 'vuetify'; |
52 | | -import Papa from 'papaparse'; |
| 50 | +import { useDisplay } from 'vuetify' |
53 | 51 |
|
54 | 52 | const { mdAndDown } = useDisplay() |
55 | 53 | const mapStore = useMapStore() |
56 | 54 |
|
57 | 55 | const showFilterDrawer = ref(true) |
58 | 56 | const showDataDrawer = ref(true) |
59 | 57 | const dataDrawerRef = ref(null) |
60 | | -const ignoreColumnInCSV = [ 'Type', 'Id', 'Location Id', 'Spatialzone Id', 'Temporalzone Id', 'Three D Info', 'Process Taxonomies Identifier', 'Model Type Id', 'Process Taxonomies Process Level', 'Process Taxonomies Function Id', 'Process Taxonomies Id', 'Process Taxonomies Process Alt Name Id', 'Spatial Zone Type Id', 'Temporal Zone Type Id', 'Location Lon', 'Location Name', 'Location Lat', 'Location Pt','process_taxonomies_identifier','process_taxonomies_process_level','process_taxonomies_function_id','process_taxonomies_id','process_taxonomies_process_alt_name_id'] |
61 | | -
|
62 | | -const renameColumnInCSV = { |
63 | | -"figure num": "Figure Number", |
64 | | -"citation citation": "Citation", |
65 | | -"location long name": "Location Name", |
66 | | -"location area km2": "Location Area [km^2]", |
67 | | -"process taxonomies process": "Taxonomy Processes", |
68 | | -"geol info": "Geological Info", |
69 | | -"topo info": "Topographic Info", |
70 | | -"num spatial zones": "Number of Spatial Zones", |
71 | | -"spatial zone type spatial property": "Spatial Zone Type", |
72 | | -"num temporal zones": "Number of Temporal Zones", |
73 | | -"temporal zone type temporal property": "Temporal Zone Type", |
74 | | -"model type name": "Model Type", |
75 | | -} |
76 | 58 |
|
77 | 59 | const onFilter = (data) => { |
78 | 60 | const filters = { |
@@ -129,90 +111,6 @@ const translateData = () => { |
129 | 111 | } |
130 | 112 | } |
131 | 113 |
|
132 | | -function flattenItem(obj, parentKey = '', result = {}) { |
133 | | - for (const key in obj) { |
134 | | - if (Object.prototype.hasOwnProperty.call(obj, key)) { |
135 | | - let newKey = parentKey ? `${parentKey} ${key}` : key; |
136 | | -
|
137 | | - // Convert column names as per requirement |
138 | | - newKey = renamecsvColumn(newKey); |
139 | | -
|
140 | | - // Ignore columns as per requirement |
141 | | - if (ignoreColumnInCSV.includes(newKey)) { |
142 | | - continue; |
143 | | - } |
144 | | -
|
145 | | - if (typeof obj[key] === 'object' && !Array.isArray(obj[key])) { |
146 | | - // Recursive call for nested objects |
147 | | - flattenItem(obj[key], newKey, result); |
148 | | - } else if (Array.isArray(obj[key])) { |
149 | | - if (obj[key].every(item => typeof item === 'string' || typeof item === 'number')) { |
150 | | - result[newKey] = obj[key].join(', '); |
151 | | - } else { |
152 | | - const arrayValues = {}; |
153 | | - obj[key].forEach(item => { |
154 | | - for (const subKey in item) { |
155 | | - // Ignore columns as per requirement |
156 | | - if (ignoreColumnInCSV.includes(`${key}_${subKey}`)) { |
157 | | - continue; |
158 | | - } |
159 | | - if (Object.prototype.hasOwnProperty.call(item, subKey)) { |
160 | | - if (!arrayValues[subKey]) { |
161 | | - arrayValues[subKey] = []; |
162 | | - } |
163 | | - arrayValues[subKey].push(item[subKey]); |
164 | | - } |
165 | | - } |
166 | | - }); |
167 | | - for (const arrayKey in arrayValues) { |
168 | | - result[renamecsvColumn(`${newKey} ${arrayKey}`)] = arrayValues[arrayKey].join(', '); |
169 | | - } |
170 | | - } |
171 | | - } else { |
172 | | - // Assign values directly |
173 | | - result[newKey] = obj[key]; |
174 | | - } |
175 | | - } |
176 | | - } |
177 | | - return result; |
178 | | -} |
179 | | -
|
180 | | -function flattenJSON(data) { |
181 | | - const result = []; |
182 | | - data.forEach(item => { |
183 | | - // As per requirement only include properties, not geometry |
184 | | - result.push(flattenItem(item.properties)); |
185 | | - }); |
186 | | - return result; |
187 | | -} |
188 | | -
|
189 | | -const downloadMapData = () => { |
190 | | - const jsonData = mapStore.currentFilteredData; |
191 | | - const flattenedData = flattenJSON(jsonData); |
192 | | -
|
193 | | - const csv = Papa.unparse(flattenedData, {}); |
194 | | -
|
195 | | - const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' }); |
196 | | -
|
197 | | - const link = document.createElement('a'); |
198 | | - link.href = URL.createObjectURL(blob); |
199 | | - link.setAttribute('download', 'data.csv'); |
200 | | - document.body.appendChild(link); |
201 | | - link.click(); |
202 | | - document.body.removeChild(link); |
203 | | -} |
204 | | -
|
205 | | -const renamecsvColumn = (name) => { |
206 | | - let newName = name.replaceAll("_", " "); |
207 | | - newName = newName.replace(/\b\w/g, char => char.toLowerCase()); |
208 | | - if (renameColumnInCSV[newName]) { |
209 | | - newName = renameColumnInCSV[newName]; |
210 | | - } else { |
211 | | - newName = newName.replace(/\b\w/g, char => char.toUpperCase()); |
212 | | - } |
213 | | - return newName; |
214 | | -} |
215 | | -
|
216 | 114 |
|
217 | 115 | </script> |
218 | 116 |
|
|
0 commit comments