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
860 stars 180 forks source link

load entire file at once using fromURL #435

Closed AlexanderZeilmann closed 1 month ago

AlexanderZeilmann commented 1 month ago

Is there a way to load an entire file using fromURL?

Use case

I have a tiff-file with 550 pages, where each page is a 100x100 pixels image. I want to load the entire file and do something with all the data in the browser. When I use the following code, 65 HTTP requests are sent, which lead to a page load time of around 10 seconds

import { fromUrl } from 'geotiff';
const tiff = await fromUrl(
   'http://example.com/img.tif',
);
const depth = await tiff.getImageCount()

When I use the following code, only one request is sent and the page load time is down to 1 second.

import { fromBlob } from 'geotiff';
const tiff = await fetch(
   'http://example.com/img.tif',
)
.then((x) => x.blob())
.then((x) => fromBlob(x));
const depth = await tiff.getImageCount()

Am I missing a better way?

constantinius commented 1 month ago

Hi @AlexanderZeilmann

The fromUrl source method tries to be smart and only load the parts which are actually needed. Indeed, if you want to fetch the whole file, it is better with the second option you showed. As far as I know there is not really a better version to do it.

AlexanderZeilmann commented 1 month ago

Hey @constantinius thank you for the quick answer. Then I will use the second way :) This is a really nice library by the way, thank you :D