@loaders.gl/geotiff 4.1.0-alpha.3 → 4.1.0-alpha.5

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 (54) hide show
  1. package/dist/dist.dev.js +1257 -1073
  2. package/dist/geotiff-loader.d.ts +21 -0
  3. package/dist/geotiff-loader.d.ts.map +1 -0
  4. package/dist/geotiff-loader.js +59 -0
  5. package/dist/geotiff-loader.js.map +1 -0
  6. package/dist/index.cjs +46 -40
  7. package/dist/index.d.ts +2 -1
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +2 -1
  10. package/dist/index.js.map +1 -1
  11. package/dist/lib/load-geotiff.d.ts +1 -1
  12. package/dist/lib/load-geotiff.d.ts.map +1 -1
  13. package/dist/lib/load-geotiff.js +1 -5
  14. package/dist/lib/load-geotiff.js.map +1 -1
  15. package/dist/lib/ome/load-ome-tiff.d.ts +1 -1
  16. package/dist/lib/ome/load-ome-tiff.d.ts.map +1 -1
  17. package/dist/lib/ome/load-ome-tiff.js +1 -1
  18. package/dist/lib/ome/load-ome-tiff.js.map +1 -1
  19. package/dist/lib/ome/ome-indexers.d.ts +1 -0
  20. package/dist/lib/ome/ome-indexers.d.ts.map +1 -1
  21. package/dist/lib/ome/ome-indexers.js.map +1 -1
  22. package/dist/lib/tiff-pixel-source.d.ts +1 -2
  23. package/dist/lib/tiff-pixel-source.d.ts.map +1 -1
  24. package/dist/lib/tiff-pixel-source.js +1 -2
  25. package/dist/lib/tiff-pixel-source.js.map +1 -1
  26. package/{src/lib/utils/Pool.ts → dist/lib/utils/Pool.ts.disabled} +1 -1
  27. package/dist/loaders.d.ts +13 -0
  28. package/dist/loaders.d.ts.map +1 -0
  29. package/dist/loaders.js +32 -0
  30. package/dist/loaders.js.map +1 -0
  31. package/package.json +3 -3
  32. package/src/geotiff-loader.ts +115 -0
  33. package/src/index.ts +3 -1
  34. package/src/lib/load-geotiff.ts +8 -8
  35. package/src/lib/ome/load-ome-tiff.ts +1 -1
  36. package/src/lib/ome/ome-indexers.ts +5 -2
  37. package/src/lib/tiff-pixel-source.ts +5 -5
  38. package/src/lib/utils/Pool.ts.disabled +99 -0
  39. package/src/lib/utils/proxies.ts.disabled +96 -0
  40. package/src/loaders.ts +68 -0
  41. package/dist/lib/utils/Pool.d.ts +0 -29
  42. package/dist/lib/utils/Pool.d.ts.map +0 -1
  43. package/dist/lib/utils/Pool.js +0 -60
  44. package/dist/lib/utils/Pool.js.map +0 -1
  45. package/dist/lib/utils/proxies.d.ts +0 -6
  46. package/dist/lib/utils/proxies.d.ts.map +0 -1
  47. package/dist/lib/utils/proxies.js +0 -54
  48. package/dist/lib/utils/proxies.js.map +0 -1
  49. package/dist/typings/geotiff.d.ts +0 -60
  50. package/dist/typings/geotiff.d.ts.map +0 -1
  51. package/dist/typings/geotiff.js +0 -2
  52. package/dist/typings/geotiff.js.map +0 -1
  53. package/src/typings/geotiff.ts +0 -76
  54. /package/{src/lib/utils/proxies.ts → dist/lib/utils/proxies.ts.disabled} +0 -0
