diff --git a/index.js b/index.js index e6821e3..b84668b 100644 --- a/index.js +++ b/index.js @@ -68,15 +68,22 @@ async function getStats(image){ const decoder = getDecoder(fd); const tile = await image.getTileOrStrip(colIndex, rowIndex, bandIndex, decoder); const dataView = new DataView(tile.data); + const { byteLength } = dataView; for (let y = 0; y < tileHeight; y++) { for (let x = 0; x < tileWidth; x++) { const pixelOffset = ((y * tileWidth) + x) * bytesPerPixel; - const value = reader.call( - dataView, pixelOffset + srcSampleOffsets[bandIndex], image.littleEndian, - ); - if (value != noDataValue && !isNaN(value)) { - if (typeof min === 'undefined' || value < min) min = value; - else if (typeof max === 'undefined' || value > max) max = value; + + const byteOffset = pixelOffset + srcSampleOffsets[bandIndex]; + if (byteOffset >= byteLength) { + /* we've reached the end of this row, + so we can continue to the next row */ + continue; + } else { + const value = reader.call(dataView, byteOffset, image.littleEndian); + if (value != noDataValue && !isNaN(value)) { + if (typeof min === 'undefined' || value < min) min = value; + else if (typeof max === 'undefined' || value > max) max = value; + } } } } diff --git a/test/data/setup.sh b/test/data/setup.sh index 57534c5..37e5023 100644 --- a/test/data/setup.sh +++ b/test/data/setup.sh @@ -1,3 +1,6 @@ +# files used by georaster for testing +wget https://geotiff-stats.s3.amazonaws.com/GeogToWGS84GeoKey5.tif -O GeogToWGS84GeoKey5.tif + # files used by geotiff.js for testing 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 diff --git a/test/test.js b/test/test.js index f81e983..498fd0e 100644 --- a/test/test.js +++ b/test/test.js @@ -5,36 +5,37 @@ const { getStats } = require('../index.js'); const SECONDS_TO_MILLISECONDS = 1000; -describe("GeoTIFF.js Test Data", function() { - this.timeout(50 * SECONDS_TO_MILLISECONDS); - it('GeoTIFF without Statistics', async function() { - const data = readFileSync('./test/data/initial.tiff'); +async function getStatsFromFilepath(filepath) { + const data = readFileSync(filepath); const arrayBuffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength); const geotiff = await fromArrayBuffer(arrayBuffer); const image = await geotiff.getImage(); - const { bands } = await getStats(image, true); + return await getStats(image, true); +} + +describe("GeoTIFF.js Test Data", function() { + this.timeout(50 * SECONDS_TO_MILLISECONDS); + it('GeoTIFF without Statistics', async function() { + const { bands } = await getStatsFromFilepath('./test/data/initial.tiff'); expect(bands[0].min).to.equal(0); expect(bands[0].max).to.equal(65507); }); it('GeoTIFF with Statistics', async function() { - const data = readFileSync('./test/data/stats.tiff'); - const arrayBuffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength); - const geotiff = await fromArrayBuffer(arrayBuffer); - const image = await geotiff.getImage(); - const { bands } = await getStats(image, true); + const { bands } = await getStatsFromFilepath('./test/data/initial.tiff'); expect(bands[0].min).to.equal(0); expect(bands[0].max).to.equal(65507); }); + it('GeoTIFF with Color Palette', async function() { + const { bands } = await getStatsFromFilepath('./test/data/GeogToWGS84GeoKey5.tif'); + expect(bands[0].min).to.equal(0); + expect(bands[0].max).to.equal(2); + }) }); describe("Landsat Data", function() { this.timeout(5 * SECONDS_TO_MILLISECONDS); it('should get stats for Landsat Scene', async function() { - const data = readFileSync('./test/data/LC80120312013106LGN01_B6.tif'); - const arrayBuffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength); - const geotiff = await fromArrayBuffer(arrayBuffer); - const image = await geotiff.getImage(); - const { bands } = await getStats(image, true); + const { bands } = await getStatsFromFilepath('./test/data/LC80120312013106LGN01_B6.tif'); expect(bands[0].min).to.equal(0); expect(bands[0].max).to.equal(62196); }); @@ -42,11 +43,7 @@ describe("Landsat Data", function() { describe("GHSL Data", function() { it('should get stats for worldwide GHSL', async function() { - const data = readFileSync('./test/data/GHS_POP_E2015_GLOBE_R2019A_54009_250_V1_0.tif'); - const arrayBuffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength); - const geotiff = await fromArrayBuffer(arrayBuffer); - const image = await geotiff.getImage(); - const { bands } = await getStats(image, true); + const { bands } = await getStatsFromFilepath('./test/data/GHS_POP_E2015_GLOBE_R2019A_54009_250_V1_0.tif'); expect(bands[0].min).to.equal(0); expect(bands[0].max).to.equal(442590.9375 ); });