GithubHelp home page GithubHelp logo

ihmeuw / wkb-raster Goto Github PK

View Code? Open in Web Editor NEW
5.0 5.0 0.0 32 KB

JavaScript parser for the Well-Known Binary (WKB) RASTER type used in PostGIS

License: BSD 3-Clause "New" or "Revised" License

TypeScript 100.00%

wkb-raster's Introduction

wkb-raster

JavaScript parser for the Well-Known Binary (WKB) RASTER type used in PostGIS

This small library ("Look, Ma, no runtime dependencies!") allows one to parse the binary format returned by the PostGIS raster function ST_AsBinary. According to the spec:

The WKB format for RASTER is meant for transport. Takes into account endiannes and avoids any padding. Still, beside padding and endiannes, it matches the internal serialized format (see RFC1), for quick input/output.

The parsed object contains metadata about the raster plus one or more raster bands, each with its own metadata. The pixel data itself constitutes the data property of a band. It's represented as a JavaScript TypedArray.

Installation

$ npm install wkb-raster

Example Usage

import parseRaster from 'wkb-raster';
import { Pool } from 'pg'

// Make a connection pool to query PostGIS.
const pool = new Pool();

async function getAndParseRaster() {
  // Retrieve some WKB RASTER data.
  const sql = 'SELECT ST_AsBinary(rast) AS rast FROM my_raster_table LIMIT 1';
  const { rows } = await pool.query(sql);

  // Retrieve the Node Buffer containing the WKB representation of the raster.
  const buffer = rows[0].rast;
  // It's likely Node used a giant Buffer with lots of other things in it to store our data,
  // so we make a copy of just the portion we need.
  const rawWKB = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);

  // Parse the raster as a JavaScript object.
  const raster = parseRaster(rawWKB);

  // Here's what it contains.
  const {
    metadata: {
      // all these properties represent numbers
      endianness, // 1: little-endian ; 2: big-endian
      version,    // format version
      nBands,     // number of bands
      scaleX,     // pixel width (in geographical units)
      scaleY,     // pixel height (in geographical units)
      ipX,        // X value of upper-left corner (in geographical units)
      ipY,        // Y value of upper-left corner (in geographical units)
      skewX,      // rotation about the Y-axis
      skewY,      // rotation about the X-axis
      srid,       // spatial reference ID
      width,      // number of pixel columns
      height,     // number of pixel rows
    },
    // destructuring just the first band
    bands: [{
      metadata: {
        // boolean properties
        isOffline,
        hasNodataValue,
        isNodataValue,
        // number of bytes in the band's `data` property
        byteLength,
        // represents the pixel type using a numeric ID (see the spec)
        pixtype,
        // More useful representation of the pixel type. One of:
        //   'Bool1', 'Uint2', 'Uint4', 'Int8', 'Uint8',
        //   'Int16', 'Uint16', 'Int32', 'Uint32', 'Float32', 'Float64'
        pixelType,
        // the nodata value, if defined
        nodata,
      },
      // Typed Array containing pixel data (type depends on the pixel type of the raster)
      data,
    }],
  } = raster;

  // ... Then do something wonderful with the raster!
}

Unimplemented

At present, filesystem (out-db) rasters are not supported. I'd like to include support for this, but I don't currently have a use for it, so my motivation to do so is low. PRs are welcome, however!

wkb-raster's People

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.