1 # SPDX-License-Identifier: GPL-2.0-or-later
3 from math
import sin
, cos
, atan
, atanh
, radians
, tan
, sinh
, asin
, cosh
, degrees
5 # see conversion formulas at
6 # http://en.wikipedia.org/wiki/Transverse_Mercator_projection
7 # http://mathworld.wolfram.com/MercatorProjection.html
10 class TransverseMercator
:
13 def __init__(self
, lat
=0, lon
=0):
14 self
.lat
= lat
# in degrees
15 self
.lon
= lon
# in degrees
16 self
.lat_rad
= radians(self
.lat
)
17 self
.lon_rad
= radians(self
.lon
)
19 def fromGeographic(self
, lat
, lon
):
20 lat_rad
= radians(lat
)
21 lon_rad
= radians(lon
)
22 B
= cos(lat_rad
) * sin(lon_rad
- self
.lon_rad
)
23 x
= self
.radius
* atanh(B
)
24 y
= self
.radius
* (atan(tan(lat_rad
) / cos(lon_rad
- self
.lon_rad
)) - self
.lat_rad
)
27 def toGeographic(self
, x
, y
):
31 lon
= atan(sinh(x
) / cos(D
))
32 lat
= asin(sin(D
) / cosh(x
))
34 lon
= self
.lon
+ degrees(lon
)