geotiff.js

repository·master·Indexed 21 days ago

https://github.com/geotiffjs/geotiff.js

A JavaScript library for reading geospatial metadata and raw array data from a wide variety of (Geo)TIFF file types. It supports parsing from remote sources via fetch/XHR, local ArrayBuffers, and the filesystem in both browser and Node.js environments. The library provides low-level access to metadata and raster data, including support for various compressions, data types, and BigTIFF, with capabilities to read rasters, convert visual data to RGB, and write uncompressed GeoTIFFs to ArrayBuffers.

Tokens
10.6K
Snippets
40
Records
50
Agent score
76%

What's inside geotiff.js

  1. Overview of geotiff.js

    master
    geotiff.js is a library for reading (geospatial) metadata and raw array data from a wide variety of (Geo)TIFF file types. It supports parsing from remote sources (via fetch or XHR), local ArrayBuffers, and the filesystem (via FileReader in browsers or filesystem functions in Node.js).
  2. Understand BigTIFF support limitations

    master

    geotiff.js provides limited support for the BigTIFF format due to JavaScript's handling of 64-bit integers. Users should be aware of the following behaviors:

    • Precision: 64-bit integers are read as two 32-bit integers and combined. Because JavaScript numbers are typically 64-bit floats, inaccuracies may occur for extremely large values.
    • Data Types: For 64-bit integer arrays, the library uses the default Array type. This may cause issues with certain compression algorithms if these arrays are used for pixel values.
  3. How automatic image selection and overviews work

    master

    When working with GeoTIFF objects (rather than GeoTIFFImage objects), you can call readRasters() directly on the tiff object. This enables automatic selection of the best image based on your requirements:

    • Resolution-based: If you provide width, height, resX, or resY, the library selects the best-fitting image (e.g., an overview).
    • Geographic-based: If you provide a bbox instead of a window, the library uses geographic coordinates to select the appropriate image/overview.
    // Uses geographic bbox and selects best fitting overview based on resolution
    const data = await tiff.readRasters({
      bbox: [10.34, 57.28, 13.34, 60.23],
      resX: 0.1,
      resY: 0.1
    });
  4. Migrate from geotiff.js v2 to v3

    master

    Version 3.0 introduces deferred tag reading for performance. While high-level APIs like getImage() and readRasters() remain compatible, several low-level access patterns have changed:

    GeoKeys Access

    • v2: image.geoKeys
    • v3: image.getGeoKeys() (Async)

    Async Metadata Methods

    • getTiePoints() and getGDALMetadata() are now asynchronous and must be awaited.

    ImageFileDirectory Access

    Direct property access on fileDirectory is replaced by the ImageFileDirectory class:

    • Synchronous access: Use image.fileDirectory.getValue('TagName').
    • Asynchronous/Deferred access: Use await image.fileDirectory.loadValue('TagName') for large arrays or deferred tags.
    • Checking existence: Use image.fileDirectory.hasTag('TagName') instead of checking for truthiness on the property.
    • Indexed access: Use await image.fileDirectory.loadValueIndexed('TagName', index) for specific elements in large arrays (e.g., TileOffsets).

    Custom Decoders

    Decoders now receive parameters during construction. Use addDecoder to register them with a parameter extraction function that can handle deferred values.

  5. Install and import GeoTIFF

    master

    GeoTIFF.js can be used in Node.js environments via require or import, or directly in the browser using a <script> tag. The library exposes factory functions like fromUrl, fromUrls, fromArrayBuffer, and fromBlob to create GeoTIFF objects.

    // Node.js CommonJS
    const GeoTIFF = require('geotiff');
    const { fromUrl, fromUrls, fromArrayBuffer, fromBlob } = GeoTIFF;
    
    // ES Modules
    import GeoTIFF, { fromUrl, fromUrls, fromArrayBuffer, fromBlob } from 'geotiff';
    
    // Browser CDN
    // <script src="https://cdn.jsdelivr.net/npm/geotiff"></script>
    // console.log(GeoTIFF);
  6. Run tests and setup test data

    master

    To run the library's test suite, you must first set up the test data. This requires GDAL and ImageMagick to be installed on your system.

    1. Install Dependencies

    Ubuntu (using ubuntugis-unstable):

    sudo add-apt-repository -y ppa:ubuntugis/ubuntugis-unstable
    sudo apt-get update
    sudo apt-get install -y gdal-bin imagemagick

    MacOS (using Homebrew):

    brew install wget gdal imagemagick

    2. Setup Test Data

    Once the tools are installed, run the setup script located in the test/data directory:

    cd test/data
    sh setup_data.sh
    cd -

    3. Execute Tests

    Run the test suite using npm:

    npm test
  7. Perform in-browser testing

    master

    To run tests directly in a browser environment, start the development server and navigate to the provided local URL.

    ```bash
    npm run dev

    After running the command, navigate to http://localhost:8090/test/ in your browser.

  8. Setup the geotiff.js development environment

    master

    To set up the repository for development, clone the repository and install the dependencies using npm.

    # clone repo
    git clone https://github.com/constantinius/geotiff.js.git
    cd geotiff.js/
    
    # install development dependencies
    npm install