@loaders.gl/geotiff 4.1.0-alpha.1 → 4.1.0-alpha.11

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 (69) 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/ome/ome-utils.d.ts.map +1 -1
  23. package/dist/lib/ome/ome-utils.js.map +1 -1
  24. package/dist/lib/ome/omexml.d.ts.map +1 -1
  25. package/dist/lib/ome/omexml.js.map +1 -1
  26. package/dist/lib/ome/utils.d.ts.map +1 -1
  27. package/dist/lib/ome/utils.js.map +1 -1
  28. package/dist/lib/tiff-pixel-source.d.ts +1 -2
  29. package/dist/lib/tiff-pixel-source.d.ts.map +1 -1
  30. package/dist/lib/tiff-pixel-source.js +1 -2
  31. package/dist/lib/tiff-pixel-source.js.map +1 -1
  32. package/{src/lib/utils/Pool.ts → dist/lib/utils/Pool.ts.disabled} +3 -2
  33. package/{src/lib/utils/proxies.ts → dist/lib/utils/proxies.ts.disabled} +2 -1
  34. package/dist/lib/utils/tiff-utils.d.ts.map +1 -1
  35. package/dist/lib/utils/tiff-utils.js.map +1 -1
  36. package/dist/loaders.d.ts +13 -0
  37. package/dist/loaders.d.ts.map +1 -0
  38. package/dist/loaders.js +32 -0
  39. package/dist/loaders.js.map +1 -0
  40. package/dist/types.d.ts.map +1 -1
  41. package/dist/types.js.map +1 -1
  42. package/package.json +5 -5
  43. package/src/geotiff-loader.ts +115 -0
  44. package/src/index.ts +5 -2
  45. package/src/lib/load-geotiff.ts +10 -9
  46. package/src/lib/ome/load-ome-tiff.ts +3 -2
  47. package/src/lib/ome/ome-indexers.ts +7 -3
  48. package/src/lib/ome/ome-utils.ts +2 -1
  49. package/src/lib/ome/omexml.ts +2 -1
  50. package/src/lib/ome/utils.ts +2 -1
  51. package/src/lib/tiff-pixel-source.ts +7 -6
  52. package/src/lib/utils/Pool.ts.disabled +99 -0
  53. package/src/lib/utils/proxies.ts.disabled +96 -0
  54. package/src/lib/utils/tiff-utils.ts +2 -1
  55. package/src/loaders.ts +68 -0
  56. package/src/types.ts +2 -1
  57. package/dist/lib/utils/Pool.d.ts +0 -29
  58. package/dist/lib/utils/Pool.d.ts.map +0 -1
  59. package/dist/lib/utils/Pool.js +0 -60
  60. package/dist/lib/utils/Pool.js.map +0 -1
  61. package/dist/lib/utils/proxies.d.ts +0 -6
  62. package/dist/lib/utils/proxies.d.ts.map +0 -1
  63. package/dist/lib/utils/proxies.js +0 -54
  64. package/dist/lib/utils/proxies.js.map +0 -1
  65. package/dist/typings/geotiff.d.ts +0 -60
  66. package/dist/typings/geotiff.d.ts.map +0 -1
  67. package/dist/typings/geotiff.js +0 -2
  68. package/dist/typings/geotiff.js.map +0 -1
  69. package/src/typings/geotiff.ts +0 -75
@@ -1,8 +1,9 @@
1
- // loaders.gl, MIT license
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
2
3
  // Copyright (c) vis.gl contributors
3
4
 
4
5
  /** eslint-disable */
5
- import type {FileDirectory} from 'geotiff';
6
+ import {FileDirectory} from 'geotiff';
6
7
 
7
8
  // import Worker from 'web-worker:./decoder.worker.ts';
8
9
 
@@ -1,4 +1,5 @@
1
- // loaders.gl, MIT license
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
2
3
  // Copyright (c) vis.gl contributors
3
4
 
4
5
  import type {GeoTIFF} from 'geotiff';
