geotiff.js - GeoTIFF Image Decoding

3.0.5 · active · verified Sun Apr 19

geotiff.js is a robust JavaScript library designed for decoding GeoTIFF and TIFF images in both browser and Node.js environments. It enables developers to read and parse these file formats, extract raw raster data across various data types and compressions (including Packbits, LZW, Deflate, JPEG, LERC, Zstandard, and WebP), and access rich geospatial metadata like GeoKeys and TIFF tags. The current stable version is 3.0.5, with ongoing active development and regular patch releases, alongside beta versions (e.g., 3.1.0-beta.0) that introduce new features like multi-stripped and tiled writing support. Key differentiators include its pure JavaScript implementation, configurable worker pools for efficient decoding, and selective reading of file portions to optimize bandwidth, making it an essential tool for web-based GIS applications that require client-side processing of georeferenced imagery.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to asynchronously load a GeoTIFF from a URL, access its primary image, read raster data from its bands, and retrieve geospatial metadata like GeoKeys and the bounding box.

import { fromUrl, GeoTIFFImage } from 'geotiff';

async function processGeoTIFF() {
  try {
    // Example URL for a small GeoTIFF file (e.g., a Cloud Optimized GeoTIFF).
    // In a real application, ensure the URL is accessible and CORS-friendly.
    const url = 'https://geotiffjs.github.io/geotiff.js/test/data/example.tif';

    console.log(`Attempting to open GeoTIFF from URL: ${url}`);
    const tiff = await fromUrl(url);

    // Get the first image (most common case). GeoTIFFs can contain multiple images.
    const image: GeoTIFFImage = await tiff.getImage();
    console.log(`Opened image: Width = ${image.getWidth()}, Height = ${image.getHeight()}`);

    // Read raster data for the first band. The 'readRasters' method returns an array of TypedArrays.
    // 'interleave: false' returns separate arrays for each band.
    const rasters = await image.readRasters({ interleave: false });
    console.log(`Read raster data. Number of bands: ${rasters.length}`);
    if (rasters.length > 0 && rasters[0].length > 0) {
      console.log(`First pixel value (band 0): ${rasters[0][0]}`);
    }

    // Access image metadata, such as GeoKeys or other TIFF tags.
    const geoKeys = image.getGeoKeys();
    console.log('GeoKeys (if present):', geoKeys);

    // Get the bounding box of the image in its projected coordinates.
    const boundingBox = image.getBoundingBox();
    console.log('Bounding Box [minX, minY, maxX, maxY]:', boundingBox);

    console.log('Successfully processed GeoTIFF.');
  } catch (error) {
    console.error('Error processing GeoTIFF:', error);
  }
}

processGeoTIFF();

view raw JSON →