1

2# -*- coding: utf-8 -*-

3

4 u'''A pure Python version of I{Karney}'s C{Exact Transverse Mercator} (ETM) projection.

5

6 Classes L{Etm}, L{ETMError} and L{ExactTransverseMercator}, transcoded from I{Karney}'s

7 C++ class U{TransverseMercatorExact<https://GeographicLib.SourceForge.io/C++/doc/

8 classGeographicLib_1_1TransverseMercatorExact.html>}, abbreviated as C{TMExact} below.

9

10 Class L{ExactTransverseMercator} provides C{Exact Transverse Mercator} projections while

11 instances of class L{Etm} represent ETM C{(easting, northing)} locations. See also

12 I{Karney}'s utility U{TransverseMercatorProj<https://GeographicLib.SourceForge.io/C++/doc/

13 TransverseMercatorProj.1.html>} and use C{"python[3] -m pygeodesy.etm ..."} to compare

14 the results.

15

16 Following is a copy of I{Karney}'s U{TransverseMercatorExact.hpp

17<https://GeographicLib.SourceForge.io/C++/doc/TransverseMercatorExact_8hpp_source.html>}

18 file C{Header}.

19

20 Copyright (C) U{Charles Karney<mailto:Karney@Alum.MIT.edu>} (2008-2023) and licensed

21 under the MIT/X11 License. For more information, see the U{GeographicLib<https://

22 GeographicLib.SourceForge.io>} documentation.

23

24 The method entails using the U{Thompson Transverse Mercator<https://WikiPedia.org/

25 wiki/Transverse_Mercator_projection>} as an intermediate projection. The projections

26 from the intermediate coordinates to C{phi, lam} and C{x, y} are given by elliptic

27 functions. The inverse of these projections are found by Newton's method with a

28 suitable starting guess.

29

30 The relevant section of L.P. Lee's paper U{Conformal Projections Based On Jacobian

31 Elliptic Functions<https://DOI.org/10.3138/X687-1574-4325-WM62>} in part V, pp

32 67-101. The C++ implementation and notation closely follow Lee, with the following

33 exceptions::

34

35 Lee here Description

36

37 x/a xi Northing (unit Earth)

38

39 y/a eta Easting (unit Earth)

40

41 s/a sigma xi + i * eta

42

43 y x Easting

44

45 x y Northing

46

47 k e Eccentricity

48

49 k^2 mu Elliptic function parameter

50

51 k'^2 mv Elliptic function complementary parameter

52

53 m k Scale

54

55 zeta zeta Complex longitude = Mercator = chi in paper

56

57 s sigma Complex GK = zeta in paper

58

59 Minor alterations have been made in some of Lee's expressions in an attempt to

60 control round-off. For example, C{atanh(sin(phi))} is replaced by C{asinh(tan(phi))}

61 which maintains accuracy near C{phi = pi/2}. Such changes are noted in the code.

62'''

63# make sure int/int division yields float quotient, see .basics

64 from __future__ import division as _; del _ # PYCHOK semicolon

65

66 from pygeodesy.basics import map1, neg, neg_, _xinstanceof

67 from pygeodesy.constants import EPS, EPS02, PI_2, PI_4, _K0_UTM, \

68 _1_EPS, _0_0, _0_1, _0_5, _1_0, _2_0, \

69 _3_0, _4_0, _90_0, isnear0, isnear90

70 from pygeodesy.datums import _ellipsoidal_datum, _WGS84, _EWGS84

71# from pygeodesy.ellipsoids import _EWGS84 # from .datums

72 from pygeodesy.elliptic import _ALL_LAZY, Elliptic

73# from pygeodesy.errors import _incompatible # from .named

74 from pygeodesy.fmath import cbrt, hypot, hypot1, hypot2

75 from pygeodesy.fsums import Fsum, fsum1f_

76 from pygeodesy.interns import NN, _COMMASPACE_, _DASH_, _near_, _SPACE_, \

77 _spherical_, _usage

78 from pygeodesy.karney import _copyBit, _diff182, _fix90, _norm2, _norm180, \

79 _tand, _unsigned2

80# from pygeodesy.lazily import _ALL_LAZY # from .elliptic

81 from pygeodesy.named import callername, _incompatible, _NamedBase

82 from pygeodesy.namedTuples import Forward4Tuple, Reverse4Tuple

83 from pygeodesy.props import deprecated_method, deprecated_property_RO, \

84 Property_RO, property_RO, _update_all, \

85 property_doc_

86 from pygeodesy.streprs import Fmt, pairs, unstr

87 from pygeodesy.units import Degrees, Scalar_

88 from pygeodesy.utily import atan1d, atan2d, _loneg, sincos2

89 from pygeodesy.utm import _cmlon, _LLEB, _parseUTM5, _toBand, _toXtm8, \

90 _to7zBlldfn, Utm, UTMError

91

92 from math import asinh, atan2, degrees, radians, sinh, sqrt

93

94__all__ = _ALL_LAZY.etm

95__version__ = '23.10.15'

96

97_OVERFLOW = _1_EPS**2 # about 2e+31

98_TAYTOL = pow(EPS, 0.6)

99_TAYTOL2 = _TAYTOL * _2_0

100_TOL_10 = EPS * _0_1

101_TRIPS = 21 # C++ 10

102

103

104 def _overflow(x):

105 '''(INTERNAL) Like C{copysign0(OVERFLOW, B{x})}.

106 '''

107 return _copyBit(_OVERFLOW, x)

108

109

110 class ETMError(UTMError):

111 '''Exact Transverse Mercator (ETM) parse, projection or other

112 L{Etm} issue or L{ExactTransverseMercator} conversion failure.

113 '''

114 pass

115

116

117 class Etm(Utm):

118 '''Exact Transverse Mercator (ETM) coordinate, a sub-class of L{Utm},

119 a Universal Transverse Mercator (UTM) coordinate using the

120 L{ExactTransverseMercator} projection for highest accuracy.

121

122 @note: Conversion of (geodetic) lat- and longitudes to/from L{Etm}

123 coordinates is 3-4 times slower than to/from L{Utm}.

124

125 @see: Karney's U{Detailed Description<https://GeographicLib.SourceForge.io/

126 html/classGeographicLib_1_1TransverseMercatorExact.html#details>}.

127 '''

128 _Error = ETMError # see utm.UTMError

129 _exactTM = None

130

131 __init__ = Utm.__init__

132 '''New L{Etm} Exact Transverse Mercator coordinate, raising L{ETMError}s.

133

134 @see: L{Utm.__init__} for more information.

135

136 @example:

137

138 >>> import pygeodesy

139 >>> u = pygeodesy.Etm(31, 'N', 448251, 5411932)

140 '''

141

