@loaders.gl/scientific 0.0.0-stage → 5.0.0-alpha.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -2
- package/dist/index.cjs +4313 -0
- package/dist/index.cjs.map +6 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/netcdf/bundled.cjs +1394 -0
- package/dist/netcdf/bundled.d.ts +2 -0
- package/dist/netcdf/bundled.d.ts.map +1 -0
- package/dist/netcdf/bundled.js +5 -0
- package/dist/netcdf/bundled.js.map +1 -0
- package/dist/netcdf/index.cjs +1394 -0
- package/dist/netcdf/index.cjs.map +6 -0
- package/dist/netcdf/index.d.ts +7 -0
- package/dist/netcdf/index.d.ts.map +1 -0
- package/dist/netcdf/index.js +8 -0
- package/dist/netcdf/index.js.map +1 -0
- package/dist/netcdf/iobuffer/iobuffer.d.ts +253 -0
- package/dist/netcdf/iobuffer/iobuffer.d.ts.map +1 -0
- package/dist/netcdf/iobuffer/iobuffer.js +450 -0
- package/dist/netcdf/iobuffer/iobuffer.js.map +1 -0
- package/dist/netcdf/netcdf-format.cjs +39 -0
- package/dist/netcdf/netcdf-format.d.ts +11 -0
- package/dist/netcdf/netcdf-format.d.ts.map +1 -0
- package/dist/netcdf/netcdf-format.js +14 -0
- package/dist/netcdf/netcdf-format.js.map +1 -0
- package/dist/netcdf/netcdf-loader-with-parser.cjs +995 -0
- package/dist/netcdf/netcdf-loader-with-parser.d.ts +60 -0
- package/dist/netcdf/netcdf-loader-with-parser.d.ts.map +1 -0
- package/dist/netcdf/netcdf-loader-with-parser.js +35 -0
- package/dist/netcdf/netcdf-loader-with-parser.js.map +1 -0
- package/dist/netcdf/netcdf-loader.cjs +84 -0
- package/dist/netcdf/netcdf-loader.d.ts +81 -0
- package/dist/netcdf/netcdf-loader.d.ts.map +1 -0
- package/dist/netcdf/netcdf-loader.js +41 -0
- package/dist/netcdf/netcdf-loader.js.map +1 -0
- package/dist/netcdf/netcdf-source-loader-types.cjs +78 -0
- package/dist/netcdf/netcdf-source-loader-types.d.ts +28 -0
- package/dist/netcdf/netcdf-source-loader-types.d.ts.map +1 -0
- package/dist/netcdf/netcdf-source-loader-types.js +32 -0
- package/dist/netcdf/netcdf-source-loader-types.js.map +1 -0
- package/dist/netcdf/netcdf-source-loader.cjs +1319 -0
- package/dist/netcdf/netcdf-source-loader.d.ts +59 -0
- package/dist/netcdf/netcdf-source-loader.d.ts.map +1 -0
- package/dist/netcdf/netcdf-source-loader.js +421 -0
- package/dist/netcdf/netcdf-source-loader.js.map +1 -0
- package/dist/netcdf/netcdfjs/netcdf-reader.d.ts +68 -0
- package/dist/netcdf/netcdfjs/netcdf-reader.d.ts.map +1 -0
- package/dist/netcdf/netcdfjs/netcdf-reader.js +172 -0
- package/dist/netcdf/netcdfjs/netcdf-reader.js.map +1 -0
- package/dist/netcdf/netcdfjs/netcdf-types.d.ts +70 -0
- package/dist/netcdf/netcdfjs/netcdf-types.d.ts.map +1 -0
- package/dist/netcdf/netcdfjs/netcdf-types.js +3 -0
- package/dist/netcdf/netcdfjs/netcdf-types.js.map +1 -0
- package/dist/netcdf/netcdfjs/read-data.d.ts +19 -0
- package/dist/netcdf/netcdfjs/read-data.d.ts.map +1 -0
- package/dist/netcdf/netcdfjs/read-data.js +51 -0
- package/dist/netcdf/netcdfjs/read-data.js.map +1 -0
- package/dist/netcdf/netcdfjs/read-header.d.ts +16 -0
- package/dist/netcdf/netcdfjs/read-header.d.ts.map +1 -0
- package/dist/netcdf/netcdfjs/read-header.js +227 -0
- package/dist/netcdf/netcdfjs/read-header.js.map +1 -0
- package/dist/netcdf/netcdfjs/read-type.d.ts +36 -0
- package/dist/netcdf/netcdfjs/read-type.d.ts.map +1 -0
- package/dist/netcdf/netcdfjs/read-type.js +135 -0
- package/dist/netcdf/netcdfjs/read-type.js.map +1 -0
- package/dist/netcdf/unbundled.cjs +1394 -0
- package/dist/netcdf/unbundled.d.ts +2 -0
- package/dist/netcdf/unbundled.d.ts.map +1 -0
- package/dist/netcdf/unbundled.js +5 -0
- package/dist/netcdf/unbundled.js.map +1 -0
- package/dist/netcdf.dev.js +3153 -0
- package/dist/netcdf.min.js +12 -0
- package/dist/zarr/bundled.d.ts +2 -0
- package/dist/zarr/bundled.d.ts.map +1 -0
- package/dist/zarr/bundled.js +5 -0
- package/dist/zarr/bundled.js.map +1 -0
- package/dist/zarr/geo-zarr-source-loader.d.ts +121 -0
- package/dist/zarr/geo-zarr-source-loader.d.ts.map +1 -0
- package/dist/zarr/geo-zarr-source-loader.js +692 -0
- package/dist/zarr/geo-zarr-source-loader.js.map +1 -0
- package/dist/zarr/index.cjs +14747 -0
- package/dist/zarr/index.cjs.map +6 -0
- package/dist/zarr/index.d.ts +11 -0
- package/dist/zarr/index.d.ts.map +1 -0
- package/dist/zarr/index.js +10 -0
- package/dist/zarr/index.js.map +1 -0
- package/dist/zarr/lib/consolidated-zarr.d.ts +31 -0
- package/dist/zarr/lib/consolidated-zarr.d.ts.map +1 -0
- package/dist/zarr/lib/consolidated-zarr.js +113 -0
- package/dist/zarr/lib/consolidated-zarr.js.map +1 -0
- package/dist/zarr/lib/load-zarr.d.ts +11 -0
- package/dist/zarr/lib/load-zarr.d.ts.map +1 -0
- package/dist/zarr/lib/load-zarr.js +22 -0
- package/dist/zarr/lib/load-zarr.js.map +1 -0
- package/dist/zarr/lib/spatial-multiscales.d.ts +14 -0
- package/dist/zarr/lib/spatial-multiscales.d.ts.map +1 -0
- package/dist/zarr/lib/spatial-multiscales.js +34 -0
- package/dist/zarr/lib/spatial-multiscales.js.map +1 -0
- package/dist/zarr/lib/utils.d.ts +28 -0
- package/dist/zarr/lib/utils.d.ts.map +1 -0
- package/dist/zarr/lib/utils.js +137 -0
- package/dist/zarr/lib/utils.js.map +1 -0
- package/dist/zarr/lib/zarr-data-cache.d.ts +13 -0
- package/dist/zarr/lib/zarr-data-cache.d.ts.map +1 -0
- package/dist/zarr/lib/zarr-data-cache.js +70 -0
- package/dist/zarr/lib/zarr-data-cache.js.map +1 -0
- package/dist/zarr/lib/zarr-pixel-source.d.ts +41 -0
- package/dist/zarr/lib/zarr-pixel-source.d.ts.map +1 -0
- package/dist/zarr/lib/zarr-pixel-source.js +97 -0
- package/dist/zarr/lib/zarr-pixel-source.js.map +1 -0
- package/dist/zarr/lib/zarrita-pixel-source.d.ts +40 -0
- package/dist/zarr/lib/zarrita-pixel-source.d.ts.map +1 -0
- package/dist/zarr/lib/zarrita-pixel-source.js +96 -0
- package/dist/zarr/lib/zarrita-pixel-source.js.map +1 -0
- package/dist/zarr/ome-zarr-source-loader.d.ts +195 -0
- package/dist/zarr/ome-zarr-source-loader.d.ts.map +1 -0
- package/dist/zarr/ome-zarr-source-loader.js +523 -0
- package/dist/zarr/ome-zarr-source-loader.js.map +1 -0
- package/dist/zarr/spatial-data-source.d.ts +95 -0
- package/dist/zarr/spatial-data-source.d.ts.map +1 -0
- package/dist/zarr/spatial-data-source.js +242 -0
- package/dist/zarr/spatial-data-source.js.map +1 -0
- package/dist/zarr/types.d.ts +105 -0
- package/dist/zarr/types.d.ts.map +1 -0
- package/dist/zarr/types.js +5 -0
- package/dist/zarr/types.js.map +1 -0
- package/dist/zarr/unbundled.d.ts +2 -0
- package/dist/zarr/unbundled.d.ts.map +1 -0
- package/dist/zarr/unbundled.js +5 -0
- package/dist/zarr/unbundled.js.map +1 -0
- package/dist/zarr/zarr-array-source.d.ts +102 -0
- package/dist/zarr/zarr-array-source.d.ts.map +1 -0
- package/dist/zarr/zarr-array-source.js +132 -0
- package/dist/zarr/zarr-array-source.js.map +1 -0
- package/dist/zarr/zarr-loader-types.d.ts +236 -0
- package/dist/zarr/zarr-loader-types.d.ts.map +1 -0
- package/dist/zarr/zarr-loader-types.js +128 -0
- package/dist/zarr/zarr-loader-types.js.map +1 -0
- package/dist/zarr.dev.js +16574 -0
- package/dist/zarr.min.js +79 -0
- package/package.json +62 -4
- package/src/index.ts +52 -0
- package/src/netcdf/bundled.ts +5 -0
- package/src/netcdf/index.ts +14 -0
- package/src/netcdf/iobuffer/iobuffer.ts +510 -0
- package/src/netcdf/netcdf-format.ts +16 -0
- package/src/netcdf/netcdf-loader-with-parser.ts +54 -0
- package/src/netcdf/netcdf-loader.ts +62 -0
- package/src/netcdf/netcdf-source-loader-types.ts +45 -0
- package/src/netcdf/netcdf-source-loader.ts +526 -0
- package/src/netcdf/netcdfjs/LICENSE +24 -0
- package/src/netcdf/netcdfjs/netcdf-reader.ts +196 -0
- package/src/netcdf/netcdfjs/netcdf-types.ts +75 -0
- package/src/netcdf/netcdfjs/read-data.ts +71 -0
- package/src/netcdf/netcdfjs/read-header.ts +277 -0
- package/src/netcdf/netcdfjs/read-type.ts +147 -0
- package/src/netcdf/unbundled.ts +5 -0
- package/src/zarr/bundled.ts +5 -0
- package/src/zarr/geo-zarr-source-loader.ts +998 -0
- package/src/zarr/index.ts +60 -0
- package/src/zarr/lib/consolidated-zarr.ts +190 -0
- package/src/zarr/lib/load-zarr.ts +31 -0
- package/src/zarr/lib/spatial-multiscales.ts +47 -0
- package/src/zarr/lib/utils.ts +162 -0
- package/src/zarr/lib/zarr-data-cache.ts +91 -0
- package/src/zarr/lib/zarr-pixel-source.ts +134 -0
- package/src/zarr/lib/zarrita-pixel-source.ts +141 -0
- package/src/zarr/ome-zarr-source-loader.ts +837 -0
- package/src/zarr/spatial-data-source.ts +370 -0
- package/src/zarr/types.ts +129 -0
- package/src/zarr/unbundled.ts +5 -0
- package/src/zarr/zarr-array-source.ts +225 -0
- package/src/zarr/zarr-loader-types.ts +166 -0
|
@@ -0,0 +1,692 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
import * as zarrita from 'zarrita';
|
|
5
|
+
import { createTileGrid, createScanQueryMetadata, getRasterViewportBoundingBox } from '@loaders.gl/core/loader-utils';
|
|
6
|
+
import { getSpatialMultiscaleLevels } from "./lib/spatial-multiscales.js";
|
|
7
|
+
import { ZarrSource } from "./ome-zarr-source-loader.js";
|
|
8
|
+
import { cloneZarrSelection, getCachedZarrSelection, getZarrSelectionKey } from "./lib/zarr-data-cache.js";
|
|
9
|
+
// __VERSION__ is injected by babel-plugin-version-inline
|
|
10
|
+
// @ts-ignore TS2304: Cannot find name '__VERSION__'.
|
|
11
|
+
const VERSION = typeof "5.0.0-alpha.13" !== 'undefined' ? "5.0.0-alpha.13" : 'latest';
|
|
12
|
+
/** Source factory for GeoZarr and regular CF/xarray Zarr raster arrays. */
|
|
13
|
+
export const GeoZarrSourceLoaderWithParser = {
|
|
14
|
+
dataType: null,
|
|
15
|
+
/** Runtime source type marker used by `createDataSource()` type inference. */
|
|
16
|
+
dataSource: null,
|
|
17
|
+
batchType: null,
|
|
18
|
+
name: 'GeoZarrSourceLoader',
|
|
19
|
+
id: 'geozarr',
|
|
20
|
+
module: 'scientific',
|
|
21
|
+
version: VERSION,
|
|
22
|
+
extensions: ['zarr'],
|
|
23
|
+
mimeTypes: [],
|
|
24
|
+
type: 'geozarr',
|
|
25
|
+
fromUrl: true,
|
|
26
|
+
fromBlob: false,
|
|
27
|
+
options: {
|
|
28
|
+
zarr: {
|
|
29
|
+
metadataPath: 'auto',
|
|
30
|
+
path: null,
|
|
31
|
+
labels: undefined,
|
|
32
|
+
requireConsolidatedMetadata: true
|
|
33
|
+
},
|
|
34
|
+
geozarr: {
|
|
35
|
+
array: undefined,
|
|
36
|
+
spatialDimensions: undefined,
|
|
37
|
+
transform: undefined,
|
|
38
|
+
coordinateReferenceSystem: undefined,
|
|
39
|
+
defaultSelection: undefined
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
defaultOptions: {
|
|
43
|
+
zarr: {
|
|
44
|
+
metadataPath: 'auto',
|
|
45
|
+
path: null,
|
|
46
|
+
labels: undefined,
|
|
47
|
+
requireConsolidatedMetadata: true
|
|
48
|
+
},
|
|
49
|
+
geozarr: {
|
|
50
|
+
array: undefined,
|
|
51
|
+
spatialDimensions: undefined,
|
|
52
|
+
transform: undefined,
|
|
53
|
+
coordinateReferenceSystem: undefined,
|
|
54
|
+
defaultSelection: undefined
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
testURL: (url) => /\.zarr(?:$|[/?#])/i.test(url),
|
|
58
|
+
createDataSource: (data, options, coreApi) => new GeoZarrRasterSource(data, options, coreApi)
|
|
59
|
+
};
|
|
60
|
+
/** Viewport-driven raster source for GeoZarr and regular CF/xarray Zarr data variables. */
|
|
61
|
+
export class GeoZarrRasterSource extends ZarrSource {
|
|
62
|
+
/** Shared source initialization request. */
|
|
63
|
+
initializationPromise = null;
|
|
64
|
+
/** Shared metadata-only multiscale discovery request. */
|
|
65
|
+
metadataPromise = null;
|
|
66
|
+
/** Creates a GeoZarr raster source. */
|
|
67
|
+
constructor(data, options, coreApi) {
|
|
68
|
+
super(data, options, coreApi);
|
|
69
|
+
}
|
|
70
|
+
/** Returns normalized geospatial, array, and selectable-dimension metadata. */
|
|
71
|
+
async getMetadata() {
|
|
72
|
+
if (!this.metadataPromise) {
|
|
73
|
+
this.metadataPromise = this.loadGridMetadata().catch(error => {
|
|
74
|
+
this.metadataPromise = null;
|
|
75
|
+
throw error;
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
return await this.metadataPromise;
|
|
79
|
+
}
|
|
80
|
+
/** Opens sibling array metadata without selecting or decoding their samples. */
|
|
81
|
+
async loadGridMetadata() {
|
|
82
|
+
const { metadata, group } = await this.getInitializationPromise();
|
|
83
|
+
const groupAttributes = metadata.metadata?.groupAttributes;
|
|
84
|
+
const layout = getSpatialMultiscaleLevels(groupAttributes, metadata.array);
|
|
85
|
+
const levels = [];
|
|
86
|
+
for (const level of layout) {
|
|
87
|
+
if (level.array === metadata.array) {
|
|
88
|
+
levels.push({ id: level.id, array: level.array, metadata });
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
const options = this.options;
|
|
92
|
+
const source = new GeoZarrRasterSource(this.data, {
|
|
93
|
+
...options,
|
|
94
|
+
geozarr: { ...options.geozarr, array: level.array, transform: undefined }
|
|
95
|
+
}, this.hasCoreApi ? this.coreApi : undefined);
|
|
96
|
+
try {
|
|
97
|
+
const initialization = await source.initialize(undefined, true, group);
|
|
98
|
+
levels.push({ id: level.id, array: level.array, metadata: initialization.metadata });
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
// Unsupported siblings remain advertised; never publish a complete set with missing levels.
|
|
102
|
+
levels.push({ id: level.id, array: level.array });
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const normalizedLevels = levels.length
|
|
106
|
+
? levels
|
|
107
|
+
: [{ id: metadata.array, array: metadata.array, metadata }];
|
|
108
|
+
const matrices = normalizedLevels.map(level => getSpatialChunkMatrix(level.id, level.metadata));
|
|
109
|
+
const commonCoordinateReferenceSystem = typeof metadata.crs === 'string' &&
|
|
110
|
+
normalizedLevels.every(level => level.metadata?.crs === metadata.crs)
|
|
111
|
+
? metadata.crs
|
|
112
|
+
: undefined;
|
|
113
|
+
return {
|
|
114
|
+
...metadata,
|
|
115
|
+
...(levels.length ? { levels } : {}),
|
|
116
|
+
tileGrid: createTileGrid(commonCoordinateReferenceSystem, matrices)
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
/** Discovers GeoZarr variables, spatial bounds, and selectable dimensions. */
|
|
120
|
+
async getQueryMetadata(options = {}) {
|
|
121
|
+
if (options.signal?.aborted)
|
|
122
|
+
throw new DOMException('The operation was aborted', 'AbortError');
|
|
123
|
+
const metadata = await this.getMetadata();
|
|
124
|
+
if (options.signal?.aborted)
|
|
125
|
+
throw new DOMException('The operation was aborted', 'AbortError');
|
|
126
|
+
const fields = [{ name: metadata.array, type: metadata.dtype, nullable: true, metadata: {} }];
|
|
127
|
+
return createScanQueryMetadata({
|
|
128
|
+
sourceType: 'geozarr',
|
|
129
|
+
queryType: 'raster',
|
|
130
|
+
execution: { status: 'supported', method: 'getRaster' },
|
|
131
|
+
name: metadata.name,
|
|
132
|
+
schema: { fields, metadata: {} },
|
|
133
|
+
capabilities: {
|
|
134
|
+
bounds: 'pushdown',
|
|
135
|
+
levelOfDetail: 'unsupported'
|
|
136
|
+
},
|
|
137
|
+
spatial: metadata.boundingBox
|
|
138
|
+
? {
|
|
139
|
+
bounds: { minimum: metadata.boundingBox[0], maximum: metadata.boundingBox[1] },
|
|
140
|
+
coordinateReferenceSystems: typeof metadata.crs === 'string' ? [metadata.crs] : undefined
|
|
141
|
+
}
|
|
142
|
+
: undefined
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
/** Loads a clipped native-resolution 2D window for the requested viewport and named selection. */
|
|
146
|
+
async getRaster(parameters) {
|
|
147
|
+
const initialization = await this.getInitializationPromise(parameters.signal);
|
|
148
|
+
const { array, metadata, xDimensionIndex, yDimensionIndex } = initialization;
|
|
149
|
+
const requestedBoundingBox = getRasterViewportBoundingBox(parameters.viewport);
|
|
150
|
+
const viewportCoordinateReferenceSystem = parameters.viewport.crs;
|
|
151
|
+
if (metadata.crs &&
|
|
152
|
+
viewportCoordinateReferenceSystem &&
|
|
153
|
+
metadata.crs !== viewportCoordinateReferenceSystem) {
|
|
154
|
+
throw new Error(`GeoZarrRasterSource does not support reprojection. Requested ${viewportCoordinateReferenceSystem}, source ${metadata.crs}.`);
|
|
155
|
+
}
|
|
156
|
+
if (parameters.resampleMethod === 'bilinear') {
|
|
157
|
+
throw new Error('GeoZarrRasterSource currently supports native nearest-neighbor windows only.');
|
|
158
|
+
}
|
|
159
|
+
const clippedBoundingBox = intersectBoundingBoxes(requestedBoundingBox, metadata.boundingBox);
|
|
160
|
+
if (!clippedBoundingBox) {
|
|
161
|
+
return createEmptyRaster(metadata, requestedBoundingBox);
|
|
162
|
+
}
|
|
163
|
+
const pixelWindow = getPixelWindow(clippedBoundingBox, metadata);
|
|
164
|
+
if (pixelWindow.columnStart === pixelWindow.columnStop ||
|
|
165
|
+
pixelWindow.rowStart === pixelWindow.rowStop) {
|
|
166
|
+
return createEmptyRaster(metadata, clippedBoundingBox);
|
|
167
|
+
}
|
|
168
|
+
const selection = normalizeSelection(parameters.selection, metadata.selectionDimensions);
|
|
169
|
+
const zarritaSelection = metadata.dimensions.map((dimensionName, dimensionIndex) => {
|
|
170
|
+
if (dimensionIndex === xDimensionIndex) {
|
|
171
|
+
return zarrita.slice(pixelWindow.columnStart, pixelWindow.columnStop);
|
|
172
|
+
}
|
|
173
|
+
if (dimensionIndex === yDimensionIndex) {
|
|
174
|
+
return zarrita.slice(pixelWindow.rowStart, pixelWindow.rowStop);
|
|
175
|
+
}
|
|
176
|
+
return selection[dimensionName];
|
|
177
|
+
});
|
|
178
|
+
const chunk = await getCachedZarrSelection(array, getZarrSelectionKey(zarritaSelection), () => zarrita.get(array, zarritaSelection), parameters.signal);
|
|
179
|
+
if (!chunk || typeof chunk !== 'object' || !('data' in chunk) || !('shape' in chunk)) {
|
|
180
|
+
throw new Error('Failed to read GeoZarr raster selection.');
|
|
181
|
+
}
|
|
182
|
+
const width = pixelWindow.columnStop - pixelWindow.columnStart;
|
|
183
|
+
const height = pixelWindow.rowStop - pixelWindow.rowStart;
|
|
184
|
+
const typedData = cloneZarrSelection(chunk).data;
|
|
185
|
+
const data = xDimensionIndex < yDimensionIndex ? transposeRaster(typedData, width, height) : typedData;
|
|
186
|
+
const outputBoundingBox = getPixelWindowBoundingBox(pixelWindow, metadata.transform);
|
|
187
|
+
return {
|
|
188
|
+
data,
|
|
189
|
+
width,
|
|
190
|
+
height,
|
|
191
|
+
bandCount: 1,
|
|
192
|
+
dtype: metadata.dtype,
|
|
193
|
+
interleaved: false,
|
|
194
|
+
noData: metadata.noData,
|
|
195
|
+
boundingBox: outputBoundingBox,
|
|
196
|
+
crs: metadata.crs,
|
|
197
|
+
metadata: {
|
|
198
|
+
...metadata.metadata,
|
|
199
|
+
array: metadata.array,
|
|
200
|
+
dimensions: metadata.dimensions,
|
|
201
|
+
selection,
|
|
202
|
+
pixelWindow
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
/** Returns the shared initialization request for this source. */
|
|
207
|
+
async getInitializationPromise(signal) {
|
|
208
|
+
if (!this.initializationPromise) {
|
|
209
|
+
this.initializationPromise = this.initialize(signal);
|
|
210
|
+
}
|
|
211
|
+
const initializationPromise = this.initializationPromise;
|
|
212
|
+
try {
|
|
213
|
+
return await initializationPromise;
|
|
214
|
+
}
|
|
215
|
+
catch (error) {
|
|
216
|
+
if (this.initializationPromise === initializationPromise) {
|
|
217
|
+
this.initializationPromise = null;
|
|
218
|
+
}
|
|
219
|
+
throw error;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
/** Opens the selected data array and resolves GeoZarr or CF/xarray geospatial metadata. */
|
|
223
|
+
async initialize(signal, metadataOnly = false, validatedGroup) {
|
|
224
|
+
const group = validatedGroup || await this.openGroup(signal);
|
|
225
|
+
const options = this.options.geozarr;
|
|
226
|
+
const arrayPath = options?.array;
|
|
227
|
+
if (!arrayPath) {
|
|
228
|
+
throw new Error('GeoZarrRasterSource requires a geozarr.array data-variable path.');
|
|
229
|
+
}
|
|
230
|
+
const array = await zarrita.open(group.resolve(arrayPath), { kind: 'array', signal });
|
|
231
|
+
const groupAttributes = normalizeAttributes(group.attrs);
|
|
232
|
+
const arrayAttributes = normalizeAttributes(array.attrs);
|
|
233
|
+
const layout = getSpatialMultiscaleLevels(groupAttributes, arrayPath);
|
|
234
|
+
const level = layout.find(candidate => candidate.array === arrayPath);
|
|
235
|
+
const levelGroupAttributes = level?.group
|
|
236
|
+
? normalizeAttributes((await zarrita.open(group.resolve(level.group), { kind: 'group', signal })).attrs)
|
|
237
|
+
: {};
|
|
238
|
+
const attributes = {
|
|
239
|
+
...groupAttributes,
|
|
240
|
+
...levelGroupAttributes,
|
|
241
|
+
...level?.attributes,
|
|
242
|
+
...arrayAttributes
|
|
243
|
+
};
|
|
244
|
+
const coordinateReferenceSystemKeys = ['proj:code', 'proj:wkt2', 'proj:projjson', 'proj:epsg'];
|
|
245
|
+
const coordinateReferenceSystemAttributes = [
|
|
246
|
+
arrayAttributes, level?.attributes, levelGroupAttributes
|
|
247
|
+
].find(candidate => candidate &&
|
|
248
|
+
coordinateReferenceSystemKeys.some(key => Object.hasOwn(candidate, key)));
|
|
249
|
+
if (coordinateReferenceSystemAttributes) {
|
|
250
|
+
for (const key of coordinateReferenceSystemKeys) {
|
|
251
|
+
delete attributes[key];
|
|
252
|
+
if (Object.hasOwn(coordinateReferenceSystemAttributes, key)) {
|
|
253
|
+
attributes[key] = coordinateReferenceSystemAttributes[key];
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (level) {
|
|
258
|
+
// A root affine does not establish the geometry of each differently sampled child.
|
|
259
|
+
attributes['spatial:transform'] =
|
|
260
|
+
arrayAttributes['spatial:transform'] ??
|
|
261
|
+
level.attributes['spatial:transform'] ??
|
|
262
|
+
levelGroupAttributes['spatial:transform'];
|
|
263
|
+
}
|
|
264
|
+
const dimensions = getDimensionNames(array, attributes);
|
|
265
|
+
const spatialDimensions = getSpatialDimensions(dimensions, attributes, options?.spatialDimensions);
|
|
266
|
+
const yDimensionIndex = dimensions.indexOf(spatialDimensions[0]);
|
|
267
|
+
const xDimensionIndex = dimensions.indexOf(spatialDimensions[1]);
|
|
268
|
+
const width = array.shape[xDimensionIndex];
|
|
269
|
+
const height = array.shape[yDimensionIndex];
|
|
270
|
+
const coordinateTransform = options?.transform ||
|
|
271
|
+
normalizeAffineTransform(attributes['spatial:transform']) ||
|
|
272
|
+
(!metadataOnly ? await loadCFCoordinateTransform(group, spatialDimensions, signal) : null);
|
|
273
|
+
if (!coordinateTransform) {
|
|
274
|
+
throw new Error('GeoZarrRasterSource requires spatial:transform metadata, explicit geozarr.transform, or regular 1D CF coordinate arrays.');
|
|
275
|
+
}
|
|
276
|
+
const declaredRegistration = normalizeRegistration(attributes['spatial:registration']);
|
|
277
|
+
const transform = declaredRegistration === 'node'
|
|
278
|
+
? convertNodeTransformToPixelTransform(coordinateTransform)
|
|
279
|
+
: coordinateTransform;
|
|
280
|
+
const declaredBoundingBox = normalizeBoundingBox(attributes['spatial:bbox']);
|
|
281
|
+
const boundingBox = declaredRegistration === 'pixel' && declaredBoundingBox
|
|
282
|
+
? toRasterBoundingBox(declaredBoundingBox)
|
|
283
|
+
: getTransformBoundingBox(transform, width, height);
|
|
284
|
+
const coordinateReferenceSystem = options?.coordinateReferenceSystem ||
|
|
285
|
+
normalizeDeclaredCoordinateReferenceSystem(attributes) ||
|
|
286
|
+
await loadCFCoordinateReferenceSystem(group, attributes, spatialDimensions, signal);
|
|
287
|
+
const defaultSelection = options?.defaultSelection || {};
|
|
288
|
+
const selectionDimensions = dimensions
|
|
289
|
+
.map((dimensionName, dimensionIndex) => ({
|
|
290
|
+
name: dimensionName,
|
|
291
|
+
size: array.shape[dimensionIndex],
|
|
292
|
+
defaultIndex: defaultSelection[dimensionName] ?? 0
|
|
293
|
+
}))
|
|
294
|
+
.filter(dimension => !spatialDimensions.includes(dimension.name));
|
|
295
|
+
normalizeSelection(undefined, selectionDimensions);
|
|
296
|
+
const dtype = normalizeDtype(array.dtype);
|
|
297
|
+
const noData = normalizeNumber(attributes._FillValue) ?? normalizeNumber(attributes.missing_value);
|
|
298
|
+
const metadata = {
|
|
299
|
+
name: normalizeString(attributes.long_name) || normalizeString(attributes.title) || arrayPath,
|
|
300
|
+
title: normalizeString(attributes.title) || normalizeString(groupAttributes.title),
|
|
301
|
+
abstract: normalizeString(attributes.description) || normalizeString(groupAttributes.description),
|
|
302
|
+
keywords: normalizeStringArray(groupAttributes.keywords),
|
|
303
|
+
attributions: this.options.core?.attributions || [],
|
|
304
|
+
crs: coordinateReferenceSystem,
|
|
305
|
+
boundingBox,
|
|
306
|
+
width,
|
|
307
|
+
height,
|
|
308
|
+
bandCount: 1,
|
|
309
|
+
dtype,
|
|
310
|
+
tileSize: {
|
|
311
|
+
width: array.chunks[xDimensionIndex],
|
|
312
|
+
height: array.chunks[yDimensionIndex]
|
|
313
|
+
},
|
|
314
|
+
noData,
|
|
315
|
+
metadata: {
|
|
316
|
+
groupAttributes,
|
|
317
|
+
arrayAttributes,
|
|
318
|
+
sourceTransform: coordinateTransform,
|
|
319
|
+
sourceBoundingBox: declaredBoundingBox
|
|
320
|
+
},
|
|
321
|
+
array: arrayPath,
|
|
322
|
+
dimensions,
|
|
323
|
+
spatialDimensions,
|
|
324
|
+
selectionDimensions,
|
|
325
|
+
transform,
|
|
326
|
+
registration: declaredRegistration
|
|
327
|
+
};
|
|
328
|
+
return { group, array, metadata, xDimensionIndex, yDimensionIndex };
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
/** Converts arbitrary Zarrita attributes into a plain object. */
|
|
332
|
+
function normalizeAttributes(attributes) {
|
|
333
|
+
return attributes && typeof attributes === 'object' ? attributes : {};
|
|
334
|
+
}
|
|
335
|
+
/** Resolves array dimension names from Zarr v3 or xarray's Zarr v2 convention. */
|
|
336
|
+
function getDimensionNames(array, attributes) {
|
|
337
|
+
const dimensions = array.dimensionNames?.length
|
|
338
|
+
? [...array.dimensionNames]
|
|
339
|
+
: normalizeStringArray(attributes._ARRAY_DIMENSIONS);
|
|
340
|
+
if (!dimensions ||
|
|
341
|
+
dimensions.length !== array.shape.length ||
|
|
342
|
+
new Set(dimensions).size !== dimensions.length) {
|
|
343
|
+
throw new Error('GeoZarrRasterSource requires unique Zarr dimension_names or xarray _ARRAY_DIMENSIONS matching the array shape.');
|
|
344
|
+
}
|
|
345
|
+
return dimensions;
|
|
346
|
+
}
|
|
347
|
+
/** Resolves logical y/x dimensions from options, GeoZarr metadata, or common CF names. */
|
|
348
|
+
function getSpatialDimensions(dimensions, attributes, optionDimensions) {
|
|
349
|
+
const declaredDimensions = optionDimensions || normalizeSpatialDimensions(attributes['spatial:dimensions']);
|
|
350
|
+
const spatialDimensions = declaredDimensions || [
|
|
351
|
+
findDimension(dimensions, ['y', 'lat', 'latitude']),
|
|
352
|
+
findDimension(dimensions, ['x', 'lon', 'longitude'])
|
|
353
|
+
];
|
|
354
|
+
if (spatialDimensions.length !== 2 ||
|
|
355
|
+
!spatialDimensions[0] ||
|
|
356
|
+
!spatialDimensions[1] ||
|
|
357
|
+
!dimensions.includes(spatialDimensions[0]) ||
|
|
358
|
+
!dimensions.includes(spatialDimensions[1]) ||
|
|
359
|
+
spatialDimensions[0] === spatialDimensions[1]) {
|
|
360
|
+
throw new Error('GeoZarrRasterSource could not resolve two spatial dimensions. Set spatial:dimensions or geozarr.spatialDimensions.');
|
|
361
|
+
}
|
|
362
|
+
return spatialDimensions;
|
|
363
|
+
}
|
|
364
|
+
/** Finds the first dimension whose case-insensitive name matches a candidate. */
|
|
365
|
+
function findDimension(dimensions, candidates) {
|
|
366
|
+
return dimensions.find(dimension => candidates.includes(dimension.toLowerCase())) || '';
|
|
367
|
+
}
|
|
368
|
+
/** Normalizes a two-string logical spatial-dimension tuple. */
|
|
369
|
+
function normalizeSpatialDimensions(value) {
|
|
370
|
+
return Array.isArray(value) && value.length === 2 && value.every(item => typeof item === 'string')
|
|
371
|
+
? [value[0], value[1]]
|
|
372
|
+
: null;
|
|
373
|
+
}
|
|
374
|
+
/** Normalizes a six-number affine transform tuple. */
|
|
375
|
+
function normalizeAffineTransform(value) {
|
|
376
|
+
return Array.isArray(value) && value.length === 6 && value.every(Number.isFinite)
|
|
377
|
+
? value
|
|
378
|
+
: null;
|
|
379
|
+
}
|
|
380
|
+
/** Normalizes a flat four-number bounding box. */
|
|
381
|
+
function normalizeBoundingBox(value) {
|
|
382
|
+
return Array.isArray(value) && value.length === 4 && value.every(Number.isFinite)
|
|
383
|
+
? value
|
|
384
|
+
: null;
|
|
385
|
+
}
|
|
386
|
+
/** Converts a flat `[minimumX, minimumY, maximumX, maximumY]` box to RasterSource form. */
|
|
387
|
+
function toRasterBoundingBox(boundingBox) {
|
|
388
|
+
return [
|
|
389
|
+
[boundingBox[0], boundingBox[1]],
|
|
390
|
+
[boundingBox[2], boundingBox[3]]
|
|
391
|
+
];
|
|
392
|
+
}
|
|
393
|
+
/** Normalizes GeoZarr grid registration. */
|
|
394
|
+
function normalizeRegistration(value) {
|
|
395
|
+
return value === 'node' ? 'node' : 'pixel';
|
|
396
|
+
}
|
|
397
|
+
/** Converts a node-centered GeoZarr transform to the pixel-corner transform used by RasterSource. */
|
|
398
|
+
function convertNodeTransformToPixelTransform(transform) {
|
|
399
|
+
const [scaleX, rotationX, translateX, rotationY, scaleY, translateY] = transform;
|
|
400
|
+
return [
|
|
401
|
+
scaleX,
|
|
402
|
+
rotationX,
|
|
403
|
+
translateX - (scaleX + rotationX) / 2,
|
|
404
|
+
rotationY,
|
|
405
|
+
scaleY,
|
|
406
|
+
translateY - (rotationY + scaleY) / 2
|
|
407
|
+
];
|
|
408
|
+
}
|
|
409
|
+
/** Loads regular one-dimensional CF coordinate variables and derives a pixel-corner transform. */
|
|
410
|
+
async function loadCFCoordinateTransform(group, spatialDimensions, signal) {
|
|
411
|
+
try {
|
|
412
|
+
const [yCoordinates, xCoordinates] = await Promise.all([
|
|
413
|
+
loadCoordinateValues(group, spatialDimensions[0], signal),
|
|
414
|
+
loadCoordinateValues(group, spatialDimensions[1], signal)
|
|
415
|
+
]);
|
|
416
|
+
const scaleX = getRegularCoordinateSpacing(xCoordinates, spatialDimensions[1]);
|
|
417
|
+
const scaleY = getRegularCoordinateSpacing(yCoordinates, spatialDimensions[0]);
|
|
418
|
+
return [scaleX, 0, xCoordinates[0] - scaleX / 2, 0, scaleY, yCoordinates[0] - scaleY / 2];
|
|
419
|
+
}
|
|
420
|
+
catch {
|
|
421
|
+
return null;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
/** Loads all values from one one-dimensional coordinate variable. */
|
|
425
|
+
async function loadCoordinateValues(group, coordinateName, signal) {
|
|
426
|
+
const coordinateArray = await zarrita.open(group.resolve(coordinateName), {
|
|
427
|
+
kind: 'array',
|
|
428
|
+
signal
|
|
429
|
+
});
|
|
430
|
+
if (coordinateArray.shape.length !== 1 || coordinateArray.shape[0] < 2) {
|
|
431
|
+
throw new Error(`Coordinate ${coordinateName} must be a one-dimensional array with at least two values.`);
|
|
432
|
+
}
|
|
433
|
+
const coordinateChunk = await zarrita.get(coordinateArray, [null], { signal });
|
|
434
|
+
if (!coordinateChunk || typeof coordinateChunk !== 'object' || !('data' in coordinateChunk)) {
|
|
435
|
+
throw new Error(`Could not read coordinate ${coordinateName}.`);
|
|
436
|
+
}
|
|
437
|
+
return Array.from(coordinateChunk.data);
|
|
438
|
+
}
|
|
439
|
+
/** Validates that coordinate values are finite and regularly spaced. */
|
|
440
|
+
function getRegularCoordinateSpacing(coordinates, coordinateName) {
|
|
441
|
+
const spacing = (coordinates.at(-1) - coordinates[0]) / (coordinates.length - 1);
|
|
442
|
+
if (!Number.isFinite(spacing) || spacing === 0) {
|
|
443
|
+
throw new Error(`Coordinate ${coordinateName} does not have finite non-zero spacing.`);
|
|
444
|
+
}
|
|
445
|
+
const tolerance = Math.max(Math.abs(spacing) * 1e-6, Number.EPSILON);
|
|
446
|
+
for (let index = 0; index < coordinates.length; index++) {
|
|
447
|
+
const expectedCoordinate = coordinates[0] + spacing * index;
|
|
448
|
+
if (!Number.isFinite(coordinates[index]) ||
|
|
449
|
+
Math.abs(coordinates[index] - expectedCoordinate) > tolerance) {
|
|
450
|
+
throw new Error(`Coordinate ${coordinateName} is not regularly spaced.`);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
return spacing;
|
|
454
|
+
}
|
|
455
|
+
/** Resolves CRS metadata from a CF grid-mapping variable or longitude/latitude dimensions. */
|
|
456
|
+
async function loadCFCoordinateReferenceSystem(group, attributes, spatialDimensions, signal) {
|
|
457
|
+
const gridMapping = normalizeString(attributes.grid_mapping);
|
|
458
|
+
if (gridMapping) {
|
|
459
|
+
const gridMappingName = gridMapping.trim().split(/[\s:]/, 1)[0];
|
|
460
|
+
try {
|
|
461
|
+
const gridMappingArray = await zarrita.open(group.resolve(gridMappingName), {
|
|
462
|
+
kind: 'array',
|
|
463
|
+
signal
|
|
464
|
+
});
|
|
465
|
+
const gridMappingAttributes = normalizeAttributes(gridMappingArray.attrs);
|
|
466
|
+
const coordinateReferenceSystem = normalizeString(gridMappingAttributes.crs_wkt) ||
|
|
467
|
+
normalizeString(gridMappingAttributes.spatial_ref) ||
|
|
468
|
+
normalizeString(gridMappingAttributes['proj:wkt2']) ||
|
|
469
|
+
normalizeString(gridMappingAttributes['proj:code']);
|
|
470
|
+
if (coordinateReferenceSystem) {
|
|
471
|
+
return coordinateReferenceSystem;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
catch {
|
|
475
|
+
// Fall through to the common longitude/latitude CRS below.
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
const normalizedDimensions = spatialDimensions.map(dimension => dimension.toLowerCase());
|
|
479
|
+
if (['lat', 'latitude'].includes(normalizedDimensions[0]) &&
|
|
480
|
+
['lon', 'longitude'].includes(normalizedDimensions[1])) {
|
|
481
|
+
return 'EPSG:4326';
|
|
482
|
+
}
|
|
483
|
+
return undefined;
|
|
484
|
+
}
|
|
485
|
+
/** Computes a bounding box from all four transformed raster corners. */
|
|
486
|
+
function getTransformBoundingBox(transform, width, height) {
|
|
487
|
+
const corners = [
|
|
488
|
+
transformPosition(transform, 0, 0),
|
|
489
|
+
transformPosition(transform, width, 0),
|
|
490
|
+
transformPosition(transform, 0, height),
|
|
491
|
+
transformPosition(transform, width, height)
|
|
492
|
+
];
|
|
493
|
+
const xCoordinates = corners.map(position => position[0]);
|
|
494
|
+
const yCoordinates = corners.map(position => position[1]);
|
|
495
|
+
return [
|
|
496
|
+
[Math.min(...xCoordinates), Math.min(...yCoordinates)],
|
|
497
|
+
[Math.max(...xCoordinates), Math.max(...yCoordinates)]
|
|
498
|
+
];
|
|
499
|
+
}
|
|
500
|
+
/** Applies an affine transform to one column/row position. */
|
|
501
|
+
function transformPosition(transform, column, row) {
|
|
502
|
+
const [scaleX, rotationX, translateX, rotationY, scaleY, translateY] = transform;
|
|
503
|
+
return [
|
|
504
|
+
scaleX * column + rotationX * row + translateX,
|
|
505
|
+
rotationY * column + scaleY * row + translateY
|
|
506
|
+
];
|
|
507
|
+
}
|
|
508
|
+
/** Intersects two source-coordinate bounding boxes. */
|
|
509
|
+
function intersectBoundingBoxes(leftBoundingBox, rightBoundingBox) {
|
|
510
|
+
if (!rightBoundingBox) {
|
|
511
|
+
return null;
|
|
512
|
+
}
|
|
513
|
+
const minimumX = Math.max(leftBoundingBox[0][0], rightBoundingBox[0][0]);
|
|
514
|
+
const minimumY = Math.max(leftBoundingBox[0][1], rightBoundingBox[0][1]);
|
|
515
|
+
const maximumX = Math.min(leftBoundingBox[1][0], rightBoundingBox[1][0]);
|
|
516
|
+
const maximumY = Math.min(leftBoundingBox[1][1], rightBoundingBox[1][1]);
|
|
517
|
+
return maximumX > minimumX && maximumY > minimumY
|
|
518
|
+
? [
|
|
519
|
+
[minimumX, minimumY],
|
|
520
|
+
[maximumX, maximumY]
|
|
521
|
+
]
|
|
522
|
+
: null;
|
|
523
|
+
}
|
|
524
|
+
/** Converts an axis-aligned source bounding box into a clipped integer pixel window. */
|
|
525
|
+
function getPixelWindow(boundingBox, metadata) {
|
|
526
|
+
const [scaleX, rotationX, translateX, rotationY, scaleY, translateY] = metadata.transform;
|
|
527
|
+
if (rotationX !== 0 || rotationY !== 0) {
|
|
528
|
+
throw new Error('GeoZarrRasterSource does not yet support rotated affine window reads.');
|
|
529
|
+
}
|
|
530
|
+
const columnCoordinates = [
|
|
531
|
+
(boundingBox[0][0] - translateX) / scaleX,
|
|
532
|
+
(boundingBox[1][0] - translateX) / scaleX
|
|
533
|
+
];
|
|
534
|
+
const rowCoordinates = [
|
|
535
|
+
(boundingBox[0][1] - translateY) / scaleY,
|
|
536
|
+
(boundingBox[1][1] - translateY) / scaleY
|
|
537
|
+
];
|
|
538
|
+
return {
|
|
539
|
+
columnStart: clamp(Math.floor(Math.min(...columnCoordinates)), 0, metadata.width),
|
|
540
|
+
columnStop: clamp(Math.ceil(Math.max(...columnCoordinates)), 0, metadata.width),
|
|
541
|
+
rowStart: clamp(Math.floor(Math.min(...rowCoordinates)), 0, metadata.height),
|
|
542
|
+
rowStop: clamp(Math.ceil(Math.max(...rowCoordinates)), 0, metadata.height)
|
|
543
|
+
};
|
|
544
|
+
}
|
|
545
|
+
/** Computes exact source bounds for an integer pixel window. */
|
|
546
|
+
function getPixelWindowBoundingBox(pixelWindow, transform) {
|
|
547
|
+
return getTransformBoundingBox([
|
|
548
|
+
transform[0],
|
|
549
|
+
transform[1],
|
|
550
|
+
transformPosition(transform, pixelWindow.columnStart, pixelWindow.rowStart)[0],
|
|
551
|
+
transform[3],
|
|
552
|
+
transform[4],
|
|
553
|
+
transformPosition(transform, pixelWindow.columnStart, pixelWindow.rowStart)[1]
|
|
554
|
+
], pixelWindow.columnStop - pixelWindow.columnStart, pixelWindow.rowStop - pixelWindow.rowStart);
|
|
555
|
+
}
|
|
556
|
+
/** Normalizes and validates indices for all non-spatial dimensions. */
|
|
557
|
+
function normalizeSelection(requestedSelection, dimensions) {
|
|
558
|
+
const knownDimensions = new Set(dimensions.map(dimension => dimension.name));
|
|
559
|
+
for (const requestedDimension of Object.keys(requestedSelection || {})) {
|
|
560
|
+
if (!knownDimensions.has(requestedDimension)) {
|
|
561
|
+
throw new Error(`Unknown GeoZarr selection dimension ${requestedDimension}.`);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
return Object.fromEntries(dimensions.map(dimension => {
|
|
565
|
+
const index = requestedSelection?.[dimension.name] ?? dimension.defaultIndex;
|
|
566
|
+
if (!Number.isInteger(index) || index < 0 || index >= dimension.size) {
|
|
567
|
+
throw new Error(`GeoZarr ${dimension.name} index ${index} is out of bounds for dimension size ${dimension.size}.`);
|
|
568
|
+
}
|
|
569
|
+
return [dimension.name, index];
|
|
570
|
+
}));
|
|
571
|
+
}
|
|
572
|
+
/** Transposes a C-order 2D raster whose physical dimensions are `[x, y]`. */
|
|
573
|
+
function transposeRaster(source, width, height) {
|
|
574
|
+
const TypedArrayConstructor = source.constructor;
|
|
575
|
+
const target = new TypedArrayConstructor(source.length);
|
|
576
|
+
for (let row = 0; row < height; row++) {
|
|
577
|
+
for (let column = 0; column < width; column++) {
|
|
578
|
+
target[row * width + column] = source[column * height + row];
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
return target;
|
|
582
|
+
}
|
|
583
|
+
/** Creates a zero-sized raster for a viewport outside the source extent. */
|
|
584
|
+
function createEmptyRaster(metadata, boundingBox) {
|
|
585
|
+
const TypedArrayConstructor = getTypedArrayConstructor(metadata.dtype);
|
|
586
|
+
return {
|
|
587
|
+
data: new TypedArrayConstructor(0),
|
|
588
|
+
width: 0,
|
|
589
|
+
height: 0,
|
|
590
|
+
bandCount: 1,
|
|
591
|
+
dtype: metadata.dtype,
|
|
592
|
+
interleaved: false,
|
|
593
|
+
noData: metadata.noData,
|
|
594
|
+
boundingBox,
|
|
595
|
+
crs: metadata.crs,
|
|
596
|
+
metadata: metadata.metadata
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
/** Converts a Zarrita dtype into the raster-source dtype union. */
|
|
600
|
+
function normalizeDtype(dtype) {
|
|
601
|
+
switch (dtype) {
|
|
602
|
+
case 'uint8':
|
|
603
|
+
case 'uint16':
|
|
604
|
+
case 'uint32':
|
|
605
|
+
case 'int8':
|
|
606
|
+
case 'int16':
|
|
607
|
+
case 'int32':
|
|
608
|
+
case 'float32':
|
|
609
|
+
case 'float64':
|
|
610
|
+
return dtype;
|
|
611
|
+
default:
|
|
612
|
+
throw new Error(`GeoZarr dtype ${dtype} is not currently supported.`);
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
/** Returns the typed-array constructor represented by a raster dtype. */
|
|
616
|
+
function getTypedArrayConstructor(dtype) {
|
|
617
|
+
switch (dtype) {
|
|
618
|
+
case 'uint8':
|
|
619
|
+
return Uint8Array;
|
|
620
|
+
case 'uint16':
|
|
621
|
+
return Uint16Array;
|
|
622
|
+
case 'uint32':
|
|
623
|
+
return Uint32Array;
|
|
624
|
+
case 'int8':
|
|
625
|
+
return Int8Array;
|
|
626
|
+
case 'int16':
|
|
627
|
+
return Int16Array;
|
|
628
|
+
case 'int32':
|
|
629
|
+
return Int32Array;
|
|
630
|
+
case 'float32':
|
|
631
|
+
return Float32Array;
|
|
632
|
+
case 'float64':
|
|
633
|
+
return Float64Array;
|
|
634
|
+
default:
|
|
635
|
+
throw new Error(`Unsupported GeoZarr dtype ${dtype}.`);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
/** Returns a finite number or `undefined`. */
|
|
639
|
+
function normalizeNumber(value) {
|
|
640
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : undefined;
|
|
641
|
+
}
|
|
642
|
+
/** Returns a non-empty string or `undefined`. */
|
|
643
|
+
function normalizeString(value) {
|
|
644
|
+
return typeof value === 'string' && value.length ? value : undefined;
|
|
645
|
+
}
|
|
646
|
+
/** Returns a string array or `undefined`. */
|
|
647
|
+
function normalizeStringArray(value) {
|
|
648
|
+
return Array.isArray(value) && value.every(item => typeof item === 'string')
|
|
649
|
+
? [...value]
|
|
650
|
+
: undefined;
|
|
651
|
+
}
|
|
652
|
+
/** Clamps a number to an inclusive range. */
|
|
653
|
+
function clamp(value, minimum, maximum) {
|
|
654
|
+
return Math.min(maximum, Math.max(minimum, value));
|
|
655
|
+
}
|
|
656
|
+
/** Describes spatial storage chunks only when the affine is axis aligned and rightward. */
|
|
657
|
+
function getSpatialChunkMatrix(id, metadata) {
|
|
658
|
+
if (!metadata)
|
|
659
|
+
return { id };
|
|
660
|
+
const { transform, tileSize, width, height } = metadata;
|
|
661
|
+
const [scaleX, rotationX, translateX, rotationY, scaleY, translateY] = transform;
|
|
662
|
+
if (rotationX !== 0 || rotationY !== 0 || scaleX <= 0 || scaleY === 0 ||
|
|
663
|
+
!transform.every(Number.isFinite) || !tileSize || !width || !height) {
|
|
664
|
+
return { id };
|
|
665
|
+
}
|
|
666
|
+
return {
|
|
667
|
+
id,
|
|
668
|
+
resolution: scaleX,
|
|
669
|
+
resolutionY: Math.abs(scaleY),
|
|
670
|
+
origin: [translateX, translateY],
|
|
671
|
+
cornerOfOrigin: scaleY < 0 ? 'topLeft' : 'bottomLeft',
|
|
672
|
+
tileSize: [tileSize.width, tileSize.height],
|
|
673
|
+
matrixSize: [Math.ceil(width / tileSize.width), Math.ceil(height / tileSize.height)]
|
|
674
|
+
};
|
|
675
|
+
}
|
|
676
|
+
/** Retains declared CRS definitions without resolving them, and normalizes numeric EPSG identifiers. */
|
|
677
|
+
function normalizeDeclaredCoordinateReferenceSystem(attributes) {
|
|
678
|
+
const wellKnownText = normalizeString(attributes['proj:wkt2']);
|
|
679
|
+
if (wellKnownText)
|
|
680
|
+
return wellKnownText;
|
|
681
|
+
const code = normalizeString(attributes['proj:code']);
|
|
682
|
+
if (code)
|
|
683
|
+
return code;
|
|
684
|
+
const epsg = attributes['proj:epsg'];
|
|
685
|
+
if (typeof epsg === 'number' && Number.isSafeInteger(epsg) && epsg > 0)
|
|
686
|
+
return `EPSG:${epsg}`;
|
|
687
|
+
const projection = attributes['proj:projjson'];
|
|
688
|
+
return projection && typeof projection === 'object' && !Array.isArray(projection)
|
|
689
|
+
? projection
|
|
690
|
+
: undefined;
|
|
691
|
+
}
|
|
692
|
+
//# sourceMappingURL=geo-zarr-source-loader.js.map
|