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
845 stars 175 forks source link

Writing .TIF file and re-opening it fails with `Invalid byte order value` error #383

Closed ThomasAribart closed 11 months ago

ThomasAribart commented 11 months ago

Hello and thanks for this awesome lib' 🙌

I'm encountering an error when writing + reading a .tif file with the writeArrayBuffer and fromArrayBuffer utils:

import { readFileSync, writeFileSync } from 'fs';
import { fromArrayBuffer, writeArrayBuffer } from 'geotiff';

// Example from docs
const values = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const metadata = {
  height: 3,
  width: 3,
};

const arrayBuffer = (await writeArrayBuffer(
  values,
  metadata,
)) as ArrayBuffer;

writeFileSync(
  './foo.tif',
  Buffer.from(arrayBuffer),
);

const { buffer } = readFileSync('./foo.tif');
// 👇 ❌ Fails with 'Invalid byte order value.' error
const tiff = await fromArrayBuffer(buffer);

When logging the two buffers, they indeed seem different.

What am I doing wrong ?

ThomasAribart commented 11 months ago

Solving this as I managed to make it work with fromFile instead of readFileSync + fromArrayBuffer