142 @property_doc_(''' the ETM projection (L{ExactTransverseMercator}).''')

143 def exactTM(self):

144 '''Get the ETM projection (L{ExactTransverseMercator}).

145 '''

146 if self._exactTM is None:

147 self.exactTM = self.datum.exactTM # ExactTransverseMercator(datum=self.datum)

148 return self._exactTM

149

150 @exactTM.setter # PYCHOK setter!

151 def exactTM(self, exactTM):

152 '''Set the ETM projection (L{ExactTransverseMercator}).

153

154 @raise ETMError: The B{C{exacTM}}'s datum incompatible

155 with this ETM coordinate's C{datum}.

156 '''

157 _xinstanceof(ExactTransverseMercator, exactTM=exactTM)

158

159 E = self.datum.ellipsoid

160 if E != exactTM.ellipsoid: # may be None

161 raise ETMError(repr(exactTM), txt=_incompatible(repr(E)))

162 self._exactTM = exactTM

163 self._scale0 = exactTM.k0

164

165 def parse(self, strETM, name=NN):

166 '''Parse a string to a similar L{Etm} instance.

167

168 @arg strETM: The ETM coordinate (C{str}),

169 see function L{parseETM5}.

170 @kwarg name: Optional instance name (C{str}),

171 overriding this name.

172

173 @return: The instance (L{Etm}).

174

175 @raise ETMError: Invalid B{C{strETM}}.

176

177 @see: Function L{pygeodesy.parseUPS5}, L{pygeodesy.parseUTM5}

178 and L{pygeodesy.parseUTMUPS5}.

179 '''

180 return parseETM5(strETM, datum=self.datum, Etm=self.classof,

181 name=name or self.name)

182

183 @deprecated_method

184 def parseETM(self, strETM): # PYCHOK no cover

185 '''DEPRECATED, use method L{Etm.parse}.

186 '''

187 return self.parse(strETM)

188

189 def toLatLon(self, LatLon=None, unfalse=True, **unused): # PYCHOK expected

190 '''Convert this ETM coordinate to an (ellipsoidal) geodetic point.

191

192 @kwarg LatLon: Optional, ellipsoidal class to return the geodetic

193 point (C{LatLon}) or C{None}.

194 @kwarg unfalse: Unfalse B{C{easting}} and B{C{northing}} if

195 C{falsed} (C{bool}).

196

197 @return: This ETM coordinate as (B{C{LatLon}}) or a

198 L{LatLonDatum5Tuple}C{(lat, lon, datum, gamma,

199 scale)} if B{C{LatLon}} is C{None}.

200

201 @raise ETMError: This ETM coordinate's C{exacTM} and this C{datum}

202 incompatible or no convergence transforming to

203 lat- and longitude.

204

205 @raise TypeError: Invalid or non-ellipsoidal B{C{LatLon}}.

206

207 @example:

208

209 >>> from pygeodesy import ellipsoidalVincenty as eV, Etm

210 >>> u = Etm(31, 'N', 448251.795, 5411932.678)

211 >>> ll = u.toLatLon(eV.LatLon) # 48°51′29.52″N, 002°17′40.20″E

212 '''

213 if not self._latlon or self._latlon._toLLEB_args != (unfalse, self.exactTM):

214 self._toLLEB(unfalse=unfalse)

215 return self._latlon5(LatLon)

216

217 def _toLLEB(self, unfalse=True, **unused): # PYCHOK signature

218 '''(INTERNAL) Compute (ellipsoidal) lat- and longitude.

219 '''

220 xTM, d = self.exactTM, self.datum

221 # double check that this and exactTM's ellipsoid match

222 if xTM._E != d.ellipsoid: # PYCHOK no cover

223 t = repr(d.ellipsoid)

224 raise ETMError(repr(xTM._E), txt=_incompatible(t))

225

226 e, n = self.eastingnorthing2(falsed=not unfalse)

227 lon0 = _cmlon(self.zone) if bool(unfalse) == self.falsed else None

228 lat, lon, g, k = xTM.reverse(e, n, lon0=lon0)

229

230 ll = _LLEB(lat, lon, datum=d, name=self.name) # utm._LLEB

231 ll._gamma = g

232 ll._scale = k

233 self._latlon5args(ll, _toBand, unfalse, xTM)

234

235 def toUtm(self): # PYCHOK signature

236 '''Copy this ETM to a UTM coordinate.

237

238 @return: The UTM coordinate (L{Utm}).

239 '''

240 return self._xcopy2(Utm)

241

242

243 class ExactTransverseMercator(_NamedBase):

244 '''Pure Python version of Karney's C++ class U{TransverseMercatorExact

245 <https://GeographicLib.SourceForge.io/C++/doc/TransverseMercatorExact_8cpp_source.html>},

246 a numerically exact transverse Mercator projection, further referred to as C{TMExact}.

247 '''

248 _datum = _WGS84 # Datum

249 _E = _EWGS84 # Ellipsoid

250 _extendp = False # use extended domain

251# _iteration = None # ._sigmaInv2 and ._zetaInv2

252 _k0 = _K0_UTM # central scale factor

253 _lat0 = _0_0 # central parallel

254 _lon0 = _0_0 # central meridian

255 _mu = _EWGS84.e2 # 1st eccentricity squared

256 _mv = _EWGS84.e21 # 1 - ._mu

257 _raiser = False # throw Error

258 _sigmaC = None # most recent _sigmaInv04 case C{int}

259 _zetaC = None # most recent _zetaInv04 case C{int}

260

261 def __init__(self, datum=_WGS84, lon0=0, k0=_K0_UTM, extendp=False, name=NN, raiser=False):

262 '''New L{ExactTransverseMercator} projection.

263

264 @kwarg datum: The I{non-spherical} datum or ellipsoid (L{Datum},

265 L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}).

266 @kwarg lon0: Central meridian, default (C{degrees180}).

267 @kwarg k0: Central scale factor (C{float}).

268 @kwarg extendp: Use the I{extended} domain (C{bool}), I{standard} otherwise.

269 @kwarg name: Optional name for the projection (C{str}).

270 @kwarg raiser: If C{True}, throw an L{ETMError} for convergence failures (C{bool}).

271

272 @raise ETMError: Near-spherical B{C{datum}} or C{ellipsoid} or invalid B{C{lon0}}

273 or B{C{k0}}.

274

275 @see: U{Constructor TransverseMercatorExact<https://GeographicLib.SourceForge.io/

276 html/classGeographicLib_1_1TransverseMercatorExact.html>} for more details,

277 especially on B{X{extendp}}.

278

279 @note: For all 255.5K U{TMcoords.dat<https://Zenodo.org/record/32470>} tests (with

280 C{0 <= lat <= 84} and C{0 <= lon}) the maximum error is C{5.2e-08 .forward}

281 (or 52 nano-meter) easting and northing and C{3.8e-13 .reverse} (or 0.38

282 pico-degrees) lat- and longitude (with Python 3.7.3+, 2.7.16+, PyPy6 3.5.3

283 and PyPy6 2.7.13, all in 64-bit on macOS 10.13.6 High Sierra C{x86_64} and

284 12.2 Monterey C{arm64} and C{"arm64_x86_64"}).

285 '''

