Coverage for pyrdnap / rdnap2018.py: 93%
242 statements
« prev ^ index » next coverage.py v7.14.0, created at 2026-06-06 16:53 -0400
« prev ^ index » next coverage.py v7.14.0, created at 2026-06-06 16:53 -0400
2# -*- coding: utf-8 -*-
4u'''Main classes L{RDNAP2018v1} and L{RDNAP2018v2} follow C{variant 1} respectively C{variant 2} of
5the U{RDNAPTRANS(tm)2018_v220627<https://formulieren.kadaster.nl/aanvragen_rdnaptrans>} specification.
6Each provide a C{forward} method to convert geodetic lat-/longitudes and heights to C{RD} coodinates
7and C{NAP} heights and a C{reverse} method for converting the other way.
9The L{RDNAP2018v1.forward} and C{.reverse} results are within the C{RDNAPTRANS(tm)2018_v220627}
10self-validation requirements of C{0.000000010 degrees} respectively C{0.0010 meter} for points inside
11the C{RD} region, see B{C{Note below}}. Class L{RDNAP2018v2} does not.
13@note: L{RDNAP2018v1}, C{PyRDNAP} and C{pyrdnap} have B{not been formally validated} and are
14 B{not certified} to carry the trademark C{RDNAPTRANS(tm)}.
15'''
16# make sure int/int division yields float quotient, see .basics
17from __future__ import division as _; del _ # noqa: E702 ;
19from pyrdnap.rd0 import _RD, _RD0 as A0, RDNAP7Tuple
20from pyrdnap.v_grids import RDNAPError, _V_grid, _v_gridz_import
21from pyrdnap.__pygeodesy import (_0_0, _0_5, _1_0, _2_0,
22 _isNAN, _isNAN0, _earth_datum,
23 _ALL_DOCS, _ALL_OTHER, _FOR_DOCS,
24 _NamedBase, notOverloaded)
25from pygeodesy import (map1, EPS0, EPS1, NAN, PI_2, PI, PI2, # "consterns"
26 Datum, Ellipsoid, LatLonDatum3Tuple, # datums, ellipsoids
27 property_RO, property_ROnce, # props
28 Lamd, Lat, Lon, Phid, # units
29 sincos2, sincos2d) # utily
31from math import asin, atan, copysign, degrees, exp, \
32 fabs, floor, hypot, radians, sin, sqrt
34__all__ = ()
35__version__ = '26.06.06'
37_TOL_D = 1e-9 # degrees 2.3.3f+
38_TOL_M = 1e-6 # meter
39_TOL_R = radians(_TOL_D) # 2e-11
40_TRIPS = 16 # 5..6 sufficient
43class _RDNAPbase(_NamedBase): # in .rd0._RD.regionB
44 '''(INTERNAL) L{RDNAP2018v1}C{/-v2} base class.
45 '''
46 _datum = None # forward, v1 reverse Datum, lazily (GRS80)
47 _EETRS = None # forward, v1 reverse Ellipsoid, lazily
48 _raiser = False
50 def __init__(self, a_ellipsoid=None, f=None, raiser=False, **name):
51 '''New C{RDNAP2018v1} or C{-v2} instance.
53 @kwarg a_ellipsoid: An ellipsoid (L{Ellipsoid}) or the ellipsoid's equatorial
54 radius (C{scalar}, conventionally in C{meter}), see B{C{f}}
55 or a datum (L{Datum}). Default C{Datums.GRS80} for ETRS89.
56 @kwarg f: The flattening of the ellipsoid (C{scalar}) if B{C{a_ellipsoid}} is
57 specified as C{scalar}, ignored otherwise.
58 @kwarg raiser: If C{True} raise an L{RDNAPError} for lat-/longitudes outside
59 the C{RD} region (C{bool}).
60 @kwarg name: Optional name (C{str}).
62 @raise RDNAPError: Ellipsoid (or datum) is not oblate (i.e. is spherical or
63 prolate) or the datum's C{transform} is not C{unity}.
64 '''
65 if a_ellipsoid is f is None:
66 self._datum = A0.D80 # GRS80 (ETRS89)
67 else:
68 _earth_datum(self, a_ellipsoid, f, **name) # sets self._datum
69 self._EETRS = E = self._datum.ellipsoid
70 if not E.isOblate:
71 raise RDNAPError('not oblate: %r' % (E,))
72 if raiser: # PYCHOK no cover
73 T = self._datum.transform
74 if not T.isunity:
75 raise RDNAPError('not unity: %r' % (T,))
76 self._raiser = True
77 if name:
78 self.name = name
80 def forward(self, lat, lon, height=0, raiser=None, name='forward'):
81 '''Convert GRS80 (ETRS98) geodetic C{(B{lat}, B{lon})} and B{C{height}}
82 to local C{(RDx, RDy)} coordinates and C{NAPh} quasi-geoid-height.
84 @arg lat: Latitude (C{degrees} geodetic).
85 @arg lon: Longitude (C{degrees} geodetic).
86 @kwarg height: Height, optional (C{meter} above geoid) or C{NAN}
87 to ignore C{NAPh} interpolation.
88 @kwarg raiser: If C{True} raise an L{RDNAPError} if B{C{lat}} or
89 B{C{lon}} is outside the C{RD} region (C{bool}),
90 if C{False} don't, overriding property C{raiser}.
91 @kwarg name: Optional name (C{str}).
93 @return: An L{RDNAP7Tuple}C{(RDx, RDy, NAPh, lat, lon, height, datum)}
94 with local C{RDx}, C{RDy} coordinates and C{NAPh} height.
95 '''
96 lat, lon = Lat(lat), Lon(lon)
97 lat0, lon0 = \
98 lat_, lon_ = self._forwardXform2(raiser, lat, lon)
99 for _ in range(_TRIPS): # 2.3.3a-f, 1..2
100 latc, lonc = self._rdlatlon2(lat_, lon_, lat0, lon0)
101 if fabs(latc - lat_) < _TOL_D and \
102 fabs(lonc - lon_) < _TOL_D:
103 break
104 lat_, lon_ = latc, lonc
106 phiClamC = _ellipsoidal2spherical(latc, lonc)
107 RDx, RDy = _spherical2oblique(*phiClamC)
108 NAPh = NAN if _isNAN(height) else (height - # NOT lat0, lon0
109 self._rdNAPh_v(lat, lon, latc, lonc)) # 2.5.2
110 return RDNAP7Tuple(RDx, RDy, NAPh,
111 lat, lon, height, self.forwardDatum, name=name)
113 def forward3(self, lat, lon, name='forward3'):
114 '''Datum transform C{(B{lat}, B{lon})} from GRS80 (ETRS98) to Bessel1841 (RD-Bessel).
116 @return: A L{LatLonDatum3Tuple}C{(lat, lon, datum)}.
117 '''
118 x, y, z = _geodetic2cartesian(lat, lon, A0.H0_ETRS, self._EETRS)
119 x, y, z = _RD._xETRS2RD.transform(x, y, z)
120 lat, lon = _cartesian2geodetic(x, y, z, A0.E0)
121 return LatLonDatum3Tuple(lat, lon, A0.D0, name=name)
123 @property_RO
124 def forwardDatum(self):
125 '''Get the C{forward} datum (L{Datum}, default GRS80).
126 '''
127 return self._datum
129 def _inside2(self, raiser, lat, lon):
130 # default and variant 2: no datum Xform
131 if (raiser or (raiser is None and self._raiser)) and \
132 not _RD.isinside(lat, lon):
133 raise self._outsidError(lat, lon)
134 return lat, lon
136 _forwardXform2 = _inside2 # no datum Xform
138 def isinside(self, lat, lon, eps=0, B=False):
139 '''Is geodetic C{(B{lat}, B{lon})} inside the C{RD} region (C{bool})?
141 @kwarg eps: Over-/undersize the C{RD} region (C{degrees}).
142 @kwarg B: Use C{B{B}=True} for the C{RD-Bessel} regionB in case
143 C{(B{lat}, B{lon})} are Bessel1841 and not GRS80.
144 '''
145 return _RD.isinside(Lat(lat), Lon(lon), eps, B)
147 def _outsidError(self, *lat_lon):
148 # format an RDNAPError for C{lat_lon} outside C{RD} region
149 return RDNAPError('%r outside %s' % (lat_lon, self.region))
151 @property_RO
152 def _rdgrid(self): # PYCHOK no cover
153 raise notOverloaded(self)
155 def _rdlatlon2(self, lat, lon, lat0=None, lon0=None): # 2.3.2
156 # return the RD-corrected C{(lat, lon)}
157 if _RD.isinside(lat, lon):
158 c_f_N_f6 = _RD._c_f_N_f6(lat, lon)
159 lat_corr = _bilinear(self._rdgrid._lat_corr, *c_f_N_f6)
160 lon_corr = _bilinear(self._rdgrid._lon_corr, *c_f_N_f6)
162 if lat0 is lon0 is None: # reverse
163 lat += lat_corr
164 lon += lon_corr
165 else: # forward
166 lat = lat0 - lat_corr
167 lon = lon0 - lon_corr
168 return lat, lon # NAN, NAN?
170 def rdNAPh(self, lat, lon, raiser=False): # 2.5.1 and 3.5
171 '''Interpolate the C{NAPh} quasi-geoid-height I{within} the C{RD} region.
173 @arg lat: Latitude (C{degrees} geodetic).
174 @arg lon: Longitude (C{degrees} geodetic).
175 @kwarg raiser: If C{True} raise an L{RDNAPError} if B{C{lat}} or
176 B{C{lon}} is outside the C{RD} region (C{bool}),
177 otherwise don't and return C{NAN}.
179 @return: C{NAPh} (C{meter}) or C{NAN} if C{B{raiser} is False} and
180 B{C{lat}} or B{C{lon}} is outside the C{RD} region.
181 '''
182 return self._rdNAPh(Lat(lat), Lon(lon), raiser)
184 def _rdNAPh(self, lat, lon, raiser):
185 # return C{NAPh} at C{(lat, lon)} or C{NAN} if outside
186 if _RD.isinside(lat, lon): # eps=0
187 c_f_N_f6 = _RD._c_f_N_f6(lat, lon)
188 return _bilinear(self._rdgrid._NAP_h, *c_f_N_f6)
189 elif raiser:
190 raise self._outsidError(lat, lon)
191 return NAN # c0 2.5.1e+
193 def _rdNAPh_v(self, lat1, lon1, lat2, lon2):
194 # get C{NAPh} at geodetic C{lat1, lon1} for variant 1 or at the
195 # RD-corrected or inverse-projected C{lat2, lon2} for variant 2
196 if self.variant == 2:
197 lat1, lon1 = lat2, lon2
198 return self._rdNAPh(lat1, lon1, False)
200 @property_ROnce
201 def region(self):
202 '''Get the C{RD} region as L{Bounds4Tuple}C{(latS, lonW, latN, lonE)}, all C{GRS80 (ETRS89) degrees}.
203 '''
204 return _RD.region
206 @property_ROnce
207 def regionB(self): # in .rd0._RD.regionB
208 '''Get the C{RD} region as L{Bounds4Tuple}C{(latS, lonW, latN, lonE)}, all C{Bessel1841 (RD-Bessel) degrees}.
209 '''
210 S, W, N, E = r = self.region # Bounds4Tuple
211 _R = _RDNAPbase
212 _f = _R().forward3
213 nB = r.name.replace(_R.region.name, _R.regionB.name)
214 s, w, _ = _f(S, W)
215 n, e, _ = _f(N, E)
216 return r.classof(s, w, n, e, name=nB) # r.dup(latS=S, ...)
218 def reverse3(self, lat, lon, name='reverse3'):
219 '''Datum transform C{(B{lat}, B{lon})} from Bessel1841 (RD-Bessel) to GRS80 (ETRS98).
221 @return: A L{LatLon3Tuple}C{(lat, lon, datum)}.
222 '''
223 x, y, z = _geodetic2cartesian(lat, lon, A0.H0, A0.E0)
224 x, y, z = _RD._xRD2ETRS.transform(x, y, z)
225 lat, lon = _cartesian2geodetic(x, y, z, self._EETRS)
226 return LatLonDatum3Tuple(lat, lon, self.forwardDatum, name=name)
228 def _reverse(self, RDx, RDy, NAPh, toRD, raiser=None, name='reverse'):
229 '''(INTERNAL) Convert local C{(B{RDx}, B{RDy})} coordinates and
230 B{C{NAPh}} quasi-geoid-height to GRS80 (ETRS89) or Bessel1841
231 (RD-Bessel) geodetic C{lat}, C{lon} and C{height}.
232 '''
233 phiClamC = _oblique2spherical(RDx, RDy)
234 latlon = _spherical2ellipsoidal(*phiClamC)
236 latc, lonc = self._rdlatlon2(*latlon)
237 lat, lon, _ = self._reverseXform3(raiser, latc, lonc)
238 h = NAN if _isNAN(NAPh) else (NAPh +
239 self._rdNAPh_v(lat, lon, *latlon))
241 if toRD: # RD_Bessel
242 lat, lon, d = latc, lonc, A0.D0
243 else: # GRS80 (ETRS89)
244 d = self._EETRS
245 return RDNAP7Tuple(RDx, RDy, NAPh,
246 lat, lon, h, d, name=name)
248 @property_RO
249 def reverseDatum(self):
250 '''Get the I{default} C{reverse} datum (L{Datum}), GRS80 or Bessel1841.
251 '''
252 return {1: self._datum, # A0.D80
253 2: A0.D0}.get(self.variant)
255 def _reverseXform3(self, *raiser_lat_lon):
256 # datum transform C{(lat, lon)} from RD-Bessel to GRS80 (ETRS89)
257 # and raise an C{RDNAPError} if outside the C{RD} region
258 lat, lon = self._inside2(*raiser_lat_lon)
259 return self.reverse3(lat, lon)
261 def similarity(self, inverse=None): # PYCHOK no cover
262 raise notOverloaded(self, inverse=inverse) # PYCHOK None
264 def toStr(self, prec=9, **unused): # PYCHOK signature
265 '''Return this C{RDNAP20181v1} or C{-v2} instance as a string.
267 @kwarg prec: Precision, number of decimal digits (0..9).
269 @return: This C{RDNAP2018v1} or C{-v2} (C{str}).
270 '''
271 return self.attrs('name', 'variant', 'forwardDatum', prec=prec) # _ellipsoid_, _name__
273 @property_RO
274 def variant(self): # PYCHOK no cover
275 raise notOverloaded(self)
278class RDNAP2018v1(_RDNAPbase):
279 '''Transformer implementing C{variant 1} of U{RD NAP 2018 v220627
280 <https://formulieren.kadaster.nl/aanvragen_rdnaptrans>}.
282 @note: Method L{RDNAP2018v1.reverse} returns B{GRS80 (ETRS89)}
283 geodetic lat- and longitudes.
285 @note: L{RDNAP2018v1} has B{not been formally validated} and is
286 B{not certified} to carry the trademark C{RDNAPTRANS(tm)}.
287 '''
288 if _FOR_DOCS:
289 __init__ = _RDNAPbase.__init__
290 forward = _RDNAPbase.forward
291 forward3 = _RDNAPbase.forward3
292 reverse3 = _RDNAPbase.reverse3
294 def _forwardXform2(self, raiser, lat, lon):
295 # datum transform C{(lat, lon)} from GRS80 (ETRS89) to RD-Bessel
296 # and raise an C{RDNAPError} if outside the C{RD} region
297 lat, lon, _ = self.forward3(lat, lon)
298 return self._inside2(raiser, lat, lon)
300 @property_ROnce
301 def _rdgrid(self):
302 try:
303 from pyrdnap import v1grid
304 except (AttributeError, ImportError, RDNAPError):
305 v1grid = _v_gridz_import(self.variant)
306 return v1grid
308 def reverse(self, RDx, RDy, NAPh=0, toRD=False, **raiser_name):
309 '''Convert a local C{(B{RDx}, B{RDy})} point and B{C{NAPh}} height to
310 B{GRS80 (ETRS89)} geodetic C{(lat, lon, height)}, by default.
312 @arg RDx: Local C{RD} X (C{meter}, conventionally).
313 @arg RDy: Local C{RD} Y (C{meter}, conventionally).
314 @kwarg NAPh: C{NAP} quasi-geoid-height (C{meter}, conventionally)
315 or C{NAN} to ignore C{NAPh} interpolation.
316 @kwarg toRD: Use C{B{toRD}=True} to return Bessel1841 (RD-Bessel)
317 lat- and longitudes instead of GRS80 (ETRS89) (C{bool}).
318 @kwarg raiser_name: Like the C{forward} method, C{B{raiser}=None}
319 (C{bool}) and optional C{B{name}='reverse'} (C{str}).
321 @return: An L{RDNAP7Tuple}C{(RDx, RDy, NAPh, lat, lon, height, datum)}
322 with geodetic C{lat} and C{lon}, C{height} and C{datum}
323 B{GRS80 (ETRS89)} or C{Bessel1841 (RD-Bessel)}.
324 '''
325 return self._reverse(RDx, RDy, NAPh, toRD, **raiser_name)
327 def similarity(self, inverse=False):
328 '''Get the similarity transform (C{Similarity}).
330 @kwarg inverse: Use C{True} for the C{reverse} or C{False}
331 for the C{forward} transform (C{bool}).
332 '''
333 return _RD._xRD2ETRS if inverse else _RD._xETRS2RD
335 @property_ROnce
336 def variant(self):
337 '''Get this C{RDNAP2018}'s variant (C{int}).
338 '''
339 return 1
342class RDNAP2018v2(_RDNAPbase):
343 '''Transformer implementing C{variant 2} of U{RD NAP 2018 v220627
344 <https://formulieren.kadaster.nl/aanvragen_rdnaptrans>}.
346 @note: Method L{RDNAP2018v2.reverse} returns by default B{Bessel1841
347 (RD-Bessel)} and optionally GRS80 (ETRS89) geodetic lat- and
348 longitudes.
350 @note: L{RDNAP2018v2} has B{not been formally validated} and is
351 B{not certified} to carry the trademark C{RDNAPTRANS(tm)}.
352 '''
353 if _FOR_DOCS:
354 __init__ = _RDNAPbase.__init__
355 forward = _RDNAPbase.forward
356 forward3 = _RDNAPbase.forward3
357 reverse3 = _RDNAPbase.reverse3
359 @property_ROnce
360 def _rdgrid(self):
361 try:
362 from pyrdnap import v2grid
363 except (AttributeError, ImportError, RDNAPError):
364 v2grid = _v_gridz_import(self.variant)
365 return v2grid
367 def reverse(self, RDx, RDy, NAPh=0, toRD=True, **raiser_name):
368 '''Convert a local C{(B{RDx}, B{RDy})} point and B{C{NAPh}} height to
369 B{Bessel1841 (RD-Bessel)} geodetic C{(lat, lon, height)}, by default.
371 @arg RDx: Local C{RD} X (C{meter}, conventionally).
372 @arg RDy: Local C{RD} Y (C{meter}, conventionally).
373 @kwarg NAPh: C{NAP} quasi-geoid-height (C{meter}, conventionally)
374 or C{NAN} to ignore C{NAPh} interpolation.
375 @kwarg toRD: Use C{B{toRD}=False} to return GRS80 (ETRS89) lat- and
376 longitudes instead of Bessel1841 (RD-Bessel) (C{bool}).
377 @kwarg raiser_name: Like the C{forward} method, C{B{raiser}=None}
378 (C{bool}) and optional C{B{name}='reverse'} (C{str}).
380 @return: An L{RDNAP7Tuple}C{(RDx, RDy, NAPh, lat, lon, height, datum)}
381 with geodetic C{lat} and C{lon}, C{height} and C{datum}
382 B{Bessel1841 (RD-Bessel)} or C{GRS80 (ETRS89)}.
383 '''
384 return self._reverse(RDx, RDy, NAPh, toRD, **raiser_name)
386 def similarity(self, *unused): # PYCHOK signature
387 '''Get the similarity transform, always C{None}.
388 '''
389 return None
391 @property_ROnce
392 def variant(self):
393 '''Get this C{RDNAP2018}'s variant (C{int}).
394 '''
395 return 2
398def _atan3(y, x, x0): # 2.2.3e and 3.1.1i
399 # equiv to math.atan2 iff x0 is y
400 if x > 0:
401 r = atan(y / x)
402 elif x < 0:
403 r = atan(y / x) + copysign(PI, x0)
404 else:
405 r = copysign(PI_2, x0) if x0 else _0_0
406 return r
409def _atan_exp(w): # 2.4.1c
410 return atan(exp(w)) * _2_0 - PI_2
413def _bilinear(v_grid, c_latI, f_latI, latN_f, # 2.3.1f and g
414 c_lonI, f_lonI, lonN_f):
415 # interpolate a lat_corr_, lon_corr_ or NAP_...
416 assert isinstance(v_grid, _V_grid), v_grid
417 ne = v_grid(c_latI, c_lonI)
418 nw = v_grid(c_latI, f_lonI)
419 se = v_grid(f_latI, c_lonI)
420 sw = v_grid(f_latI, f_lonI)
421 lonN_f1 = _1_0 - lonN_f # == 1 - (lonN - f_lonN)
422 return (ne * lonN_f + nw * lonN_f1) * latN_f + \
423 (se * lonN_f + sw * lonN_f1) * (_1_0 - latN_f)
426def _cartesian2geodetic(x, y, z, E): # 2.2.3 == EcefUPC.reverse?
427 # convert cartesian C{(x, y, z)} to C{E}-geodetic C{(lat, lon)}
428 r = hypot(x, y)
429 if r > _TOL_M:
430 a = E.a * E.e2
431 phi_ = atan(z / r) # atan2(z, r)
432 for _ in range(_TRIPS): # 4..6
433 s = sin(phi_)
434 s *= a / sqrt(_1_0 - s**2 * E.e2)
435 phi = atan((z + s) / r) # atan2(z + s, r)
436 if fabs(phi - phi_) < _TOL_R:
437 break
438 phi_ = phi
439 else:
440 phi = copysign(PI_2, z)
441 lam = _atan3(y, x, y)
442 return map1(degrees, phi, lam) # lat, lon
445def _ellipsoidal2spherical(lat, lon): # 2.4.1
446 # convert geodetic C{(lat, lon)} to spherical C{(𝛷, 𝛬)}
447 phiC = phi = Phid(lat)
448 if PI_2 > phi > -PI_2: # 2.4.1c
449 q = A0.log_tan(phi) - A0.log_e_2(phi)
450 w = A0.N0 * q + A0.M0 # 2.4.1b
451 phiC = _atan_exp(w)
452 lamC = (Lamd(lon) - A0.LAM0) * A0.N0 + A0.LAM0C # 2.4.1d
453 return phiC, lamC # -Capital 𝛷, 𝛬
456def _eq0(r, r0=_0_0):
457 return fabs(r - r0) < _TOL_R
460# def _eq0d(d, d0=_0_0):
461# return fabs(d - d0) < _TOL_D
464def _geodetic2cartesian(lat, lon, h, E): # 2.2.1
465 # convert C{E}-geodetic C{(lat, lon)} to cartesian C{(x, y, z)}
466 y, x = sincos2d(lon)
467 z, c = sincos2d(lat)
468 n = E.a / sqrt(_1_0 - z**2 * E.e2)
469 H = _isNAN0(h)
470 c *= n + H
471 x *= c
472 y *= c
473 z *= n * (_1_0 - E.e2) + H
474 return x, y, z
477def _ne0(r, r0=_0_0):
478 return fabs(r - r0) > _TOL_R
481# def _ne0d(d, d0=_0_0):
482# return fabs(d - d0) > _TOL_D
485def _oblique2spherical(x, y): # 3.1.1
486 # inverse oblique stereographic conformal projection from
487 # C{RD (x, y)} to spherical C{(𝛷, 𝛬)}, see C++ function
488 # sterea_e_inverse in U{Proj/src/projections/sterea.cpp
489 # <https://Proj.org/en/stable/operations/projections/sterea.html>}
490 x -= A0.X0
491 y -= A0.Y0
492 r = hypot(x, y)
493 if r > _TOL_M: # x and y
494 s0, c0 = A0.sincos2PHI0C
495 sp, cp = sincos2(atan(r / A0.RK2) * _2_0) # psi atan2(r, A0.RK2)
496 ca = sp * y / r
497 xN = cp * c0 - ca * s0
498 yN = sp * x / r
499 zN = cp * s0 + ca * c0
500 phiC = asin(zN)
501 else:
502 _, xN = A0.sincos2PHI0C
503 yN = _0_0
504 phiC = A0.PHI0C # asin(sin(PHI0C))
505 lamC = _atan3(yN, xN, x) + A0.LAM0C
506 return phiC, lamC # -Capital 𝛷, 𝛬
509def _spherical2ellipsoidal(phiC, lamC): # 3.1.2
510 # inverse Gauss conformal projection from
511 # spherical C{(𝛷, 𝛬)} to geodetic C{(lat, lon)}
512 phi = phiC
513 if PI_2 > phi > -PI_2:
514 q = (A0.log_tan(phi) - A0.M0) / A0.N0
515# w = A0.log_tan(phi)
516 for _ in range(_TRIPS): # 3..6
517 phi_ = phi
518 phi = _atan_exp(A0.log_e_2(phi) + q)
519 if fabs(phi - phi_) < _TOL_R:
520 break
521 lam = (lamC - A0.LAM0C) / A0.N0 + A0.LAM0
522 lam += floor((PI - lam) / PI2) * PI2
523 return map1(degrees, phi, lam) # lat, lon
526def _spherical2oblique(phiC, lamC): # 2.4.2
527 # oblique stereographic conformal projection
528 # from spherical C{(𝛷, 𝛬)} to C{RD (x, y)}
529 x = A0.X0 # 2.4.2g
530 y = A0.Y0 # 2.4.2h
531 a = phiC - A0.PHI0C # 𝛷 - 𝛷0
532 b = lamC - A0.LAM0C # 𝛬 - 𝛬0
533 if (_ne0(a) or _ne0(b)) and (_ne0(phiC, -A0.PHI0C) or
534 _ne0(lamC, -A0.LAM0C + PI)):
535 s0, c0 = A0.sincos2PHI0C # sin(𝛷0), cos(𝛷0)
536 s, c = sincos2(phiC) # sin(𝛷), cos(𝛷)
537 sp_22 = sin(a * _0_5)**2 + \
538 sin(b * _0_5)**2 * c * c0 # sin(𝜓/2)**2
539 if EPS0 < sp_22 < EPS1:
540 # r = 2kR * tan(𝜓/2)
541 # q = r / (sin(𝜓/2) * cos(𝜓/2) * 2)
542 # = 2kR * sin(𝜓/2) / (sin(𝜓/2) * cos(𝜓/2)**2 * 2)
543 # = 2kR / (cos(𝜓/2)**2 * 2)
544 # = 2kR / ((1 - sin(𝜓/2)**2) * 2)
545 # = 2kR / (2 - sin(𝜓/2)**2 * 2)
546 t = sp_22 * _2_0 # 0 < t < 2
547 q = A0.RK2 / (_2_0 - t)
548 x += q * (c * sin(b))
549 y += q * (s - s0 + s0 * t) / c0
550 elif _eq0(a) and _eq0(b):
551 pass
552 else: # if _eq0(phiC, -A0.PHI0C) and _eq0(lamC, A0.LAM0C - PI):
553 x = y = NAN
554# else:
555# raise RDNAPError(str((phiC, lamC)))
556 return x, y
559__all__ += _ALL_DOCS(_RDNAPbase)
560__all__ += _ALL_OTHER(RDNAP2018v1, RDNAP2018v2, RDNAPError,
561 Datum, Ellipsoid, LatLonDatum3Tuple) # passed along from PyGeodesy
563# **) MIT License
564#
565# Copyright (C) 2026-2026 -- mrJean1 at Gmail -- All Rights Reserved.
566#
567# Permission is hereby granted, free of charge, to any person obtaining a
568# copy of this software and associated documentation files (the "Software"),
569# to deal in the Software without restriction, including without limitation
570# the rights to use, copy, modify, merge, publish, distribute, sublicense,
571# and/or sell copies of the Software, and to permit persons to whom the
572# Software is furnished to do so, subject to the following conditions:
573#
574# The above copyright notice and this permission notice shall be included
575# in all copies or substantial portions of the Software.
576#
577# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
578# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
579# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
580# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
581# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
582# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
583# OTHER DEALINGS IN THE SOFTWARE.