-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapStore.ts
More file actions
356 lines (293 loc) · 12.4 KB
/
MapStore.ts
File metadata and controls
356 lines (293 loc) · 12.4 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
import {
Ref, computed, reactive, ref,
} from 'vue';
import {
AnnotationTypes,
ClickedProps,
ColorFilters,
Context,
Dataset,
DisplayConfiguration,
LayerCollection,
NetCDFData,
NetCDFLayer,
RasterMapLayer,
SearchableVectorData,
VectorFeatureTableGraph,
VectorMapLayer,
} from './types';
import UVdatApi from './api/UVDATApi';
export const VECTOR_PMTILES_URL = '/public/vectortiles/us.pmtiles';
async function isVectorBaseMapAvailable(vectorMapUrl: string) {
const resp = await fetch(vectorMapUrl, { method: 'HEAD' });
return Number(resp.headers.get('content-length') ?? 0) > 0 && resp.status === 200;
}
type SideBarCard = 'indicators' | 'charts' | 'searchableVectors';
export default class MapStore {
public static osmBaseMap = ref<'none' | 'osm-raster' | 'osm-vector'>('osm-raster');
public static userIsStaff = computed(() => !!UVdatApi.user?.is_staff);
public static proModeButtonEnabled = ref(true);
public static displayConfiguration: Ref<DisplayConfiguration> = ref(
{ default_displayed_layers: [], enabled_ui: ['Collections', 'Datasets', 'Metadata'], default_tab: 'Scenarios' },
);
// Ability to toggle proMode so Staff users can see what other users see.
public static proMode = computed(() => MapStore.userIsStaff.value && MapStore.proModeButtonEnabled.value);
public static vectorBaseMapAvailable = ref(true);
public static tvaOutlineLayer = ref(true);
public static naipSatelliteLayer = ref(false);
public static tdotSatelliteLayer = ref(false);
// Collection
public static availableCollections = ref<LayerCollection[]>([]);
public static selectedCollection = ref<LayerCollection | null>(null);
// Context
public static availableContexts = ref<Context[]>([]);
public static selectedContextId = ref<number | null>(null);
// Datasets
public static datasetsByContext = reactive<Record<number, Dataset[]>>({});
// Layers
public static mapLayersByDataset = reactive<Record<number, (VectorMapLayer | RasterMapLayer | NetCDFData)[]>>({});
public static selectedMapLayers = ref<(VectorMapLayer | RasterMapLayer | NetCDFLayer)[]>([]);
public static visibleMapLayers: Ref<Set<string>> = ref(new Set());
public static selectedVectorMapLayers: Ref<VectorMapLayer[]> = computed(
() => MapStore.selectedMapLayers.value.filter((layer) => layer.type === 'vector'),
);
public static selectedRasterMapLayers: Ref<RasterMapLayer[]> = computed(
() => MapStore.selectedMapLayers.value.filter((layer) => layer.type === 'raster'),
);
public static selectedNetCDFMapLayers: Ref<NetCDFLayer[]> = computed(
() => this.selectedMapLayers.value.filter((layer) => layer.type === 'netcdf'),
);
public static async loadCollections() {
MapStore.availableCollections.value = await UVdatApi.getLayerCollections();
}
public static async loadContexts() {
MapStore.availableContexts.value = await UVdatApi.getContexts();
}
public static async loadGlobalDatasets(filters:{ unconnected: boolean }) {
const data = await UVdatApi.getGlobalDatasets(filters);
return data;
}
public static async loadDatasets(contextId: number) {
// Doesn't guard against multiple calls to loadDatasets with the same contextId,
// but that scenario isn't a concern. Also, no cancellation behavior.
if (contextId in MapStore.datasetsByContext) return;
MapStore.datasetsByContext[contextId] = await UVdatApi.getContextDatasets(contextId);
}
public static async loadLayers(datasetId: number, force = false) {
// Doesn't guard against multiple calls to loadDatasets with the same contextId,
// but that scenario isn't a concern. Also, no cancellation behavior.
if (datasetId in MapStore.mapLayersByDataset && !force) return;
MapStore.mapLayersByDataset[datasetId] = await UVdatApi.getDatasetLayers(datasetId);
}
public static async getDisplayConfiguration(initial = false) {
MapStore.displayConfiguration.value = await UVdatApi.getDisplayConfiguration();
// Loading first time process default map layers
if (initial && MapStore.displayConfiguration.value.default_displayed_layers.length) {
const datasetIds = MapStore.displayConfiguration.value.default_displayed_layers.map((item) => item.dataset_id);
const datasetIdLayers = await UVdatApi.getDatasetsLayers(datasetIds);
const layerByDataset: Record<number, (VectorMapLayer | RasterMapLayer | NetCDFData)[]> = {};
const toggleLayers: (VectorMapLayer | RasterMapLayer | NetCDFLayer)[] = [];
const enabledLayers = MapStore.displayConfiguration.value.default_displayed_layers;
datasetIdLayers.forEach((item) => {
if (item.dataset_id !== undefined) {
if (layerByDataset[item.dataset_id] === undefined) {
layerByDataset[item.dataset_id] = [];
}
layerByDataset[item.dataset_id].push(item);
}
enabledLayers.forEach((enabledLayer) => {
if (item.type === 'netcdf') {
if (enabledLayer.dataset_id === item.dataset_id) {
const netCDFLayers = ((item as NetCDFData).layers);
for (let i = 0; i < netCDFLayers.length; i += 1) {
const layer = netCDFLayers[i];
if (layer.id === enabledLayer.id) {
toggleLayers.push(layer);
}
}
}
} else if (
enabledLayer.type === item.type
&& enabledLayer.id === item.id
&& enabledLayer.dataset_id === item.dataset_id) {
toggleLayers.push(item);
}
});
});
Object.keys(layerByDataset).forEach((datasetIdKey) => {
const datasetId = parseInt(datasetIdKey, 10);
if (!Number.isNaN(datasetId)) {
MapStore.mapLayersByDataset[datasetId] = layerByDataset[datasetId];
}
});
// Now we enable these default layers
return toggleLayers;
}
return [];
}
public static mapLayerFeatureGraphs = computed(() => {
const foundMapLayerFeatureGraphs: { name: string, id: number; graphs: VectorFeatureTableGraph[] }[] = [];
MapStore.selectedVectorMapLayers.value.forEach((item) => {
if (item.default_style?.mapLayerFeatureTableGraphs && item.default_style.mapLayerFeatureTableGraphs.length) {
foundMapLayerFeatureGraphs.push({
name: item.name,
id: item.id,
graphs: item.default_style.mapLayerFeatureTableGraphs,
});
}
});
return foundMapLayerFeatureGraphs;
});
public static mapLayerFeatureGraphsVisible = ref(false);
// Graph color mapping implementation
public static enabledMapLayerFeatureColorMapping = ref(false);
public static mapLayerFeatureColorMapping: Ref<Record<number, string>> = ref({});
public static clearMapLayerFeatureColorMapping = () => {
MapStore.mapLayerFeatureColorMapping.value = {};
};
// Searchable Vector Features
public static mapLayerVectorSearchable = computed(() => {
const foundMapLayerSearchable: { name: string, id: number; searchSettings: SearchableVectorData }[] = [];
MapStore.selectedVectorMapLayers.value.forEach((item) => {
if (item.default_style?.searchableVectorFeatureData) {
foundMapLayerSearchable.push({
name: item.name,
id: item.id,
searchSettings: item.default_style.searchableVectorFeatureData,
});
}
});
return foundMapLayerSearchable;
});
// ToolTips
public static toolTipMenuOpen = ref(false);
public static toolTipsEnabled = ref(false);
public static toolTipDisplay: Ref<Record<string, boolean>> = ref({});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public static selectedFeatures: Ref<ClickedProps[]> = ref([]);
public static addSelectedFeature = (clicked: ClickedProps) => {
const found = MapStore.selectedFeatures.value.find((item) => item.id === clicked.id);
if (!found) {
MapStore.selectedFeatures.value.push(clicked);
}
};
public static clearSelectedFeatures = () => {
MapStore.selectedFeatures.value = [];
};
public static removeSelectedFeature = (id: number) => {
const foundIndex = MapStore.selectedFeatures.value.findIndex((item) => item.id === id);
if (foundIndex !== -1) {
MapStore.selectedFeatures.value.splice(foundIndex, 1);
}
};
public static selectedFeatureExpanded = ref(true);
public static selectedIds = computed(() => MapStore.selectedFeatures.value.map((item) => item.properties.vectorfeatureid));
public static hoveredFeatures: Ref<number[]> = ref([]);
public static setHoveredFeature = (feature: number) => {
MapStore.hoveredFeatures.value = [feature];
};
public static removeHoveredFeature = (feature: number) => {
const foundIndex = MapStore.hoveredFeatures.value.findIndex((item) => item === feature);
if (foundIndex !== -1) {
MapStore.hoveredFeatures.value.splice(foundIndex, 1);
}
};
public static removeHoveredFeatures = (features: number[]) => {
MapStore.hoveredFeatures.value = MapStore.hoveredFeatures.value.filter((item) => !features.includes(item));
};
public static clearHoveredFeatures = () => {
MapStore.hoveredFeatures.value = [];
};
public static pollForVectorBasemap = (pollInterval: number = 5000) => {
let cancelled = false;
async function check() {
if (cancelled) return;
if (await isVectorBaseMapAvailable(VECTOR_PMTILES_URL)) {
MapStore.vectorBaseMapAvailable.value = true;
} else {
setTimeout(() => {
check();
}, pollInterval);
}
}
check();
return {
cancel: () => {
cancelled = true;
},
};
};
// SideBar Cards
public static activeSideBarCard: Ref<undefined | SideBarCard> = ref(undefined);
public static sideBarCardSettings: Ref<
Record<SideBarCard, { name: string; width: number; icon: string, enabled: boolean; key: SideBarCard }>> = ref(
{
indicators: {
name: 'Indicators', width: 450, icon: 'mdi-thermometer', enabled: false, key: 'indicators',
},
charts: {
name: 'Chart', width: 650, icon: 'mdi-chart-bar', enabled: false, key: 'charts',
},
searchableVectors: {
name: 'Search Vector Features', width: 300, icon: 'mdi-map-search-outline', enabled: false, key: 'searchableVectors',
},
},
);
public static sideBarOpen = computed(() => (
!!(MapStore.activeSideBarCard && Object.values(MapStore.sideBarCardSettings.value).find((item) => item.enabled))));
public static currentSideBarWidth = computed(() => {
if (!MapStore.activeSideBarCard.value) {
return 0;
}
return MapStore.sideBarCardSettings.value[MapStore.activeSideBarCard.value].width;
});
public static toggleContext = (card: SideBarCard) => {
MapStore.sideBarCardSettings.value[card].enabled = !MapStore.sideBarCardSettings.value[card].enabled;
if (MapStore.sideBarCardSettings.value[card].enabled) {
MapStore.activeSideBarCard.value = card;
Object.keys(MapStore.sideBarCardSettings.value).forEach((key) => {
if (key !== card) {
MapStore.sideBarCardSettings.value[key as SideBarCard].enabled = false;
}
});
} else {
const found = Object.values(MapStore.sideBarCardSettings.value).find((item) => item.enabled);
if (found) {
MapStore.activeSideBarCard.value = found.key;
} else {
MapStore.activeSideBarCard.value = undefined;
}
}
};
public static closeSideBar = () => {
MapStore.activeSideBarCard.value = undefined;
Object.keys(MapStore.sideBarCardSettings.value).forEach((key) => {
MapStore.sideBarCardSettings.value[key as SideBarCard].enabled = false;
});
};
public static vectorColorFilters: Ref<ColorFilters[]> = ref([]);
public static toggleColorFilter = (layerId: number, layerType: (AnnotationTypes | 'all'), key: string, value: string) => {
const foundIndex = MapStore.vectorColorFilters.value.findIndex(
(item) => item.layerId === layerId && layerType === item.layerType && key === item.key,
);
if (foundIndex === -1) {
MapStore.vectorColorFilters.value.push({
layerId,
layerType,
type: 'not in',
key,
values: new Set<string>([value]),
});
} else {
const found = MapStore.vectorColorFilters.value[foundIndex];
if (found.values.has(value)) {
found.values.delete(value);
if (found.values.size === 0) {
MapStore.vectorColorFilters.value.splice(foundIndex, 1);
}
} else {
found.values.add(value);
}
}
};
}