From fc21a30d6754f37923b92ee4fe26c557ff6d9378 Mon Sep 17 00:00:00 2001 From: Blayne Chard Date: Sun, 26 Nov 2023 17:33:12 +1300 Subject: [PATCH] feat: expose gdal's NO_DATA as a getter on the image (#1230) --- packages/core/src/__test__/cog.read.test.ts | 3 +++ packages/core/src/tiff.image.ts | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/packages/core/src/__test__/cog.read.test.ts b/packages/core/src/__test__/cog.read.test.ts index ac6ec337..e1c8a133 100644 --- a/packages/core/src/__test__/cog.read.test.ts +++ b/packages/core/src/__test__/cog.read.test.ts @@ -86,6 +86,9 @@ describe('CogRead', () => { assert.equal(im.value(TiffTag.SampleFormat), SampleFormat.Float); assert.equal(im.value(TiffTag.Photometric), Photometric.MinIsBlack); + assert.equal(im.value(TiffTag.GdalNoData), '-9999'); + assert.equal(im.noData, -9999); + assert.equal(im.tagsGeo.get(TiffTagGeo.GTCitationGeoKey), 'NZGD2000 / New Zealand Transverse Mercator 2000'); assert.equal(im.tagsGeo.get(TiffTagGeo.GeodeticCitationGeoKey), 'NZGD2000'); assert.deepEqual(await im.fetch(TiffTag.StripByteCounts), [8064, 8064, 8064, 8064, 8064, 8064, 8064, 5040]); diff --git a/packages/core/src/tiff.image.ts b/packages/core/src/tiff.image.ts index 67737744..3663d1fd 100644 --- a/packages/core/src/tiff.image.ts +++ b/packages/core/src/tiff.image.ts @@ -162,6 +162,19 @@ export class TiffImage { return this.tagsGeo.get(tag) as TiffTagGeoType[T]; } + /** + * Load and parse the GDAL_NODATA Tifftag + * + * @throws if the tag is not loaded + * @returns null if the tag does not exist + */ + get noData(): number | null { + const tag = this.tags.get(TiffTag.GdalNoData); + if (tag == null) return null; + if (tag.value) return Number(tag.value); + throw new Error('GdalNoData tag is not loaded'); + } + /** * Load and unpack the GeoKeyDirectory *