generated from linz/template-javascript-hello-world
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: validate band information is consistent (#1017)
#### Motivation When tiffs are retiled they need to have consistent banding information or GDAL will not be able to join them together easily #### Modification When a retile is requested validate that all tiffs in the retile have the same consistent band information. #### Checklist _If not applicable, provide explanation of why._ - [ ] Tests updated - [ ] Docs updated - [ ] Issue linked in Title
- Loading branch information
Showing
5 changed files
with
141 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import assert from 'node:assert'; | ||
import { describe, it } from 'node:test'; | ||
|
||
import { createTiff } from '../../commands/common.js'; | ||
import { extractBandInformation } from '../band.js'; | ||
|
||
describe('extractBandInformation', () => { | ||
it('should extract basic band information (8-bit)', async () => { | ||
const testTiff = await createTiff('./src/commands/tileindex-validate/__test__/data/8b.tiff'); | ||
const bands = await extractBandInformation(testTiff); | ||
assert.equal(bands.join(','), 'uint8,uint8,uint8'); | ||
}); | ||
|
||
it('should extract basic band information (16-bit)', async () => { | ||
const testTiff = await createTiff('./src/commands/tileindex-validate/__test__/data/16b.tiff'); | ||
const bands = await extractBandInformation(testTiff); | ||
assert.equal(bands.join(','), 'uint16,uint16,uint16'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { SampleFormat, Tiff, TiffImage, TiffTag } from '@cogeotiff/core'; | ||
|
||
function getDataType(i: SampleFormat): string { | ||
switch (i) { | ||
case SampleFormat.Uint: | ||
return 'uint'; | ||
case SampleFormat.Int: | ||
return 'int'; | ||
case SampleFormat.Float: | ||
return 'float'; | ||
case SampleFormat.Void: | ||
return 'void'; | ||
case SampleFormat.ComplexFloat: | ||
return 'cfloat'; | ||
case SampleFormat.ComplexInt: | ||
return 'cint'; | ||
default: | ||
return 'unknown'; | ||
} | ||
} | ||
/** | ||
* Load the band information from a tiff and return it as a array of human friendly names | ||
* | ||
* @example | ||
* `[uint16, uint16, uint16]` 3 band uint16 | ||
* | ||
* @param tiff Tiff to extract band information from | ||
* @returns list of band information | ||
* @throws {Error} if cannot extract band information | ||
*/ | ||
export async function extractBandInformation(tiff: Tiff): Promise<string[]> { | ||
const firstImage = tiff.images[0] as TiffImage; | ||
|
||
const [dataType, bitsPerSample] = await Promise.all([ | ||
/** firstImage.fetch(TiffTag.Photometric), **/ // TODO enable RGB detection | ||
firstImage.fetch(TiffTag.SampleFormat), | ||
firstImage.fetch(TiffTag.BitsPerSample), | ||
]); | ||
|
||
if (bitsPerSample == null) { | ||
throw new Error(`Failed to extract band information from: ${tiff.source.url.href}`); | ||
} | ||
|
||
if (dataType && dataType.length !== bitsPerSample.length) { | ||
throw new Error(`Datatype and bits per sample miss match: ${tiff.source.url.href}`); | ||
} | ||
|
||
const imageBands: string[] = []; | ||
for (let i = 0; i < bitsPerSample.length; i++) { | ||
const type = getDataType(dataType ? (dataType[i] as SampleFormat) : SampleFormat.Uint); | ||
const bits = bitsPerSample[i]; | ||
imageBands.push(`${type}${bits}`); | ||
} | ||
|
||
return imageBands; | ||
} |