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
878 stars 183 forks source link

Issue of calling readRasters() #316

Open ukalpa opened 2 years ago

ukalpa commented 2 years ago

I am trying to parse Geotiff data from SRTM (https://srtm.csi.cgiar.org/wp-content/uploads/files/srtm_5x5/TIFF/srtm_21_06.zip), I can get tiff and image object following the guide however it pops the below error message when calling readRasters, I appreciate for any suggestions.

Source Code:

const tiff = await GeoTIFF.fromUrl("srtm_21_06.tif");
const image = await tiff.getImage();
console.log(
    "GeoTIFF",
    image.getWidth(),
    image.getHeight(),
    image.getTileWidth(),
    image.getResolution(),
    image.getBoundingBox()
  );
const data = await image.readRasters();

Error Info: Screenshot from 2022-06-26 22-40-48

Screenshot from 2022-06-26 22-42-58

Screenshot from 2022-06-26 22-42-09

wukong-c commented 2 years ago

Have you solved this problem? I also met

nitenzhao commented 2 years ago

I met the problem too

nadavpld commented 2 years ago

+1

asukachiharu commented 2 years ago

+1

anton-seaice commented 1 year ago

Hi all,

I had a similar problem:

Failed to read from url or blob tracker_data/sea_ice_conc_6km_cog/asi-AMSR2-s6250-20230208-v5.4.tif TypeError: Cannot read properties of undefined (reading 'offset')
    at blockedsource.js:271:29
    at Array.map (<anonymous>)
    at BlockedSource.readSliceData (blockedsource.js:259:19)
    at BlockedSource.fetch (blockedsource.js:160:17)
    at async GeoTIFFImage.getTileOrStrip (geotiffimage.js:385:20)
    at async Promise.all (:3000/index 110)
    at async GeoTIFFImage._readRaster (geotiffimage.js:517:5)
    at async GeoTIFFImage.readRasters (geotiffimage.js:611:20)

I patched this problem for me by subtracting one from the high block id at line 262:

diff --git a/node_modules/geotiff/dist-module/source/blockedsource.js b/node_modules/geotiff/dist-module/source/blockedsource.js
index 5630efb..8099b16 100644
--- a/node_modules/geotiff/dist-module/source/blockedsource.js
+++ b/node_modules/geotiff/dist-module/source/blockedsource.js
@@ -262,7 +262,7 @@ export class BlockedSource extends BaseSource {
         top = Math.min(this.fileSize, top);
       }
       const blockIdLow = Math.floor(slice.offset / this.blockSize);
-      const blockIdHigh = Math.floor(top / this.blockSize);
+      const blockIdHigh = Math.floor((top-1) / this.blockSize);
       const sliceData = new ArrayBuffer(slice.length);
       const sliceView = new Uint8Array(sliceData);

I think when the requested slice was an exact number of blocks, it would give this error because it was trying to request one more block than needed.

Problematic file is at https://data.seaice.uni-bremen.de/amsr2/asi_daygrid_swath/s6250/2023/feb/Antarctic/asi-AMSR2-s6250-20230208-v5.4.tif

ericyoungberg commented 1 year ago

+1