Skip to content

Commit 6168256

Browse files
committed
added support when gdal metadata present but doesn't include min and max
1 parent 1757d45 commit 6168256

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

Diff for: index.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ function sum(array, start, end) {
1111
return s;
1212
}
1313

14-
async function getStats(image){
14+
async function getStats(image, debug){
1515

1616
const fd = image.fileDirectory;
17+
if (debug) console.log("fd:", fd);
1718
const numBands = fd.SamplesPerPixel;
19+
if (debug) console.log("numBands:", numBands);
1820

1921
const tileWidth = image.getTileWidth();
2022
const tileHeight = image.getTileHeight();
@@ -42,6 +44,8 @@ async function getStats(image){
4244
for (let bandIndex = 0; bandIndex < numBands; bandIndex++) {
4345
let min = undefined;
4446
let max = undefined;
47+
48+
// try to get min and max via GDAL Metadata
4549
if (fd.GDAL_METADATA) {
4650
const string = fd.GDAL_METADATA;
4751
const xmlDom = parseXml(string.substring(0, string.length - 1));
@@ -58,7 +62,9 @@ async function getStats(image){
5862
}
5963
}
6064
}
61-
} else {
65+
}
66+
67+
if (min === undefined || max === undefined) {
6268
for (let rowIndex = 0; rowIndex < numTilesPerCol; rowIndex++) {
6369
for (let colIndex = 0; colIndex < numTilesPerRow; colIndex++) {
6470
const reader = sampleReaders[bandIndex];

Diff for: test/test.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ const { getStats } = require('../index.js');
55

66
const SECONDS_TO_MILLISECONDS = 1000;
77

8-
async function getStatsFromFilepath(filepath) {
8+
async function getStatsFromFilepath(filepath, debug=false) {
99
const data = readFileSync(filepath);
1010
const arrayBuffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
1111
const geotiff = await fromArrayBuffer(arrayBuffer);
1212
const image = await geotiff.getImage();
13-
return await getStats(image, true);
13+
return await getStats(image, debug);
1414
}
1515

1616
describe("GeoTIFF.js Test Data", function() {
@@ -29,6 +29,15 @@ describe("GeoTIFF.js Test Data", function() {
2929
const { bands } = await getStatsFromFilepath('./test/data/GeogToWGS84GeoKey5.tif');
3030
expect(bands[0].min).to.equal(0);
3131
expect(bands[0].max).to.equal(2);
32+
});
33+
it('RGB GeoTIFF that has GDAL Metadata without Stats', async function() {
34+
const { bands } = await getStatsFromFilepath('./test/data/rgb_raster.tif');
35+
expect(bands[0].min).to.equal(0);
36+
expect(bands[0].max).to.equal(182);
37+
expect(bands[1].min).to.equal(0);
38+
expect(bands[1].max).to.equal(255);
39+
expect(bands[2].min).to.equal(0);
40+
expect(bands[2].max).to.equal(255);
3241
})
3342
});
3443

0 commit comments

Comments
 (0)