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

@mapbox/raster-tile-query

v1.0.0

Published

Raster tile query module

Downloads

8

Readme

Build Status raster-tile-query

:warning: This project is a proof of concept and not intended for production use. :warning:

If you are looking to just get pixel values from an image, we recommend using Node Mapnik and sphericalmercator directly instead of this library. This is explained below.

To query raster tiles, such as Mapbox Terrain RGB tiles, at particular longitudes and latitudes, you'll need to know the X, Y, Z, and pixel size of your image tile to be able to interpolate which relative pixel to get information from. XYZ info is generally found in the request URL of your tile like this:

https://api.mapbox.com/v4/mapbox.terrain-rgb/3/2/3.pngraw?access_token={your-api-token}
https://api.mapbox.com/v4/mapbox.terrain-rgb/{z}/{x}/{y}.pngraw?access_token={your-api-token}

The following main steps are taken to get relative pixel values:

  • convert your lng/lat coordinates into pixel coordinates for your individual image.
  • Determine the pixel location of your image with x,y,z and image pixel size.
  • then subtract the two steps above to get relative pixel location for your image. If your latitude and longitude do not line up with the image, they will either be negative in number, or greater than the image size in pixels.
  • Use an image reading library such as Node Mapnik or get-pixels to retrieve pixel data

Here is an example script using Node Mapnik

var fs = require('fs');
var mapnik = require('mapnik');
var SM = require('@mapbox/sphericalmercator');
var sm = new SM();

/**
 * Get the pixel values of an image from a longitude and latitude when proviiding the
 * image's zxy information.
 *
 * @param {Object} options
 * @param {Buffer} options.buffer - the buffer of the image to get pixel information from
 * @param {Number} options.imageSize - the size in pixels of the image. It must be square. Typical values are 256 or 512
 * @param {Number} options.lng - the longitude value
 * @param {Number} options.lat - the latitude value
 * @param {Number} options.x - the tile `x` coordinate
 * @param {Number} options.y - the tile `y` coordinate
 * @param {Number} options.z - the zoom level of the tile
 * @returns {Object} pixel information from a particular point on the image
 */
function getPixel(options) {
  // get tile x and y pixel locations
  var tileX = options.x * options.imageSize;
  var tileY = options.y * options.imageSize;

  // get pixel coordinates of the lat/lng values at specific zoom
  var pixelCoords = sm.px([options.lng, options.lat], options.z);

  // get relative pixel coordinates to the tile
  var x = pixelCoords[0] - tileX;
  var y = pixelCoords[1] - tileY;

  // generate new mapnik image object from the buffer
  var img = new mapnik.Image.fromBytesSync(options.buffer);

  // get pixel values at relative x/y positions for this image
  var values = img.getPixel(x, y, { get_color: true });

  return values;
}