Coverage for pygeodesy/lcc.py: 96%

282 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2024-06-10 14:08 -0400

1 

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

3 

4u'''Lambert Conformal Conic (LCC) projection. 

5 

6Lambert conformal conic projection for 1- or 2-Standard Parallels classes L{Conic}, L{Conics} registry, L{LCCError} 

7and position class L{Lcc}. 

8 

9See U{LCC<https://WikiPedia.org/wiki/Lambert_conformal_conic_projection>}, U{Lambert 

10Conformal Conic to Geographic Transformation Formulae 

11<https://www.Linz.govt.NZ/data/geodetic-system/coordinate-conversion/projection-conversions/lambert-conformal-conic-geographic>}, 

12U{Lambert Conformal Conic Projection<https://MathWorld.Wolfram.com/LambertConformalConicProjection.html>} 

13and John P. Snyder U{'Map Projections - A Working Manual'<https://Pubs.USGS.gov/pp/1395/report.pdf>}, 1987, pp 107-109. 

14 

15@var Conics.Be08Lb: Conic(name='Be08Lb', lat0=50.797815, lon0=4.35921583, par1=49.8333339, par2=51.1666672, E0=649328, N0=665262, k0=1, SP=2, datum=Datum(name='GRS80', ellipsoid=Ellipsoids.GRS80, transform=Transforms.WGS84), 

16@var Conics.Be72Lb: Conic(name='Be72Lb', lat0=90, lon0=4.3674867, par1=49.8333339, par2=51.1666672, E0=150000.013, N0=5400088.438, k0=1, SP=2, datum=Datum(name='NAD83', ellipsoid=Ellipsoids.GRS80, transform=Transforms.NAD83), 

17@var Conics.Fr93Lb: Conic(name='Fr93Lb', lat0=46.5, lon0=3, par1=49, par2=44, E0=700000, N0=6600000, k0=1, SP=2, datum=Datum(name='WGS84', ellipsoid=Ellipsoids.WGS84, transform=Transforms.WGS84), 

18@var Conics.MaNLb: Conic(name='MaNLb', lat0=33.3, lon0=-5.4, par1=31.73, par2=34.87, E0=500000, N0=300000, k0=1, SP=2, datum=Datum(name='NTF', ellipsoid=Ellipsoids.Clarke1880IGN, transform=Transforms.NTF), 

19@var Conics.MxLb: Conic(name='MxLb', lat0=12, lon0=-102, par1=17.5, par2=29.5, E0=2500000, N0=0, k0=1, SP=2, datum=Datum(name='WGS84', ellipsoid=Ellipsoids.WGS84, transform=Transforms.WGS84), 

20@var Conics.PyT_Lb: Conic(name='PyT_Lb', lat0=46.8, lon0=2.33722917, par1=45.8989389, par2=47.6960144, E0=600000, N0=200000, k0=1, SP=2, datum=Datum(name='NTF', ellipsoid=Ellipsoids.Clarke1880IGN, transform=Transforms.NTF), 

21@var Conics.USA_Lb: Conic(name='USA_Lb', lat0=23, lon0=-96, par1=33, par2=45, E0=0, N0=0, k0=1, SP=2, datum=Datum(name='WGS84', ellipsoid=Ellipsoids.WGS84, transform=Transforms.WGS84), 

22@var Conics.WRF_Lb: Conic(name='WRF_Lb', lat0=40, lon0=-97, par1=33, par2=45, E0=0, N0=0, k0=1, SP=2, datum=Datum(name='WGS84', ellipsoid=Ellipsoids.WGS84, transform=Transforms.WGS84) 

23''' 

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

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

26 

27from pygeodesy.basics import copysign0, _xinstanceof, _xsubclassof 

28from pygeodesy.constants import EPS, EPS02, PI_2, _float as _F, _0_0, _0_5, \ 

29 _1_0, _2_0, _90_0 

30from pygeodesy.ellipsoidalBase import LatLonEllipsoidalBase as _LLEB 