@@ -0,0 +1,115 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import type {TypedArray} from '@loaders.gl/loader-utils';
6
+ import type {LoaderWithParser, LoaderOptions} from '@loaders.gl/loader-utils';
7
+ import {fromArrayBuffer} from 'geotiff';
8
+
9
+ // __VERSION__ is injected by babel-plugin-version-inline
10
+ // @ts-ignore TS2304: Cannot find name '__VERSION__'.
11
+ const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
12
+
13
+ // Return type definition for GeoTIFF loader
14
+ type GeoTIFFData = {
15
+ crs?: string;
16
+ bounds: number[];
17
+ metadata: Record<string, unknown>;
18
+
19
+ width: number;
20
+ height: number;
21
+ data: TypedArray;
22
+ };
23
+
24
+ /** GeoTIFF load options */
25
+ export type GeoTIFFLoaderOptions = LoaderOptions & {
26
+ geotiff?: {
27
+ enableAlpha: boolean;
28
+ };
29
+ };
30
+
31
+ /** GeoTIFF loader */
32
+ export const GeoTIFFLoader: LoaderWithParser<GeoTIFFData, never, GeoTIFFLoaderOptions> = {
33
+ id: 'geotiff',
34
+ name: 'GeoTIFF',
35
+ module: 'geotiff',
36
+ version: VERSION,
37
+ options: {
38
+ enableAlpha: true
39
+ },
40
+ mimeTypes: ['image/tiff', 'image/geotiff'],
41
+ extensions: ['geotiff', 'tiff', 'geotif', 'tif'],
42
+ parse: parseGeoTIFF
43
+ };
44
+
45
+ export function isTiff(arrayBuffer: ArrayBuffer): boolean {
46
+ const dataView = new DataView(arrayBuffer);
47
+ // Byte offset
48
+ const endianness = dataView.getUint16(0);
49
+ let littleEndian: boolean;
50
+ switch (endianness) {
51
+ case 0x4949:
52
+ littleEndian = true;
53
+ break;
54
+ case 0x4d4d:
55
+ littleEndian = false;
56
+ break;
57
+ default:
58
+ return false;
59
+ // throw new Error(`invalid byte order: 0x${endianness.toString(16)}`);
60
+ }
61
+
62
+ // Magic number
63
+ if (dataView.getUint16(2, littleEndian) !== 42) {
64
+ return false;
65
+ }
66
+
67
+ return true;
68
+ }
69
+
70
+ /**
71
+ * Loads a GeoTIFF file containing a RGB image.
72
+ */
73
+ async function parseGeoTIFF(
74
+ data: ArrayBuffer,
75
+ options?: GeoTIFFLoaderOptions
76
+ ): Promise<GeoTIFFData> {
77
+ // Load using Geotiff.js
78
+ const tiff = await fromArrayBuffer(data);
79
+
80
+ // Assumes we only have one image inside TIFF
81
+ const image = await tiff.getImage();
82
+
83
+ // Read image and size
84
+ // TODO: Add support for worker pools here.
85
+ // TODO: Add support for more image formats.
86
+ const rgbData = await image.readRGB({
87
+ enableAlpha: options?.geotiff?.enableAlpha
88
+ });
89
+
90
+ const width = image.getWidth();
91
+ const height = image.getHeight();
92
+
93
+ // Create a new ImageData object
94
+ const imageData = new Uint8ClampedArray(rgbData as unknown as Uint8Array);
95
+
96
+ // Get geo data
97
+ const bounds = image.getBoundingBox();
98
+ const metadata = image.getGeoKeys();
99
+
100
+ // ProjectedCSTypeGeoKey is the only key we support for now, we assume it is an EPSG code
101
+ let crs: string | undefined;
102
+ if (metadata?.ProjectedCSTypeGeoKey) {
103
+ crs = `EPSG:${metadata.ProjectedCSTypeGeoKey}`;
104
+ }
105
+
106
+ // Return GeoReferenced image data
107
+ return {
108
+ crs,
109
+ bounds,
110
+ width,
111
+ height,
112
+ data: imageData,
113
+ metadata
114
+ };
115
+ }
package/src/index.ts CHANGED
@@ -2,5 +2,7 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // Copyright (c) vis.gl contributors
4
4
 
5
+ export {GeoTIFFLoader} from './geotiff-loader';
6
+
5
7
  export {loadGeoTiff} from './lib/load-geotiff';
6
- export {default as TiffPixelSource} from './lib/tiff-pixel-source';
8
+ export {TiffPixelSource} from './lib/tiff-pixel-source';
@@ -4,15 +4,15 @@
4
4
 
5
5
  import {fromUrl, fromBlob, GeoTIFF} from 'geotiff';
6
6
 
7
- import {
8
- // createPoolProxy,
9
- createOffsetsProxy,
10
- checkProxies
11
- } from './utils/proxies';
7
+ // import {
8
+ // // createPoolProxy,
9
+ // createOffsetsProxy,
10
+ // checkProxies
11
+ // } from './utils/proxies.ts.disabled';
12
12
  // import Pool from './lib/Pool';
13
13
 
14
14
  import {loadOmeTiff, isOmeTiff} from './ome/load-ome-tiff';
15
- import type TiffPixelSource from './tiff-pixel-source';
15
+ import type {TiffPixelSource} from './tiff-pixel-source';
16
16
 
17
17
  /** Options for initializing a tiff pixel source. */
