| 1 | import {atan, exp, halfPi, log, pi, tan, tau} from "../math.js";
|
|---|
| 2 | import rotation from "../rotation.js";
|
|---|
| 3 | import projection from "./index.js";
|
|---|
| 4 |
|
|---|
| 5 | export function mercatorRaw(lambda, phi) {
|
|---|
| 6 | return [lambda, log(tan((halfPi + phi) / 2))];
|
|---|
| 7 | }
|
|---|
| 8 |
|
|---|
| 9 | mercatorRaw.invert = function(x, y) {
|
|---|
| 10 | return [x, 2 * atan(exp(y)) - halfPi];
|
|---|
| 11 | };
|
|---|
| 12 |
|
|---|
| 13 | export default function() {
|
|---|
| 14 | return mercatorProjection(mercatorRaw)
|
|---|
| 15 | .scale(961 / tau);
|
|---|
| 16 | }
|
|---|
| 17 |
|
|---|
| 18 | export function mercatorProjection(project) {
|
|---|
| 19 | var m = projection(project),
|
|---|
| 20 | center = m.center,
|
|---|
| 21 | scale = m.scale,
|
|---|
| 22 | translate = m.translate,
|
|---|
| 23 | clipExtent = m.clipExtent,
|
|---|
| 24 | x0 = null, y0, x1, y1; // clip extent
|
|---|
| 25 |
|
|---|
| 26 | m.scale = function(_) {
|
|---|
| 27 | return arguments.length ? (scale(_), reclip()) : scale();
|
|---|
| 28 | };
|
|---|
| 29 |
|
|---|
| 30 | m.translate = function(_) {
|
|---|
| 31 | return arguments.length ? (translate(_), reclip()) : translate();
|
|---|
| 32 | };
|
|---|
| 33 |
|
|---|
| 34 | m.center = function(_) {
|
|---|
| 35 | return arguments.length ? (center(_), reclip()) : center();
|
|---|
| 36 | };
|
|---|
| 37 |
|
|---|
| 38 | m.clipExtent = function(_) {
|
|---|
| 39 | return arguments.length ? ((_ == null ? x0 = y0 = x1 = y1 = null : (x0 = +_[0][0], y0 = +_[0][1], x1 = +_[1][0], y1 = +_[1][1])), reclip()) : x0 == null ? null : [[x0, y0], [x1, y1]];
|
|---|
| 40 | };
|
|---|
| 41 |
|
|---|
| 42 | function reclip() {
|
|---|
| 43 | var k = pi * scale(),
|
|---|
| 44 | t = m(rotation(m.rotate()).invert([0, 0]));
|
|---|
| 45 | return clipExtent(x0 == null
|
|---|
| 46 | ? [[t[0] - k, t[1] - k], [t[0] + k, t[1] + k]] : project === mercatorRaw
|
|---|
| 47 | ? [[Math.max(t[0] - k, x0), y0], [Math.min(t[0] + k, x1), y1]]
|
|---|
| 48 | : [[x0, Math.max(t[1] - k, y0)], [x1, Math.min(t[1] + k, y1)]]);
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | return reclip();
|
|---|
| 52 | }
|
|---|