@@ -1 +1 @@
1
- {"version":3,"file":"tiff-utils.d.ts","sourceRoot":"","sources":["../../../src/lib/utils/tiff-utils.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,aAAa,CAAC;AAE7C,wBAAgB,WAAW,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,OAExC;AASD,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,oCAapC;AAOD,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,WAG5C;AAED,wBAAgB,YAAY,CAAC,CAAC,SAAS,MAAM,EAAE,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC;;;EAItE;AAED,eAAO,MAAM,cAAc,uBAAuB,CAAC"}
1
+ {"version":3,"file":"tiff-utils.d.ts","sourceRoot":"","sources":["../../../src/lib/utils/tiff-utils.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,aAAa,CAAC;AAE7C,wBAAgB,WAAW,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,OAExC;AASD,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,oCAapC;AAOD,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,WAG5C;AAED,wBAAgB,YAAY,CAAC,CAAC,SAAS,MAAM,EAAE,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC;;;EAItE;AAED,eAAO,MAAM,cAAc,uBAAuB,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"tiff-utils.js","names":["ensureArray","x","Array","isArray","intToRgba","int","Number","isInteger","Error","buffer","ArrayBuffer","view","DataView","setInt32","bytes","Uint8Array","from","isInterleaved","shape","lastDimSize","length","getImageSize","source","interleaved","height","width","slice","SIGNAL_ABORTED"],"sources":["../../../src/lib/utils/tiff-utils.ts"],"sourcesContent":["// loaders.gl, MIT license\n// Copyright (c) vis.gl contributors\n\nimport type {PixelSource} from '../../types';\n\nexport function ensureArray<T>(x: T | T[]) {\n return Array.isArray(x) ? x : [x];\n}\n\n/*\n * Converts 32-bit integer color representation to RGBA tuple.\n * Used to serialize colors from OME-XML metadata.\n *\n * > console.log(intToRgba(100100));\n * > // [0, 1, 135, 4]\n */\nexport function intToRgba(int: number) {\n if (!Number.isInteger(int)) {\n throw Error('Not an integer.');\n }\n\n // Write number to int32 representation (4 bytes).\n const buffer = new ArrayBuffer(4);\n const view = new DataView(buffer);\n view.setInt32(0, int, false); // offset === 0, littleEndian === false\n\n // Take u8 view and extract number for each byte (1 byte for R/G/B/A).\n const bytes = new Uint8Array(buffer);\n return Array.from(bytes) as [number, number, number, number];\n}\n\n/*\n * Helper method to determine whether pixel data is interleaved or not.\n * > isInterleaved([1, 24, 24]) === false;\n * > isInterleaved([1, 24, 24, 3]) === true;\n */\nexport function isInterleaved(shape: number[]) {\n const lastDimSize = shape[shape.length - 1];\n return lastDimSize === 3 || lastDimSize === 4;\n}\n\nexport function getImageSize<T extends string[]>(source: PixelSource<T>) {\n const interleaved = isInterleaved(source.shape);\n const [height, width] = source.shape.slice(interleaved ? -3 : -2);\n return {height, width};\n}\n\nexport const SIGNAL_ABORTED = '__vivSignalAborted';\n"],"mappings":"AAKA,OAAO,SAASA,WAAWA,CAAIC,CAAU,EAAE;EACzC,OAAOC,KAAK,CAACC,OAAO,CAACF,CAAC,CAAC,GAAGA,CAAC,GAAG,CAACA,CAAC,CAAC;AACnC;AASA,OAAO,SAASG,SAASA,CAACC,GAAW,EAAE;EACrC,IAAI,CAACC,MAAM,CAACC,SAAS,CAACF,GAAG,CAAC,EAAE;IAC1B,MAAMG,KAAK,CAAC,iBAAiB,CAAC;EAChC;EAGA,MAAMC,MAAM,GAAG,IAAIC,WAAW,CAAC,CAAC,CAAC;EACjC,MAAMC,IAAI,GAAG,IAAIC,QAAQ,CAACH,MAAM,CAAC;EACjCE,IAAI,CAACE,QAAQ,CAAC,CAAC,EAAER,GAAG,EAAE,KAAK,CAAC;EAG5B,MAAMS,KAAK,GAAG,IAAIC,UAAU,CAACN,MAAM,CAAC;EACpC,OAAOP,KAAK,CAACc,IAAI,CAACF,KAAK,CAAC;AAC1B;AAOA,OAAO,SAASG,aAAaA,CAACC,KAAe,EAAE;EAC7C,MAAMC,WAAW,GAAGD,KAAK,CAACA,KAAK,CAACE,MAAM,GAAG,CAAC,CAAC;EAC3C,OAAOD,WAAW,KAAK,CAAC,IAAIA,WAAW,KAAK,CAAC;AAC/C;AAEA,OAAO,SAASE,YAAYA,CAAqBC,MAAsB,EAAE;EACvE,MAAMC,WAAW,GAAGN,aAAa,CAACK,MAAM,CAACJ,KAAK,CAAC;EAC/C,MAAM,CAACM,MAAM,EAAEC,KAAK,CAAC,GAAGH,MAAM,CAACJ,KAAK,CAACQ,KAAK,CAACH,WAAW,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;EACjE,OAAO;IAACC,MAAM;IAAEC;EAAK,CAAC;AACxB;AAEA,OAAO,MAAME,cAAc,GAAG,oBAAoB"}
1
+ {"version":3,"file":"tiff-utils.js","names":["ensureArray","x","Array","isArray","intToRgba","int","Number","isInteger","Error","buffer","ArrayBuffer","view","DataView","setInt32","bytes","Uint8Array","from","isInterleaved","shape","lastDimSize","length","getImageSize","source","interleaved","height","width","slice","SIGNAL_ABORTED"],"sources":["../../../src/lib/utils/tiff-utils.ts"],"sourcesContent":["// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\n\nimport type {PixelSource} from '../../types';\n\nexport function ensureArray<T>(x: T | T[]) {\n return Array.isArray(x) ? x : [x];\n}\n\n/*\n * Converts 32-bit integer color representation to RGBA tuple.\n * Used to serialize colors from OME-XML metadata.\n *\n * > console.log(intToRgba(100100));\n * > // [0, 1, 135, 4]\n */\nexport function intToRgba(int: number) {\n if (!Number.isInteger(int)) {\n throw Error('Not an integer.');\n }\n\n // Write number to int32 representation (4 bytes).\n const buffer = new ArrayBuffer(4);\n const view = new DataView(buffer);\n view.setInt32(0, int, false); // offset === 0, littleEndian === false\n\n // Take u8 view and extract number for each byte (1 byte for R/G/B/A).\n const bytes = new Uint8Array(buffer);\n return Array.from(bytes) as [number, number, number, number];\n}\n\n/*\n * Helper method to determine whether pixel data is interleaved or not.\n * > isInterleaved([1, 24, 24]) === false;\n * > isInterleaved([1, 24, 24, 3]) === true;\n */\nexport function isInterleaved(shape: number[]) {\n const lastDimSize = shape[shape.length - 1];\n return lastDimSize === 3 || lastDimSize === 4;\n}\n\nexport function getImageSize<T extends string[]>(source: PixelSource<T>) {\n const interleaved = isInterleaved(source.shape);\n const [height, width] = source.shape.slice(interleaved ? -3 : -2);\n return {height, width};\n}\n\nexport const SIGNAL_ABORTED = '__vivSignalAborted';\n"],"mappings":"AAMA,OAAO,SAASA,WAAWA,CAAIC,CAAU,EAAE;EACzC,OAAOC,KAAK,CAACC,OAAO,CAACF,CAAC,CAAC,GAAGA,CAAC,GAAG,CAACA,CAAC,CAAC;AACnC;AASA,OAAO,SAASG,SAASA,CAACC,GAAW,EAAE;EACrC,IAAI,CAACC,MAAM,CAACC,SAAS,CAACF,GAAG,CAAC,EAAE;IAC1B,MAAMG,KAAK,CAAC,iBAAiB,CAAC;EAChC;EAGA,MAAMC,MAAM,GAAG,IAAIC,WAAW,CAAC,CAAC,CAAC;EACjC,MAAMC,IAAI,GAAG,IAAIC,QAAQ,CAACH,MAAM,CAAC;EACjCE,IAAI,CAACE,QAAQ,CAAC,CAAC,EAAER,GAAG,EAAE,KAAK,CAAC;EAG5B,MAAMS,KAAK,GAAG,IAAIC,UAAU,CAACN,MAAM,CAAC;EACpC,OAAOP,KAAK,CAACc,IAAI,CAACF,KAAK,CAAC;AAC1B;AAOA,OAAO,SAASG,aAAaA,CAACC,KAAe,EAAE;EAC7C,MAAMC,WAAW,GAAGD,KAAK,CAACA,KAAK,CAACE,MAAM,GAAG,CAAC,CAAC;EAC3C,OAAOD,WAAW,KAAK,CAAC,IAAIA,WAAW,KAAK,CAAC;AAC/C;AAEA,OAAO,SAASE,YAAYA,CAAqBC,MAAsB,EAAE;EACvE,MAAMC,WAAW,GAAGN,aAAa,CAACK,MAAM,CAACJ,KAAK,CAAC;EAC/C,MAAM,CAACM,MAAM,EAAEC,KAAK,CAAC,GAAGH,MAAM,CAACJ,KAAK,CAACQ,KAAK,CAACH,WAAW,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;EACjE,OAAO;IAACC,MAAM;IAAEC;EAAK,CAAC;AACxB;AAEA,OAAO,MAAME,cAAc,GAAG,oBAAoB"}
@@ -0,0 +1,13 @@
1
+ import type { LoaderWithParser, LoaderOptions } from '@loaders.gl/loader-utils';
2
+ type GeoTiffData = {
3
+ bounds: number[];
4
+ width: number;
5
+ height: number;
6
+ imageData: ImageData;
7
+ };
8
+ interface GeoTiffLoaderOptions extends LoaderOptions {
9
+ enableAlpha: boolean;
10
+ }
11
+ export declare const GeoTiffLoader: LoaderWithParser<GeoTiffData, never, GeoTiffLoaderOptions>;
12
+ export {};
13
+ //# sourceMappingURL=loaders.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loaders.d.ts","sourceRoot":"","sources":["../src/loaders.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,gBAAgB,EAAE,aAAa,EAAC,MAAM,0BAA0B,CAAC;AAO9E,KAAK,WAAW,GAAG;IACjB,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,SAAS,CAAC;CACtB,CAAC;AAEF,UAAU,oBAAqB,SAAQ,aAAa;IAClD,WAAW,EAAE,OAAO,CAAC;CACtB;AAuCD,eAAO,MAAM,aAAa,EAAE,gBAAgB,CAAC,WAAW,EAAE,KAAK,EAAE,oBAAoB,CAWpF,CAAC"}
@@ -0,0 +1,32 @@
1
+ import { fromArrayBuffer } from 'geotiff';
2
+ const VERSION = typeof "4.1.0-alpha.11" !== 'undefined' ? "4.1.0-alpha.11" : 'latest';
3
+ const loadGeoTiff = async (data, options) => {
4
+ const tiff = await fromArrayBuffer(data);
5
+ const image = await tiff.getImage();
6
+ const rgbData = await image.readRGB({
7
+ enableAlpha: options === null || options === void 0 ? void 0 : options.enableAlpha
8
+ });
9
+ const width = image.getWidth();
10
+ const height = image.getHeight();
11
+ const imageData = new ImageData(width, height);
12
+ imageData.data.set(new Uint8ClampedArray(rgbData));
13
+ return {
14
+ bounds: [],
15
+ imageData,
16
+ width,
17
+ height
18
+ };
19
+ };
20
+ export const GeoTiffLoader = {
21
+ id: 'geotiff',
22
+ name: 'GeoTIFF',
23
+ module: 'geotiff',
24
+ version: VERSION,
25
+ options: {
26
+ enableAlpha: true
27
+ },
28
+ mimeTypes: ['image/tiff', 'image/geotiff'],
29
+ extensions: ['geotiff', 'tiff', 'geotif', 'tif'],
30
+ parse: loadGeoTiff
31
+ };
32
+ //# sourceMappingURL=loaders.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loaders.js","names":["fromArrayBuffer","VERSION","loadGeoTiff","data","options","tiff","image","getImage","rgbData","readRGB","enableAlpha","width","getWidth","height","getHeight","imageData","ImageData","set","Uint8ClampedArray","bounds","GeoTiffLoader","id","name","module","version","mimeTypes","extensions","parse"],"sources":["../src/loaders.ts"],"sourcesContent":["import {fromArrayBuffer} from 'geotiff';\nimport type {LoaderWithParser, LoaderOptions} from '@loaders.gl/loader-utils';\n\n// __VERSION__ is injected by babel-plugin-version-inline\n// @ts-ignore TS2304: Cannot find name '__VERSION__'.\nconst VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';\n\n// Type definitions\ntype GeoTiffData = {\n bounds: number[];\n width: number;\n height: number;\n imageData: ImageData;\n};\n\ninterface GeoTiffLoaderOptions extends LoaderOptions {\n enableAlpha: boolean;\n}\n\n/**\n * Loads a GeoTIFF file containing a RGB image.\n */\nconst loadGeoTiff = async (\n data: ArrayBuffer,\n options?: GeoTiffLoaderOptions\n): Promise<GeoTiffData> => {\n // Load using Geotiff.js\n const tiff = await fromArrayBuffer(data);\n\n // Assumes we only have one image inside TIFF\n const image = await tiff.getImage();\n\n // Read image and size\n // TODO: Add support for worker pools here.\n // TODO: Add support for more image formats.\n const rgbData = await image.readRGB({\n enableAlpha: options?.enableAlpha\n });\n const width = image.getWidth();\n const height = image.getHeight();\n\n // Create a new ImageData object\n const imageData = new ImageData(width, height);\n imageData.data.set(new Uint8ClampedArray(rgbData as unknown as Uint8Array));\n\n // Return GeoJSON data\n return {\n // TODO: Add bounds here\n bounds: [],\n imageData,\n width,\n height\n };\n};\n\n// Export loader\nexport const GeoTiffLoader: LoaderWithParser<GeoTiffData, never, GeoTiffLoaderOptions> = {\n id: 'geotiff',\n name: 'GeoTIFF',\n module: 'geotiff',\n version: VERSION,\n options: {\n enableAlpha: true\n },\n mimeTypes: ['image/tiff', 'image/geotiff'],\n extensions: ['geotiff', 'tiff', 'geotif', 'tif'],\n parse: loadGeoTiff\n};\n"],"mappings":"AAAA,SAAQA,eAAe,QAAO,SAAS;AAKvC,MAAMC,OAAO,GAAG,uBAAkB,KAAK,WAAW,sBAAiB,QAAQ;AAiB3E,MAAMC,WAAW,GAAG,MAAAA,CAClBC,IAAiB,EACjBC,OAA8B,KACL;EAEzB,MAAMC,IAAI,GAAG,MAAML,eAAe,CAACG,IAAI,CAAC;EAGxC,MAAMG,KAAK,GAAG,MAAMD,IAAI,CAACE,QAAQ,CAAC,CAAC;EAKnC,MAAMC,OAAO,GAAG,MAAMF,KAAK,C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CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;IAC7C,yBAAyB,CAAC,EAAE,MAAM,CAAC;CACpC;AAED,MAAM,MAAM,MAAM,CAAC,CAAC,SAAS,MAAM,EAAE,IAAI,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;AAEnF;;GAEG;AACH,MAAM,WAAW,WAAW,CAAC,CAAC,SAAS,MAAM,EAAE;IAC7C,uBAAuB;IACvB,SAAS,CAAC,GAAG,EAAE,eAAe,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;IACvD,mBAAmB;IACnB,OAAO,CAAC,GAAG,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;IACnD,WAAW,CAAC,GAAG,EAAE,KAAK,GAAG,IAAI,CAAC;IAC9B,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,KAAK,EAAE,KAAK,CAAC;IACb,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,eAAe,CAAC;CACxB"}
package/dist/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","names":[],"sources":["../src/types.ts"],"sourcesContent":["// loaders.gl, MIT license\n// Copyright (c) vis.gl contributors\n\nimport {DTYPE_LOOKUP} from './lib/ome/ome-utils';\n\nexport type Dtype = (typeof DTYPE_LOOKUP)[keyof typeof DTYPE_LOOKUP];\nexport type TypedArray = InstanceType<(typeof globalThis)[`${Dtype}Array`]>;\n\nexport interface PixelData {\n data: TypedArray;\n width: number;\n height: number;\n}\n\nexport type PixelSourceSelection<S extends string[]> = {\n [K in S[number]]: number;\n};\n\nexport interface RasterSelection<S extends string[]> {\n selection: PixelSourceSelection<S>;\n signal?: AbortSignal;\n}\n\nexport interface TileSelection<S extends string[]> {\n x: number;\n y: number;\n selection: PixelSourceSelection<S>;\n signal?: AbortSignal;\n}\n\ninterface PhysicalSize {\n size: number;\n unit: string;\n}\n\nexport interface PixelSourceMeta {\n physicalSizes?: Record<string, PhysicalSize>;\n photometricInterpretation?: number;\n}\n\nexport type Labels<S extends string[]> = [...S, 'y', 'x'] | [...S, 'y', 'x', '_c'];\n\n/**\n * Interface to load tiles from a data source\n */\nexport interface PixelSource<S extends string[]> {\n /** Loads a 2D plane */\n getRaster(sel: RasterSelection<S>): Promise<PixelData>;\n /** Loads a tile */\n getTile(sel: TileSelection<S>): Promise<PixelData>;\n onTileError(err: Error): void;\n shape: number[];\n dtype: Dtype;\n labels: Labels<S>;\n tileSize: number;\n meta?: PixelSourceMeta;\n}\n"],"mappings":""}
1
+ {"version":3,"file":"types.js","names":[],"sources":["../src/types.ts"],"sourcesContent":["// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\n\nimport {DTYPE_LOOKUP} from './lib/ome/ome-utils';\n\nexport type Dtype = (typeof DTYPE_LOOKUP)[keyof typeof DTYPE_LOOKUP];\nexport type TypedArray = InstanceType<(typeof globalThis)[`${Dtype}Array`]>;\n\nexport interface PixelData {\n data: TypedArray;\n width: number;\n height: number;\n}\n\nexport type PixelSourceSelection<S extends string[]> = {\n [K in S[number]]: number;\n};\n\nexport interface RasterSelection<S extends string[]> {\n selection: PixelSourceSelection<S>;\n signal?: AbortSignal;\n}\n\nexport interface TileSelection<S extends string[]> {\n x: number;\n y: number;\n selection: PixelSourceSelection<S>;\n signal?: AbortSignal;\n}\n\ninterface PhysicalSize {\n size: number;\n unit: string;\n}\n\nexport interface PixelSourceMeta {\n physicalSizes?: Record<string, PhysicalSize>;\n photometricInterpretation?: number;\n}\n\nexport type Labels<S extends string[]> = [...S, 'y', 'x'] | [...S, 'y', 'x', '_c'];\n\n/**\n * Interface to load tiles from a data source\n */\nexport interface PixelSource<S extends string[]> {\n /** Loads a 2D plane */\n getRaster(sel: RasterSelection<S>): Promise<PixelData>;\n /** Loads a tile */\n getTile(sel: TileSelection<S>): Promise<PixelData>;\n onTileError(err: Error): void;\n shape: number[];\n dtype: Dtype;\n labels: Labels<S>;\n tileSize: number;\n meta?: PixelSourceMeta;\n}\n"],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loaders.gl/geotiff",
3
- "version": "4.1.0-alpha.1",
3
+ "version": "4.1.0-alpha.11",
4
4
  "description": "Framework-independent loaders for tiff and geotiff",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -25,9 +25,9 @@
25
25
  "module": "dist/index.js",
26
26
  "exports": {
27
27
  ".": {
28
+ "types": "./dist/index.d.ts",
28
29
  "import": "./dist/index.js",
29
- "require": "./dist/index.cjs",
30
- "types": "./dist/index.d.ts"
30
+ "require": "./dist/index.cjs"
31
31
  }
32
32
  },
33
33
  "sideEffects": false,
@@ -46,7 +46,7 @@
46
46
  },
47
47
  "dependencies": {
48
48
  "fast-xml-parser": "^4.2.5",
49
- "geotiff": "^1.0.4"
49
+ "geotiff": "^2.1.0"
50
50
  },
51
- "gitHead": "6a4d3da93d45115ad99861474a43c3f4a0b280a7"
51
+ "gitHead": "5d3e23bf93762b48c8c1d6d926ede7a97fe43ab0"
52
52
  }
@@ -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
@@ -1,5 +1,8 @@
1
- // loaders.gl, MIT license
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
2
3
  // Copyright (c) vis.gl contributors
3
4
 
5
+ export {GeoTIFFLoader} from './geotiff-loader';
6
+
4
7
  export {loadGeoTiff} from './lib/load-geotiff';
5
- export {default as TiffPixelSource} from './lib/tiff-pixel-source';
8
+ export {TiffPixelSource} from './lib/tiff-pixel-source';
@@ -1,17 +1,18 @@
1
- // loaders.gl, MIT license
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
2
3
  // Copyright (c) vis.gl contributors
3
4
 
4
5
  import {fromUrl, fromBlob, GeoTIFF} from 'geotiff';
5
6
 
6
- import {
7
- // createPoolProxy,
8
- createOffsetsProxy,
9
- checkProxies
10
- } from './utils/proxies';
7
+ // import {
8
+ // // createPoolProxy,
9
+ // createOffsetsProxy,
10
+ // checkProxies
11
+ // } from './utils/proxies.ts.disabled';
11
12
  // import Pool from './lib/Pool';
12
13
 
13
14
  import {loadOmeTiff, isOmeTiff} from './ome/load-ome-tiff';
14
- import type TiffPixelSource from './tiff-pixel-source';
15
+ import type {TiffPixelSource} from './tiff-pixel-source';
15
16
 
16
17
  /** Options for initializing a tiff pixel source. */
17
18
  interface GeoTIFFOptions {
@@ -68,14 +69,14 @@ export async function loadGeoTiff(
68
69
  * create a proxy that intercepts calls to `tiff.getImage`
69
70
  * and injects the pre-computed offsets.
70
71
  */
71
- tiff = createOffsetsProxy(tiff, offsets);
72
+ // tiff = createOffsetsProxy(tiff, offsets);
72
73
  }
73
74
 
74
75
  /*
75
76
  * Inspect tiff source for our performance enhancing proxies.
76
77
  * Prints warnings to console if `offsets` or `pool` are missing.
77
78
  */
78
- checkProxies(tiff);
79
+ // checkProxies(tiff);
79
80
 
80
81
  const firstImage = await tiff.getImage(0);
81
82
 
@@ -1,9 +1,10 @@
1
- // loaders.gl, MIT license
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
2
3
  // Copyright (c) vis.gl contributors
3
4
 
4
5
  import type {GeoTIFF, GeoTIFFImage} from 'geotiff';
5
6
 
6
- import TiffPixelSource from '../tiff-pixel-source';
7
+ import {TiffPixelSource} from '../tiff-pixel-source';
7
8
  import {getOmeLegacyIndexer, getOmeSubIFDIndexer, OmeTiffIndexer} from './ome-indexers';
8
9
  import {getOmePixelSourceMeta} from './ome-utils';
9
10
  import {fromString} from './omexml';
@@ -1,9 +1,12 @@
1
- // loaders.gl, MIT license
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
2
3
  // Copyright (c) vis.gl contributors
3
4
 
4
- import type {GeoTIFFImage, GeoTIFF, ImageFileDirectory} from 'geotiff';
5
+ import type {GeoTIFFImage, GeoTIFF} from 'geotiff';
5
6
  import type {OMEXML} from '../ome/omexml';
6
7
 
8
+ export type ImageFileDirectory = any;
9
+
7
10
  export type OmeTiffSelection = {t: number; c: number; z: number};
8
11
  export type OmeTiffIndexer = (sel: OmeTiffSelection, z: number) => Promise<GeoTIFFImage>;
9
12
 
@@ -81,13 +84,14 @@ export function getOmeSubIFDIndexer(tiff: GeoTIFF, rootMeta: OMEXML): OmeTiffInd
81
84
  const subIfdOffset = SubIFDs[pyramidLevel - 1];
82
85
  ifdCache.set(key, tiff.parseFileDirectoryAt(subIfdOffset));
83
86
  }
84
- const ifd = (await ifdCache.get(key)) as ImageFileDirectory;
87
+ const ifd = await ifdCache.get(key);
85
88
 
86
89
  // Create a new image object manually from IFD
87
90
  // https://github.com/geotiffjs/geotiff.js/blob/8ef472f41b51d18074aece2300b6a8ad91a21ae1/src/geotiff.js#L447-L453
88
91
  return new (baseImage.constructor as any)(
89
92
  ifd.fileDirectory,
90
93
  ifd.geoKeyDirectory,
94
+ // @ts-expect-error
91
95
  tiff.dataView,
92
96
  tiff.littleEndian,
93
97
  tiff.cache,
@@ -1,4 +1,5 @@
1
- // loaders.gl, MIT license
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
2
3
  // Copyright (c) vis.gl contributors
3
4
 
4
5
  import {getDims, getLabels} from './utils';
@@ -1,4 +1,5 @@
1
- // loaders.gl, MIT license
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
2
3
  // Copyright (c) vis.gl contributors
3
4
 
4
5
  import {XMLParser} from 'fast-xml-parser';
@@ -1,4 +1,5 @@
1
- // loaders.gl, MIT license
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
2
3
  // Copyright (c) vis.gl contributors
3
4
 
4
5
  import type {OMEXML} from '../ome/omexml';
@@ -1,7 +1,8 @@
1
- // loaders.gl, MIT license
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
2
3
  // Copyright (c) vis.gl contributors
3
4
 
4
- import type {GeoTIFFImage, RasterOptions} from 'geotiff';
5
+ import type {GeoTIFFImage} from 'geotiff';
5
6
  import {getImageSize, isInterleaved, SIGNAL_ABORTED} from './utils/tiff-utils';
6
7
 
7
8
  import type {
@@ -16,7 +17,9 @@ import type {
16
17
  TypedArray
17
18
  } from '../types';
18
19
 
19
- 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> {
20
23
  public dtype: Dtype;
21
24
  public tileSize: number;
22
25
  public shape: number[];
@@ -56,7 +59,7 @@ class TiffPixelSource<S extends string[]> implements PixelSource<S> {
56
59
  return this._readRasters(image, {window, width, height, signal});
57
60
  }
58
61
 
59
- private async _readRasters(image: GeoTIFFImage, props?: RasterOptions) {
62
+ private async _readRasters(image: GeoTIFFImage, props?: ReadRasterOptions) {
60
63
  const interleave = isInterleaved(this.shape);
61
64
  const raster = await image.readRasters({interleave, ...props});
62
65
 
@@ -98,5 +101,3 @@ class TiffPixelSource<S extends string[]> implements PixelSource<S> {
98
101
  console.error(err); // eslint-disable-line no-console
99
102
  }
100
103
  }
101
-
102
- 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
+ }
@@ -1,4 +1,5 @@
1
- // loaders.gl, MIT license
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
2
3
  // Copyright (c) vis.gl contributors
3
4
 
4
5
  import type {PixelSource} from '../../types';