sphericalmercator

Transformations between the Web Mercator projection and Latitude Longitude coordinates

node-sphericalmercator provides projection math for converting between mercator meters, screen pixels (of 256x256 or configurable-size tiles), and latitude/longitude.

Compatible with nodejs packages and in-browser.

npm install sphericalmercator

Datatypes are assumed to be arrays: lon, lat is [lon, lat], and x, y is [x, y].

// By default, precomputes up to z30 
var merc = new SphericalMercator({
    size: 256
});

Convert lon, lat to screen pixel x, y from 0, 0 origin, at a certain zoom level. The inverse of ll

Convert screen pixel value to lon, lat, at a certain zoom level. The inverse of px

Convert tile xyz value to bbox of the form [w, s, e, n]

  • x {Number} x (longitude) number.
  • y {Number} y (latitude) number.
  • zoom {Number} zoom.
  • tms_style {Boolean} whether to compute using tms-style. (optional, default false)
  • srs {String} projection for resulting bbox (WGS84|900913). (optional, default 4326)

Returns bbox array of values in form [w, s, e, n].

Convert bbox to xyx bounds

  • bbox {Number} bbox in the form [w, s, e, n].
  • zoom {Number} zoom.
  • tms_style {Boolean} whether to compute using tms-style. (optional, default false)
  • srs {String} projection of input bbox (WGS84|900913). (optional, default 4326)

Returns {Object} XYZ bounds containing minX, maxX, minY, maxY properties.

Convert lon, lat values to mercator x, y

Convert mercator x, y values to lon, lat

  • mercantile provides similar utilities for projection and tile math in Python