-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUVDATApi.ts
More file actions
524 lines (446 loc) · 16.4 KB
/
UVDATApi.ts
File metadata and controls
524 lines (446 loc) · 16.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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
import axios, { AxiosInstance, AxiosRequestHeaders } from 'axios';
import { ref } from 'vue';
import OauthClient from '@girder/oauth-client/dist/oauth-client';
import {
AbstractMapLayer,
Chart,
Context,
ContextWithIds,
Dataset,
DerivedRegion,
FeatureGraphData,
FileItem,
LayerCollection,
LayerCollectionLayer,
LayerRepresentation,
NetCDFData,
NetCDFImages,
NetCDFLayer,
NetworkNode,
ProcessingTask,
PropertySummary,
RasterData,
RasterMapLayer,
SimulationType,
TableSummary,
VectorMapLayer,
} from '../types';
export const currentError = ref<string>();
export interface MetadataResponse {
levels: number;
sizeX: number;
sizeY: number;
tileWidth: number;
tileHeight: number;
magnification: number | null;
mm_x: number;
mm_y: number;
dtype: string; // Data type, e.g., "float32"
bandCount: number;
geospatial: boolean;
sourceLevels: number;
sourceSizeX: number;
sourceSizeY: number;
bounds?: Bounds;
projection: string | null;
sourceBounds?: Bounds;
bands: Record<string, BandInfo>;
frames: FrameInfo[] | false;
}
interface Bounds {
ll: Coordinate; // Lower-left coordinate
ul: Coordinate; // Upper-left coordinate
lr: Coordinate; // Lower-right coordinate
ur: Coordinate; // Upper-right coordinate
srs: string; // Spatial reference system
xmin: number;
xmax: number;
ymin: number;
ymax: number;
}
interface Coordinate {
x: number;
y: number;
}
export interface BandInfo {
min: number;
max: number;
mean: number;
stdev: number;
interpretation: string; // e.g., "gray"
nodata?: number | null;
}
interface FrameInfo {
frame: string;
bands: BandInfo[];
}
export interface NetCDFGenerateParams {
netcdf_data_id: number;
variable: string;
name: string;
description?: string;
sliding_variable?: string;
x_variable?: string;
y_variable?: string;
color_map?: string;
additional_vars?: string;
xRange?: [number, number];
yRange?: [number, number];
slicerRange?: [number, number];
}
export interface NetCDFPreviewParams {
netcdf_data_id: number;
variable: string;
i: number;
sliding_variable?: string;
x_variable?: string;
y_variable?: string;
color_map?: string;
additional_vars?: string;
xRange?: [number, number];
yRange?: [number, number];
slicerRange?: [number, number];
}
interface User {
id: number;
username: string;
is_staff: boolean;
}
interface AddDatasetParams {
name: string;
description?: string;
category?: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
metadata: Record<string, any>;
}
const baseURL = new URL((import.meta.env.VUE_APP_API_ROOT as string || 'http://localhost:8000/api/v1/'), window.location.origin);
const UVDATApiRoot = ''; // If we have another location this would be modified
export default class UVdatApi {
public static apiClient: AxiosInstance;
public static baseURL = `${baseURL}${UVDATApiRoot}/`;
public static user: User | null = null;
public static get origin(): string {
return baseURL.origin;
}
public static initialize(oauthClient: OauthClient) {
UVdatApi.apiClient = axios.create({
baseURL: `${baseURL}`,
headers: oauthClient?.authHeaders,
});
UVdatApi.apiClient.interceptors.request.use((config) => ({
...config,
headers: {
...oauthClient?.authHeaders,
...config.headers,
} as AxiosRequestHeaders,
}));
}
public static async fetchCurrentUser() : Promise<User | null> {
const { data, status } = await UVdatApi.apiClient.get('/users/me/');
if (status === 204) {
UVdatApi.user = null;
} else if (status === 200) {
UVdatApi.user = {
id: data.id,
username: data.username,
is_staff: data.is_staff,
};
} else {
throw new Error('Failed to fetch current user');
}
return UVdatApi.user;
}
public static async getContexts(): Promise<Context[]> {
return (await UVdatApi.apiClient.get('contexts')).data.results;
}
public static async getContext(id: number): Promise<ContextWithIds> {
return (await UVdatApi.apiClient.get(`contexts/${id}/`)).data;
}
public static async getContextDatasets(
contextId: number,
): Promise<Dataset[]> {
return (await UVdatApi.apiClient.get(`datasets?context=${contextId}`)).data.results;
}
public static async getContextCharts(contextId: number): Promise<Chart[]> {
return (await UVdatApi.apiClient.get(`charts?context=${contextId}`)).data.results;
}
public static async getContextDerivedRegions(
contextId: number,
): Promise<DerivedRegion[]> {
return (await UVdatApi.apiClient.get(`derived-regions?context=${contextId}`)).data
.results;
}
public static async getContextSimulationTypes(
contextId: number,
): Promise<SimulationType[]> {
return (await UVdatApi.apiClient.get(`simulations/available/context/${contextId}`))
.data;
}
public static async getDataset(datasetId: number): Promise<Dataset> {
return (await UVdatApi.apiClient.get(`datasets/${datasetId}`)).data;
}
public static async deleteLayer(layerId: number, type: 'vector' | 'raster'): Promise<{ detail: string }> {
return (await UVdatApi.apiClient.delete(`/${type}s/${layerId}/`)).data;
}
public static async deleteFileItem(fileItemId: number): Promise<{ detail: string }> {
return (await UVdatApi.apiClient.delete(`/files/${fileItemId}/`)).data;
}
public static async getGlobalDatasets(filter: { unconnected: boolean }): Promise<(Dataset & { contextCount: number })[]> {
return (await UVdatApi.apiClient.get('datasets', { params: { ...filter } })).data.results;
}
public static async getDatasetNetwork(
datasetId: number,
): Promise<NetworkNode[]> {
return (await UVdatApi.apiClient.get(`datasets/${datasetId}/network`)).data;
}
public static async getNetworkGCC(
datasetId: number,
contextId: number,
exclude_nodes: number[],
): Promise<NetworkNode[]> {
return (
await UVdatApi.apiClient.get(
`datasets/${datasetId}/gcc?context=${contextId}&exclude_nodes=${exclude_nodes.toString()}`,
)
).data;
}
public static async getMapLayer(
mapLayerId: number,
mapLayerType: string,
): Promise<VectorMapLayer | RasterMapLayer> {
return (await UVdatApi.apiClient.get(`${mapLayerType}s/${mapLayerId}`)).data;
}
public static async getRasterData(layerId: number): Promise<RasterData> {
const resolution = 0.1;
const { data } = await UVdatApi.apiClient.get(`rasters/${layerId}/raster-data/${resolution}`);
const { sourceBounds } = (
await UVdatApi.apiClient.get(`rasters/${layerId}/info/metadata`)
).data;
return {
data,
sourceBounds,
};
}
public static async clearChart(chartId: number) {
await UVdatApi.apiClient.post(`charts/${chartId}/clear/`);
}
public static async runSimulation(
simulationId: number,
contextId: number,
args: object,
) {
return (
await UVdatApi.apiClient.post(
`simulations/run/${simulationId}/context/${contextId}/`,
args,
)
).data;
}
public static async getSimulationResults(
simulationId: number,
contextId: number,
) {
return (
await UVdatApi.apiClient.get(
`simulations/${simulationId}/context/${contextId}/results/`,
)
).data;
}
public static async getDerivedRegion(regionId: number) {
const res = await UVdatApi.apiClient.get(`derived-regions/${regionId}/`);
return res.data;
}
public static async postDerivedRegion(
name: string,
context: number,
regions: number[],
op: 'union' | 'intersection' | undefined,
) {
if (!op) return;
const operation = op.toUpperCase();
const res = await UVdatApi.apiClient.post('derived-regions/', {
name,
context,
operation,
regions,
});
// eslint-disable-next-line consistent-return
return res.data;
}
public static async patchVectorLayer(layerId: number, default_style: VectorMapLayer['default_style']) {
const res = await UVdatApi.apiClient.patch(`vectors/${layerId}/`, { default_style });
return res.data;
}
public static async patchRasterLayer(layerId: number, default_style: RasterMapLayer['default_style']) {
const res = await UVdatApi.apiClient.patch(`rasters/${layerId}/`, { default_style });
return res.data;
}
public static async getLayerPropertySummary(layerId: number): Promise<PropertySummary> {
return (await UVdatApi.apiClient.get(`vectors/${layerId}/property-summary`)).data;
}
public static async getRasterMetadata(layerId: number): Promise<MetadataResponse> {
return (await UVdatApi.apiClient.get(`/rasters/${layerId}/info/metadata/`)).data;
}
public static async getLayerRepresentations(mapLayerId: number, type: 'vector' | 'raster'): Promise<LayerRepresentation[]> {
return (await UVdatApi.apiClient.get(`/layer-representations/map-layer/${mapLayerId}/`, { params: { type } })).data;
}
public static async addLayerRepresentation(layerRepresentation: LayerRepresentation) {
return (await UVdatApi.apiClient.post('/layer-representations/', { ...layerRepresentation })).data;
}
public static async patchLayerRepresentation(layerRepresentationId: number, layerRepresentation: LayerRepresentation) {
return (await UVdatApi.apiClient.patch(`/layer-representations/${layerRepresentationId}/`, { ...layerRepresentation })).data;
}
public static async deleteLayerRepresentation(layerRepresentationId: number) {
return (await UVdatApi.apiClient.delete(`/layer-representations/${layerRepresentationId}/`)).data;
}
public static async getLayerCollections(): Promise<LayerCollection[]> {
return (await UVdatApi.apiClient.get('/layer-collections/')).data.results;
}
public static async addLayerCollection(layerCollection: LayerCollection) {
return (await UVdatApi.apiClient.post('/layer-collections/', { ...layerCollection })).data;
}
public static async patchLayerCollection(layerCollectionId: number, layerCollection: LayerCollection) {
return (await UVdatApi.apiClient.patch(`/layer-collections/${layerCollectionId}/`, { ...layerCollection })).data;
}
public static async deleteLayerCollection(layerCollectionId: number) {
return (await UVdatApi.apiClient.delete(`/layer-collections/${layerCollectionId}/`)).data;
}
public static async getMapLayerCollectionList(
layers: LayerCollectionLayer[],
enabled? : boolean,
): Promise<(VectorMapLayer | RasterMapLayer | NetCDFLayer)[]> {
return (await UVdatApi.apiClient.post('/map-layers/', { layers }, { params: { enabled } })).data;
}
public static async getRasterBbox(mapLayerId: number): Promise<Bounds> {
return (await UVdatApi.apiClient.get(`/rasters/${mapLayerId}/bbox`)).data;
}
public static async getVectorBbox(mapLayerId: number): Promise<Bounds> {
return (await UVdatApi.apiClient.get(`/vectors/${mapLayerId}/bbox`)).data;
}
public static async getMapLayersBoundingBox(
rasterMapLayerIds: number[] = [],
vectorMapLayerIds: number[] = [],
netCDFMapLayerIds: number[] = [],
): Promise<Bounds> {
// Create query parameters for the request
const params = new URLSearchParams();
rasterMapLayerIds.forEach((id) => params.append('rasterMapLayerIds', id.toString()));
vectorMapLayerIds.forEach((id) => params.append('vectorMapLayerIds', id.toString()));
netCDFMapLayerIds.forEach((id) => params.append('netCDFMapLayerIds', id.toString()));
// Make the request using axios
const response = await UVdatApi.apiClient.get('/map-layers/bbox', { params });
// Return the bounding box data from the response
return response.data;
}
public static async getPropertyStatistics(mapLayerId: number, property_keys: string, bbox?: string, bins?: number) {
return (await UVdatApi.apiClient.get(
`/vectors/${mapLayerId}/property-statistics/`,
{ params: { property_keys, bbox, bins } },
)).data;
}
public static async deleteDataset(datasetId: number): Promise<{ detail: string }> {
return (await UVdatApi.apiClient.delete(`/datasets/${datasetId}/`)).data;
}
public static async getNetCDFPreview(data: NetCDFPreviewParams): Promise<{ image: string }> {
return (await UVdatApi.apiClient.post('/netcdf/preview/', { ...data })).data;
}
public static async getNetCDFLayerImages(layerId: number): Promise<NetCDFImages> {
return (await UVdatApi.apiClient.get(`/netcdf/layer/${layerId}/images/`)).data;
}
public static async deleteNetCDFLayer(layerId: number): Promise<{ message?: string, error?: string }> {
return (await UVdatApi.apiClient.delete(`/netcdf/layer/${layerId}/delete-layer/`)).data;
}
public static async generateNetCDFLayer(data: NetCDFGenerateParams): Promise<{ image: string }> {
return (await UVdatApi.apiClient.post('/netcdf/generate-layer/', { ...data })).data;
}
public static async addDataset(data: AddDatasetParams): Promise<AddDatasetParams & { id: number }> {
return (await UVdatApi.apiClient.post('/datasets/', { ...data })).data;
}
public static async convertDataset(datasetId: number): Promise<Dataset> {
return (await UVdatApi.apiClient.get(`datasets/${datasetId}/convert`)).data;
}
public static async patchFileItem(itemId: number, data:{ name?: string }) {
return (UVdatApi.apiClient.patch(`files/${itemId}/`, data));
}
public static async postFileItem(
name: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
metadata: Record<string, any>,
index: number,
dataset: number,
fileKey: string,
): Promise<{ detail: string }> {
return (await UVdatApi.apiClient.post('/files/', {
name, metadata, index, dataset, fileKey,
})).data;
}
public static async getDatasetFiles(datasetId: number): Promise<FileItem[]> {
return (await UVdatApi.apiClient.get(`/datasets/${datasetId}/file_items`)).data;
}
public static async getDatasetLayers(datasetId: number): Promise<(VectorMapLayer | RasterMapLayer | NetCDFData)[]> {
return (await UVdatApi.apiClient.get(`/datasets/${datasetId}/map_layers`)).data;
}
public static async getProcessingTasks(): Promise<ProcessingTask[]> {
return (await UVdatApi.apiClient.get('/processing-tasks')).data;
}
public static async getFilteredProcessingTasks(
status: ProcessingTask['status'],
): Promise<ProcessingTask[]> {
return (await UVdatApi.apiClient.get('/processing-tasks/filtered/', { params: { status } })).data;
}
public static async cancelProcessingTask(taskId: number): Promise<{ detail: string }> {
return (await UVdatApi.apiClient.post(`/processing-tasks/${taskId}/cancel/`)).data;
}
public static async updateMapLayerName(id: number, type: AbstractMapLayer['type'], name: string) {
return (await UVdatApi.apiClient.patch('/map-layers/update-name/', { name, type, id })).data;
}
public static async updateFileItem(id: number, name: string) {
return (await UVdatApi.apiClient.patch(`files/${id}/`, { name })).data;
}
public static async updateDataset(id: number, name: string, category: string, description: string) {
return (await UVdatApi.apiClient.patch(`/datasets/${id}/`, { name, category, description })).data;
}
public static async addContext(
data:{ name?: string, datasets: number[], default_map_zoom: number, default_map_center: number[] },
) {
return (await (UVdatApi.apiClient.post<Context>('contexts/', data))).data;
}
public static async patchContext(
itemId: number,
data:{ name?: string, datasets: number[], default_map_zoom?: number, default_map_center?: number[] },
) {
return (UVdatApi.apiClient.patch(
`contexts/${itemId}/all/`,
{
...data,
default_map_zoom: data.default_map_zoom ? Math.round(data.default_map_zoom) : undefined,
},
));
}
public static async deleteContext(contextId: number): Promise<{ detail: string }> {
return (await UVdatApi.apiClient.delete(`/contexts/${contextId}/`)).data;
}
public static async getVectorTableSummary(layerId: number): Promise<TableSummary> {
return (await UVdatApi.apiClient.get('/vectorfeature/tabledata/table-summary/', { params: { layerId } })).data;
}
public static async getFeatureGraphData(
tableType: string,
vectorFeatureId: number,
xAxis: string = 'index',
yAxis: string = 'mean_va',
filter?: string,
filterVal?: string,
): Promise<FeatureGraphData> {
const response = await UVdatApi.apiClient.get('/vectorfeature/tabledata/feature-graph/', {
params: {
tableType,
vectorFeatureId,
xAxis,
yAxis,
filter,
filterVal,
},
});
return response.data;
}
}