@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.
Files changed (175) hide show
  1. package/README.md +21 -2
  2. package/dist/index.cjs +4313 -0
  3. package/dist/index.cjs.map +6 -0
  4. package/dist/index.d.ts +11 -0
  5. package/dist/index.d.ts.map +1 -0
  6. package/dist/index.js +8 -0
  7. package/dist/index.js.map +1 -0
  8. package/dist/netcdf/bundled.cjs +1394 -0
  9. package/dist/netcdf/bundled.d.ts +2 -0
  10. package/dist/netcdf/bundled.d.ts.map +1 -0
  11. package/dist/netcdf/bundled.js +5 -0
  12. package/dist/netcdf/bundled.js.map +1 -0
  13. package/dist/netcdf/index.cjs +1394 -0
  14. package/dist/netcdf/index.cjs.map +6 -0
  15. package/dist/netcdf/index.d.ts +7 -0
  16. package/dist/netcdf/index.d.ts.map +1 -0
  17. package/dist/netcdf/index.js +8 -0
  18. package/dist/netcdf/index.js.map +1 -0
  19. package/dist/netcdf/iobuffer/iobuffer.d.ts +253 -0
  20. package/dist/netcdf/iobuffer/iobuffer.d.ts.map +1 -0
  21. package/dist/netcdf/iobuffer/iobuffer.js +450 -0
  22. package/dist/netcdf/iobuffer/iobuffer.js.map +1 -0
  23. package/dist/netcdf/netcdf-format.cjs +39 -0
  24. package/dist/netcdf/netcdf-format.d.ts +11 -0
  25. package/dist/netcdf/netcdf-format.d.ts.map +1 -0
  26. package/dist/netcdf/netcdf-format.js +14 -0
  27. package/dist/netcdf/netcdf-format.js.map +1 -0
  28. package/dist/netcdf/netcdf-loader-with-parser.cjs +995 -0
  29. package/dist/netcdf/netcdf-loader-with-parser.d.ts +60 -0
  30. package/dist/netcdf/netcdf-loader-with-parser.d.ts.map +1 -0
  31. package/dist/netcdf/netcdf-loader-with-parser.js +35 -0
  32. package/dist/netcdf/netcdf-loader-with-parser.js.map +1 -0
  33. package/dist/netcdf/netcdf-loader.cjs +84 -0
  34. package/dist/netcdf/netcdf-loader.d.ts +81 -0
  35. package/dist/netcdf/netcdf-loader.d.ts.map +1 -0
  36. package/dist/netcdf/netcdf-loader.js +41 -0
  37. package/dist/netcdf/netcdf-loader.js.map +1 -0
  38. package/dist/netcdf/netcdf-source-loader-types.cjs +78 -0
  39. package/dist/netcdf/netcdf-source-loader-types.d.ts +28 -0
  40. package/dist/netcdf/netcdf-source-loader-types.d.ts.map +1 -0
  41. package/dist/netcdf/netcdf-source-loader-types.js +32 -0
  42. package/dist/netcdf/netcdf-source-loader-types.js.map +1 -0
  43. package/dist/netcdf/netcdf-source-loader.cjs +1319 -0
  44. package/dist/netcdf/netcdf-source-loader.d.ts +59 -0
  45. package/dist/netcdf/netcdf-source-loader.d.ts.map +1 -0
  46. package/dist/netcdf/netcdf-source-loader.js +421 -0
  47. package/dist/netcdf/netcdf-source-loader.js.map +1 -0
  48. package/dist/netcdf/netcdfjs/netcdf-reader.d.ts +68 -0
  49. package/dist/netcdf/netcdfjs/netcdf-reader.d.ts.map +1 -0
  50. package/dist/netcdf/netcdfjs/netcdf-reader.js +172 -0
  51. package/dist/netcdf/netcdfjs/netcdf-reader.js.map +1 -0
  52. package/dist/netcdf/netcdfjs/netcdf-types.d.ts +70 -0
  53. package/dist/netcdf/netcdfjs/netcdf-types.d.ts.map +1 -0
  54. package/dist/netcdf/netcdfjs/netcdf-types.js +3 -0
  55. package/dist/netcdf/netcdfjs/netcdf-types.js.map +1 -0
  56. package/dist/netcdf/netcdfjs/read-data.d.ts +19 -0
  57. package/dist/netcdf/netcdfjs/read-data.d.ts.map +1 -0
  58. package/dist/netcdf/netcdfjs/read-data.js +51 -0
  59. package/dist/netcdf/netcdfjs/read-data.js.map +1 -0
  60. package/dist/netcdf/netcdfjs/read-header.d.ts +16 -0
  61. package/dist/netcdf/netcdfjs/read-header.d.ts.map +1 -0
  62. package/dist/netcdf/netcdfjs/read-header.js +227 -0
  63. package/dist/netcdf/netcdfjs/read-header.js.map +1 -0
  64. package/dist/netcdf/netcdfjs/read-type.d.ts +36 -0
  65. package/dist/netcdf/netcdfjs/read-type.d.ts.map +1 -0
  66. package/dist/netcdf/netcdfjs/read-type.js +135 -0
  67. package/dist/netcdf/netcdfjs/read-type.js.map +1 -0
  68. package/dist/netcdf/unbundled.cjs +1394 -0
  69. package/dist/netcdf/unbundled.d.ts +2 -0
  70. package/dist/netcdf/unbundled.d.ts.map +1 -0
  71. package/dist/netcdf/unbundled.js +5 -0
  72. package/dist/netcdf/unbundled.js.map +1 -0
  73. package/dist/netcdf.dev.js +3153 -0
  74. package/dist/netcdf.min.js +12 -0
  75. package/dist/zarr/bundled.d.ts +2 -0
  76. package/dist/zarr/bundled.d.ts.map +1 -0
  77. package/dist/zarr/bundled.js +5 -0
  78. package/dist/zarr/bundled.js.map +1 -0
  79. package/dist/zarr/geo-zarr-source-loader.d.ts +121 -0
  80. package/dist/zarr/geo-zarr-source-loader.d.ts.map +1 -0
  81. package/dist/zarr/geo-zarr-source-loader.js +692 -0
  82. package/dist/zarr/geo-zarr-source-loader.js.map +1 -0
  83. package/dist/zarr/index.cjs +14747 -0
  84. package/dist/zarr/index.cjs.map +6 -0
  85. package/dist/zarr/index.d.ts +11 -0
  86. package/dist/zarr/index.d.ts.map +1 -0
  87. package/dist/zarr/index.js +10 -0
  88. package/dist/zarr/index.js.map +1 -0
  89. package/dist/zarr/lib/consolidated-zarr.d.ts +31 -0
  90. package/dist/zarr/lib/consolidated-zarr.d.ts.map +1 -0
  91. package/dist/zarr/lib/consolidated-zarr.js +113 -0
  92. package/dist/zarr/lib/consolidated-zarr.js.map +1 -0
  93. package/dist/zarr/lib/load-zarr.d.ts +11 -0
  94. package/dist/zarr/lib/load-zarr.d.ts.map +1 -0
  95. package/dist/zarr/lib/load-zarr.js +22 -0
  96. package/dist/zarr/lib/load-zarr.js.map +1 -0
  97. package/dist/zarr/lib/spatial-multiscales.d.ts +14 -0
  98. package/dist/zarr/lib/spatial-multiscales.d.ts.map +1 -0
  99. package/dist/zarr/lib/spatial-multiscales.js +34 -0
  100. package/dist/zarr/lib/spatial-multiscales.js.map +1 -0
  101. package/dist/zarr/lib/utils.d.ts +28 -0
  102. package/dist/zarr/lib/utils.d.ts.map +1 -0
  103. package/dist/zarr/lib/utils.js +137 -0
  104. package/dist/zarr/lib/utils.js.map +1 -0
  105. package/dist/zarr/lib/zarr-data-cache.d.ts +13 -0
  106. package/dist/zarr/lib/zarr-data-cache.d.ts.map +1 -0
  107. package/dist/zarr/lib/zarr-data-cache.js +70 -0
  108. package/dist/zarr/lib/zarr-data-cache.js.map +1 -0
  109. package/dist/zarr/lib/zarr-pixel-source.d.ts +41 -0
  110. package/dist/zarr/lib/zarr-pixel-source.d.ts.map +1 -0
  111. package/dist/zarr/lib/zarr-pixel-source.js +97 -0
  112. package/dist/zarr/lib/zarr-pixel-source.js.map +1 -0
  113. package/dist/zarr/lib/zarrita-pixel-source.d.ts +40 -0
  114. package/dist/zarr/lib/zarrita-pixel-source.d.ts.map +1 -0
  115. package/dist/zarr/lib/zarrita-pixel-source.js +96 -0
  116. package/dist/zarr/lib/zarrita-pixel-source.js.map +1 -0
  117. package/dist/zarr/ome-zarr-source-loader.d.ts +195 -0
  118. package/dist/zarr/ome-zarr-source-loader.d.ts.map +1 -0
  119. package/dist/zarr/ome-zarr-source-loader.js +523 -0
  120. package/dist/zarr/ome-zarr-source-loader.js.map +1 -0
  121. package/dist/zarr/spatial-data-source.d.ts +95 -0
  122. package/dist/zarr/spatial-data-source.d.ts.map +1 -0
  123. package/dist/zarr/spatial-data-source.js +242 -0
  124. package/dist/zarr/spatial-data-source.js.map +1 -0
  125. package/dist/zarr/types.d.ts +105 -0
  126. package/dist/zarr/types.d.ts.map +1 -0
  127. package/dist/zarr/types.js +5 -0
  128. package/dist/zarr/types.js.map +1 -0
  129. package/dist/zarr/unbundled.d.ts +2 -0
  130. package/dist/zarr/unbundled.d.ts.map +1 -0
  131. package/dist/zarr/unbundled.js +5 -0
  132. package/dist/zarr/unbundled.js.map +1 -0
  133. package/dist/zarr/zarr-array-source.d.ts +102 -0
  134. package/dist/zarr/zarr-array-source.d.ts.map +1 -0
  135. package/dist/zarr/zarr-array-source.js +132 -0
  136. package/dist/zarr/zarr-array-source.js.map +1 -0
  137. package/dist/zarr/zarr-loader-types.d.ts +236 -0
  138. package/dist/zarr/zarr-loader-types.d.ts.map +1 -0
  139. package/dist/zarr/zarr-loader-types.js +128 -0
  140. package/dist/zarr/zarr-loader-types.js.map +1 -0
  141. package/dist/zarr.dev.js +16574 -0
  142. package/dist/zarr.min.js +79 -0
  143. package/package.json +62 -4
  144. package/src/index.ts +52 -0
  145. package/src/netcdf/bundled.ts +5 -0
  146. package/src/netcdf/index.ts +14 -0
  147. package/src/netcdf/iobuffer/iobuffer.ts +510 -0
  148. package/src/netcdf/netcdf-format.ts +16 -0
  149. package/src/netcdf/netcdf-loader-with-parser.ts +54 -0
  150. package/src/netcdf/netcdf-loader.ts +62 -0
  151. package/src/netcdf/netcdf-source-loader-types.ts +45 -0
  152. package/src/netcdf/netcdf-source-loader.ts +526 -0
  153. package/src/netcdf/netcdfjs/LICENSE +24 -0
  154. package/src/netcdf/netcdfjs/netcdf-reader.ts +196 -0
  155. package/src/netcdf/netcdfjs/netcdf-types.ts +75 -0
  156. package/src/netcdf/netcdfjs/read-data.ts +71 -0
  157. package/src/netcdf/netcdfjs/read-header.ts +277 -0
  158. package/src/netcdf/netcdfjs/read-type.ts +147 -0
  159. package/src/netcdf/unbundled.ts +5 -0
  160. package/src/zarr/bundled.ts +5 -0
  161. package/src/zarr/geo-zarr-source-loader.ts +998 -0
  162. package/src/zarr/index.ts +60 -0
  163. package/src/zarr/lib/consolidated-zarr.ts +190 -0
  164. package/src/zarr/lib/load-zarr.ts +31 -0
  165. package/src/zarr/lib/spatial-multiscales.ts +47 -0
  166. package/src/zarr/lib/utils.ts +162 -0
  167. package/src/zarr/lib/zarr-data-cache.ts +91 -0
  168. package/src/zarr/lib/zarr-pixel-source.ts +134 -0
  169. package/src/zarr/lib/zarrita-pixel-source.ts +141 -0
  170. package/src/zarr/ome-zarr-source-loader.ts +837 -0
  171. package/src/zarr/spatial-data-source.ts +370 -0
  172. package/src/zarr/types.ts +129 -0
  173. package/src/zarr/unbundled.ts +5 -0
  174. package/src/zarr/zarr-array-source.ts +225 -0
  175. package/src/zarr/zarr-loader-types.ts +166 -0
