Import_3ds: Improved distance cue chunk import
[blender-addons.git] / io_import_dxf / transverse_mercator.py
blob8071c7756f68b283ccc8570df536a00e8cd3b48c
1 # SPDX-FileCopyrightText: 2014-2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 from math import sin, cos, atan, atanh, radians, tan, sinh, asin, cosh, degrees
7 # see conversion formulas at
8 # http://en.wikipedia.org/wiki/Transverse_Mercator_projection
9 # http://mathworld.wolfram.com/MercatorProjection.html
12 class TransverseMercator:
13 radius = 6378137
15 def __init__(self, lat=0, lon=0):
16 self.lat = lat # in degrees
17 self.lon = lon # in degrees
18 self.lat_rad = radians(self.lat)
19 self.lon_rad = radians(self.lon)
21 def fromGeographic(self, lat, lon):
22 lat_rad = radians(lat)
23 lon_rad = radians(lon)
24 B = cos(lat_rad) * sin(lon_rad - self.lon_rad)
25 x = self.radius * atanh(B)
26 y = self.radius * (atan(tan(lat_rad) / cos(lon_rad - self.lon_rad)) - self.lat_rad)
27 return x, y
29 def toGeographic(self, x, y):
30 x /= self.radius
31 y /= self.radius
32 D = y + self.lat_rad
33 lon = atan(sinh(x) / cos(D))
34 lat = asin(sin(D) / cosh(x))
36 lon = self.lon + degrees(lon)
37 lat = degrees(lat)
38 return lat, lon