Skip to content

Commit c824b70

Browse files
committed
added another test
1 parent dd2ae81 commit c824b70

File tree

3 files changed

+33
-26
lines changed

3 files changed

+33
-26
lines changed

Diff for: index.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,22 @@ async function getStats(image){
6868
const decoder = getDecoder(fd);
6969
const tile = await image.getTileOrStrip(colIndex, rowIndex, bandIndex, decoder);
7070
const dataView = new DataView(tile.data);
71+
const { byteLength } = dataView;
7172
for (let y = 0; y < tileHeight; y++) {
7273
for (let x = 0; x < tileWidth; x++) {
7374
const pixelOffset = ((y * tileWidth) + x) * bytesPerPixel;
74-
const value = reader.call(
75-
dataView, pixelOffset + srcSampleOffsets[bandIndex], image.littleEndian,
76-
);
77-
if (value != noDataValue && !isNaN(value)) {
78-
if (typeof min === 'undefined' || value < min) min = value;
79-
else if (typeof max === 'undefined' || value > max) max = value;
75+
76+
const byteOffset = pixelOffset + srcSampleOffsets[bandIndex];
77+
if (byteOffset >= byteLength) {
78+
/* we've reached the end of this row,
79+
so we can continue to the next row */
80+
continue;
81+
} else {
82+
const value = reader.call(dataView, byteOffset, image.littleEndian);
83+
if (value != noDataValue && !isNaN(value)) {
84+
if (typeof min === 'undefined' || value < min) min = value;
85+
else if (typeof max === 'undefined' || value > max) max = value;
86+
}
8087
}
8188
}
8289
}

Diff for: test/data/setup.sh

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# files used by georaster for testing
2+
wget https://geotiff-stats.s3.amazonaws.com/GeogToWGS84GeoKey5.tif -O GeogToWGS84GeoKey5.tif
3+
14
# files used by geotiff.js for testing
25
wget https://github.com/EOxServer/autotest/raw/f8d9f4bde6686abbda09c711d4bf5239f5378aa9/autotest/data/meris/MER_FRS_1P_reduced/ENVISAT-MER_FRS_1PNPDE20060816_090929_000001972050_00222_23322_0058_uint16_reduced_compressed.tif -O initial.tiff
36

Diff for: test/test.js

+17-20
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,45 @@ const { getStats } = require('../index.js');
55

66
const SECONDS_TO_MILLISECONDS = 1000;
77

8-
describe("GeoTIFF.js Test Data", function() {
9-
this.timeout(50 * SECONDS_TO_MILLISECONDS);
10-
it('GeoTIFF without Statistics', async function() {
11-
const data = readFileSync('./test/data/initial.tiff');
8+
async function getStatsFromFilepath(filepath) {
9+
const data = readFileSync(filepath);
1210
const arrayBuffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
1311
const geotiff = await fromArrayBuffer(arrayBuffer);
1412
const image = await geotiff.getImage();
15-
const { bands } = await getStats(image, true);
13+
return await getStats(image, true);
14+
}
15+
16+
describe("GeoTIFF.js Test Data", function() {
17+
this.timeout(50 * SECONDS_TO_MILLISECONDS);
18+
it('GeoTIFF without Statistics', async function() {
19+
const { bands } = await getStatsFromFilepath('./test/data/initial.tiff');
1620
expect(bands[0].min).to.equal(0);
1721
expect(bands[0].max).to.equal(65507);
1822
});
1923
it('GeoTIFF with Statistics', async function() {
20-
const data = readFileSync('./test/data/stats.tiff');
21-
const arrayBuffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
22-
const geotiff = await fromArrayBuffer(arrayBuffer);
23-
const image = await geotiff.getImage();
24-
const { bands } = await getStats(image, true);
24+
const { bands } = await getStatsFromFilepath('./test/data/initial.tiff');
2525
expect(bands[0].min).to.equal(0);
2626
expect(bands[0].max).to.equal(65507);
2727
});
28+
it('GeoTIFF with Color Palette', async function() {
29+
const { bands } = await getStatsFromFilepath('./test/data/GeogToWGS84GeoKey5.tif');
30+
expect(bands[0].min).to.equal(0);
31+
expect(bands[0].max).to.equal(2);
32+
})
2833
});
2934

3035
describe("Landsat Data", function() {
3136
this.timeout(5 * SECONDS_TO_MILLISECONDS);
3237
it('should get stats for Landsat Scene', async function() {
33-
const data = readFileSync('./test/data/LC80120312013106LGN01_B6.tif');
34-
const arrayBuffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
35-
const geotiff = await fromArrayBuffer(arrayBuffer);
36-
const image = await geotiff.getImage();
37-
const { bands } = await getStats(image, true);
38+
const { bands } = await getStatsFromFilepath('./test/data/LC80120312013106LGN01_B6.tif');
3839
expect(bands[0].min).to.equal(0);
3940
expect(bands[0].max).to.equal(62196);
4041
});
4142
});
4243

4344
describe("GHSL Data", function() {
4445
it('should get stats for worldwide GHSL', async function() {
45-
const data = readFileSync('./test/data/GHS_POP_E2015_GLOBE_R2019A_54009_250_V1_0.tif');
46-
const arrayBuffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
47-
const geotiff = await fromArrayBuffer(arrayBuffer);
48-
const image = await geotiff.getImage();
49-
const { bands } = await getStats(image, true);
46+
const { bands } = await getStatsFromFilepath('./test/data/GHS_POP_E2015_GLOBE_R2019A_54009_250_V1_0.tif');
5047
expect(bands[0].min).to.equal(0);
5148
expect(bands[0].max).to.equal(442590.9375 );
5249
});

0 commit comments

Comments
 (0)