286 if extendp:

287 self._extendp = True

288 if name:

289 self.name = name

290 if raiser:

291 self.raiser = True

292

293 TM = ExactTransverseMercator

294 if datum not in (TM._datum, TM._E, None):

295 self.datum = datum # invokes ._resets

296 if lon0 or lon0 != TM._lon0:

297 self.lon0 = lon0

298 if k0 is not TM._k0:

299 self.k0 = k0

300

301 @property_doc_(''' the datum (L{Datum}).''')

302 def datum(self):

303 '''Get the datum (L{Datum}) or C{None}.

304 '''

305 return self._datum

306

307 @datum.setter # PYCHOK setter!

308 def datum(self, datum):

309 '''Set the datum and ellipsoid (L{Datum}, L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}).

310

311 @raise ETMError: Near-spherical B{C{datum}} or C{ellipsoid}.

312 '''

313 d = _ellipsoidal_datum(datum, name=self.name) # raiser=_datum_)

314 self._resets(d)

315 self._datum = d

316

317 @Property_RO

318 def _e(self):

319 '''(INTERNAL) Get and cache C{_e}.

320 '''

321 return self._E.e

322

323 @Property_RO

324 def _1_e_90(self): # PYCHOK no cover

325 '''(INTERNAL) Get and cache C{(1 - _e) * 90}.

326 '''

327 return (_1_0 - self._e) * _90_0

328

329 @property_RO

330 def ellipsoid(self):

331 '''Get the ellipsoid (L{Ellipsoid}).

332 '''

333 return self._E

334

335 @Property_RO

336 def _e_PI_2(self):

337 '''(INTERNAL) Get and cache C{_e * PI / 2}.

338 '''

339 return self._e * PI_2

340

341 @Property_RO

342 def _e_PI_4_(self):

343 '''(INTERNAL) Get and cache C{-_e * PI / 4}.

344 '''

345 return -self._e * PI_4

346

347 @Property_RO

348 def _1_e_PI_2(self):

349 '''(INTERNAL) Get and cache C{(1 - _e) * PI / 2}.

350 '''

351 return (_1_0 - self._e) * PI_2

352

353 @Property_RO

354 def _1_2e_PI_2(self):

355 '''(INTERNAL) Get and cache C{(1 - 2 * _e) * PI / 2}.

356 '''

357 return (_1_0 - self._e * _2_0) * PI_2

358

359 @property_RO

360 def equatoradius(self):

361 '''Get the C{ellipsoid}'s equatorial radius, semi-axis (C{meter}).

362 '''

363 return self._E.a

364

365 a = equatoradius

366

367 @Property_RO

368 def _e_TAYTOL(self):

369 '''(INTERNAL) Get and cache C{e * TAYTOL}.

370 '''

371 return self._e * _TAYTOL

372

373 @Property_RO

374 def _Eu(self):

375 '''(INTERNAL) Get and cache C{Elliptic(_mu)}.

376 '''

377 return Elliptic(self._mu)

378

379 @Property_RO

380 def _Eu_cE(self):

381 '''(INTERNAL) Get and cache C{_Eu.cE}.

382 '''

383 return self._Eu.cE

384

385 def _Eu_2cE_(self, xi):

386 '''(INTERNAL) Return C{_Eu.cE * 2 - B{xi}}.

387 '''

388 return self._Eu_cE * _2_0 - xi

389

390 @Property_RO

391 def _Eu_cE_4(self):

392 '''(INTERNAL) Get and cache C{_Eu.cE / 4}.

393 '''

394 return self._Eu_cE / _4_0

395

396 @Property_RO

397 def _Eu_cK(self):

398 '''(INTERNAL) Get and cache C{_Eu.cK}.

399 '''

400 return self._Eu.cK

401

402 @Property_RO

403 def _Eu_cK_cE(self):

404 '''(INTERNAL) Get and cache C{_Eu.cK / _Eu.cE}.

405 '''

406 return self._Eu_cK / self._Eu_cE

407

408 @Property_RO

409 def _Eu_2cK_PI(self):

410 '''(INTERNAL) Get and cache C{_Eu.cK * 2 / PI}.

411 '''

412 return self._Eu_cK / PI_2

413

414 @Property_RO

415 def _Ev(self):

416 '''(INTERNAL) Get and cache C{Elliptic(_mv)}.

417 '''

418 return Elliptic(self._mv)

419

420 @Property_RO

421 def _Ev_cK(self):

422 '''(INTERNAL) Get and cache C{_Ev.cK}.

423 '''

424 return self._Ev.cK

425

426 @Property_RO

427 def _Ev_cKE(self):

428 '''(INTERNAL) Get and cache C{_Ev.cKE}.

429 '''

430 return self._Ev.cKE

431

432 @Property_RO

433 def _Ev_3cKE_4(self):

434 '''(INTERNAL) Get and cache C{_Ev.cKE * 3 / 4}.

435 '''

436 return self._Ev_cKE * 0.75 # _0_75

437

438 @Property_RO

439 def _Ev_5cKE_4(self):

440 '''(INTERNAL) Get and cache C{_Ev.cKE * 5 / 4}.

441 '''

442 return self._Ev_cKE * 1.25 # _1_25

443

444 @Property_RO

445 def extendp(self):

446 '''Get the domain (C{bool}), I{extended} or I{standard}.

447 '''

448 return self._extendp

449

450 @property_RO

451 def flattening(self):

452 '''Get the C{ellipsoid}'s flattening (C{scalar}).

453 '''

454 return self._E.f

455

456 f = flattening

457

458 def forward(self, lat, lon, lon0=None, name=NN): # MCCABE 13

459 '''Forward projection, from geographic to transverse Mercator.

460

461 @arg lat: Latitude of point (C{degrees}).

462 @arg lon: Longitude of point (C{degrees}).

463 @kwarg lon0: Central meridian (C{degrees180}), overriding

464 the default if not C{None}.

465 @kwarg name: Optional name (C{str}).

466

467 @return: L{Forward4Tuple}C{(easting, northing, gamma, scale)}.

468

469 @see: C{void TMExact::Forward(real lon0, real lat, real lon,

470 real &x, real &y,

471 real &gamma, real &k)}.

472

473 @raise ETMError: No convergence, thrown iff property

474 C{B{raiser}=True}.

475 '''