18
18
  interface GeoTIFFOptions {
@@ -69,14 +69,14 @@ export async function loadGeoTiff(
69
69
  * create a proxy that intercepts calls to `tiff.getImage`
70
70
  * and injects the pre-computed offsets.
71
71
  */
72
- tiff = createOffsetsProxy(tiff, offsets);
72
+ // tiff = createOffsetsProxy(tiff, offsets);
73
73
  }
74
74
 
75
75
  /*
76
76
  * Inspect tiff source for our performance enhancing proxies.
77
77
  * Prints warnings to console if `offsets` or `pool` are missing.
78
78
  */
79
- checkProxies(tiff);
79
+ // checkProxies(tiff);
80
80
 
81
81
  const firstImage = await tiff.getImage(0);
82
82
 
@@ -4,7 +4,7 @@
4
4
 
5
5
  import type {GeoTIFF, GeoTIFFImage} from 'geotiff';
6
6
 
7
- import TiffPixelSource from '../tiff-pixel-source';
7
+ import {TiffPixelSource} from '../tiff-pixel-source';
8
8
  import {getOmeLegacyIndexer, getOmeSubIFDIndexer, OmeTiffIndexer} from './ome-indexers';
9
9
  import {getOmePixelSourceMeta} from './ome-utils';
10
10
  import {fromString} from './omexml';
@@ -2,9 +2,11 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // Copyright (c) vis.gl contributors
4
4
 
5
- import type {GeoTIFFImage, GeoTIFF, ImageFileDirectory} from 'geotiff';
5
+ import type {GeoTIFFImage, GeoTIFF} from 'geotiff';
6
6
  import type {OMEXML} from '../ome/omexml';
7
7
 
8
+ export type ImageFileDirectory = any;
9
+
8
10
  export type OmeTiffSelection = {t: number; c: number; z: number};
9
11
  export type OmeTiffIndexer = (sel: OmeTiffSelection, z: number) => Promise<GeoTIFFImage>;
10
12
 
@@ -82,13 +84,14 @@ export function getOmeSubIFDIndexer(tiff: GeoTIFF, rootMeta: OMEXML): OmeTiffInd
82
84
  const subIfdOffset = SubIFDs[pyramidLevel - 1];
83
85
  ifdCache.set(key, tiff.parseFileDirectoryAt(subIfdOffset));
84
86
  }
85
- const ifd = (await ifdCache.get(key)) as ImageFileDirectory;
87
+ const ifd = await ifdCache.get(key);
86
88
 
87
89
  // Create a new image object manually from IFD
88
90
  // https://github.com/geotiffjs/geotiff.js/blob/8ef472f41b51d18074aece2300b6a8ad91a21ae1/src/geotiff.js#L447-L453
