geotiffjs / geotiff.js

geotiff.js is a small library to parse TIFF files for visualization or analysis. It is written in pure JavaScript, and is usable in both the browser and node.js applications.
https://geotiffjs.github.io/
MIT License
859 stars 179 forks source link

`blockSize === null` is correct? #371

Open fornaeffe opened 1 year ago

fornaeffe commented 1 year ago

I'm totally newbye, I found an unexpected behavior of geotiff.js, and I'm writing here just in case it is a true bug and not just some error I did in writing my code.

I was trying to fetch a GeoTIFF from WCS:

import { fromUrl  } from "geotiff";

const CRS = 'EPSG:32632'
const x = 580239
const y = 4917120
const width = 120
const height = 100
const mPerPixel = 20

let WCSurl = 'https://tinitaly.pi.ingv.it/TINItaly_1_1/wcs?' +
    'SERVICE=WCS' +
    '&VERSION=1.0.0' +
    '&REQUEST=GetCoverage' +
    '&FORMAT=GeoTIFF' +
    '&COVERAGE=TINItaly_1_1:tinitaly_dem' +
    '&BBOX=' + [(x - width / 2 * mPerPixel),(y - height / 2 * mPerPixel),(x + width / 2 * mPerPixel),(y + height / 2 * mPerPixel)].join(',') +
    '&CRS=' + CRS +
    '&RESPONSE_CRS=' + CRS +
    '&WIDTH='+ width +
    '&HEIGHT=' + height

async function loadDEM() {
    const myGeoTIFF = await fromUrl(WCSurl)
    const myGeoTIFFImage = await myGeoTIFF.getImage()
    const myRaster = await myGeoTIFFImage.readRasters()
    console.log(myRaster)
}

But I got an error:

Uncaught (in promise) AggregateError: Request failed at BlockedSource.fetch (blockedsource.js:153:1) at async GeoTIFF.fromSource (geotiff.js:547:1)

Trying to debug, I noticed that in the function maybeWrapInBlockedSource (remote.js line 153), blockSize is undefined (correctly, I think, because I did not specified blocksize), but when the function compares it with null (blockSize === null) it returns false, and the function returns a new BlockedSource instead of the source itself.

When the server returns a full file instead of a block, the subsequent functions throw errors.

I resolved by adding the option allowFullFile = true, but I think it's unintuitive to have to add it in such a case... Maybe the test blockSize === null should have returned true even if blockSize is undefined? (something like blockSize == null?)

async function loadDEM() {
    const myGeoTIFF = await fromUrl(WCSurl, {allowFullFile: true})
    const myGeoTIFFImage = await myGeoTIFF.getImage()
    const myRaster = await myGeoTIFFImage.readRasters()
    console.log(myRaster)
}

Edit: I'm using GeoTIFF 2.0.7, webpack 5.88.1, typescript 5.1.6 and Chrome 114.0.5735.199