476 lat = _fix90(lat - self._lat0)

477 lon, _ = _diff182((self.lon0 if lon0 is None else lon0), lon)

478 if self.extendp:

479 backside = _lat = _lon = False

480 else: # enforce the parity

481 lat, _lat = _unsigned2(lat)

482 lon, _lon = _unsigned2(lon)

483 backside = lon > 90

484 if backside: # PYCHOK no cover

485 lon = _loneg(lon)

486 if lat == 0:

487 _lat = True

488

489 # u, v = coordinates for the Thompson TM, Lee 54

490 if lat == 90: # isnear90(lat)

491 u = self._Eu_cK

492 v = self._iteration = self._zetaC = 0

493 elif lat == 0 and lon == self._1_e_90: # PYCHOK no cover

494 u = self._iteration = self._zetaC = 0

495 v = self._Ev_cK

496 else: # tau = tan(phi), taup = sinh(psi)

497 tau, lam = _tand(lat), radians(lon)

498 u, v = self._zetaInv2(self._E.es_taupf(tau), lam)

499

500 sncndn6 = self._sncndn6(u, v)

501 y, x, _ = self._sigma3(v, *sncndn6)

502 g, k = (lon, self.k0) if isnear90(lat) else \

503 self._zetaScaled(sncndn6, ll=False)

504

505 if backside:

506 y, g = self._Eu_2cE_(y), _loneg(g)

507 y *= self._k0_a

508 x *= self._k0_a

509 if _lat:

510 y, g = neg_(y, g)

511 if _lon:

512 x, g = neg_(x, g)

513 return Forward4Tuple(x, y, g, k, iteration=self._iteration,

514 name=name or self.name)

515

516 def _Inv03(self, psi, dlam, _3_mv_e): # (xi, deta, _3_mv)

517 '''(INTERNAL) Partial C{_zetaInv04} or C{_sigmaInv04}, Case 2

518 '''

519 # atan2(dlam-psi, psi+dlam) + 45d gives arg(zeta - zeta0) in

520 # range [-135, 225). Subtracting 180 (multiplier is negative)

521 # makes range [-315, 45). Multiplying by 1/3 (for cube root)

522 # gives range [-105, 15). In particular the range [-90, 180]

523 # in zeta space maps to [-90, 0] in w space as required.

524 a = atan2(dlam - psi, psi + dlam) / _3_0 - PI_4

525 s, c = sincos2(a)

526 h = hypot(psi, dlam)

527 r = cbrt(h * _3_mv_e)

528 u = r * c

529 v = r * s + self._Ev_cK

530 # Error using this guess is about 0.068 * rad^(5/3)

531 return u, v, h

532

533 @property_RO

534 def iteration(self):

535 '''Get the most recent C{ExactTransverseMercator.forward}

536 or C{ExactTransverseMercator.reverse} iteration number

537 (C{int}) or C{None} if not available/applicable.

538 '''

539 return self._iteration

540

541 @property_doc_(''' the central scale factor (C{float}).''')

542 def k0(self):

543 '''Get the central scale factor (C{float}), aka I{C{scale0}}.

544 '''

545 return self._k0 # aka scale0

546

547 @k0.setter # PYCHOK setter!

548 def k0(self, k0):

549 '''Set the central scale factor (C{float}), aka I{C{scale0}}.

550

551 @raise ETMError: Invalid B{C{k0}}.

552 '''

553 k0 = Scalar_(k0=k0, Error=ETMError, low=_TOL_10, high=_1_0)

554 if self._k0 != k0:

555 ExactTransverseMercator._k0_a._update(self) # redo ._k0_a

556 self._k0 = k0

557

558 @Property_RO

559 def _k0_a(self):

560 '''(INTERNAL) Get and cache C{k0 * equatoradius}.

561 '''

562 return self.k0 * self.equatoradius

563

564 @property_doc_(''' the central meridian (C{degrees180}).''')

565 def lon0(self):

566 '''Get the central meridian (C{degrees180}).

567 '''

568 return self._lon0

569

570 @lon0.setter # PYCHOK setter!

571 def lon0(self, lon0):

572 '''Set the central meridian (C{degrees180}).

573

574 @raise ETMError: Invalid B{C{lon0}}.

575 '''

576 self._lon0 = _norm180(Degrees(lon0=lon0, Error=ETMError))

577

578 @deprecated_property_RO

579 def majoradius(self): # PYCHOK no cover

580 '''DEPRECATED, use property C{equatoradius}.'''

581 return self.equatoradius

582

583 @Property_RO

584 def _1_mu_2(self):

585 '''(INTERNAL) Get and cache C{_mu / 2 + 1}.

586 '''

587 return self._mu * _0_5 + _1_0

588

589 @Property_RO

590 def _3_mv(self):

591 '''(INTERNAL) Get and cache C{3 / _mv}.

592 '''

593 return _3_0 / self._mv

594

595 @Property_RO

596 def _3_mv_e(self):

597 '''(INTERNAL) Get and cache C{3 / (_mv * _e)}.

598 '''

599 return _3_0 / (self._mv * self._e)

600

601 def _Newton2(self, taup, lam, u, v, C, *psi): # or (xi, eta, u, v)

602 '''(INTERNAL) Invert C{_zetaInv2} or C{_sigmaInv2} using Newton's method.

603

604 @return: 2-Tuple C{(u, v)}.

605

606 @raise ETMError: No convergence.

607 '''

608 sca1, tol2 = _1_0, _TOL_10

609 if psi: # _zetaInv2

610 sca1 = sca1 / hypot1(taup) # /= chokes PyChecker

611 tol2 = tol2 / max(psi[0], _1_0)**2

612

613 _zeta3 = self._zeta3

614 _zetaDwd2 = self._zetaDwd2

615 else: # _sigmaInv2

616 _zeta3 = self._sigma3

617 _zetaDwd2 = self._sigmaDwd2

618

619 d2, r = tol2, self.raiser

620 _U_2 = Fsum(u).fsum2_

621 _V_2 = Fsum(v).fsum2_

622 # min iterations 2, max 6 or 7, mean 3.9 or 4.0

623 for i in range(1, _TRIPS): # GEOGRAPHICLIB_PANIC

624 sncndn6 = self._sncndn6(u, v)

625 du, dv = _zetaDwd2(*sncndn6)

626 T, L, _ = _zeta3(v, *sncndn6)

627 T = (taup - T) * sca1

628 L -= lam

629 u, dU = _U_2(T * du, L * dv)

630 v, dV = _V_2(T * dv, -L * du)

631 if d2 < tol2:

632 r = False

633 break

634 d2 = hypot2(dU, dV)

635