31from pygeodesy.datums import Datums, _ellipsoidal_datum 

32from pygeodesy.errors import _IsnotError, _ValueError 

33from pygeodesy.fmath import hypot, _ALL_LAZY 

34from pygeodesy.interns import NN, _COMMASPACE_, _ellipsoidal_, _GRS80_, _k0_, \ 

35 _lat0_, _lon0_, _m_, _NAD83_, _NTF_, _SPACE_, \ 

36 _WGS84_, _C_ # PYCHOK used! 

37# from pygeodesy.lazily import _ALL_LAZY # from .fmath 

38from pygeodesy.named import _lazyNamedEnumItem as _lazy, _name2__, _NamedBase, \ 

39 _NamedEnum, _NamedEnumItem, _xnamed 

40from pygeodesy.namedTuples import EasNor3Tuple, LatLonDatum3Tuple, \ 

41 LatLon2Tuple, _LL4Tuple, PhiLam2Tuple 

42from pygeodesy.props import deprecated_method, Property, Property_RO, _update_all 

43from pygeodesy.streprs import Fmt, _fstrENH2, _xzipairs 

44from pygeodesy.units import Easting, Height, _heigHt, Lam_, Northing, Phi_, \ 

45 Scalar_ 

46from pygeodesy.utily import atan1, degrees90, degrees180, sincos2, tanPI_2_2 

47 

48from math import atan, fabs, log, radians, sin, sqrt 

49 

50__all__ = _ALL_LAZY.lcc 

51__version__ = '24.06.09' 

52 

53_E0_ = 'E0' 

54_N0_ = 'N0' 

55_par1_ = 'par1' 

56_par2_ = 'par2' 

57_SP_ = 'SP' 

58 

59 

60class Conic(_NamedEnumItem): 

61 '''Lambert conformal conic projection (1- or 2-SP). 

62 ''' 

63 _auth = NN # authorization (C{str}) 

64 _datum = None # datum (L{Datum}) 

65 _name = NN # Conic.__name__, set below 

66 

67 _e = _0_0 # ellipsoid excentricity (C{float}) 

68 _E0 = _0_0 # false easting (C{float}) 

69 _k0 = _1_0 # scale factor (C{float}) 

70 _N0 = _0_0 # false northing (C{float}) 

71 _SP = 0 # 1- or 2-SP (C{int}) 

72 

73 _opt3 = _0_0 # optional, longitude (C{radians}) 

74 _par1 = _0_0 # 1st std parallel (C{radians}) 

75 _par2 = _0_0 # 2nd std parallel (C{radians}) 

76 _phi0 = _0_0 # origin lat (C{radians}) 

77 _lam0 = _0_0 # origin lon (C{radians}) 

78 

79 _aF = _0_0 # precomputed F (C{float}) 

80 _n = _0_0 # precomputed n (C{float}) 

81 _1_n = _0_0 # precomputed 1 / n (C{float}) 

82 _r0 = _0_0 # precomputed rho0 (C{float}) 

83 

84 def __init__(self, latlon0, par1, par2=None, E0=0, N0=0, 

85 k0=1, opt3=0, auth=NN, **name): 

86 '''New Lambert conformal conic projection. 

87 

88 @arg latlon0: Origin with (ellipsoidal) datum (C{LatLon}). 

89 @arg par1: First standard parallel (C{degrees90}). 

90 @kwarg par2: Optional, second standard parallel (C{degrees90}). 

91 @kwarg E0: Optional, false easting (C{meter}). 

92 @kwarg N0: Optional, false northing (C{meter}). 

93 @kwarg k0: Optional scale factor (C{scalar}). 

94 @kwarg opt3: Optional meridian (C{degrees180}). 

95 @kwarg auth: Optional authentication authority (C{str}). 

96 @kwarg name: Optional C{B{name}=NN} for the conic (C{str}). 

97 

98 @return: A Lambert projection (L{Conic}). 

99 

100 @raise TypeError: Non-ellipsoidal B{C{latlon0}}. 

101 

102 @raise ValueError: Invalid B{C{par1}}, B{C{par2}}, 

103 B{C{E0}}, B{C{N0}}, B{C{k0}} 

104 or B{C{opt3}}. 

105 ''' 