@@ -0,0 +1,60 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ export {loadZarr} from './lib/load-zarr';
6
+ export {default as ZarrPixelSource} from './lib/zarr-pixel-source';
7
+ export {
8
+ OMEZarrSourceLoaderWithParser as OMEZarrSourceLoader,
9
+ OMEZarrImageSource,
10
+ loadZarrConsolidatedMetadata
11
+ } from './ome-zarr-source-loader';
12
+ export {
13
+ GeoZarrSourceLoaderWithParser as GeoZarrSourceLoader,
14
+ GeoZarrRasterSource
15
+ } from './geo-zarr-source-loader';
16
+ export {
17
+ ZarrArraySourceLoaderWithParser as ZarrArraySourceLoader,
18
+ ZarrArraySource
19
+ } from './zarr-array-source';
20
+ export {
21
+ SpatialDataSourceLoaderWithParser as SpatialDataSourceLoader,
22
+ SpatialDataSource
23
+ } from './spatial-data-source';
24
+ export type {
25
+ GetZarrArrayParameters,
26
+ ZarrArrayData,
27
+ ZarrArraySelection,
28
+ ZarrArraySlice,
29
+ ZarrArraySourceLoaderOptions,
30
+ ZarrArraySourceMetadata
31
+ } from './zarr-array-source';
32
+ export type {
33
+ SpatialDataElementFormat,
34
+ SpatialDataElementKind,
35
+ SpatialDataElementMetadata,
36
+ SpatialDataSourceLoaderOptions,
37
+ SpatialDataSourceMetadata,
38
+ SpatialDataZarrElementSource
39
+ } from './spatial-data-source';
40
+ export type {
41
+ GetOMEZarrParameters,
42
+ LoadConsolidatedMetadataOptions,
43
+ LoadZarrConsolidatedMetadataOptions,
44
+ OMEZarrChannelMetadata,
45
+ OMEZarrLevelMetadata,
46
+ OMEZarrSourceLoaderMetadata,
47
+ ZarrConsolidatedFormat,
48
+ ZarrConsolidatedMetadata,
49
+ ZarrMetadataPath,
50
+ ZarrSourceLoader,
51
+ ZarrSourceLoaderOptions
52
+ } from './ome-zarr-source-loader';
53
+ export type {
54
+ GeoZarrLevelMetadata,
55
+ GeoZarrAffineTransform,
56
+ GeoZarrSelectionDimension,
57
+ GeoZarrSourceLoaderOptions,
58
+ GeoZarrSourceMetadata,
59
+ GetGeoZarrParameters
60
+ } from './geo-zarr-source-loader';
@@ -0,0 +1,190 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ /** Consolidated metadata files supported by Zarr v2 and v3 stores. */
6
+ export type ZarrMetadataPath = 'auto' | '.zmetadata' | 'zmetadata' | 'zarr.json';
7
+
8
+ /** Zarr format generation represented by normalized consolidated metadata. */
9
+ export type ZarrConsolidatedFormat = 'v2' | 'v3';
10
+
11
+ /** Normalized consolidated metadata and the top-level nodes it describes. */
12
+ export type ZarrConsolidatedMetadata = {
13
+ /** Zarr format generation used by the consolidated metadata document. */
14
+ format: ZarrConsolidatedFormat;
15
+ /** Metadata file that was loaded. */
16
+ metadataPath: '.zmetadata' | 'zmetadata' | 'zarr.json';
17
+ /** Format-specific consolidated metadata entries keyed by Zarr path. */
18
+ metadata: Record<string, unknown>;
19
+ /** Attributes declared on the root group. */
20
+ rootAttributes: Record<string, unknown>;
21
+ /** Top-level group paths. */
22
+ topLevelGroups: string[];
23
+ /** Top-level array paths. */
24
+ topLevelArrays: string[];
25
+ };
26
+
27
+ type V2ConsolidatedMetadata = {
28
+ metadata?: Record<string, unknown>;
29
+ };
30
+
31
+ type V3ConsolidatedMetadata = {
32
+ attributes?: Record<string, unknown>;
33
+ consolidated_metadata?: {
34
+ metadata?: Record<string, unknown>;
35
+ };
36
+ };
37
+
38
+ /** Options for loading consolidated Zarr metadata. */
39
+ export type LoadConsolidatedMetadataOptions = {
40
+ /** Metadata file to load, or `auto` to probe v3 and v2 names in order. */
41
+ metadataPath?: ZarrMetadataPath;
42
+ /** Abort signal forwarded to metadata requests. */
43
+ signal?: AbortSignal;
44
+ };
45
+
46
+ /**
47
+ * Loads and normalizes consolidated Zarr metadata across v2 and v3 layouts.
48
+ */
49
+ export async function loadConsolidatedMetadata(
50
+ url: string,
51
+ fetcher: (url: string, options?: RequestInit) => Promise<Response>,
52
+ options: LoadConsolidatedMetadataOptions = {}
53
+ ): Promise<ZarrConsolidatedMetadata> {
54
+ const metadataPaths = getMetadataPaths(options.metadataPath);
55
+ let urlEnd = url.length;
56
+ while (urlEnd > 0 && url[urlEnd - 1] === '/') {
57
+ urlEnd--;
58
+ }
59
+ const normalizedUrl = url.slice(0, urlEnd);
60
+ const errors: string[] = [];
61
+
62
+ for (const metadataPath of metadataPaths) {
63
+ const metadataUrl = `${normalizedUrl}/${metadataPath}`;
64
+ const response = await fetcher(metadataUrl, {signal: options.signal});
65
+
66
+ if (response.status === 404) {
67
+ errors.push(`${metadataPath}: 404`);
68
+ continue;
69
+ }
70
+
71
+ if (!response.ok) {
72
+ errors.push(`${metadataPath}: ${response.status} ${response.statusText}`);
73
+ continue;
74
+ }
75
+
76
+ try {
77
+ const json = (await response.json()) as V2ConsolidatedMetadata | V3ConsolidatedMetadata;
78
+ return normalizeConsolidatedMetadata(json, metadataPath);
79
+ } catch (error) {
80
+ if (
81
+ options.signal?.aborted ||
82
+ (options.metadataPath !== undefined && options.metadataPath !== 'auto')
83
+ ) {
84
+ throw error;
85
+ }
86
+ const message = error instanceof Error ? error.message : String(error);
87
+ errors.push(`${metadataPath}: ${message}`);
88
+ }
89
+ }
90
+
91
+ throw new Error(
92
+ `Could not load consolidated Zarr metadata for ${url}. Tried ${metadataPaths.join(', ')}. ${errors.join('; ')}`
93
+ );
94
+ }
95
+
96
+ /** Returns metadata filenames in the order they should be probed. */
97
+ function getMetadataPaths(
98
+ metadataPath: ZarrMetadataPath = 'auto'
99
+ ): Array<'.zmetadata' | 'zmetadata' | 'zarr.json'> {
100
+ if (metadataPath === 'auto') {
101
+ return ['zarr.json', '.zmetadata', 'zmetadata'];
102
+ }
103
+
104
+ return [metadataPath];
105
+ }
106
+
107
+ /** Normalizes one v2 or v3 consolidated metadata document. */
108
+ function normalizeConsolidatedMetadata(
109
+ metadata: V2ConsolidatedMetadata | V3ConsolidatedMetadata,
110
+ metadataPath: '.zmetadata' | 'zmetadata' | 'zarr.json'
111
+ ): ZarrConsolidatedMetadata {
112
+ if (metadataPath === 'zarr.json') {
113
+ const normalizedMetadata = (metadata as V3ConsolidatedMetadata).consolidated_metadata?.metadata;
114
+ if (!normalizedMetadata || typeof normalizedMetadata !== 'object') {
115
+ throw new Error('Invalid zarr.json: missing consolidated_metadata.metadata object.');
116
+ }
117
+
118
+ return {
119
+ format: 'v3',
120
+ metadataPath,
121
+ metadata: normalizedMetadata,
122
+ rootAttributes: {...((metadata as V3ConsolidatedMetadata).attributes || {})},
123
+ topLevelGroups: extractV3TopLevelNodes(normalizedMetadata, 'group'),
124
+ topLevelArrays: extractV3TopLevelNodes(normalizedMetadata, 'array')
125
+ };
126
+ }
127
+
128
+ const normalizedMetadata = (metadata as V2ConsolidatedMetadata).metadata;
129
+ if (!normalizedMetadata || typeof normalizedMetadata !== 'object') {
130
+ throw new Error(`Invalid ${metadataPath}: missing metadata object.`);
131
+ }
132
+
133
+ return {
134
+ format: 'v2',
135
+ metadataPath,
136
+ metadata: normalizedMetadata,
137
+ rootAttributes: getV2RootAttributes(normalizedMetadata),
138
+ topLevelGroups: extractV2TopLevelNodes(Object.keys(normalizedMetadata), 'zgroup'),
139
+ topLevelArrays: extractV2TopLevelNodes(Object.keys(normalizedMetadata), 'zarray')
140
+ };
141
+ }
142
+
143
+ /** Extracts root attributes from a consolidated Zarr v2 metadata map. */
144
+ function getV2RootAttributes(metadata: Record<string, unknown>): Record<string, unknown> {
145
+ const attributes = metadata['.zattrs'];
146
+ return attributes && typeof attributes === 'object'
147
+ ? {...(attributes as Record<string, unknown>)}
148
+ : {};
149
+ }
150
+
151
+ /** Extracts top-level v2 nodes represented by `.zgroup` or `.zarray` keys. */
152
+ function extractV2TopLevelNodes(paths: string[], nodeType: 'zgroup' | 'zarray'): string[] {
153
+ const suffix = `.${nodeType}`;
154
+ const nodePaths = paths
155
+ .filter(path => path === suffix || path.endsWith(`/${suffix}`))
156
+ .map(path => path.slice(0, -suffix.length).replace(/\/+$/, ''));
157
+ return extractDirectChildren(nodePaths);
158
+ }
159
+
160
+ /** Extracts top-level v3 nodes with the requested `node_type`. */
161
+ function extractV3TopLevelNodes(
162
+ metadata: Record<string, unknown>,
163
+ nodeType: 'group' | 'array'
164
+ ): string[] {
165
+ const nodePaths = Object.entries(metadata)
166
+ .filter(([, value]) =>
167
+ Boolean(
168
+ value &&
169
+ typeof value === 'object' &&
170
+ (value as {node_type?: unknown}).node_type === nodeType
171
+ )
172
+ )
173
+ .map(([path]) => path);
174
+ return extractDirectChildren(nodePaths);
175
+ }
176
+
177
+ /** Returns normalized paths that are direct children of the store root. */
178
+ function extractDirectChildren(paths: string[]): string[] {
179
+ const directChildren = new Set<string>();
180
+
181
+ for (const path of paths) {
182
+ const normalizedPath = path.replace(/^\/+|\/+$/g, '');
183
+ if (!normalizedPath || normalizedPath.includes('/')) {
184
+ continue;
185
+ }
186
+ directChildren.add(normalizedPath);
187
+ }
188
+
189
+ return [...directChildren].sort();
190
+ }
@@ -0,0 +1,31 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import type {ZarrStore} from '../types';
6
+ import {loadMultiscales, guessTileSize, guessLabels, normalizeStore, validLabels} from './utils';
7
+ import ZarrPixelSource from './zarr-pixel-source';
8
+
9
+ interface ZarrOptions {
10
+ labels?: string[];
11
+ }
12
+
13
+ export async function loadZarr(root: string | ZarrStore, options: ZarrOptions = {}) {
14
+ const store = normalizeStore(root);
15
+ const {data, rootAttrs} = await loadMultiscales(store);
16
+ const tileSize = guessTileSize(data[0]);
17
+
18
+ // If no labels are provided, inspect the root attributes for the store.
19
+ // For now, we only infer labels for OME-Zarr.
20
+ const labels = options.labels ?? guessLabels(rootAttrs);
21
+
22
+ if (!validLabels(labels, data[0].shape)) {
23
+ throw new Error('Invalid labels for Zarr array dimensions.');
24
+ }
25
+
26
+ const pyramid = data.map(arr => new ZarrPixelSource(arr, labels, tileSize));
27
+ return {
28
+ data: pyramid,
29
+ metadata: rootAttrs
30
+ };
31
+ }
@@ -0,0 +1,47 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ /** Resolved child array within a generic Zarr multiscales layout. */
6
+ export type SpatialMultiscaleLevel = {
7
+ /** Original asset identifier. */
8
+ id: string;
9
+ /** Selected variable's array path at this level. */
10
+ array: string;
11
+ /** Optional level group containing that variable. */
12
+ group?: string;
13
+ /** Absolute spatial metadata advertised on the layout entry. */
14
+ attributes: Record<string, unknown>;
15
+ };
16
+
17
+ /** Resolves direct-array or common-variable group layouts without following external paths. */
18
+ export function getSpatialMultiscaleLevels(
19
+ attributes: Record<string, unknown>,
20
+ array: string
21
+ ): SpatialMultiscaleLevel[] {
22
+ const multiscales = attributes?.multiscales as {layout?: unknown} | undefined;
23
+ const layout = multiscales?.layout;
24
+ if (!Array.isArray(layout) || !layout.length) return [];
25
+ const entries = layout as Array<Record<string, unknown>>;
26
+ if (entries.some(entry => !entry || !isChildPath(entry.asset))) {
27
+ throw new Error('Spatial multiscales assets must be relative child paths.');
28
+ }
29
+ const assets = entries.map(entry => entry.asset as string);
30
+ if (new Set(assets).size !== assets.length) throw new Error('Duplicate spatial multiscales asset.');
31
+ const matches = assets.filter(asset => array === asset || array.startsWith(`${asset}/`));
32
+ if (matches.length !== 1) return [];
33
+ const suffix = array.slice(matches[0].length);
34
+ return entries.map((entry, index) => ({
35
+ id: assets[index],
36
+ array: `${assets[index]}${suffix}`,
37
+ ...(suffix ? {group: assets[index]} : {}),
38
+ attributes: entry
39
+ }));
40
+ }
41
+
42
+ /** Accepts literal relative child paths, rejecting URL, encoded, and traversal syntax. */
43
+ function isChildPath(value: unknown): value is string {
44
+ return typeof value === 'string' && value.length > 0 &&
45
+ !/[\\%:?#\u0000-\u0020]/.test(value) &&
46
+ value.split('/').every(segment => segment !== '' && segment !== '.' && segment !== '..');
47
+ }
@@ -0,0 +1,162 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import type {ZarrArray} from 'zarr';
6
+ import type {ZarrStore} from '../types';
7
+ import type {PixelSource, RootAttrs, Labels} from '../types';
8
+
9
+ import {openGroup, HTTPStore} from 'zarr';
10
+
11
+ export function normalizeStore(source: string | ZarrStore): ZarrStore {
12
+ if (typeof source === 'string') {
13
+ return new HTTPStore(source);
14
+ }
15
+ return source;
16
+ }
17
+
18
+ export async function loadMultiscales(store: ZarrStore, path = '') {
19
+ const grp = await openGroup(store, path);
20
+ const rootAttrs = (await grp.attrs.asObject()) as RootAttrs;
21
+ const multiscales =
22
+ ('multiscales' in rootAttrs ? rootAttrs.multiscales : undefined) ||
23
+ ('ome' in rootAttrs ? rootAttrs.ome?.multiscales : undefined);
24
+
25
+ // Root of Zarr store must implement multiscales extension.
26
+ // https://github.com/zarr-developers/zarr-specs/issues/50
27
+ if (!Array.isArray(multiscales)) {
28
+ throw new Error('Cannot find Zarr multiscales metadata.');
29
+ }
30
+
31
+ const {datasets} = multiscales[0];
32
+ const promises = datasets.map(d => grp.getItem(d.path)) as Promise<ZarrArray>[];
33
+
34
+ return {
35
+ data: await Promise.all(promises),
36
+ rootAttrs
37
+ };
38
+ }
39
+
40
+ /*
41
+ * Creates an ES6 map of 'label' -> index
42
+ * > const labels = ['a', 'b', 'c', 'd'];
43
+ * > const dims = getDims(labels);
44
+ * > dims('a') === 0;
45
+ * > dims('b') === 1;
46
+ * > dims('c') === 2;
47
+ * > dims('hi!'); // throws
48
+ */
49
+ export function getDims<S extends string>(labels: S[]) {
50
+ const lookup = new Map(labels.map((name, i) => [name, i]));
51
+ if (lookup.size !== labels.length) {
52
+ throw Error('Labels must be unique, found duplicated label.');
53
+ }
54
+ return (name: S) => {
55
+ const index = lookup.get(name);
56
+ if (index === undefined) {
57
+ throw Error('Invalid dimension.');
58
+ }
59
+ return index;
60
+ };
61
+ }
62
+
63
+ function prevPowerOf2(x: number) {
64
+ return 2 ** Math.floor(Math.log2(x));
65
+ }
66
+
67
+ /*
68
+ * Helper method to determine whether pixel data is interleaved or not.
69
+ * > isInterleaved([1, 24, 24]) === false;
70
+ * > isInterleaved([1, 24, 24, 3]) === true;
71
+ */
72
+ export function isInterleaved(shape: number[]) {
73
+ const lastDimSize = shape[shape.length - 1];
74
+ return lastDimSize === 3 || lastDimSize === 4;
75
+ }
76
+
77
+ export function guessTileSize(
78
+ arr: {shape: number[]; chunks: number[]},
79
+ labels?: readonly string[]
80
+ ) {
81
+ const interleaved = labels ? labels[labels.length - 1] === '_c' : isInterleaved(arr.shape);
82
+ const [yChunk, xChunk] = arr.chunks.slice(interleaved ? -3 : -2);
83
+ const size = Math.min(yChunk, xChunk);
84
+ // deck.gl requirement for power-of-two tile size.
85
+ return prevPowerOf2(size);
86
+ }
87
+
88
+ export function guessLabels(rootAttrs: RootAttrs) {
89
+ if ('omero' in rootAttrs && rootAttrs.omero) {
90
+ return ['t', 'c', 'z', 'y', 'x'] as Labels<['t', 'c', 'z']>;
91
+ }
92
+ if ('ome' in rootAttrs && rootAttrs.ome?.omero) {
93
+ return ['t', 'c', 'z', 'y', 'x'] as Labels<['t', 'c', 'z']>;
94
+ }
95
+ throw new Error(
96
+ 'Could not infer dimension labels for Zarr source. Must provide dimension labels.'
97
+ );
98
+ }
99
+
100
+ /*
101
+ * The 'indexer' for a Zarr-based source translates
102
+ * a 'selection' to an array of indices that align to
103
+ * the labeled dimensions.
104
+ *
105
+ * > const labels = ['a', 'b', 'y', 'x'];
106
+ * > const indexer = getIndexer(labels);
107
+ * > console.log(indexer({ a: 10, b: 20 }));
108
+ * > // [10, 20, 0, 0]
109
+ */
110
+ export function getIndexer<T extends string>(labels: T[]) {
111
+ const size = labels.length;
112
+ const dims = getDims(labels);
113
+ return (sel: {[K in T]: number} | number[]) => {
114
+ if (Array.isArray(sel)) {
115
+ return [...sel];
116
+ }
117
+ const selection: number[] = Array(size).fill(0);
118
+ for (const [key, value] of Object.entries(sel)) {
119
+ selection[dims(key as T)] = value as number;
120
+ }
121
+ return selection;
122
+ };
123
+ }
124
+
125
+ export function getImageSize<T extends string[]>(
126
+ source: PixelSource<T>
127
+ ): {height: number; width: number} {
128
+ const interleaved = isInterleaved(source.shape);
129
+ // 2D image data in Zarr are represented as (..., rows, columns [, bands])
130
+ // If an image is interleaved (RGB/A), we need to ignore the last dimension (bands)
131
+ // to get the height and weight of the image.
132
+ const [height, width] = source.shape.slice(interleaved ? -3 : -2);
133
+ return {height, width};
134
+ }
135
+
136
+ /**
137
+ * Preserves (double) slashes earlier in the path, so this works better
138
+ * for URLs. From https://stackoverflow.com/a/46427607
139
+ * @param args parts of a path or URL to join.
140
+ */
141
+ export function joinUrlParts(...args: string[]) {
142
+ return args
143
+ .map((part, i) => {
144
+ if (i === 0) return part.trim().replace(/[/]*$/g, '');
145
+ return part.trim().replace(/(^[/]*|[/]*$)/g, '');
146
+ })
147
+ .filter(x => x.length)
148
+ .join('/');
149
+ }
150
+
151
+ export function validLabels(labels: string[], shape: number[]): labels is Labels<string[]> {
152
+ if (labels.length !== shape.length) {
153
+ throw new Error('Labels do not match Zarr array shape.');
154
+ }
155
+ const n = labels.length;
156
+ if (labels[n - 1] === '_c') {
157
+ // last three dimensions are [row, column, bands]
158
+ return labels[n - 3] === 'y' && labels[n - 2] === 'x' && labels[n - 1] === '_c';
159
+ }
160
+ // last two dimensions are [row, column]
161
+ return labels[n - 2] === 'y' && labels[n - 1] === 'x';
162
+ }
@@ -0,0 +1,91 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import type {TypedArray} from '@loaders.gl/core/loader-utils';
6
+
7
+ type CachedData = {
8
+ data: TypedArray;
9
+ shape: number[];
10
+ };
11
+
12
+ const MAX_CACHED_SELECTIONS = 32;
13
+ const cacheByArray = new WeakMap<object, Map<string, Promise<CachedData>>>();
14
+
15
+ /** Reads and caches a decoded Zarr selection, sharing in-flight requests. */
16
+ export function getCachedZarrSelection<T extends CachedData>(
17
+ array: object,
18
+ key: string,
19
+ read: () => Promise<T>,
20
+ signal?: AbortSignal
21
+ ): Promise<T> {
22
+ let cache = cacheByArray.get(array);
23
+ if (!cache) {
24
+ cache = new Map();
25
+ cacheByArray.set(array, cache);
26
+ }
27
+
28
+ const cached = cache.get(key);
29
+ if (cached) {
30
+ cache.delete(key);
31
+ cache.set(key, cached);
32
+ return awaitWithAbort(cached as Promise<T>, signal);
33
+ }
34
+
35
+ const request = read().catch(error => {
36
+ if (cache?.get(key) === request) {
37
+ cache.delete(key);
38
+ }
39
+ throw error;
40
+ });
41
+ cache.set(key, request);
42
+ while (cache.size > MAX_CACHED_SELECTIONS) {
43
+ const oldestKey = cache.keys().next().value;
44
+ if (oldestKey === undefined) break;
45
+ cache.delete(oldestKey);
46
+ }
47
+ return awaitWithAbort(request, signal);
48
+ }
49
+
50
+ /** Returns an independent result so callers cannot mutate the cached buffer. */
51
+ export function cloneZarrSelection<T extends CachedData>(selection: T): T {
52
+ return {
53
+ ...selection,
54
+ data: selection.data.slice() as TypedArray,
55
+ shape: [...selection.shape]
56
+ } as T;
57
+ }
58
+
59
+ /** Applies cancellation to one cache waiter without aborting the shared read. */
60
+ function awaitWithAbort<T>(promise: Promise<T>, signal?: AbortSignal): Promise<T> {
61
+ if (!signal) return promise;
62
+ if (signal.aborted)
63
+ return Promise.reject(signal.reason || new Error('The operation was aborted.'));
64
+ return new Promise<T>((resolve, reject) => {
65
+ const abort = () => {
66
+ cleanup();
67
+ reject(signal.reason || new Error('The operation was aborted.'));
68
+ };
69
+ const cleanup = () => signal.removeEventListener('abort', abort);
70
+ signal.addEventListener('abort', abort, {once: true});
71
+ promise.then(
72
+ value => {
73
+ cleanup();
74
+ resolve(value);
75
+ },
76
+ error => {
77
+ cleanup();
78
+ reject(error);
79
+ }
80
+ );
81
+ });
82
+ }
83
+
84
+ /** Creates a stable cache key for a Zarrita selection. */
85
+ export function getZarrSelectionKey(selection: readonly unknown[]): string {
86
+ return JSON.stringify(selection, (_key, value) =>
87
+ value && typeof value === 'object' && 'start' in value && 'stop' in value && 'step' in value
88
+ ? {start: value.start ?? null, stop: value.stop ?? null, step: value.step ?? null}
89
+ : value
90
+ );
91
+ }