npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

wkb-raster

v1.1.0

Published

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

Downloads

18

Readme

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!