UNPKG

2.36 kBMarkdownView Raw
1[![Build Status](https://travis-ci.com/mapbox/sphericalmercator.svg?branch=master)](http://travis-ci.com/mapbox/sphericalmercator)
2
3`sphericalmercator` provides projection math for converting between
4mercator meters, screen pixels (of 256x256 or configurable-size tiles), and
5latitude/longitude.
6
7Compatible with nodejs packages and in-browser.
8
9## Installation
10
11`npm install @mapbox/sphericalmercator`
12
13## API
14
15Some datatypes are assumed to be arrays: `ll` is `[lon, lat]`, `xy` and `px` are
16`[x, y]`.
17
18```javascript
19// By default, precomputes up to z30
20var merc = new SphericalMercator({
21 size: 256,
22 antimeridian: true
23});
24```
25
26### `px(ll, zoom)`
27
28Convert lon, lat to screen pixel x, y from 0, 0 origin, at a certain zoom level.
29The inverse of `ll`
30
31If `antimeridian: true` is passed on initialization of the `SphericalMercator` object, this method will support converting longitude values up to 360°.
32
33### `ll(px, zoom)`
34
35Convert screen pixel value to lon, lat, at a certain zoom level. The inverse
36of `px`
37
38### `bbox(x, y, zoom, tms_style, srs)`
39
40Convert tile xyz value to bbox of the form `[w, s, e, n]`
41
42* `x` {Number} x (longitude) number.
43* `y` {Number} y (latitude) number.
44* `zoom` {Number} zoom.
45* `tms_style` {Boolean} whether to compute using tms-style. (optional, default false)
46* `srs` {String} projection for resulting bbox (WGS84|900913). (optional, default WGS84)
47
48Returns bbox array of values in form `[w, s, e, n]`.
49
50### `xyz(bbox, zoom, tms_style, srs)`
51
52Convert bbox to xyz bounds
53
54* `bbox` {Number} bbox in the form `[w, s, e, n]`.
55* `zoom` {Number} zoom.
56* `tms_style` {Boolean} whether to compute using tms-style. (optional, default false)
57* `srs` {String} projection of input bbox (WGS84|900913). (optional, default WGS84)
58
59Returns {Object} XYZ bounds containing minX, maxX, minY, maxY properties.
60
61### `convert(bbox, to)`
62
63Convert bbox from 900913 to WGS84 or vice versa
64
65* `bbox` {Number} bbox in the form `[w, s, e, n]`.
66* `to` {String} projection of resulting bbox (WGS84|900913). (optional, default WGS84)
67
68Returns bbox array of values in form `[w, s, e, n]`.
69
70### `forward(ll)`
71
72Convert lon, lat values to mercator x, y
73
74### `inverse(xy)`
75
76Convert mercator x, y values to lon, lat
77
78## See Also
79
80* [mercantile](https://github.com/sgillies/mercantile) provides similar utilities for projection and tile math in Python