106 if latlon0 is not None: 

107 _xinstanceof(_LLEB, latlon0=latlon0) 

108 self._phi0, self._lam0 = latlon0.philam 

109 

110 self._par1 = Phi_(par1=par1) 

111 self._par2 = self._par1 if par2 is None else Phi_(par2=par2) 

112 

113 if k0 != 1: 

114 self._k0 = Scalar_(k0=k0) 

115 if E0: 

116 self._E0 = Northing(E0=E0, falsed=True) 

117 if N0: 

118 self._N0 = Easting(N0=N0, falsed=True) 

119 if opt3: 

120 self._opt3 = Lam_(opt3=opt3) 

121 

122 self.toDatum(latlon0.datum)._dup2(self) 

123 self._register(Conics, name) 

124 elif name: 

125 self.name = name 

126 if auth: 

127 self._auth = str(auth) 

128 

129 @Property_RO 

130 def auth(self): 

131 '''Get the authentication authority (C{str}). 

132 ''' 

133 return self._auth 

134 

135 @deprecated_method 

136 def convertDatum(self, datum): 

137 '''DEPRECATED, use method L{Conic.toDatum}.''' 

138 return self.toDatum(datum) 

139 

140 @Property_RO 

141 def datum(self): 

142 '''Get the datum (L{Datum}). 

143 ''' 

144 return self._datum 

145 

146 @Property_RO 

147 def E0(self): 

148 '''Get the false easting (C{meter}). 

149 ''' 

150 return self._E0 

151 

152 @Property_RO 

153 def k0(self): 

154 '''Get scale factor (C{float}). 

155 ''' 

156 return self._k0 

157 

158 @Property_RO 

159 def lat0(self): 

160 '''Get the origin latitude (C{degrees90}). 

161 ''' 

162 return degrees90(self._phi0) 

163 

164 @Property_RO 

165 def latlon0(self): 

166 '''Get the central origin (L{LatLon2Tuple}C{(lat, lon)}). 

167 ''' 

168 return LatLon2Tuple(self.lat0, self.lon0, name=self.name) 

169 

170 @Property_RO 

171 def lam0(self): 

172 '''Get the central meridian (C{radians}). 

173 ''' 

174 return self._lam0 

175 

176 @Property_RO 

177 def lon0(self): 

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

179 ''' 

180 return degrees180(self._lam0) 

181 

182 @Property_RO 

183 def N0(self): 

184 '''Get the false northing (C{meter}). 

185 ''' 

186 return self._N0 

187 

188 @Property_RO 

189 def name2(self): 

190 '''Get the conic and datum names as "conic.datum" (C{str}). 

191 ''' 

192 return self._DOT_(self.datum.name) 

193 

194 @Property_RO 

195 def opt3(self): 

196 '''Get the optional meridian (C{degrees180}). 

197 ''' 

198 return degrees180(self._opt3) 

199 

200 @Property_RO 

201 def par1(self): 

202 '''Get the 1st standard parallel (C{degrees90}). 

203 ''' 

204 return degrees90(self._par1) 

205 

206 @Property_RO 

207 def par2(self): 

208 '''Get the 2nd standard parallel (C{degrees90}). 

209 ''' 

210 return degrees90(self._par2) 

211 

212 @Property_RO 

213 def phi0(self): 

214 '''Get the origin latitude (C{radians}). 

215 ''' 

216 return self._phi0 

217 

218 @Property_RO 

219 def philam0(self): 

220 '''Get the central origin (L{PhiLam2Tuple}C{(phi, lam)}). 

221 ''' 

222 return PhiLam2Tuple(self.phi0, self.lam0, name=self.name) 

223 

224 @Property_RO 

225 def SP(self): 

226 '''Get the number of standard parallels (C{int}). 