636 self._iteration = i

637 if r: # PYCHOK no cover

638 n = callername(up=2, underOK=True)

639 t = unstr(n, taup, lam, u, v, C=C)

640 raise ETMError(Fmt.no_convergence(d2, tol2), txt=t)

641 return u, v

642

643 @property_doc_(''' raise an L{ETMError} for convergence failures (C{bool}).''')

644 def raiser(self):

645 '''Get the error setting (C{bool}).

646 '''

647 return self._raiser

648

649 @raiser.setter # PYCHOK setter!

650 def raiser(self, raiser):

651 '''Set the error setting (C{bool}), if C{True} throw an L{ETMError}

652 for convergence failures.

653 '''

654 self._raiser = bool(raiser)

655

656 def reset(self, lat0, lon0):

657 '''Set the central parallel and meridian.

658

659 @arg lat0: Latitude of the central parallel (C{degrees90}).

660 @arg lon0: Longitude of the central parallel (C{degrees180}).

661

662 @return: 2-Tuple C{(lat0, lon0)} of the previous central

663 parallel and meridian.

664

665 @raise ETMError: Invalid B{C{lat0}} or B{C{lon0}}.

666 '''

667 t = self._lat0, self.lon0

668 self._lat0 = _fix90(Degrees(lat0=lat0, Error=ETMError))

669 self. lon0 = lon0

670 return t

671

672 def _resets(self, datum):

673 '''(INTERNAL) Set the ellipsoid and elliptic moduli.

674

675 @arg datum: Ellipsoidal datum (C{Datum}).

676

677 @raise ETMError: Near-spherical B{C{datum}} or C{ellipsoid}.

678 '''

679 E = datum.ellipsoid

680 mu = E.e2 # .eccentricity1st2

681 mv = E.e21 # _1_0 - mu

682 if isnear0(E.e) or isnear0(mu, eps0=EPS02) \

683 or isnear0(mv, eps0=EPS02): # or sqrt(mu) != E.e

684 raise ETMError(ellipsoid=E, txt=_near_(_spherical_))

685

686 if self._datum or self._E:

687 _i = ExactTransverseMercator.iteration._uname

688 _update_all(self, _i, '_sigmaC', '_zetaC') # _under

689

690 self._E = E

691 self._mu = mu

692 self._mv = mv

693

694 def reverse(self, x, y, lon0=None, name=NN):

695 '''Reverse projection, from Transverse Mercator to geographic.

696

697 @arg x: Easting of point (C{meters}).

698 @arg y: Northing of point (C{meters}).

699 @kwarg lon0: Central meridian (C{degrees180}), overriding

700 the default if not C{None}.

701 @kwarg name: Optional name (C{str}).

702

703 @return: L{Reverse4Tuple}C{(lat, lon, gamma, scale)}.

704

705 @see: C{void TMExact::Reverse(real lon0, real x, real y,

706 real &lat, real &lon,

707 real &gamma, real &k)}

708

709 @raise ETMError: No convergence, thrown iff property

710 C{B{raiser}=True}.

711 '''

712 # undoes the steps in .forward.

713 xi = y / self._k0_a

714 eta = x / self._k0_a

715 if self.extendp:

716 backside = _lat = _lon = False

717 else: # enforce the parity

718 eta, _lon = _unsigned2(eta)

719 xi, _lat = _unsigned2(xi)

720 backside = xi > self._Eu_cE

721 if backside: # PYCHOK no cover

722 xi = self._Eu_2cE_(xi)

723

724 # u, v = coordinates for the Thompson TM, Lee 54

725 if xi or eta != self._Ev_cKE:

726 u, v = self._sigmaInv2(xi, eta)

727 else: # PYCHOK no cover

728 u = self._iteration = self._sigmaC = 0

729 v = self._Ev_cK

730

731 if v or u != self._Eu_cK:

732 g, k, lat, lon = self._zetaScaled(self._sncndn6(u, v))

733 else: # PYCHOK no cover

734 g, k, lat, lon = _0_0, self.k0, _90_0, _0_0

735

736 if backside: # PYCHOK no cover

737 lon, g = _loneg(lon), _loneg(g)

738 if _lat:

739 lat, g = neg_(lat, g)

740 if _lon:

741 lon, g = neg_(lon, g)

742 lat += self._lat0

743 lon += self._lon0 if lon0 is None else _norm180(lon0)

744 return Reverse4Tuple(lat, _norm180(lon), g, k, # _norm180(lat)

745 iteration=self._iteration,

746 name=name or self.name)

747

748 def _scaled2(self, tau, d2, snu, cnu, dnu, snv, cnv, dnv):

749 '''(INTERNAL) C{scaled}.

750

751 @note: Argument B{C{d2}} is C{_mu * cnu**2 + _mv * cnv**2}

752 from C{._zeta3}.

753

754 @return: 2-Tuple C{(convergence, scale)}.

755

756 @see: C{void TMExact::Scale(real tau, real /*lam*/,

757 real snu, real cnu, real dnu,

758 real snv, real cnv, real dnv,

759 real &gamma, real &k)}.

760 '''

761 mu, mv = self._mu, self._mv

762 cnudnv = cnu * dnv

763 # Lee 55.12 -- negated for our sign convention. g gives

764 # the bearing (clockwise from true north) of grid north

765 g = atan2d(mv * cnv * snv * snu, cnudnv * dnu)

766 # Lee 55.13 with nu given by Lee 9.1 -- in sqrt change

767 # the numerator from (1 - snu^2 * dnv^2) to (_mv * snv^2

768 # + cnu^2 * dnv^2) to maintain accuracy near phi = 90

769 # and change the denomintor from (dnu^2 + dnv^2 - 1) to

770 # (_mu * cnu^2 + _mv * cnv^2) to maintain accuracy near

771 # phi = 0, lam = 90 * (1 - e). Similarly rewrite sqrt in

772 # 9.1 as _mv + _mu * c^2 instead of 1 - _mu * sin(phi)^2

773 if d2 > 0:

774 # originally: sec2 = 1 + tau**2 # sec(phi)^2

775 # d2 = (mu * cnu**2 + mv * cnv**2)

776 # q2 = (mv * snv**2 + cnudnv**2) / d2

777 # k = sqrt(mv + mu / sec2) * sqrt(sec2) * sqrt(q2)

778 # = sqrt(mv * sec2 + mu) * sqrt(q2)

779 # = sqrt(mv + mv * tau**2 + mu) * sqrt(q2)

780 k, q2 = _0_0, (mv * snv**2 + cnudnv**2)

781 if q2 > 0:

782 k2 = fsum1f_(mu, mv, mv * tau**2)

783 if k2 > 0:

784 k = sqrt(k2) * sqrt(q2 / d2) * self.k0

