Installed leaflet in extlib
[mediagoblin.git] / extlib / leaflet / src / geo / projection / Projection.Mercator.js
1
2 L.Projection.Mercator = {
3 MAX_LATITUDE: 85.0840591556,
4
5 R_MINOR: 6356752.3142,
6 R_MAJOR: 6378137,
7
8 project: function(/*LatLng*/ latlng) /*-> Point*/ {
9 var d = L.LatLng.DEG_TO_RAD,
10 max = this.MAX_LATITUDE,
11 lat = Math.max(Math.min(max, latlng.lat), -max),
12 r = this.R_MAJOR,
13 x = latlng.lng * d * r,
14 y = lat * d,
15 tmp = this.R_MINOR / r,
16 eccent = Math.sqrt(1.0 - tmp * tmp),
17 con = eccent * Math.sin(y);
18
19 con = Math.pow((1 - con)/(1 + con), eccent * 0.5);
20
21 var ts = Math.tan(0.5 * ((Math.PI * 0.5) - y)) / con;
22 y = -r * Math.log(ts);
23
24 return new L.Point(x, y);
25 },
26
27 unproject: function(/*Point*/ point, /*Boolean*/ unbounded) /*-> LatLng*/ {
28 var d = L.LatLng.RAD_TO_DEG,
29 r = this.R_MAJOR,
30 lng = point.x * d / r,
31 tmp = this.R_MINOR / r,
32 eccent = Math.sqrt(1 - (tmp * tmp)),
33 ts = Math.exp(- point.y / r),
34 phi = Math.PI/2 - 2 * Math.atan(ts),
35 numIter = 15,
36 tol = 1e-7,
37 i = numIter,
38 dphi = 0.1,
39 con;
40
41 while ((Math.abs(dphi) > tol) && (--i > 0)) {
42 con = eccent * Math.sin(phi);
43 dphi = Math.PI/2 - 2 * Math.atan(ts * Math.pow((1.0 - con)/(1.0 + con), 0.5 * eccent)) - phi;
44 phi += dphi;
45 }
46
47 return new L.LatLng(phi * d, lng, unbounded);
48 }
49 };