227 ''' 

228 return self._SP 

229 

230 def toDatum(self, datum): 

231 '''Convert this conic to the given datum. 

232 

233 @arg datum: Ellipsoidal datum to use (L{Datum}, L{Ellipsoid}, 

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

235 

236 @return: Converted conic, unregistered (L{Conic}). 

237 

238 @raise TypeError: Non-ellipsoidal B{C{datum}}. 

239 ''' 

240 d = _ellipsoidal_datum(datum, name=self.name) 

241 E = d.ellipsoid 

242 if not E.isEllipsoidal: 

243 raise _IsnotError(_ellipsoidal_, datum=datum) 

244 

245 c = self 

246 if c._e != E.e or c._datum != d: 

247 

248 c = Conic(None, 0, name=self._name) 

249 self._dup2(c) 

250 c._datum = d 

251 c._e = E.e 

252 

253 if fabs(c._par1 - c._par2) < EPS: 

254 m1 = c._mdef(c._phi0) 

255 t1 = c._tdef(c._phi0) 

256 t0 = t1 

257 k = 1 # _1_0 

258 n = sin(c._phi0) 

259 sp = 1 

260 else: 

261 m1 = c._mdef(c._par1) 

262 m2 = c._mdef(c._par2) 

263 t1 = c._tdef(c._par1) 

264 t2 = c._tdef(c._par2) 

265 t0 = c._tdef(c._phi0) 

266 k = c._k0 

267 n = (log(m1) - log(m2)) \ 

268 / (log(t1) - log(t2)) 

269 sp = 2 

270 

271 F = m1 / (n * pow(t1, n)) 

272 

273 c._aF = k * E.a * F 

274 c._n = n 

275 c._1_n = _1_0 / n 

276 c._r0 = c._rdef(t0) 

277 c._SP = sp 

278 

279 return c 

280 

281 def toStr(self, prec=8, name=NN, **unused): # PYCHOK expected 

282 '''Return this conic as a string. 

283 

284 @kwarg prec: Number of (decimal) digits, unstripped (C{int}). 

285 @kwarg name: Overriding name (C{str}) or C{None} to exclude 

286 this conic's name. 

287 

288 @return: Conic attributes (C{str}). 

289 ''' 

290 a = [name, prec, _lat0_, _lon0_, _par1_, _par2_, 

291 _E0_, _N0_, _k0_, _SP_] 

292 if self._SP == 1: 

293 _ = a.pop(a.index(_par2_)) 

294 return self._instr(datum=self.datum, *a) 

295 

296 def _dup2(self, c): 

297 '''(INTERNAL) Copy this conic to C{c}. 

298 

299 @arg c: Duplicate (L{Conic}). 

300 ''' 

301 _update_all(c) 

302 

303 c._auth = self._auth 

304 c._datum = self._datum 

305 

306 c._e = self._e 

307 c._E0 = self._E0 

308 c._k0 = self._k0 

309 c._N0 = self._N0 

310 c._SP = self._SP 

311 

312 c._par1 = self._par1 

313 c._par2 = self._par2 

314 c._phi0 = self._phi0 

315 c._lam0 = self._lam0 

316 c._opt3 = self._opt3 

317 

318 c._aF = self._aF 

319 c._n = self._n 

320 c._1_n = self._1_n 

321 c._r0 = self._r0 

322 

323 def _mdef(self, a): 

324 '''(INTERNAL) Compute m(a). 

325 ''' 

326 s, c = sincos2(a) 

327 s = _1_0 - (s * self._e)**2 

328 return (c / sqrt(s)) if s > EPS02 else _0_0 

329 

330 def _pdef(self, a): 

331 '''(INTERNAL) Compute p(a). 

332 ''' 

333 s = self._e * sin(a) 

334 return pow((_1_0 - s) / (_1_0 + s), self._e * _0_5) 

335 

336 def _rdef(self, t): 

337 '''(INTERNAL) Compute r(t). 

338 ''' 

339 return self._aF * pow(t, self._n) 

340 

341 def _tdef(self, a): 

342 '''(INTERNAL) Compute t(lat). 

343 ''' 

344 return max(_0_0, tanPI_2_2(-a) / self._pdef(a)) 

345 

346 def _xdef(self, t_x): 

347 '''(INTERNAL) Compute x(t_x). 

348 ''' 

349 return PI_2 - atan(t_x) * _2_0 # XXX + self._phi0 

350 

351 

352Conic._name = Conic.__name__ 

353 

354 

355class Conics(_NamedEnum): 

356 '''(INTERNAL) L{Conic} registry, I{must} be a sub-class 

357 to accommodate the L{_LazyNamedEnumItem} properties. 

358 ''' 

359 def _Lazy(self, lat, lon, datum_name, *args, **kwds): 

360 '''(INTERNAL) Instantiate the L{Conic}. 

361 ''' 

362 return Conic(_LLEB(lat, lon, datum=Datums.get(datum_name)), *args, **kwds) 

363 

364Conics = Conics(Conic) # PYCHOK singleton 

365'''Some pre-defined L{Conic}s, all I{lazily} instantiated.''' 

366Conics._assert( # <https://SpatialReference.org/ref/sr-org/...> 

367# AsLb = _lazy('AsLb', _F(-14.2666667), _F(170), _NAD27_, _0_0, _0_0, 

368# E0=_F(500000), N0=_0_0, auth='EPSG:2155'), # American Samoa ... SP=1 ! 

369 Be08Lb = _lazy('Be08Lb', _F(50.7978150), _F(4.359215833), _GRS80_, _F(49.8333339), _F(51.1666672), 

370 E0=_F(649328.0), N0=_F(665262.0), auth='EPSG:9802'), # Belgium 

371 Be72Lb = _lazy('Be72Lb', _90_0, _F(4.3674867), _NAD83_, _F(49.8333339), _F(51.1666672), 

372 E0=_F(150000.013), N0=_F(5400088.438), auth='EPSG:31370'), # Belgium 

373 Fr93Lb = _lazy('Fr93Lb', _F(46.5), _F(3), _WGS84_, _F(49), _F(44), 

374 E0=_F(700000), N0=_F(6600000), auth='EPSG:2154'), # RFG93, France 

375 MaNLb = _lazy('MaNLb', _F(33.3), _F(-5.4), _NTF_, _F(31.73), _F(34.87), 

376 E0=_F(500000), N0=_F(300000)), # Marocco 

377 MxLb = _lazy('MxLb', _F(12), _F(-102), _WGS84_, _F(17.5), _F(29.5), 

378 E0=_F(2500000), N0=_0_0, auth='EPSG:2155'), # Mexico 

379 PyT_Lb = _lazy('PyT_Lb', _F(46.8), _F(2.33722917), _NTF_, _F(45.89893890000052), _F(47.69601440000037), 

380 E0=_F(600000), N0=_F(200000), auth='Test'), # France? 

381 USA_Lb = _lazy('USA_Lb', _F(23), _F(-96), _WGS84_, _F(33), _F(45), 

382 E0=_0_0, N0=_0_0), # Conterminous, contiguous USA? 

383 WRF_Lb = _lazy('WRF_Lb', _F(40), _F(-97), _WGS84_, _F(33), _F(45), 

384 E0=_0_0, N0=_0_0, auth='EPSG:4326') # World 

385) 

386 

387 

388class LCCError(_ValueError): 

389 '''Lambert Conformal Conic C{LCC} or other L{Lcc} issue. 

390 ''' 

391 pass 

392 

393 

394class Lcc(_NamedBase): 

395 '''Lambert conformal conic East-/Northing location. 

396 ''' 

397 _conic = Conics.WRF_Lb # Lambert projection (L{Conic}) 

398 _easting = _0_0 # Easting (C{float}) 

399 _height = 0 # height (C{meter}) 

400 _northing = _0_0 # Northing (C{float}) 

401 

402 def __init__(self, e, n, h=0, conic=Conics.WRF_Lb, **name): 

403 '''New L{Lcc} Lamber conformal conic position. 

404 

405 @arg e: Easting (C{meter}). 

406 @arg n: Northing (C{meter}). 

407 @kwarg h: Optional height (C{meter}). 

408 @kwarg conic: Optional, the conic projection (L{Conic}). 

409 @kwarg name: Optional C{B{name}=NN} (C{str}). 

410 

411 @return: The Lambert location (L{Lcc}). 

412 

413 @raise LCCError: Invalid B{C{h}} or invalid or 

414 negative B{C{e}} or B{C{n}}. 

415 

416 @raise TypeError: If B{C{conic}} is not L{Conic}. 

417 ''' 

418 if conic not in (None, Lcc._conic): 

419 self.conic = conic 

420 self._easting = Easting(e, falsed=conic.E0 > 0, Error=LCCError) 

421 self._northing = Northing(n, falsed=conic.N0 > 0, Error=LCCError) 

422 if h: 

423 self._height = Height(h=h, Error=LCCError) 

424 if name: 

425 self.name = name 

426 

427 @Property 

428 def conic(self): 

429 '''Get the conic projection (L{Conic}). 

430 ''' 

431 return self._conic 

432 

433 @conic.setter # PYCHOK setter! 

434 def conic(self, conic): 

435 '''Set the conic projection (L{Conic}). 

436 

437 @raise TypeError: Invalid B{C{conic}}. 

438 ''' 

439 _xinstanceof(Conic, conic=conic) 

440 if conic != self._conic: 

441 _update_all(self) 

442 self._conic = conic 

443 

444# def dup(self, name=NN, **e_n_h_conic): # PYCHOK signature 

445# '''Duplicate this location with some attributes modified. 

446# 

447# @kwarg e_n_h_conic: Use keyword argument C{B{e}=...}, C{B{n}=...}, 

448# C{B{h}=...} and/or C{B{conic}=...} to override 

449# the current C{easting}, C{northing} C{height} 

450# or C{conic} projection, respectively. 

451# ''' 

452# def _args_kwds(e=None, n=None, **kwds): 

453# return (e, n), kwds 

454# 

455# kwds = _xkwds(e_n_h_conic, e=self.easting, n=self.northing, 

456# h=self.height, conic=self.conic, 

457# name=self._name__(name)) 

458# args, kwds = _args_kwds(**kwds) 

459# return self.__class__(*args, **kwds) # .classof 

460 

461 @Property_RO 

462 def easting(self): 

463 '''Get the easting (C{meter}). 

464 ''' 

465 return self._easting 

466 

467 @Property_RO 

468 def height(self): 

469 '''Get the height (C{meter}). 

470 ''' 

471 return self._height 

472 

473 @Property_RO 

474 def latlon(self): 

475 '''Get the lat- and longitude in C{degrees} (L{LatLon2Tuple}). 

476 ''' 

477 ll = self.toLatLon(LatLon=None, datum=None) 

478 return LatLon2Tuple(ll.lat, ll.lon, name=self.name) 

479 

480 @Property_RO 

481 def latlonheight(self): 

482 '''Get the lat-, longitude and height (L{LatLon3Tuple}C{(lat, lon, height)}). 

483 ''' 

484 return self.latlon.to3Tuple(self.height) 

485 

486 @Property_RO 

487 def latlonheightdatum(self): 

488 '''Get the lat-, longitude in C{degrees} with height and datum (L{LatLon4Tuple}C{(lat, lon, height, datum)}). 

489 ''' 

490 return self.latlonheight.to4Tuple(self.conic.datum) 

491 

492 @Property_RO 

493 def northing(self): 

494 '''Get the northing (C{meter}). 

495 ''' 

496 return self._northing 

497 

498 @Property_RO 

499 def philam(self): 

500 '''Get the lat- and longitude in C{radians} (L{PhiLam2Tuple}). 

501 ''' 

502 return PhiLam2Tuple(radians(self.latlon.lat), 

503 radians(self.latlon.lon), name=self.name) 

504 

505 @Property_RO 

506 def philamheight(self): 

507 '''Get the lat-, longitude in C{radians} and height (L{PhiLam3Tuple}C{(phi, lam, height)}). 

508 ''' 

509 return self.philam.to3Tuple(self.height) 

510 

511 @Property_RO 

512 def philamheightdatum(self): 

513 '''Get the lat-, longitude in C{radians} with height and datum (L{PhiLam4Tuple}C{(phi, lam, height, datum)}). 

514 ''' 

515 return self.philamheight.to4Tuple(self.datum) 

516 

517 @deprecated_method 

518 def to3lld(self, datum=None): # PYCHOK no cover 

519 '''DEPRECATED, use method C{toLatLon}. 

520 

521 @kwarg datum: Optional datum to use, otherwise use this 

522 B{C{Lcc}}'s conic.datum (C{Datum}). 

523 

524 @return: A L{LatLonDatum3Tuple}C{(lat, lon, datum)}. 

525 

526 @raise TypeError: If B{C{datum}} is not ellipsoidal. 

527 ''' 

528 if datum in (None, self.conic.datum): 

529 r = LatLonDatum3Tuple(self.latlon.lat, 

530 self.latlon.lon, 

531 self.conic.datum, name=self.name) 

532 else: 

533 r = self.toLatLon(LatLon=None, datum=datum) 

534 r = LatLonDatum3Tuple(r.lat, r.lon, r.datum, name=r.name) 

535 return r 

536 

537 def toLatLon(self, LatLon=None, datum=None, height=None, **LatLon_kwds): 

538 '''Convert this L{Lcc} to an (ellipsoidal) geodetic point. 

539 

540 @kwarg LatLon: Optional, ellipsoidal class to return the 

541 geodetic point (C{LatLon}) or C{None}. 

542 @kwarg datum: Optional datum to use, otherwise use this 

543 B{C{Lcc}}'s conic.datum (L{Datum}, L{Ellipsoid}, 

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

545 @kwarg height: Optional height for the point, overriding 

546 the default height (C{meter}). 

547 @kwarg LatLon_kwds: Optional, additional B{C{LatLon}} keyword 

548 arguments, ignored if C{B{LatLon} is None}. 

549 

550 @return: The point (B{C{LatLon}}) or a 

551 L{LatLon4Tuple}C{(lat, lon, height, datum)} 

552 if B{C{LatLon}} is C{None}. 

553 

554 @raise TypeError: If B{C{LatLon}} or B{C{datum}} is 

555 not ellipsoidal or not valid. 

556 ''' 

557 if LatLon: 

558 _xsubclassof(_LLEB, LatLon=LatLon) 

559 

560 c = self.conic 

561 if datum not in (None, self.conic.datum): 

562 c = c.toDatum(datum) 

563 

564 e = self.easting - c._E0 

565 n = c._r0 - self.northing + c._N0 

566 

567 r_ = copysign0(hypot(e, n), c._n) 

568 t_ = pow(r_ / c._aF, c._1_n) 

569 

570 x = c._xdef(t_) # XXX c._lam0 

571 for self._iteration in range(10): # max 4 trips 

572 p, x = x, c._xdef(t_ * c._pdef(x)) 

573 if fabs(x - p) < 1e-9: # XXX EPS too small? 

574 break 

575 lat = degrees90(x) 

576 lon = degrees180((atan1(e, n) + c._opt3) * c._1_n + c._lam0) 

577 

578 h = _heigHt(self, height) 

579 return _LL4Tuple(lat, lon, h, c.datum, LatLon, LatLon_kwds, 

580 inst=self, name=self.name) 

581 

582 def toRepr(self, prec=0, fmt=Fmt.SQUARE, sep=_COMMASPACE_, m=_m_, C=False, **unused): # PYCHOK expected 

583 '''Return a string representation of this L{Lcc} position. 

584 

585 @kwarg prec: Number of (decimal) digits, unstripped (C{int}). 

586 @kwarg fmt: Enclosing backets format (C{str}). 

587 @kwarg sep: Optional separator between name:values (C{str}). 

588 @kwarg m: Optional unit of the height, default meter (C{str}). 

589 @kwarg C: Optionally, include name of conic and datum (C{bool}). 

590 

591 @return: This Lcc as "[E:meter, N:meter, H:m, C:Conic.Datum]" 

592 (C{str}). 

593 ''' 

594 t, T = _fstrENH2(self, prec, m) 

595 if C: 

596 t += self.conic.name2, 

597 T += _C_, 

598 return _xzipairs(T, t, sep=sep, fmt=fmt) 

599 

600 def toStr(self, prec=0, sep=_SPACE_, m=_m_): # PYCHOK expected 

601 '''Return a string representation of this L{Lcc} position. 

602 

603 @kwarg prec: Number of (decimal) digits, unstripped (C{int}). 

604 @kwarg sep: Optional separator to join (C{str}) or C{None} 

605 to return an unjoined C{tuple} of C{str}s. 

606 @kwarg m: Optional height units, default C{meter} (C{str}). 

607 

608 @return: This Lcc as I{"easting nothing"} in C{meter} plus 

609 I{" height"} suffixed with B{C{m}} if height is 

610 non-zero (C{str}). 

611 ''' 

612 t, _ = _fstrENH2(self, prec, m) 

613 return t if sep is None else sep.join(t) 

614 

615 

616def toLcc(latlon, conic=Conics.WRF_Lb, height=None, Lcc=Lcc, 

617 **name_Lcc_kwds): 

618 '''Convert an (ellipsoidal) geodetic point to a I{Lambert} location. 

619 

620 @arg latlon: Ellipsoidal point (C{LatLon}). 

621 @kwarg conic: Optional Lambert projection to use (L{Conic}). 

622 @kwarg height: Optional height for the point, overriding the 

623 default height (C{meter}). 

624 @kwarg Lcc: Class to return the I{Lambert} location (L{Lcc}). 

625 @kwarg name_Lcc_kwds: Optional C{B{name}=NN} (C{str}) for the 

626 location and optional, additional B{C{Lcc}} keyword 

627 arguments, ignored if B{C{Lcc}} is C{None}. 

628 

629 @return: The I{Lambert} location (L{Lcc}) or an 

630 L{EasNor3Tuple}C{(easting, northing, height)} if 

631 C{B{Lcc} is None}. 

632 

633 @raise TypeError: If B{C{latlon}} is not ellipsoidal. 

634 ''' 

635 _xinstanceof(_LLEB, latlon=latlon) 

636 name, Lcc_kwds = _name2__(name_Lcc_kwds) 

637 

638 a, b = latlon.philam 

639 c = conic.toDatum(latlon.datum) 

640 

641 t = c._n * (b - c._lam0) - c._opt3 

642 st, ct = sincos2(t) 

643 

644 r = c._rdef(c._tdef(a)) 

645 e = c._E0 + r * st 

646 n = c._N0 + c._r0 - r * ct 

647 

648 h = _heigHt(latlon, height) 

649 r = EasNor3Tuple(e, n, h) if Lcc is None else \ 

650 Lcc(e, n, h=h, conic=c, **Lcc_kwds) 

651 return _xnamed(r, name) if name else r 

652 

653 

654if __name__ == '__main__': 

655 

656 from pygeodesy.interns import _NL_, _NLATvar_ 

657 from pygeodesy.lazily import printf 

658 

659 # __doc__ of this file, force all into registery 

660 t = _NL_ + Conics.toRepr(all=True, asorted=True) 

661 printf(_NLATvar_.join(t.split(_NL_))) 

662 

663# **) MIT License 

664# 

665# Copyright (C) 2016-2024 -- mrJean1 at Gmail -- All Rights Reserved. 

666# 

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

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

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

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

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

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

673# 

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

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

676# 

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

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

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

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

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

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

683# OTHER DEALINGS IN THE SOFTWARE.