785 else:

786 k = _OVERFLOW

787 return g, k

788

789 def _sigma3(self, v, snu, cnu, dnu, snv, cnv, dnv):

790 '''(INTERNAL) C{sigma}.

791

792 @return: 3-Tuple C{(xi, eta, d2)}.

793

794 @see: C{void TMExact::sigma(real /*u*/, real snu, real cnu, real dnu,

795 real v, real snv, real cnv, real dnv,

796 real &xi, real &eta)}.

797

798 @raise ETMError: No convergence.

799 '''

800 mu = self._mu * cnu

801 mv = self._mv * cnv

802 # Lee 55.4 writing

803 # dnu^2 + dnv^2 - 1 = _mu * cnu^2 + _mv * cnv^2

804 d2 = cnu * mu + cnv * mv

805 mu *= snu * dnu

806 mv *= snv * dnv

807 if d2 > 0: # /= chokes PyChecker

808 mu = mu / d2

809 mv = mv / d2

810 else:

811 mu, mv = map1(_overflow, mu, mv)

812 xi = self._Eu.fE(snu, cnu, dnu) - mu

813 v -= self._Ev.fE(snv, cnv, dnv) - mv

814 return xi, v, d2

815

816 def _sigmaDwd2(self, snu, cnu, dnu, snv, cnv, dnv):

817 '''(INTERNAL) C{sigmaDwd}.

818

819 @return: 2-Tuple C{(du, dv)}.

820

821 @see: C{void TMExact::dwdsigma(real /*u*/, real snu, real cnu, real dnu,

822 real /*v*/, real snv, real cnv, real dnv,

823 real &du, real &dv)}.

824 '''

825 snuv = snu * snv

826 # Reciprocal of 55.9: dw / ds = dn(w)^2/_mv,

827 # expanding complex dn(w) using A+S 16.21.4

828 d = self._mv * (cnv**2 + self._mu * snuv**2)**2

829 r = cnv * dnu * dnv

830 i = cnu * snuv * self._mu

831 du = (r**2 - i**2) / d

832 dv = neg(r * i * _2_0 / d)

833 return du, dv

834

835 def _sigmaInv2(self, xi, eta):

836 '''(INTERNAL) Invert C{sigma} using Newton's method.

837

838 @return: 2-Tuple C{(u, v)}.

839

840 @see: C{void TMExact::sigmainv(real xi, real eta,

841 real &u, real &v)}.

842

843 @raise ETMError: No convergence.

844 '''

845 u, v, t, self._sigmaC = self._sigmaInv04(xi, eta)

846 if not t:

847 u, v = self._Newton2(xi, eta, u, v, self._sigmaC)

848 return u, v

849

850 def _sigmaInv04(self, xi, eta):

851 '''(INTERNAL) Starting point for C{sigmaInv}.

852

853 @return: 4-Tuple C{(u, v, trip, Case)}.

854

855 @see: C{bool TMExact::sigmainv0(real xi, real eta,

856 real &u, real &v)}.

857 '''

858 t = False

859 d = eta - self._Ev_cKE

860 if eta > self._Ev_5cKE_4 or (xi < d and xi < -self._Eu_cE_4):

861 # sigma as a simple pole at

862 # w = w0 = Eu.K() + i * Ev.K()

863 # and sigma is approximated by

864 # sigma = (Eu.E() + i * Ev.KE()) + 1 / (w - w0)

865 u, v = _norm2(xi - self._Eu_cE, -d)

866 u += self._Eu_cK

867 v += self._Ev_cK

868 C = 1

869

870 elif (eta > self._Ev_3cKE_4 and xi < self._Eu_cE_4) or d > 0:

871 # At w = w0 = i * Ev.K(), we have

872 # sigma = sigma0 = i * Ev.KE()

873 # sigma' = sigma'' = 0

874 # including the next term in the Taylor series gives:

875 # sigma = sigma0 - _mv / 3 * (w - w0)^3

876 # When inverting this, we map arg(w - w0) = [-pi/2, -pi/6]

877 # to arg(sigma - sigma0) = [-pi/2, pi/2] mapping arg =

878 # [-pi/2, -pi/6] to [-pi/2, pi/2]

879 u, v, h = self._Inv03(xi, d, self._3_mv)

880 t = h < _TAYTOL2

881 C = 2

882

883 else: # use w = sigma * Eu.K/Eu.E (correct in limit _e -> 0)

884 u = v = self._Eu_cK_cE

885 u *= xi

886 v *= eta

887 C = 3

888

889 return u, v, t, C

890

891 def _sncndn6(self, u, v):

892 '''(INTERNAL) Get 6-tuple C{(snu, cnu, dnu, snv, cnv, dnv)}.

893 '''

894 # snu, cnu, dnu = self._Eu.sncndn(u)

895 # snv, cnv, dnv = self._Ev.sncndn(v)

896 return self._Eu.sncndn(u) + self._Ev.sncndn(v)

897

898 def toStr(self, joined=_COMMASPACE_, **kwds): # PYCHOK signature

899 '''Return a C{str} representation.

900

901 @kwarg joined: Separator to join the attribute strings

902 (C{str} or C{None} or C{NN} for non-joined).

903 @kwarg kwds: Optional, overriding keyword arguments.

904 '''

905 d = dict(datum=self.datum.name, lon0=self.lon0,

906 k0=self.k0, extendp=self.extendp)

907 if self.name:

908 d.update(name=self.name)

909 t = pairs(d, **kwds)

910 return joined.join(t) if joined else t

911

912 def _zeta3(self, unused, snu, cnu, dnu, snv, cnv, dnv): # _sigma3 signature

913 '''(INTERNAL) C{zeta}.

914

915 @return: 3-Tuple C{(taup, lambda, d2)}.

916

917 @see: C{void TMExact::zeta(real /*u*/, real snu, real cnu, real dnu,

918 real /*v*/, real snv, real cnv, real dnv,

919 real &taup, real &lam)}

920 '''

921 e, cnu2, mv = self._e, cnu**2, self._mv

922 # Overflow value like atan(overflow) = pi/2

923 t1 = t2 = _overflow(snu)

924 # Lee 54.17 but write

925 # atanh(snu * dnv) = asinh(snu * dnv / sqrt(cnu^2 + _mv * snu^2 * snv^2))

926 # atanh(_e * snu / dnv) = asinh(_e * snu / sqrt(_mu * cnu^2 + _mv * cnv^2))

927 d1 = cnu2 + mv * (snu * snv)**2

928 if d1 > EPS02: # _EPSmin

929 t1 = snu * dnv / sqrt(d1)

930 else:

931 d1 = 0

932 d2 = self._mu * cnu2 + mv * cnv**2

