@gisatcz/deckgl-geolib 0.0.3 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +2 -0
- package/.eslintrc.cjs +3 -0
- package/dist/cjs/index.js +99799 -18
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/index.min.js +5 -0
- package/dist/cjs/index.min.js.map +1 -0
- package/dist/cjs/types/cogbitmaplayer/CogBitmapLayer.d.ts +16 -0
- package/dist/cjs/types/cogterrainlayer/CogTerrainLayer.d.ts +18 -0
- package/dist/cjs/types/cogtiles/cogtiles.d.ts +29 -0
- package/dist/cjs/types/geoimage/geoimage.d.ts +49 -0
- package/dist/cjs/types/index.d.ts +11 -2
- package/dist/cjs/types/utilities/tileurls.d.ts +4 -0
- package/dist/esm/index.js +99797 -18
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/index.min.js +5 -0
- package/dist/esm/index.min.js.map +1 -0
- package/dist/esm/types/cogbitmaplayer/CogBitmapLayer.d.ts +16 -0
- package/dist/esm/types/cogterrainlayer/CogTerrainLayer.d.ts +18 -0
- package/dist/esm/types/cogtiles/cogtiles.d.ts +29 -0
- package/dist/esm/types/geoimage/geoimage.d.ts +49 -0
- package/dist/esm/types/index.d.ts +11 -2
- package/dist/esm/types/utilities/tileurls.d.ts +4 -0
- package/package.json +42 -36
- package/rollup.config.mjs +70 -0
- package/src/cogbitmaplayer/CogBitmapLayer.ts +104 -0
- package/src/cogbitmaplayer/README.md +52 -0
- package/src/cogterrainlayer/CogTerrainLayer.ts +174 -0
- package/src/cogterrainlayer/README.md +41 -0
- package/src/cogtiles/README.md +72 -0
- package/src/cogtiles/cogtiles.ts +355 -0
- package/src/cogtiles/lzw.js +256 -0
- package/src/geoimage/README.md +110 -0
- package/src/geoimage/geoimage.ts +390 -0
- package/src/index.ts +11 -0
- package/src/utilities/tileurls.ts +21 -0
- package/tsconfig.json +3 -0
- package/README.md +0 -1
- package/dist/cjs/classes/geoImage/GeoImage.d.ts +0 -38
- package/dist/cjs/classes/geoImage/index.d.ts +0 -1
- package/dist/cjs/classes/geoImage/interface.d.ts +0 -32
- package/dist/cjs/classes/index.d.ts +0 -1
- package/dist/cjs/hooks/index.d.ts +0 -1
- package/dist/cjs/hooks/useGeoData/index.d.ts +0 -1
- package/dist/cjs/hooks/useGeoData/useGeoData.d.ts +0 -9
- package/dist/cjs/index.d.ts +0 -2
- package/dist/cjs/types/classes/geoImage/GeoImage.d.ts +0 -38
- package/dist/cjs/types/classes/geoImage/index.d.ts +0 -1
- package/dist/cjs/types/classes/geoImage/interface.d.ts +0 -32
- package/dist/cjs/types/classes/index.d.ts +0 -1
- package/dist/cjs/types/hooks/index.d.ts +0 -1
- package/dist/cjs/types/hooks/useGeoData/index.d.ts +0 -1
- package/dist/cjs/types/hooks/useGeoData/useGeoData.d.ts +0 -9
- package/dist/esm/classes/geoImage/GeoImage.d.ts +0 -38
- package/dist/esm/classes/geoImage/index.d.ts +0 -1
- package/dist/esm/classes/geoImage/interface.d.ts +0 -32
- package/dist/esm/classes/index.d.ts +0 -1
- package/dist/esm/hooks/index.d.ts +0 -1
- package/dist/esm/hooks/useGeoData/index.d.ts +0 -1
- package/dist/esm/hooks/useGeoData/useGeoData.d.ts +0 -9
- package/dist/esm/index.d.ts +0 -2
- package/dist/esm/types/classes/geoImage/GeoImage.d.ts +0 -38
- package/dist/esm/types/classes/geoImage/index.d.ts +0 -1
- package/dist/esm/types/classes/geoImage/interface.d.ts +0 -32
- package/dist/esm/types/classes/index.d.ts +0 -1
- package/dist/esm/types/hooks/index.d.ts +0 -1
- package/dist/esm/types/hooks/useGeoData/index.d.ts +0 -1
- package/dist/esm/types/hooks/useGeoData/useGeoData.d.ts +0 -9
- package/dist/index.d.ts +0 -80
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# COGTILES
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
#### A Javascript library for easier tile functionality with COG files
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
Class allows using CogTiff files with Deck.gl TileLayer.
|
|
12
|
+
|
|
13
|
+
Provides extra functionality for CogTiff files
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
## Methods
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
`async initializeCog(url : string)`- returns a cog from a given URL and initializes object variables
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
`async getTile(x: number, y: number, z: number)` - returns a promise, when resolved returns a tile. `x, y, z ` are coordinates in tile grid
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
`getTileSize(CogTiff)` - returns `number` containing the width of a single tile
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
`getZoomRange(cog:CogTiff)` - returns` [minZoom, maxZoom]`
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
`getBoundsAsLatLon(cog:CogTiff)` - returns image bounds as `[left, bottom, right, top]`
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
`getOriginAsLatLon(cog:CogTiff)`- returns origin of COG as `[x, y]`
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
`getImageTileIndex(img:CogTiffImage)` - returns index of an image from COG in tile system as`[x, y, z]`
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
`getResolutionFromZoomLevel`- returns `number` in meters per pixel
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
`getZoomLevelFromResolution(tileSize: number, resolution: number)` returns `number` zoom level calculated from `resolution` (the size of one pixel in meters) and `tileSize` (width of a tile)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
`getLatLon` - returns `[latitude, longitude]` from offset in meters
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
/* eslint 'max-len': [1, { code: 100, comments: 999, ignoreStrings: true, ignoreUrls: true }] */
|
|
2
|
+
// COG loading
|
|
3
|
+
import { CogTiff, CogTiffImage } from '@cogeotiff/core';
|
|
4
|
+
import { SourceUrl } from '@chunkd/source-url';
|
|
5
|
+
|
|
6
|
+
// Image compression support
|
|
7
|
+
import { inflate } from 'pako';
|
|
8
|
+
import jpeg from 'jpeg-js';
|
|
9
|
+
import { worldToLngLat } from '@math.gl/web-mercator';
|
|
10
|
+
import LZWDecoder from './lzw';
|
|
11
|
+
|
|
12
|
+
// Bitmap styling
|
|
13
|
+
import GeoImage, { GeoImageOptions } from '../geoimage/geoimage'; // TODO: remove absolute path
|
|
14
|
+
|
|
15
|
+
const EARTH_CIRCUMFERENCE = 40075000.0;
|
|
16
|
+
const EARTH_HALF_CIRCUMFERENCE = 20037500.0;
|
|
17
|
+
|
|
18
|
+
class CogTiles {
|
|
19
|
+
cog: CogTiff;
|
|
20
|
+
|
|
21
|
+
zoomRange = [0, 0];
|
|
22
|
+
|
|
23
|
+
tileSize: number;
|
|
24
|
+
|
|
25
|
+
lowestOriginTileOffset = [0, 0];
|
|
26
|
+
|
|
27
|
+
lowestOriginTileSize = 0;
|
|
28
|
+
|
|
29
|
+
loaded: boolean = false;
|
|
30
|
+
|
|
31
|
+
geo: GeoImage = new GeoImage();
|
|
32
|
+
|
|
33
|
+
lzw: LZWDecoder = new LZWDecoder();
|
|
34
|
+
|
|
35
|
+
options: GeoImageOptions;
|
|
36
|
+
|
|
37
|
+
constructor(options: GeoImageOptions) {
|
|
38
|
+
this.options = options;
|
|
39
|
+
|
|
40
|
+
// this.testCog()
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async initializeCog(url: string) {
|
|
44
|
+
// Set native fetch instead node-fetch to SourceUrl
|
|
45
|
+
SourceUrl.fetch = async (input, init) => {
|
|
46
|
+
const res = await fetch(input, init);
|
|
47
|
+
return res;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const sourceUrl = new SourceUrl(url);
|
|
51
|
+
this.cog = await CogTiff.create(sourceUrl);
|
|
52
|
+
|
|
53
|
+
this.cog.images.forEach((image:CogTiffImage) => {
|
|
54
|
+
image.loadGeoTiffTags();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
/*
|
|
58
|
+
console.log("---- START OF COG INFO DUMP ----")
|
|
59
|
+
this.cog.images[0].tags.forEach((tag) => {
|
|
60
|
+
//console.log(tag.value.name)
|
|
61
|
+
console.log(tag.name + ":")
|
|
62
|
+
console.log(tag.value)
|
|
63
|
+
})
|
|
64
|
+
console.log("---- END OF COG INFO DUMP ----")
|
|
65
|
+
*/
|
|
66
|
+
// console.log(this.cog)
|
|
67
|
+
|
|
68
|
+
this.tileSize = this.getTileSize(this.cog);
|
|
69
|
+
|
|
70
|
+
this.lowestOriginTileOffset = this.getImageTileIndex(
|
|
71
|
+
this.cog.images[this.cog.images.length - 1],
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
this.zoomRange = this.getZoomRange(this.cog);
|
|
75
|
+
|
|
76
|
+
return this.cog;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
getTileSize(cog: CogTiff) {
|
|
80
|
+
return cog.images[cog.images.length - 1].tileSize.width;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
getZoomRange(cog: CogTiff) {
|
|
84
|
+
const img = cog.images[cog.images.length - 1];
|
|
85
|
+
|
|
86
|
+
const minZoom = this.getZoomLevelFromResolution(
|
|
87
|
+
cog.images[cog.images.length - 1].tileSize.width,
|
|
88
|
+
img.resolution[0],
|
|
89
|
+
);
|
|
90
|
+
const maxZoom = minZoom + (cog.images.length - 1);
|
|
91
|
+
|
|
92
|
+
return [minZoom, maxZoom];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
getBoundsAsLatLon(cog: CogTiff) {
|
|
96
|
+
const { bbox } = cog.images[cog.images.length - 1];
|
|
97
|
+
|
|
98
|
+
// console.log(bbox)
|
|
99
|
+
|
|
100
|
+
const minX = Math.min(bbox[0], bbox[2]);
|
|
101
|
+
const maxX = Math.max(bbox[0], bbox[2]);
|
|
102
|
+
const minY = Math.min(bbox[1], bbox[3]);
|
|
103
|
+
const maxY = Math.max(bbox[1], bbox[3]);
|
|
104
|
+
|
|
105
|
+
const minXYDeg = this.getLatLon([minX, minY]);
|
|
106
|
+
const maxXYDeg = this.getLatLon([maxX, maxY]);
|
|
107
|
+
|
|
108
|
+
return [minXYDeg[0], minXYDeg[1], maxXYDeg[0], maxXYDeg[1]] as [number, number, number, number];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
getOriginAsLatLon(cog: CogTiff) {
|
|
112
|
+
const { origin } = cog.images[cog.images.length - 1];
|
|
113
|
+
return this.getLatLon(origin);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
getImageTileIndex(img: CogTiffImage) {
|
|
117
|
+
const ax = EARTH_HALF_CIRCUMFERENCE + img.origin[0];
|
|
118
|
+
const ay = -(EARTH_HALF_CIRCUMFERENCE + (img.origin[1] - EARTH_CIRCUMFERENCE));
|
|
119
|
+
// let mpt = img.resolution[0] * img.tileSize.width;
|
|
120
|
+
|
|
121
|
+
const mpt = img.tileSize.width * this.getResolutionFromZoomLevel(
|
|
122
|
+
img.tileSize.width,
|
|
123
|
+
this.getZoomLevelFromResolution(
|
|
124
|
+
img.tileSize.width,
|
|
125
|
+
img.resolution[0],
|
|
126
|
+
),
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
const ox = Math.round(ax / mpt);
|
|
130
|
+
const oy = Math.round(ay / mpt);
|
|
131
|
+
|
|
132
|
+
const oz = this.getZoomLevelFromResolution(img.tileSize.width, img.resolution[0]);
|
|
133
|
+
|
|
134
|
+
return [ox, oy, oz];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
getResolutionFromZoomLevel(tileSize: number, z: number) {
|
|
138
|
+
return (EARTH_CIRCUMFERENCE / tileSize) / (2 ** z);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
getZoomLevelFromResolution(tileSize: number, resolution: number) {
|
|
142
|
+
return Math.round(Math.log2(EARTH_CIRCUMFERENCE / (resolution * tileSize)));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
getLatLon(input: number[]) {
|
|
146
|
+
const ax = EARTH_HALF_CIRCUMFERENCE + input[0];
|
|
147
|
+
const ay = -(EARTH_HALF_CIRCUMFERENCE + (input[1] - EARTH_CIRCUMFERENCE));
|
|
148
|
+
|
|
149
|
+
const cartesianPosition = [
|
|
150
|
+
ax * (512 / EARTH_CIRCUMFERENCE),
|
|
151
|
+
ay * (512 / EARTH_CIRCUMFERENCE),
|
|
152
|
+
];
|
|
153
|
+
const cartographicPosition = worldToLngLat(cartesianPosition);
|
|
154
|
+
const cartographicPositionAdjusted = [cartographicPosition[0], -cartographicPosition[1]];
|
|
155
|
+
|
|
156
|
+
return cartographicPositionAdjusted;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async getTile(x: number, y: number, z: number) {
|
|
160
|
+
const wantedMpp = this.getResolutionFromZoomLevel(this.tileSize, z);
|
|
161
|
+
const img = this.cog.getImageByResolution(wantedMpp);
|
|
162
|
+
// await img.loadGeoTiffTags(1)
|
|
163
|
+
let offset: number[] = [0, 0];
|
|
164
|
+
|
|
165
|
+
if (z === this.zoomRange[0]) {
|
|
166
|
+
offset = this.lowestOriginTileOffset;
|
|
167
|
+
} else {
|
|
168
|
+
const power = 2 ** (z - this.zoomRange[0]);
|
|
169
|
+
offset[0] = Math.floor(this.lowestOriginTileOffset[0] * power);
|
|
170
|
+
offset[1] = Math.floor(this.lowestOriginTileOffset[1] * power);
|
|
171
|
+
}
|
|
172
|
+
const tilesX = img.tileCount.x;
|
|
173
|
+
const tilesY = img.tileCount.y;
|
|
174
|
+
// console.log("------OFFSET IS------ " + offset[0] + " ; " + offset[1])
|
|
175
|
+
|
|
176
|
+
const ox = offset[0];
|
|
177
|
+
const oy = offset[1];
|
|
178
|
+
|
|
179
|
+
// console.log("Asking for " + Math.floor(x - ox) + " : " + Math.floor(y - oy))
|
|
180
|
+
|
|
181
|
+
let decompressed: string;
|
|
182
|
+
let decoded: any;
|
|
183
|
+
|
|
184
|
+
this.options.numOfChannels = Number(img.tags.get(277).value);
|
|
185
|
+
this.options.noDataValue = this.getNoDataValue(img.tags);
|
|
186
|
+
|
|
187
|
+
if (!this.options.format) {
|
|
188
|
+
// More information about TIFF tags: https://www.awaresystems.be/imaging/tiff/tifftags.html
|
|
189
|
+
this.options.format = this.getFormat(
|
|
190
|
+
img.tags.get(339).value as Array<number>,
|
|
191
|
+
img.tags.get(258).value as Array<number>,
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
let bitsPerSample = img.tags.get(258)!.value;
|
|
196
|
+
if (Array.isArray(bitsPerSample)) {
|
|
197
|
+
if (this.options.type === 'terrain') {
|
|
198
|
+
let c = 0;
|
|
199
|
+
bitsPerSample.forEach((sample) => {
|
|
200
|
+
c += sample;
|
|
201
|
+
});
|
|
202
|
+
bitsPerSample = c;
|
|
203
|
+
} else {
|
|
204
|
+
[bitsPerSample] = bitsPerSample;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// const samplesPerPixel = img.tags.get(277)!.value
|
|
209
|
+
// console.log("Samples per pixel:" + samplesPerPixel)
|
|
210
|
+
// console.log("Bits per sample: " + bitsPerSample)
|
|
211
|
+
// console.log("Single channel pixel format: " + bitsPerSample/)
|
|
212
|
+
|
|
213
|
+
if (x - ox >= 0 && y - oy >= 0 && x - ox < tilesX && y - oy < tilesY) {
|
|
214
|
+
// console.log("getting tile: " + [x - ox, y - oy]);
|
|
215
|
+
const tile = await img.getTile((x - ox), (y - oy));
|
|
216
|
+
// console.time("Request to data time: ")
|
|
217
|
+
|
|
218
|
+
switch (img.compression) {
|
|
219
|
+
case 'image/jpeg':
|
|
220
|
+
decoded = jpeg.decode(tile!.bytes, { useTArray: true });
|
|
221
|
+
break;
|
|
222
|
+
case 'application/deflate':
|
|
223
|
+
decoded = await inflate(tile!.bytes);
|
|
224
|
+
break;
|
|
225
|
+
case 'application/lzw':
|
|
226
|
+
decoded = this.lzw.decodeBlock(tile!.bytes.buffer);
|
|
227
|
+
break;
|
|
228
|
+
default:
|
|
229
|
+
console.warn(`Unexpected compression method: ${img.compression}`);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
let decompressedFormatted;
|
|
233
|
+
// bitsPerSample = 8
|
|
234
|
+
|
|
235
|
+
switch (this.options.format) {
|
|
236
|
+
case 'uint8':
|
|
237
|
+
decompressedFormatted = new Uint8Array(decoded.buffer); break;
|
|
238
|
+
case 'uint16':
|
|
239
|
+
decompressedFormatted = new Uint16Array(decoded.buffer); break;
|
|
240
|
+
case 'uint32':
|
|
241
|
+
decompressedFormatted = new Uint32Array(decoded.buffer); break;
|
|
242
|
+
case 'int8':
|
|
243
|
+
decompressedFormatted = new Int8Array(decoded.buffer); break;
|
|
244
|
+
case 'int16':
|
|
245
|
+
decompressedFormatted = new Int16Array(decoded.buffer); break;
|
|
246
|
+
case 'int32':
|
|
247
|
+
decompressedFormatted = new Int32Array(decoded.buffer); break;
|
|
248
|
+
case 'float32':
|
|
249
|
+
decompressedFormatted = new Float32Array(decoded.buffer); break;
|
|
250
|
+
case 'float64':
|
|
251
|
+
decompressedFormatted = new Float64Array(decoded.buffer); break;
|
|
252
|
+
default: decompressedFormatted = null;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// console.log(decompressedFormatted)
|
|
256
|
+
|
|
257
|
+
decompressed = await this.geo.getMap({
|
|
258
|
+
rasters: [decompressedFormatted],
|
|
259
|
+
width: this.tileSize,
|
|
260
|
+
height: this.tileSize,
|
|
261
|
+
}, this.options);
|
|
262
|
+
|
|
263
|
+
// console.log(decompressed.length)
|
|
264
|
+
|
|
265
|
+
return decompressed;
|
|
266
|
+
}
|
|
267
|
+
return null;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
getFormat(sampleFormat: number[]|number, bitsPerSample:number[]|number) {
|
|
271
|
+
// TO DO: what if there are different channels formats
|
|
272
|
+
let uniqueSampleFormat = sampleFormat;
|
|
273
|
+
let uniqueBitsPerSample = bitsPerSample;
|
|
274
|
+
if (Array.isArray(sampleFormat)) { [uniqueSampleFormat] = sampleFormat; }
|
|
275
|
+
if (Array.isArray(bitsPerSample)) { [uniqueBitsPerSample] = bitsPerSample; }
|
|
276
|
+
|
|
277
|
+
let dataType;
|
|
278
|
+
switch (uniqueSampleFormat) {
|
|
279
|
+
case 1: // Unsigned integer
|
|
280
|
+
switch (uniqueBitsPerSample) {
|
|
281
|
+
case 8: dataType = 'uint8'; break;
|
|
282
|
+
case 16: dataType = 'uint16'; break;
|
|
283
|
+
case 32: dataType = 'uint32'; break;
|
|
284
|
+
default: dataType = null;
|
|
285
|
+
}
|
|
286
|
+
break;
|
|
287
|
+
case 2: // Signed integer
|
|
288
|
+
switch (uniqueBitsPerSample) {
|
|
289
|
+
case 8: dataType = 'int8'; break;
|
|
290
|
+
case 16: dataType = 'int16'; break;
|
|
291
|
+
case 32: dataType = 'int32'; break;
|
|
292
|
+
default: dataType = null;
|
|
293
|
+
}
|
|
294
|
+
break;
|
|
295
|
+
case 3: // Floating point
|
|
296
|
+
switch (uniqueBitsPerSample) {
|
|
297
|
+
case 32: dataType = 'float32'; break;
|
|
298
|
+
case 64: dataType = 'float64'; break;
|
|
299
|
+
default: dataType = null;
|
|
300
|
+
}
|
|
301
|
+
break;
|
|
302
|
+
default:
|
|
303
|
+
throw new Error('Unknown data format.');
|
|
304
|
+
}
|
|
305
|
+
// console.log('Data type is: ', dataType)
|
|
306
|
+
return dataType;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
getNoDataValue(tags) {
|
|
310
|
+
if (tags.has(42113)) {
|
|
311
|
+
const noDataValue = tags.get(42113).value;
|
|
312
|
+
if (typeof noDataValue === 'string' || noDataValue instanceof String) {
|
|
313
|
+
const parsedValue = noDataValue.replace(/[\0\s]/g, '');
|
|
314
|
+
return Number(parsedValue);
|
|
315
|
+
}
|
|
316
|
+
return Number.isNaN(Number(noDataValue)) ? undefined : Number(noDataValue);
|
|
317
|
+
}
|
|
318
|
+
return undefined;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
async testCog() {
|
|
322
|
+
const url = 'https://gisat-gis.eu-central-1.linodeobjects.com/eman/versions/v2/Quadrants/Q3_Bolivia_ASTER_2002_RGB_COG_LZW.tif';
|
|
323
|
+
this.options = {
|
|
324
|
+
type: 'image', multiplier: 1.0, useChannel: 1, alpha: 180, clipLow: 1, clipHigh: Number.MAX_VALUE,
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
const c = await this.initializeCog(url);
|
|
328
|
+
const middleImage = c.images[Math.floor(c.images.length / 2)];
|
|
329
|
+
|
|
330
|
+
console.log(middleImage);
|
|
331
|
+
|
|
332
|
+
const imageTileIndex = this.getImageTileIndex(middleImage);
|
|
333
|
+
|
|
334
|
+
console.log(imageTileIndex);
|
|
335
|
+
|
|
336
|
+
const x = Math.floor(middleImage.tileCount.x / 2);
|
|
337
|
+
const y = Math.floor(middleImage.tileCount.y / 2);
|
|
338
|
+
|
|
339
|
+
console.log(c.getTile(x, y, Math.floor(c.images.length / 2)));
|
|
340
|
+
|
|
341
|
+
const tileGlobalX = x + imageTileIndex[0];
|
|
342
|
+
const tileGlobalY = y + imageTileIndex[1];
|
|
343
|
+
const tileGlobalZ = imageTileIndex[2];
|
|
344
|
+
|
|
345
|
+
const tile = await this.getTile(tileGlobalX, tileGlobalY, tileGlobalZ);
|
|
346
|
+
|
|
347
|
+
if (tile === false) {
|
|
348
|
+
console.log("couldn't retrieve tile");
|
|
349
|
+
} else {
|
|
350
|
+
console.log(tile);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
export default CogTiles;
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/* eslint-disable no-mixed-operators */
|
|
2
|
+
/* eslint-disable max-len */
|
|
3
|
+
/* eslint-disable no-bitwise */
|
|
4
|
+
/* eslint-disable no-param-reassign */
|
|
5
|
+
/* eslint-disable no-plusplus */
|
|
6
|
+
/* eslint-disable max-classes-per-file */
|
|
7
|
+
/* eslint 'max-len': [1, { code: 100, comments: 999, ignoreStrings: true, ignoreUrls: true }] */
|
|
8
|
+
|
|
9
|
+
// LITERALLY STOLEN CODE FROM GITHUB
|
|
10
|
+
// NOT MY RESPONSIBILITY
|
|
11
|
+
|
|
12
|
+
function decodeRowAcc(row, stride) {
|
|
13
|
+
let length = row.length - stride;
|
|
14
|
+
let offset = 0;
|
|
15
|
+
do {
|
|
16
|
+
for (let i = stride; i > 0; i--) {
|
|
17
|
+
row[offset + stride] += row[offset];
|
|
18
|
+
offset++;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
length -= stride;
|
|
22
|
+
} while (length > 0);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function decodeRowFloatingPoint(row, stride, bytesPerSample) {
|
|
26
|
+
let index = 0;
|
|
27
|
+
let count = row.length;
|
|
28
|
+
const wc = count / bytesPerSample;
|
|
29
|
+
|
|
30
|
+
while (count > stride) {
|
|
31
|
+
for (let i = stride; i > 0; --i) {
|
|
32
|
+
row[index + stride] += row[index];
|
|
33
|
+
++index;
|
|
34
|
+
}
|
|
35
|
+
count -= stride;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const copy = row.slice();
|
|
39
|
+
for (let i = 0; i < wc; ++i) {
|
|
40
|
+
for (let b = 0; b < bytesPerSample; ++b) {
|
|
41
|
+
row[(bytesPerSample * i) + b] = copy[((bytesPerSample - b - 1) * wc) + i];
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function applyPredictor(
|
|
47
|
+
block,
|
|
48
|
+
predictor,
|
|
49
|
+
width,
|
|
50
|
+
height,
|
|
51
|
+
bitsPerSample,
|
|
52
|
+
planarConfiguration,
|
|
53
|
+
) {
|
|
54
|
+
if (!predictor || predictor === 1) {
|
|
55
|
+
return block;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
for (let i = 0; i < bitsPerSample.length; ++i) {
|
|
59
|
+
if (bitsPerSample[i] % 8 !== 0) {
|
|
60
|
+
throw new Error('When decoding with predictor, only multiple of 8 bits are supported.');
|
|
61
|
+
}
|
|
62
|
+
if (bitsPerSample[i] !== bitsPerSample[0]) {
|
|
63
|
+
throw new Error('When decoding with predictor, all samples must have the same size.');
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const bytesPerSample = bitsPerSample[0] / 8;
|
|
68
|
+
const stride = planarConfiguration === 2 ? 1 : bitsPerSample.length;
|
|
69
|
+
|
|
70
|
+
for (let i = 0; i < height; ++i) {
|
|
71
|
+
// Last strip will be truncated if height % stripHeight != 0
|
|
72
|
+
if (i * stride * width * bytesPerSample >= block.byteLength) {
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
let row;
|
|
76
|
+
if (predictor === 2) { // horizontal prediction
|
|
77
|
+
switch (bitsPerSample[0]) {
|
|
78
|
+
case 8:
|
|
79
|
+
row = new Uint8Array(block, i * stride * width * bytesPerSample, stride * width * bytesPerSample);
|
|
80
|
+
break;
|
|
81
|
+
case 16:
|
|
82
|
+
row = new Uint16Array(block, i * stride * width * bytesPerSample, stride * width * bytesPerSample / 2);
|
|
83
|
+
break;
|
|
84
|
+
case 32:
|
|
85
|
+
row = new Uint32Array(block, i * stride * width * bytesPerSample, stride * width * bytesPerSample / 4);
|
|
86
|
+
break;
|
|
87
|
+
default:
|
|
88
|
+
throw new Error(`Predictor 2 not allowed with ${bitsPerSample[0]} bits per sample.`);
|
|
89
|
+
}
|
|
90
|
+
decodeRowAcc(row, stride, bytesPerSample);
|
|
91
|
+
} else if (predictor === 3) { // horizontal floating point
|
|
92
|
+
row = new Uint8Array(block, i * stride * width * bytesPerSample, stride * width * bytesPerSample);
|
|
93
|
+
decodeRowFloatingPoint(row, stride, bytesPerSample);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return block;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
class BaseDecoder {
|
|
100
|
+
async decode(fileDirectory, buffer) {
|
|
101
|
+
const decoded = await this.decodeBlock(buffer);
|
|
102
|
+
const predictor = fileDirectory.Predictor || 1;
|
|
103
|
+
if (predictor !== 1) {
|
|
104
|
+
const isTiled = !fileDirectory.StripOffsets;
|
|
105
|
+
const tileWidth = isTiled ? fileDirectory.TileWidth : fileDirectory.ImageWidth;
|
|
106
|
+
const tileHeight = isTiled
|
|
107
|
+
? fileDirectory.TileLength
|
|
108
|
+
: (
|
|
109
|
+
fileDirectory.RowsPerStrip || fileDirectory.ImageLength
|
|
110
|
+
);
|
|
111
|
+
return applyPredictor(
|
|
112
|
+
decoded,
|
|
113
|
+
predictor,
|
|
114
|
+
tileWidth,
|
|
115
|
+
tileHeight,
|
|
116
|
+
fileDirectory.BitsPerSample,
|
|
117
|
+
fileDirectory.PlanarConfiguration,
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
return decoded;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const MIN_BITS = 9;
|
|
125
|
+
const CLEAR_CODE = 256; // clear code
|
|
126
|
+
const EOI_CODE = 257; // end of information
|
|
127
|
+
const MAX_BYTELENGTH = 12;
|
|
128
|
+
|
|
129
|
+
function getByte(array, position, length) {
|
|
130
|
+
const d = position % 8;
|
|
131
|
+
const a = Math.floor(position / 8);
|
|
132
|
+
const de = 8 - d;
|
|
133
|
+
const ef = (position + length) - ((a + 1) * 8);
|
|
134
|
+
let fg = (8 * (a + 2)) - (position + length);
|
|
135
|
+
const dg = ((a + 2) * 8) - position;
|
|
136
|
+
fg = Math.max(0, fg);
|
|
137
|
+
if (a >= array.length) {
|
|
138
|
+
console.warn('ran off the end of the buffer before finding EOI_CODE (end on input code)');
|
|
139
|
+
return EOI_CODE;
|
|
140
|
+
}
|
|
141
|
+
let chunk1 = array[a] & ((2 ** (8 - d)) - 1);
|
|
142
|
+
chunk1 <<= (length - de);
|
|
143
|
+
let chunks = chunk1;
|
|
144
|
+
if (a + 1 < array.length) {
|
|
145
|
+
let chunk2 = array[a + 1] >>> fg;
|
|
146
|
+
chunk2 <<= Math.max(0, (length - dg));
|
|
147
|
+
chunks += chunk2;
|
|
148
|
+
}
|
|
149
|
+
if (ef > 8 && a + 2 < array.length) {
|
|
150
|
+
const hi = ((a + 3) * 8) - (position + length);
|
|
151
|
+
const chunk3 = array[a + 2] >>> hi;
|
|
152
|
+
chunks += chunk3;
|
|
153
|
+
}
|
|
154
|
+
return chunks;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function appendReversed(dest, source) {
|
|
158
|
+
for (let i = source.length - 1; i >= 0; i--) {
|
|
159
|
+
dest.push(source[i]);
|
|
160
|
+
}
|
|
161
|
+
return dest;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function decompress(input) {
|
|
165
|
+
const dictionaryIndex = new Uint16Array(4093);
|
|
166
|
+
const dictionaryChar = new Uint8Array(4093);
|
|
167
|
+
for (let i = 0; i <= 257; i++) {
|
|
168
|
+
dictionaryIndex[i] = 4096;
|
|
169
|
+
dictionaryChar[i] = i;
|
|
170
|
+
}
|
|
171
|
+
let dictionaryLength = 258;
|
|
172
|
+
let byteLength = MIN_BITS;
|
|
173
|
+
let position = 0;
|
|
174
|
+
|
|
175
|
+
function initDictionary() {
|
|
176
|
+
dictionaryLength = 258;
|
|
177
|
+
byteLength = MIN_BITS;
|
|
178
|
+
}
|
|
179
|
+
function getNext(array) {
|
|
180
|
+
const byte = getByte(array, position, byteLength);
|
|
181
|
+
position += byteLength;
|
|
182
|
+
return byte;
|
|
183
|
+
}
|
|
184
|
+
function addToDictionary(i, c) {
|
|
185
|
+
dictionaryChar[dictionaryLength] = c;
|
|
186
|
+
dictionaryIndex[dictionaryLength] = i;
|
|
187
|
+
dictionaryLength++;
|
|
188
|
+
return dictionaryLength - 1;
|
|
189
|
+
}
|
|
190
|
+
function getDictionaryReversed(n) {
|
|
191
|
+
const rev = [];
|
|
192
|
+
for (let i = n; i !== 4096; i = dictionaryIndex[i]) {
|
|
193
|
+
rev.push(dictionaryChar[i]);
|
|
194
|
+
}
|
|
195
|
+
return rev;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const result = [];
|
|
199
|
+
initDictionary();
|
|
200
|
+
const array = new Uint8Array(input);
|
|
201
|
+
let code = getNext(array);
|
|
202
|
+
let oldCode;
|
|
203
|
+
while (code !== EOI_CODE) {
|
|
204
|
+
if (code === CLEAR_CODE) {
|
|
205
|
+
initDictionary();
|
|
206
|
+
code = getNext(array);
|
|
207
|
+
while (code === CLEAR_CODE) {
|
|
208
|
+
code = getNext(array);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (code === EOI_CODE) {
|
|
212
|
+
break;
|
|
213
|
+
} else if (code > CLEAR_CODE) {
|
|
214
|
+
throw new Error(`corrupted code at scanline ${code}`);
|
|
215
|
+
} else {
|
|
216
|
+
const val = getDictionaryReversed(code);
|
|
217
|
+
appendReversed(result, val);
|
|
218
|
+
oldCode = code;
|
|
219
|
+
}
|
|
220
|
+
} else if (code < dictionaryLength) {
|
|
221
|
+
const val = getDictionaryReversed(code);
|
|
222
|
+
appendReversed(result, val);
|
|
223
|
+
addToDictionary(oldCode, val[val.length - 1]);
|
|
224
|
+
oldCode = code;
|
|
225
|
+
} else {
|
|
226
|
+
const oldVal = getDictionaryReversed(oldCode);
|
|
227
|
+
if (!oldVal) {
|
|
228
|
+
throw new Error(
|
|
229
|
+
`Bogus entry. Not in dictionary,
|
|
230
|
+
${oldCode} / ${dictionaryLength},
|
|
231
|
+
position: ${position}`,
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
appendReversed(result, oldVal);
|
|
235
|
+
result.push(oldVal[oldVal.length - 1]);
|
|
236
|
+
addToDictionary(oldCode, oldVal[oldVal.length - 1]);
|
|
237
|
+
oldCode = code;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (dictionaryLength + 1 >= (2 ** byteLength)) {
|
|
241
|
+
if (byteLength === MAX_BYTELENGTH) {
|
|
242
|
+
oldCode = undefined;
|
|
243
|
+
} else {
|
|
244
|
+
byteLength++;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
code = getNext(array);
|
|
248
|
+
}
|
|
249
|
+
return new Uint8Array(result);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export default class LZWDecoder extends BaseDecoder {
|
|
253
|
+
decodeBlock(buffer) {
|
|
254
|
+
return decompress(buffer, false).buffer;
|
|
255
|
+
}
|
|
256
|
+
}
|