89
91
  return new (baseImage.constructor as any)(
90
92
  ifd.fileDirectory,
91
93
  ifd.geoKeyDirectory,
94
+ // @ts-expect-error
92
95
  tiff.dataView,
93
96
  tiff.littleEndian,
94
97
  tiff.cache,
@@ -2,7 +2,7 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // Copyright (c) vis.gl contributors
4
4
 
5
- import type {GeoTIFFImage, RasterOptions} from 'geotiff';
5
+ import type {GeoTIFFImage} from 'geotiff';
6
6
  import {getImageSize, isInterleaved, SIGNAL_ABORTED} from './utils/tiff-utils';
7
7
 
8
8
  import type {
@@ -17,7 +17,9 @@ import type {
17
17
  TypedArray
18
18
  } from '../types';
19
19
 
20
- class TiffPixelSource<S extends string[]> implements PixelSource<S> {
20
+ type ReadRasterOptions = Record<string, any>;
21
+
22
+ export class TiffPixelSource<S extends string[]> implements PixelSource<S> {
21
23
  public dtype: Dtype;
22
24
  public tileSize: number;
23
25
  public shape: number[];
@@ -57,7 +59,7 @@ class TiffPixelSource<S extends string[]> implements PixelSource<S> {
57
59
  return this._readRasters(image, {window, width, height, signal});
58
60
  }
59
61
 
60
- private async _readRasters(image: GeoTIFFImage, props?: RasterOptions) {
62
+ private async _readRasters(image: GeoTIFFImage, props?: ReadRasterOptions) {
61
63
  const interleave = isInterleaved(this.shape);
62
64
  const raster = await image.readRasters({interleave, ...props});
63
65
 
@@ -99,5 +101,3 @@ class TiffPixelSource<S extends string[]> implements PixelSource<S> {
99
101
  console.error(err); // eslint-disable-line no-console
100
102
  }
101
103
  }
102
-
103
- export default TiffPixelSource;
@@ -0,0 +1,99 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ /** eslint-disable */
6
+ import {FileDirectory} from 'geotiff';
7
+
8
+ // import Worker from 'web-worker:./decoder.worker.ts';
9
+
10
+ // https://developer.mozilla.org/en-US/docs/Web/API/NavigatorConcurrentHardware/hardwareConcurrency
11
+ // We need to give a different way of getting this for safari, so 4 is probably a safe bet
12
+ // for parallel processing in the meantime. More can't really hurt since they'll just block
13
+ // each other and not the UI thread, which is the real benefit.
14
+ const defaultPoolSize = globalThis?.navigator?.hardwareConcurrency ?? 4;
15
+
16
+ /**
17
+ * Pool for workers to decode chunks of the images.
18
+ * This is a line-for-line copy of GeoTIFFs old implementation: https://github.com/geotiffjs/geotiff.js/blob/v1.0.0-beta.8/src/pool.js
19
+ */
20
+ export default class Pool {
21
+ workers: Worker[];
22
+ idleWorkers: Worker[];
23
+ waitQueue: any[];
24
+ decoder: null;
25
+
26
+ /**
27
+ * @constructor
28
+ * @param {Number} size The size of the pool. Defaults to the number of CPUs
29
+ * available. When this parameter is `null` or 0, then the
30
+ * decoding will be done in the main thread.
31
+ */
32
+ constructor(size = defaultPoolSize) {
33
+ this.workers = [];
34
+ this.idleWorkers = [];
35
+ this.waitQueue = [];
36
+ this.decoder = null;
37
+
38
+ // eslint-disable-next-line no-plusplus
39
+ for (let i = 0; i < size; ++i) {
40
+ const w = new Worker('./decoder.worker');
41
+ this.workers.push(w);
42
+ this.idleWorkers.push(w);
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Decode the given block of bytes with the set compression method.
48
+ * @param {ArrayBuffer} buffer the array buffer of bytes to decode.
49
+ * @returns {Promise.<ArrayBuffer>} the decoded result as a `Promise`
50
+ */
51
+ async decode(fileDirectory: FileDirectory, buffer: ArrayBuffer) {
52
+ const currentWorker = await this.waitForWorker();
53
+ return new Promise((resolve, reject) => {
54
+ currentWorker.onmessage = (event) => {
55
+ // this.workers.push(currentWorker);
56
+ // eslint-disable-next-line
57
+ this.finishTask(currentWorker);
58
+ resolve(event.data[0]);
59
+ };
60
+ currentWorker.onerror = (error) => {
61
+ // this.workers.push(currentWorker);
62
+ // eslint-disable-next-line
63
+ this.finishTask(currentWorker);
64
+ reject(error);
65
+ };
66
+ currentWorker.postMessage(['decode', fileDirectory, buffer], [buffer]);
67
+ });
68
+ }
69
+
70
+ async waitForWorker() {
71
+ const idleWorker = this.idleWorkers.pop();
72
+ if (idleWorker) {
73
+ return idleWorker;
74
+ }
75
+ const waiter: any = {};
76
+ const promise = new Promise((resolve) => {
77
+ waiter.resolve = resolve;
78
+ });
79
+
80
+ this.waitQueue.push(waiter);
81
+ return promise as Promise<Worker>;
82
+ }
83
+
84
+ async finishTask(currentWorker: Worker) {
85
+ const waiter = this.waitQueue.pop();
86
+ if (waiter) {
87
+ waiter.resolve(currentWorker);
88
+ } else {
89
+ this.idleWorkers.push(currentWorker);
90
+ }
91
+ }
92
+
93
+ destroy() {
94
+ // eslint-disable-next-line no-plusplus
95
+ for (let i = 0; i < this.workers.length; ++i) {
96
+ this.workers[i].terminate();
97
+ }
98
+ }
99
+ }
@@ -0,0 +1,96 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import type {GeoTIFF} from 'geotiff';
6
+ import type Pool from './Pool';
7
+
8
+ const VIV_PROXY_KEY = '__viv';
9
+ const OFFSETS_PROXY_KEY = `${VIV_PROXY_KEY}-offsets` as const;
10
+ const POOL_PROXY_KEY = `${VIV_PROXY_KEY}-decoder-pool` as const;
11
+
12
+ /*
13
+ * Inspect if the GeoTIFF source is wrapped in our proxies,
14
+ * and warn if missing.
15
+ */
16
+ export function checkProxies(tiff: GeoTIFF) {
17
+ if (!isProxy(tiff, OFFSETS_PROXY_KEY)) {
18
+ console.warn('GeoTIFF source is missing offsets proxy.'); // eslint-disable-line no-console
19
+ }
20
+
21
+ if (!isProxy(tiff, POOL_PROXY_KEY)) {
22
+ console.warn('GeoTIFF source is missing decoder-pool proxy.'); // eslint-disable-line no-console
23
+ }
24
+ }
25
+
26
+ /*
27
+ * > isProxy(tiff, POOL_PROXY_KEY) === true; // false
28
+ * > tiff = createPoolProxy(tiff, new Pool());
29
+ * > isProxy(tiff, POOL_PROXY_KEY) === true; // true
30
+ */
31
+ function isProxy(tiff: GeoTIFF, proxyFlag: string) {
32
+ return (tiff as any)[proxyFlag] as boolean;
33
+ }
34
+
35
+ /*
36
+ * Creates an ES6 Proxy that wraps a GeoTIFF object. The proxy
37
+ * handler intercepts calls to `tiff.getImage` and uses our custom
38
+ * pre-computed offsets to pre-fetch the correct file directory.
39
+ *
40
+ * This is a bit of a hack. Internally GeoTIFF inspects `this.ifdRequests`
41
+ * to see which fileDirectories need to be traversed. By adding the
42
+ * ifdRequest for an 'index' manually, GeoTIFF will await that request
43
+ * rather than traversing the file system remotely.
44
+ */
45
+ export function createOffsetsProxy(tiff: GeoTIFF, offsets: number[]) {
46
+ const get = (target: GeoTIFF, key: any) => {
47
+ // Intercept `tiff.getImage`
48
+ if (key === 'getImage') {
49
+ return (index: number) => {
50
+ // Manually add ifdRequest to tiff if missing and we have an offset.
51
+ if (!(index in target.ifdRequests) && index in offsets) {
52
+ const offset = offsets[index];
53
+ target.ifdRequests[index] = target.parseFileDirectoryAt(offset);
54
+ }
55
+ return target.getImage(index);
56
+ };
57
+ }
58
+
59
+ // tiff['__viv-offsets'] === true
60
+ if (key === OFFSETS_PROXY_KEY) {
61
+ return true;
62
+ }
63
+
64
+ return Reflect.get(target, key);
65
+ };
66
+ return new Proxy(tiff, {get});
67
+ }
68
+
69
+ /*
70
+ * Creates an ES6 Proxy that wraps a GeoTIFF object. The proxy
71
+ * handler intercepts calls to `tiff.readRasters` and injects
72
+ * a pool argument to every call. This means our TiffPixelSource
73
+ * doesn't need to be aware of whether a decoder pool is in use.
74
+ *
75
+ * > tiff.readRasters({ window }) -> tiff.readRasters({ window, pool });
76
+ */
77
+ export function createPoolProxy(tiff: GeoTIFF, pool: Pool) {
78
+ const get = (target: GeoTIFF, key: any) => {
79
+ // Intercept calls to `image.readRasters`
80
+ if (key === 'readRasters') {
81
+ return (options: Parameters<typeof target.readRasters>) => {
82
+ // Inject `pool` argument with other raster options.
83
+ // @ts-ignore
84
+ return target.readRasters({...options, pool});
85
+ };
86
+ }
87
+
88
+ // tiff['__viv-decoder-pool'] === true
89
+ if (key === POOL_PROXY_KEY) {
90
+ return true;
91
+ }
92
+
93
+ return Reflect.get(target, key);
94
+ };
95
+ return new Proxy(tiff, {get});
96
+ }
package/src/loaders.ts ADDED
@@ -0,0 +1,68 @@
1
+ import {fromArrayBuffer} from 'geotiff';
2
+ import type {LoaderWithParser, LoaderOptions} from '@loaders.gl/loader-utils';
3
+
4
+ // __VERSION__ is injected by babel-plugin-version-inline
5
+ // @ts-ignore TS2304: Cannot find name '__VERSION__'.
6
+ const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
7
+
8
+ // Type definitions
9
+ type GeoTiffData = {
10
+ bounds: number[];
11
+ width: number;
12
+ height: number;
13
+ imageData: ImageData;
14
+ };
15
+
16
+ interface GeoTiffLoaderOptions extends LoaderOptions {
17
+ enableAlpha: boolean;
18
+ }
19
+
20
+ /**
21
+ * Loads a GeoTIFF file containing a RGB image.
22
+ */
23
+ const loadGeoTiff = async (
24
+ data: ArrayBuffer,
25
+ options?: GeoTiffLoaderOptions
26
+ ): Promise<GeoTiffData> => {
27
+ // Load using Geotiff.js
28
+ const tiff = await fromArrayBuffer(data);
29
+
30
+ // Assumes we only have one image inside TIFF
31
+ const image = await tiff.getImage();
32
+
33
+ // Read image and size
34
+ // TODO: Add support for worker pools here.
35
+ // TODO: Add support for more image formats.
36
+ const rgbData = await image.readRGB({
37
+ enableAlpha: options?.enableAlpha
38
+ });
39
+ const width = image.getWidth();
40
+ const height = image.getHeight();
41
+
42
+ // Create a new ImageData object
43
+ const imageData = new ImageData(width, height);
44
+ imageData.data.set(new Uint8ClampedArray(rgbData as unknown as Uint8Array));
45
+
46
+ // Return GeoJSON data
47
+ return {
48
+ // TODO: Add bounds here
49
+ bounds: [],
50
+ imageData,
51
+ width,
52
+ height
53
+ };
54
+ };
55
+
56
+ // Export loader
57
+ export const GeoTiffLoader: LoaderWithParser<GeoTiffData, never, GeoTiffLoaderOptions> = {
58
+ id: 'geotiff',
59
+ name: 'GeoTIFF',
60
+ module: 'geotiff',
61
+ version: VERSION,
62
+ options: {
63
+ enableAlpha: true
64
+ },
65
+ mimeTypes: ['image/tiff', 'image/geotiff'],
66
+ extensions: ['geotiff', 'tiff', 'geotif', 'tif'],
67
+ parse: loadGeoTiff
68
+ };
@@ -1,29 +0,0 @@
1
- /** eslint-disable */
2
- import type { FileDirectory } from 'geotiff';
3
- /**
4
- * Pool for workers to decode chunks of the images.
5
- * This is a line-for-line copy of GeoTIFFs old implementation: https://github.com/geotiffjs/geotiff.js/blob/v1.0.0-beta.8/src/pool.js
6
- */
7
- export default class Pool {
8
- workers: Worker[];
9
- idleWorkers: Worker[];
10
- waitQueue: any[];
11
- decoder: null;
12
- /**
13
- * @constructor
14
- * @param {Number} size The size of the pool. Defaults to the number of CPUs
15
- * available. When this parameter is `null` or 0, then the
16
- * decoding will be done in the main thread.
17
- */
18
- constructor(size?: number);
19
- /**
20
- * Decode the given block of bytes with the set compression method.
21
- * @param {ArrayBuffer} buffer the array buffer of bytes to decode.
22
- * @returns {Promise.<ArrayBuffer>} the decoded result as a `Promise`
23
- */
24
- decode(fileDirectory: FileDirectory, buffer: ArrayBuffer): Promise<unknown>;
25
- waitForWorker(): Promise<Worker>;
26
- finishTask(currentWorker: Worker): Promise<void>;
27
- destroy(): void;
28
- }
29
- //# sourceMappingURL=Pool.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Pool.d.ts","sourceRoot":"","sources":["../../../src/lib/utils/Pool.ts"],"names":[],"mappings":"AAIA,qBAAqB;AACrB,OAAO,KAAK,EAAC,aAAa,EAAC,MAAM,SAAS,CAAC;AAU3C;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,IAAI;IACvB,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,SAAS,EAAE,GAAG,EAAE,CAAC;IACjB,OAAO,EAAE,IAAI,CAAC;IAEd;;;;;OAKG;gBACS,IAAI,SAAkB;IAclC;;;;OAIG;IACG,MAAM,CAAC,aAAa,EAAE,aAAa,EAAE,MAAM,EAAE,WAAW;IAmBxD,aAAa;IAcb,UAAU,CAAC,aAAa,EAAE,MAAM;IAStC,OAAO;CAMR"}
@@ -1,60 +0,0 @@
1
- var _globalThis$navigator, _globalThis$navigator2;
2
- const defaultPoolSize = (_globalThis$navigator = globalThis === null || globalThis === void 0 ? void 0 : (_globalThis$navigator2 = globalThis.navigator) === null || _globalThis$navigator2 === void 0 ? void 0 : _globalThis$navigator2.hardwareConcurrency) !== null && _globalThis$navigator !== void 0 ? _globalThis$navigator : 4;
3
- export default class Pool {
4
- constructor() {
5
- let size = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultPoolSize;
6
- this.workers = void 0;
7
- this.idleWorkers = void 0;
8
- this.waitQueue = void 0;
9
- this.decoder = void 0;
10
- this.workers = [];
11
- this.idleWorkers = [];
12
- this.waitQueue = [];
13
- this.decoder = null;
14
- for (let i = 0; i < size; ++i) {
15
- const w = new Worker('./decoder.worker');
16
- this.workers.push(w);
17
- this.idleWorkers.push(w);
18
- }
19
- }
20
- async decode(fileDirectory, buffer) {
21
- const currentWorker = await this.waitForWorker();
22
- return new Promise((resolve, reject) => {
23
- currentWorker.onmessage = event => {
24
- this.finishTask(currentWorker);
25
- resolve(event.data[0]);
26
- };
27
- currentWorker.onerror = error => {
28
- this.finishTask(currentWorker);
29
- reject(error);
30
- };
31
- currentWorker.postMessage(['decode', fileDirectory, buffer], [buffer]);
32
- });
33
- }
34
- async waitForWorker() {
35
- const idleWorker = this.idleWorkers.pop();
36
- if (idleWorker) {
37
- return idleWorker;
38
- }
39
- const waiter = {};
40
- const promise = new Promise(resolve => {
41
- waiter.resolve = resolve;
42
- });
43
- this.waitQueue.push(waiter);
44
- return promise;
45
- }
46
- async finishTask(currentWorker) {
47
- const waiter = this.waitQueue.pop();
48
- if (waiter) {
49
- waiter.resolve(currentWorker);
50
- } else {
51
- this.idleWorkers.push(currentWorker);
52
- }
53
- }
54
- destroy() {
55
- for (let i = 0; i < this.workers.length; ++i) {
56
- this.workers[i].terminate();
57
- }
58
- }
59
- }
60
- //# sourceMappingURL=Pool.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Pool.js","names":["defaultPoolSize","_globalThis$navigator","globalThis","_globalThis$navigator2","navigator","hardwareConcurrency","Pool","constructor","size","arguments","length","undefined","workers","idleWorkers","waitQueue","decoder","i","w","Worker","push","decode","fileDirectory","buffer","currentWorker","waitForWorker","Promise","resolve","reject","onmessage","event","finishTask","data","onerror","error","postMessage","idleWorker","pop","waiter","promise","destroy","terminate"],"sources":["../../../src/lib/utils/Pool.ts"],"sourcesContent":["// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\n\n/** eslint-disable */\nimport type {FileDirectory} from 'geotiff';\n\n// import Worker from 'web-worker:./decoder.worker.ts';\n\n// https://developer.mozilla.org/en-US/docs/Web/API/NavigatorConcurrentHardware/hardwareConcurrency\n// We need to give a different way of getting this for safari, so 4 is probably a safe bet\n// for parallel processing in the meantime. More can't really hurt since they'll just block\n// each other and not the UI thread, which is the real benefit.\nconst defaultPoolSize = globalThis?.navigator?.hardwareConcurrency ?? 4;\n\n/**\n * Pool for workers to decode chunks of the images.\n * This is a line-for-line copy of GeoTIFFs old implementation: https://github.com/geotiffjs/geotiff.js/blob/v1.0.0-beta.8/src/pool.js\n */\nexport default class Pool {\n workers: Worker[];\n idleWorkers: Worker[];\n waitQueue: any[];\n decoder: null;\n\n /**\n * @constructor\n * @param {Number} size The size of the pool. Defaults to the number of CPUs\n * available. When this parameter is `null` or 0, then the\n * decoding will be done in the main thread.\n */\n constructor(size = defaultPoolSize) {\n this.workers = [];\n this.idleWorkers = [];\n this.waitQueue = [];\n this.decoder = null;\n\n // eslint-disable-next-line no-plusplus\n for (let i = 0; i < size; ++i) {\n const w = new Worker('./decoder.worker');\n this.workers.push(w);\n this.idleWorkers.push(w);\n }\n }\n\n /**\n * Decode the given block of bytes with the set compression method.\n * @param {ArrayBuffer} buffer the array buffer of bytes to decode.\n * @returns {Promise.<ArrayBuffer>} the decoded result as a `Promise`\n */\n async decode(fileDirectory: FileDirectory, buffer: ArrayBuffer) {\n const currentWorker = await this.waitForWorker();\n return new Promise((resolve, reject) => {\n currentWorker.onmessage = (event) => {\n // this.workers.push(currentWorker);\n // eslint-disable-next-line\n this.finishTask(currentWorker);\n resolve(event.data[0]);\n };\n currentWorker.onerror = (error) => {\n // this.workers.push(currentWorker);\n // eslint-disable-next-line\n this.finishTask(currentWorker);\n reject(error);\n };\n currentWorker.postMessage(['decode', fileDirectory, buffer], [buffer]);\n });\n }\n\n async waitForWorker() {\n const idleWorker = this.idleWorkers.pop();\n if (idleWorker) {\n return idleWorker;\n }\n const waiter: any = {};\n const promise = new Promise((resolve) => {\n waiter.resolve = resolve;\n });\n\n this.waitQueue.push(waiter);\n return promise as Promise<Worker>;\n }\n\n async finishTask(currentWorker: Worker) {\n const waiter = this.waitQueue.pop();\n if (waiter) {\n waiter.resolve(currentWorker);\n } else {\n this.idleWorkers.push(currentWorker);\n }\n }\n\n destroy() {\n // eslint-disable-next-line no-plusplus\n for (let i = 0; i < this.workers.length; ++i) {\n this.workers[i].terminate();\n }\n }\n}\n"],"mappings":";AAaA,MAAMA,eAAe,IAAAC,qBAAA,GAAGC,UAAU,aAAVA,UAAU,wBAAAC,sBAAA,GAAVD,UAAU,CAAEE,SAAS,cAAAD,sBAAA,uBAArBA,sBAAA,CAAuBE,mBAAmB,cAAAJ,qBAAA,cAAAA,qBAAA,GAAI,CAAC;AAMvE,eAAe,MAAMK,IAAI,CAAC;EAYxBC,WAAWA,CAAA,EAAyB;IAAA,IAAxBC,IAAI,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAGT,eAAe;IAAA,KAXlCY,OAAO;IAAA,KACPC,WAAW;IAAA,KACXC,SAAS;IAAA,KACTC,OAAO;IASL,IAAI,CAACH,OAAO,GAAG,EAAE;IACjB,IAAI,CAACC,WAAW,GAAG,EAAE;IACrB,IAAI,CAACC,SAAS,GAAG,EAAE;IACnB,IAAI,CAACC,OAAO,GAAG,IAAI;IAGnB,KAAK,IAAIC,CAAC,GAAG,CAAC,EAAEA,CAAC,GAAGR,IAAI,EAAE,EAAEQ,CAAC,EAAE;MAC7B,MAAMC,CAAC,GAAG,IAAIC,MAAM,CAAC,kBAAkB,CAAC;MACxC,IAAI,CAACN,OAAO,CAACO,IAAI,CAACF,CAAC,CAAC;MACpB,IAAI,CAACJ,WAAW,CAACM,IAAI,CAACF,CAAC,CAAC;IAC1B;EACF;EAOA,MAAMG,MAAMA,CAACC,aAA4B,EAAEC,MAAmB,EAAE;IAC9D,MAAMC,aAAa,GAAG,MAAM,IAAI,CAACC,aAAa,CAAC,CAAC;IAChD,OAAO,IAAIC,OAAO,CAAC,CAACC,OAAO,EAAEC,MAAM,KAAK;MACtCJ,aAAa,CAACK,SAAS,GAAIC,KAAK,IAAK;QAGnC,IAAI,CAACC,UAAU,CAACP,aAAa,CAAC;QAC9BG,OAAO,CAACG,KAAK,CAACE,IAAI,CAAC,CAAC,CAAC,CAAC;MACxB,CAAC;MACDR,aAAa,CAACS,OAAO,GAAIC,KAAK,IAAK;QAGjC,IAAI,CAACH,UAAU,CAACP,aAAa,CAAC;QAC9BI,MAAM,CAACM,KAAK,CAAC;MACf,CAAC;MACDV,aAAa,CAACW,WAAW,CAAC,CAAC,QAAQ,EAAEb,aAAa,EAAEC,MAAM,CAAC,EAAE,CAACA,MAAM,CAAC,CAAC;IACxE,CAAC,CAAC;EACJ;EAEA,MAAME,aAAaA,CAAA,EAAG;IACpB,MAAMW,UAAU,GAAG,IAAI,CAACtB,WAAW,CAACuB,GAAG,CAAC,CAAC;IACzC,IAAID,UAAU,EAAE;MACd,OAAOA,UAAU;IACnB;IACA,MAAME,MAAW,GAAG,CAAC,CAAC;IACtB,MAAMC,OAAO,GAAG,IAAIb,OAAO,CAAEC,OAAO,IAAK;MACvCW,MAAM,CAACX,OAAO,GAAGA,OAAO;IAC1B,CAAC,CAAC;IAEF,IAAI,CAACZ,SAAS,CAACK,IAAI,CAACkB,MAAM,CAAC;IAC3B,OAAOC,OAAO;EAChB;EAEA,MAAMR,UAAUA,CAACP,aAAqB,EAAE;IACtC,MAAMc,MAAM,GAAG,IAAI,CAACvB,SAAS,CAACsB,GAAG,CAAC,CAAC;IACnC,IAAIC,MAAM,EAAE;MACVA,MAAM,CAACX,OAAO,CAACH,aAAa,CAAC;IAC/B,CAAC,MAAM;MACL,IAAI,CAACV,WAAW,CAACM,IAAI,CAACI,aAAa,CAAC;IACtC;EACF;EAEAgB,OAAOA,CAAA,EAAG;IAER,KAAK,IAAIvB,CAAC,GAAG,CAAC,EAAEA,CAAC,GAAG,IAAI,CAACJ,OAAO,CAACF,MAAM,EAAE,EAAEM,CAAC,EAAE;MAC5C,IAAI,CAACJ,OAAO,CAACI,CAAC,CAAC,CAACwB,SAAS,CAAC,CAAC;IAC7B;EACF;AACF"}
@@ -1,6 +0,0 @@
1
- import type { GeoTIFF } from 'geotiff';
2
- import type Pool from './Pool';
3
- export declare function checkProxies(tiff: GeoTIFF): void;
4
- export declare function createOffsetsProxy(tiff: GeoTIFF, offsets: number[]): GeoTIFF;
5
- export declare function createPoolProxy(tiff: GeoTIFF, pool: Pool): GeoTIFF;
6
- //# sourceMappingURL=proxies.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"proxies.d.ts","sourceRoot":"","sources":["../../../src/lib/utils/proxies.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,OAAO,EAAC,MAAM,SAAS,CAAC;AACrC,OAAO,KAAK,IAAI,MAAM,QAAQ,CAAC;AAU/B,wBAAgB,YAAY,CAAC,IAAI,EAAE,OAAO,QAQzC;AAqBD,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,WAsBlE;AAUD,wBAAgB,eAAe,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,WAmBxD"}
@@ -1,54 +0,0 @@
1
- const VIV_PROXY_KEY = '__viv';
2
- const OFFSETS_PROXY_KEY = `${VIV_PROXY_KEY}-offsets`;
3
- const POOL_PROXY_KEY = `${VIV_PROXY_KEY}-decoder-pool`;
4
- export function checkProxies(tiff) {
5
- if (!isProxy(tiff, OFFSETS_PROXY_KEY)) {
6
- console.warn('GeoTIFF source is missing offsets proxy.');
7
- }
8
- if (!isProxy(tiff, POOL_PROXY_KEY)) {
9
- console.warn('GeoTIFF source is missing decoder-pool proxy.');
10
- }
11
- }
12
- function isProxy(tiff, proxyFlag) {
13
- return tiff[proxyFlag];
14
- }
15
- export function createOffsetsProxy(tiff, offsets) {
16
- const get = (target, key) => {
17
- if (key === 'getImage') {
18
- return index => {
19
- if (!(index in target.ifdRequests) && index in offsets) {
20
- const offset = offsets[index];
21
- target.ifdRequests[index] = target.parseFileDirectoryAt(offset);
22
- }
23
- return target.getImage(index);
24
- };
25
- }
26
- if (key === OFFSETS_PROXY_KEY) {
27
- return true;
28
- }
29
- return Reflect.get(target, key);
30
- };
31
- return new Proxy(tiff, {
32
- get
33
- });
34
- }
35
- export function createPoolProxy(tiff, pool) {
36
- const get = (target, key) => {
37
- if (key === 'readRasters') {
38
- return options => {
39
- return target.readRasters({
40
- ...options,
41
- pool
42
- });
43
- };
44
- }
45
- if (key === POOL_PROXY_KEY) {
46
- return true;
47
- }
48
- return Reflect.get(target, key);
49
- };
50
- return new Proxy(tiff, {
51
- get
52
- });
53
- }
54
- //# sourceMappingURL=proxies.js.map