933 if d2 > EPS02: # _EPSmin

934 t2 = sinh(e * asinh(e * snu / sqrt(d2)))

935 else:

936 d2 = 0

937 # psi = asinh(t1) - asinh(t2)

938 # taup = sinh(psi)

939 taup = t1 * hypot1(t2) - t2 * hypot1(t1)

940 lam = (atan2(dnu * snv, cnu * cnv) -

941 atan2(cnu * snv * e, dnu * cnv) * e) if d1 and d2 else _0_0

942 return taup, lam, d2

943

944 def _zetaDwd2(self, snu, cnu, dnu, snv, cnv, dnv):

945 '''(INTERNAL) C{zetaDwd}.

946

947 @return: 2-Tuple C{(du, dv)}.

948

949 @see: C{void TMExact::dwdzeta(real /*u*/, real snu, real cnu, real dnu,

950 real /*v*/, real snv, real cnv, real dnv,

951 real &du, real &dv)}.

952 '''

953 cnu2 = cnu**2 * self._mu

954 cnv2 = cnv**2

955 dnuv = dnu * dnv

956 dnuv2 = dnuv**2

957 snuv = snu * snv

958 snuv2 = snuv**2 * self._mu

959 # Lee 54.21 but write (see A+S 16.21.4)

960 # (1 - dnu^2 * snv^2) = (cnv^2 + _mu * snu^2 * snv^2)

961 d = self._mv * (cnv2 + snuv2)**2 # max(d, EPS02)?

962 du = cnu * dnuv * (cnv2 - snuv2) / d

963 dv = cnv * snuv * (cnu2 + dnuv2) / d

964 return du, neg(dv)

965

966 def _zetaInv2(self, taup, lam):

967 '''(INTERNAL) Invert C{zeta} using Newton's method.

968

969 @return: 2-Tuple C{(u, v)}.

970

971 @see: C{void TMExact::zetainv(real taup, real lam,

972 real &u, real &v)}.

973

974 @raise ETMError: No convergence.

975 '''

976 psi = asinh(taup)

977 u, v, t, self._zetaC = self._zetaInv04(psi, lam)

978 if not t:

979 u, v = self._Newton2(taup, lam, u, v, self._zetaC, psi)

980 return u, v

981

982 def _zetaInv04(self, psi, lam):

983 '''(INTERNAL) Starting point for C{zetaInv}.

984

985 @return: 4-Tuple C{(u, v, trip, Case)}.

986

987 @see: C{bool TMExact::zetainv0(real psi, real lam, # radians

988 real &u, real &v)}.

989 '''

990 if lam > self._1_2e_PI_2:

991 d = lam - self._1_e_PI_2

992 if psi < d and psi < self._e_PI_4_: # PYCHOK no cover

993 # N.B. this branch is normally *not* taken because psi < 0

994 # is converted psi > 0 by .forward. There's a log singularity

995 # at w = w0 = Eu.K() + i * Ev.K(), corresponding to the south

996 # pole, where we have, approximately

997 # psi = _e + i * pi/2 - _e * atanh(cos(i * (w - w0)/(1 + _mu/2)))

998 # Inverting this gives:

999 e = self._e # eccentricity

1000 s, c = sincos2((PI_2 - lam) / e)

1001 h, r = sinh(_1_0 - psi / e), self._1_mu_2

1002 u = self._Eu_cK - r * asinh(s / hypot(c, h))

1003 v = self._Ev_cK - r * atan2(c, h)

1004 return u, v, False, 1

1005

1006 elif psi < self._e_PI_2:

1007 # At w = w0 = i * Ev.K(), we have

1008 # zeta = zeta0 = i * (1 - _e) * pi/2

1009 # zeta' = zeta'' = 0

1010 # including the next term in the Taylor series gives:

1011 # zeta = zeta0 - (_mv * _e) / 3 * (w - w0)^3

1012 # When inverting this, we map arg(w - w0) = [-90, 0]

1013 # to arg(zeta - zeta0) = [-90, 180]

1014 u, v, h = self._Inv03(psi, d, self._3_mv_e)

1015 return u, v, (h < self._e_TAYTOL), 2

1016

1017 # Use spherical TM, Lee 12.6 -- writing C{atanh(sin(lam) /

1018 # cosh(psi)) = asinh(sin(lam) / hypot(cos(lam), sinh(psi)))}.

1019 # This takes care of the log singularity at C{zeta = Eu.K()},

1020 # corresponding to the north pole.

1021 s, c = sincos2(lam)

1022 h, r = sinh(psi), self._Eu_2cK_PI

1023 # But scale to put 90, 0 on the right place

1024 u = r * atan2(h, c)

1025 v = r * asinh(s / hypot(h, c))

1026 return u, v, False, 3

1027

1028 def _zetaScaled(self, sncndn6, ll=True):

1029 '''(INTERNAL) Recompute (T, L) from (u, v) to improve accuracy of Scale.

1030

1031 @arg sncndn6: 6-Tuple C{(snu, cnu, dnu, snv, cnv, dnv)}.

1032

1033 @return: 2-Tuple C{(g, k)} if not C{B{ll}} else

1034 4-tuple C{(g, k, lat, lon)}.

1035 '''

1036 t, lam, d2 = self._zeta3(None, *sncndn6)

1037 tau = self._E.es_tauf(t)

1038 g_k = self._scaled2(tau, d2, *sncndn6)

1039 if ll:

1040 g_k += atan1d(tau), degrees(lam)

1041 return g_k # or (g, k, lat, lon)

1042

1043

1044 def parseETM5(strUTM, datum=_WGS84, Etm=Etm, falsed=True, name=NN):

1045 '''Parse a string representing a UTM coordinate, consisting

1046 of C{"zone[band] hemisphere easting northing"}.

1047

1048 @arg strUTM: A UTM coordinate (C{str}).

1049 @kwarg datum: Optional datum to use (L{Datum}, L{Ellipsoid},

1050 L{Ellipsoid2} or L{a_f2Tuple}).

1051 @kwarg Etm: Optional class to return the UTM coordinate

1052 (L{Etm}) or C{None}.

1053 @kwarg falsed: Both easting and northing are C{falsed} (C{bool}).

1054 @kwarg name: Optional B{C{Etm}} name (C{str}).

1055

1056 @return: The UTM coordinate (B{C{Etm}}) or if B{C{Etm}} is

1057 C{None}, a L{UtmUps5Tuple}C{(zone, hemipole, easting,

1058 northing, band)}. The C{hemipole} is the hemisphere

1059 C{'N'|'S'}.

1060

1061 @raise ETMError: Invalid B{C{strUTM}}.

1062

1063 @raise TypeError: Invalid or near-spherical B{C{datum}}.

1064

1065 @example:

1066

1067 >>> u = parseETM5('31 N 448251 5411932')

1068 >>> u.toRepr() # [Z:31, H:N, E:448251, N:5411932]

1069 >>> u = parseETM5('31 N 448251.8 5411932.7')

1070 >>> u.toStr() # 31 N 448252 5411933

1071 '''

