Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): correct loading of sub array geotags #1214

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
11 changes: 9 additions & 2 deletions packages/core/src/__benchmark__/source.file.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { readFile, stat } from 'fs/promises';
import { readFile, stat } from 'node:fs/promises';
import { gunzip } from 'node:zlib';
import { promisify } from 'node:util';
import { Source } from '../source.js';

const gunzipP = promisify(gunzip);

export class TestFileSource implements Source {
url: URL;
data: Promise<Buffer>;

constructor(fileName: URL) {
this.url = fileName;
this.data = readFile(this.url);
this.data = readFile(this.url).then((buf) => {
if (this.url.pathname.endsWith('gz')) return gunzipP(buf);
return buf;
});
}

async fetch(offset: number, length: number): Promise<ArrayBuffer> {
Expand Down
33 changes: 33 additions & 0 deletions packages/core/src/__test__/cog.read.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,37 @@ describe('CogRead', () => {
assert.equal(im.tagsGeo.get(TiffTagGeo.GeogCitationGeoKey), 'NZGD2000');
assert.deepEqual(await im.fetch(TiffTag.StripByteCounts), [8064, 8064, 8064, 8064, 8064, 8064, 8064, 5040]);
});

it('should read sub array ifds', async () => {
const source = new TestFileSource(
new URL('../../data/east_coast_phase3_2023_AY31_1000_3335.tif.gz', import.meta.url),
);
const tiff = await CogTiff.create(source);

assert.equal(tiff.images.length, 5);
const im = tiff.images[0];

assert.equal(im.isGeoTagsLoaded, true);
assert.equal(im.epsg, 2193);
assert.equal(im.compression, TiffMimeType.Lzw);

const geoTags = [...im.tagsGeo.keys()].map((key) => TiffTagGeo[key]);
assert.deepEqual(geoTags, [
'GTModelTypeGeoKey',
'GTRasterTypeGeoKey',
'GTCitationGeoKey',
'GeographicTypeGeoKey',
'GeogAngularUnitsGeoKey',
'GeogEllipsoidGeoKey',
'GeogSemiMajorAxisGeoKey',
'GeogSemiMinorAxisGeoKey',
'GeogInvFlatteningGeoKey',
'GeogTOWGS84GeoKey',
'ProjectedCSTypeGeoKey',
'PCSCitationGeoKey',
'ProjLinearUnitsGeoKey',
]);

assert.deepEqual(im.tagsGeo.get(TiffTagGeo.GeogTOWGS84GeoKey), [0, 0, 0, 0, 0, 0, 0]);
});
});
4 changes: 4 additions & 0 deletions packages/core/src/cog.tiff.image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export class CogTiffImage {
if (sourceTag.value == null) return;
const geoTags = sourceTag.value as Uint16Array;
if (typeof geoTags === 'number') throw new Error('Invalid geo tags found');

for (let i = 4; i <= geoTags[3] * 4; i += 4) {
const key = geoTags[i] as TiffTagGeo;
const locationTagId = geoTags[i + 1];
Expand All @@ -149,6 +150,9 @@ export class CogTiffImage {
const count = geoTags[i + 2];
if (typeof tag.value === 'string') {
this.tagsGeo.set(key, tag.value.slice(offset, offset + count - 1).trim());
} else if (Array.isArray(tag.value)) {
if (count === 1) this.tagsGeo.set(key, tag.value[offset]);
else this.tagsGeo.set(key, tag.value.slice(offset, offset + count) as any);
} else {
throw new Error('Failed to extract GeoTiffTags');
}
Expand Down
Loading