1072 r = _parseUTM5(strUTM, datum, Etm, falsed, Error=ETMError, name=name)

1073 return r

1074

1075

1076 def toEtm8(latlon, lon=None, datum=None, Etm=Etm, falsed=True,

1077 name=NN, strict=True,

1078 zone=None, **cmoff):

1079 '''Convert a geodetic lat-/longitude to an ETM coordinate.

1080

1081 @arg latlon: Latitude (C{degrees}) or an (ellipsoidal)

1082 geodetic C{LatLon} instance.

1083 @kwarg lon: Optional longitude (C{degrees}) or C{None}.

1084 @kwarg datum: Optional datum for the ETM coordinate,

1085 overriding B{C{latlon}}'s datum (L{Datum},

1086 L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}).

1087 @kwarg Etm: Optional class to return the ETM coordinate

1088 (L{Etm}) or C{None}.

1089 @kwarg falsed: False both easting and northing (C{bool}).

1090 @kwarg name: Optional B{C{Utm}} name (C{str}).

1091 @kwarg strict: Restrict B{C{lat}} to UTM ranges (C{bool}).

1092 @kwarg zone: Optional UTM zone to enforce (C{int} or C{str}).

1093 @kwarg cmoff: DEPRECATED, use B{C{falsed}}. Offset longitude

1094 from the zone's central meridian (C{bool}).

1095

1096 @return: The ETM coordinate as an B{C{Etm}} instance or a

1097 L{UtmUps8Tuple}C{(zone, hemipole, easting, northing,

1098 band, datum, gamma, scale)} if B{C{Etm}} is C{None}

1099 or not B{C{falsed}}. The C{hemipole} is the C{'N'|'S'}

1100 hemisphere.

1101

1102 @raise ETMError: No convergence transforming to ETM easting

1103 and northing.

1104

1105 @raise ETMError: Invalid B{C{zone}} or near-spherical or

1106 incompatible B{C{datum}} or C{ellipsoid}.

1107

1108 @raise RangeError: If B{C{lat}} outside the valid UTM bands or

1109 if B{C{lat}} or B{C{lon}} outside the valid

1110 range and L{pygeodesy.rangerrors} set to C{True}.

1111

1112 @raise TypeError: Invalid or near-spherical B{C{datum}} or

1113 B{C{latlon}} not ellipsoidal.

1114

1115 @raise ValueError: The B{C{lon}} value is missing or B{C{latlon}}

1116 is invalid.

1117 '''

1118 z, B, lat, lon, d, f, name = _to7zBlldfn(latlon, lon, datum,

1119 falsed, name, zone,

1120 strict, ETMError, **cmoff)

1121 lon0 = _cmlon(z) if f else None

1122 x, y, g, k = d.exactTM.forward(lat, lon, lon0=lon0)

1123

1124 return _toXtm8(Etm, z, lat, x, y, B, d, g, k, f,

1125 name, latlon, d.exactTM, Error=ETMError)

1126

1127

1128 if __name__ == '__main__': # MCCABE 13

1129

1130 from pygeodesy import fstr, KTransverseMercator, printf

1131 from sys import argv, exit as _exit

1132

1133 # mimick some of I{Karney}'s utility C{TransverseMercatorProj}

1134 _f = _r = _s = _t = False

1135 _as = argv[1:]

1136 while _as and _as[0].startswith(_DASH_):

1137 _a = _as.pop(0)

1138 if len(_a) < 2:

1139 _exit('%s: option %r invalid' % (_usage(*argv), _a))

1140 elif '-forward'.startswith(_a):

1141 _f, _r = True, False

1142 elif '-reverse'.startswith(_a):

1143 _f, _r = False, True

1144 elif '-series'.startswith(_a):

1145 _s, _t = True, False

1146 elif _a == '-t':

1147 _s, _t = False, True

1148 elif '-help'.startswith(_a):

1149 _exit(_usage(argv[0], '[-s | -t]',

1150 '[-f[orward] <lat> <lon>',

1151 '| -r[everse] <easting> <northing>',

1152 '| <lat> <lon>]',

1153 '| -h[elp]'))

1154 else:

1155 _exit('%s: option %r not supported' % (_usage(*argv), _a))

1156 if len(_as) > 1:

1157 f2 = map1(float, *_as[:2])

1158 else:

1159 _exit('%s ...: incomplete' % (_usage(*argv),))

1160

1161 if _s: # -series

1162 tm = KTransverseMercator()

1163 else:

1164 tm = ExactTransverseMercator(extendp=_t)

1165

1166 if _f:

1167 t = tm.forward(*f2)

1168 elif _r:

1169 t = tm.reverse(*f2)

1170 else:

1171 t = tm.forward(*f2)

1172 printf('%s: %s', tm.classname, fstr(t, sep=_SPACE_))

1173 t = tm.reverse(t.easting, t.northing)

1174 printf('%s: %s', tm.classname, fstr(t, sep=_SPACE_))

1175

1176

1177# % python3 -m pygeodesy.etm 33.33 44.44

1178# ExactTransverseMercator: 4276926.114804 4727193.767015 28.375537 1.233325

1179# ExactTransverseMercator: 33.33 44.44 28.375537 1.233325

1180

1181# % python3 -m pygeodesy.etm -s 33.33 44.44

1182# KTransverseMercator: 4276926.114804 4727193.767015 28.375537 1.233325

1183# KTransverseMercator: 33.33 44.44 28.375537 1.233325

1184

1185# % echo 33.33 44.44 | .../bin/TransverseMercatorProj

1186# 4276926.114804 4727193.767015 28.375536563148 1.233325101778

1187

1188# **) MIT License

1189#

1190# Copyright (C) 2016-2023 -- mrJean1 at Gmail -- All Rights Reserved.

1191#

1192# Permission is hereby granted, free of charge, to any person obtaining a

1193# copy of this software and associated documentation files (the "Software"),

1194# to deal in the Software without restriction, including without limitation

1195# the rights to use, copy, modify, merge, publish, distribute, sublicense,

1196# and/or sell copies of the Software, and to permit persons to whom the

1197# Software is furnished to do so, subject to the following conditions:

1198#

1199# The above copyright notice and this permission notice shall be included

1200# in all copies or substantial portions of the Software.

1201#

1202# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS

1203# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

1204# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL

1205# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR

1206# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,

1207# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR

1208# OTHER DEALINGS IN THE SOFTWARE.