Coverage for pyrdnap / rd0.py: 98%
190 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'''(INTERNAL) RijksDriehoeksmeting C{_RD} and reference C{_RD0}
5constants and classes C{RDNAP7Tuple} and C{LqRD}.
6'''
7# make sure int/int division yields float quotient, see .basics
8from __future__ import division as _; del _ # noqa: E702 ;
10from pyrdnap.v_grids import _v_assert
11from pyrdnap.__pygeodesy import (_0_0, _0_5, _1_0, _2_0, # PYCHOK used!
12 _isNAN, _isNAN0, _xinstanceof, _xsubclassof,
13 _LLEB, _xkwds,
14 _COMMASPACE_, _datum_, _lat_, _lon_, _height_,
15 _ALL_OTHER, _FOR_DOCS, _Pass, _NamedTuple)
16from pygeodesy import (NAN, NN, map1, map2, # basics, "consterns"
17 Datum, Datums, Similarity, # datums
18 Bounds4Tuple, LatLon2Tuple, PhiLam2Tuple, # namedTuples
19 Vector2Tuple, Vector3Tuple, LqRD as _LqRD, # ltp
20 Property_RO, property_ROnce, # props
21 pairs, # streprs
22 Height, Lamd, Lat, Lon, Meter, Phi, Phid, # units
23 sincos2, tanPI_2_2) # utily
25from math import atan2, ceil, fabs, floor, log, sin, sqrt
27__all__ = ()
28__version__ = '26.06.06'
30_LQRD = _LqRD() # get Amersfoort, bounds, etc. (deleted below)
33def _c_f_N_f3(*deg_SW_D):
34 # return int(ceil) and int(floor) of Normalized
35 # and (Normalized less floor) of C{deg} degrees
36 N = _degN(*deg_SW_D)
37 # assert N >= 0, N
38 f = floor(N)
39 return int(ceil(N)), int(f), (N - f)
42def _degN(deg, degSW, deg_D):
43 # return C{deg} Normalized
44 return (deg - degSW) * deg_D
47class _RDbase(object):
48 '''(INTERNAL) Base.
49 '''
50 def _preDict(self, _pred, **d):
51 # return updated dict C{d}
52 for n in self.__class__.__dict__.keys():
53 if _pred(n):
54 d[n] = getattr(self, n)
55 return d
57 def toStr(self, prec=9, **fmt_ints):
58 # return this C{_RDx} as string
59 d = self._toDict() # PYCHOK OK
60 t = pairs(d, prec=prec, **fmt_ints)
61 return _COMMASPACE_(*t)
64class _RD(_RDbase):
65 '''(INTERNAL) Bounds, constants for RDNAP2018 (ASCII.txt).
66 '''
67 lat_D = Lat(lat_D=80.0) # == 1 / 0.0125 # degrees, all
68 lon_D = Lon(lon_D=50.0) # == 1 / 0.02
70 def __init__(self):
71 S, W, N, E = self.region
72 nlat = _degN(N, S, self.lat_D) + _1_0 # 2.3.2g n-phi
73 nlon = _degN(E, W, self.lon_D) + _1_0 # 2.3.2g n-lambda
74 _v_assert(map1(int, nlat, nlon))
76 def _c_f_N_f6(self, lat, lon):
77 # return (int(ceil), int(floor), Normalized less floor) of C{lat}) + \
78 # (int(ceil), int(floor), Normalized less floor) of C{lon})
79 return _c_f_N_f3(lat, self.region.latS, self.lat_D) + \
80 _c_f_N_f3(lon, self.region.lonW, self.lon_D)
82 def isinside(self, lat, lon, eps=0, B=False): # eps=_TOL_D, 0 or -_TOLD_D
83 # is C{(lat, lon)} inside the this C{RD} region[B], optionally
84 # over-/undersized by positive respectively negative C{eps}?
85 S, W, N, E = self.regionB if B else self.region
86 # XXX use "< N" and "< E" instead of "<="?
87 return ((S - lat) <= eps and (lat - N) <= eps and
88 (W - lon) <= eps and (lon - E) <= eps) if eps else \
89 (S <= lat <= N and W <= lon <= E)
91 region = _LQRD.region # as GRS80 L{Bounds4Tuple}
93 @property_ROnce
94 def regionB(self): # as Bessel1841 L{Bounds4Tuple}
95 from pyrdnap.rdnap2018 import _RDNAPbase as _R
96 return _R().regionB
98 def _toDict(self):
99 def _p(n): # lambda
100 return n.endswith('D') or n.endswith('S')
102 return self._preDict(_p, region=self.region, regionB=self.regionB)
104 @property_ROnce
105 def _xETRS2RD(self): # transform ETRS (GRS80) to RD-Bessel
106 return Similarity(tx=-565.7346, ty=-50.4058, tz=-465.2895, s=-4.07242,
107 rx=-1.91513, ry=1.60365, rz=-9.09546, name='_xETRS2RD')
109 @property_ROnce
110 def _xRD2ETRS(self): # transform RD-Bessel to ETRS (GRS80)
111 return Similarity(tx=565.7381, ty=50.4018, tz=465.2904, s=4.07244,
112 rx=1.91514, ry=-1.60363, rz=9.09546, name='_xRD2ETRS')
114 # % python -c "import pyrdnap; print(pyrdnap.rd0._RD.toStr())"
115 # _xETRS2RD=Similarity(name='_xETRS2RD', tx=-565.73, ty=-50.406, tz=-465.29, s=-4.0724,
116 # rx=-1.9151, ry=1.6037, rz=-9.0955),
117 # _xRD2ETRS=Similarity(name='_xRD2ETRS', tx=565.74, ty=50.402, tz=465.29, s=4.0724,
118 # rx=1.9151, ry=-1.6036, rz=9.0955),
119 # lat_D=80.0, lon_D=50.0,
120 # region=RD region (latS=50.0, lonW=2.0, latN=56.0, lonE=8.0),
121 # regionB=RD regionB (latS=50.000724, lonW=1.999968, latN=56.001439, lonE=8.000842)
123_RD = _RD() # PYCHOK singleton, in .test/testRndTrips
126class _RD0(_RDbase):
127 '''(INTERNAL) C{RD} Amersfoort, NL / C{RD New} constants for RDNAP2018 (ASCII.txt).
129 @see: U{EPSG:9809<https://EPSG.io/9809-method>}, U{"Oblique Stereographic"
130 <https://PROJ.org/en/stable/operations/projections/sterea.html>} and
131 <http://geotiff.maptools.org/proj_list/oblique_stereographic.html>
132 '''
133 H0 = Meter(H0 =_LQRD.height0) # Amersfoort.height0 0.0 m
134 H0_ETRS = Meter(H0_ETRS=_LQRD.height0_ETRS) # 43.0 m
135 K0 = 0.9999079 # 2.4.1 scale factor
136 LAT0 = Lat(LAT0=_LQRD.Amersfoort.lat) # '52 9 22.178N' == 52.156160555555+°
137 LON0 = Lon(LON0=_LQRD.Amersfoort.lon) # ' 5 23 15.5E' == 5.387638888888+°
138 LAM0C = \
139 LAM0 = Lamd(LAM0=LON0) # 𝜆0, 𝛬0 = 𝜆0 on sphere 0.094032038
140 PHI0 = Phid(PHI0=LAT0) # 𝜑0 0.910296727, PHI0C 𝛷0 set below
141 X0 = Meter(X0=155000.0) # false Easting 155029.784?
142 Y0 = Meter(Y0=463000.0) # false Norting 463109.889?
144# @property_ROnce
145# def C0(self): # c, sphere
146# s, _ = self.sincos2PHI0
147# w = self._w1(s)
148# c = (w - _1_0) / (w + _1_0)
149# return (((self.N0 + s) * (_1_0 - c)) /
150# ((self.N0 - s) * (_1_0 + c)))
152# def chilam(self, lat, lon): # EPSG:9809
153# # return 2-tuple (chi, lam), conformal in radians
154# s, _ = sincos2d(lat)
155# w2 = self._w1(s) * self.C0
156# s = (w2 - _1_0) / (w2 + _1_0)
157# r = radians(lon - self.LON0) * self.N0
158# return asin(s), r
160 @property_ROnce
161 def D0(self): # lazily
162 return Datums.Bessel1841
164 @property_ROnce
165 def D80(self): # lazily
166 return Datums.GRS80
168 @property_ROnce
169 def E0(self): # lazily
170 return self.D0.ellipsoid
172 def log_e_2(self, phi):
173 e = self.E0.e
174 p = e * sin(phi)
175 return log((_1_0 + p) / (_1_0 - p)) * (e * _0_5)
177 def log_tan(self, phi):
178 return log(tanPI_2_2(phi)) # tan((phi + PI/2) / 2)
180 @property_ROnce
181 def M0(self): # 2.4.1 p 15 m
182 return self.W0 - self.N0 * self.Q0
184 @property_ROnce
185 def N0(self): # 2.4.1 p 15 n, sphere
186 E = self.E0
187 _, c = self.sincos2PHI0
188 return sqrt(c**4 * E.e2 / E.e21 + _1_0)
190 @property_ROnce
191 def PHI0C(self): # 2.4.1 p 15 𝛷0
192 # get 𝛷0, Amersfoort latitude on sphere
193 m, n = self.Rmn2
194 s, c = self.sincos2PHI0
195 return Phi(PHI0C=atan2(m * s, n * c)) # atan((m / n) * tan(PHI0))
197 @property_ROnce
198 def Q0(self): # 2.4.1 p 15 q0
199 return self.log_tan(self.PHI0) - self.log_e_2(self.PHI0)
201 @property_ROnce
202 def R(self): # 2.4.1 p 15 R, radius conformal sphere
203 m, n = self.Rmn2
204 return m * n
206 @property_ROnce
207 def RK2(self): # 2.4.2
208 return self.R * self.K0 * _2_0
210 @property_ROnce
211 def Rmn2(self): # 2.4.1 p 15 sqrt(RsubM), sqrt(RsubN)
212 # get 2-tuple (RHO0, NU0) EPSG:9809
213 E = self.E0
214 s, _ = self.sincos2PHI0
215 s = _1_0 - s**2 * E.e2
216 # assert s > 0
217 N = E.a / sqrt(s)
218 # assert N > 0
219 M = E.e21 * N / s
220 # assert M > 0
221 return map1(sqrt, M, N) # sqrt!
223 @property_ROnce
224 def sincos2PHI0(self): # 𝜑0
225 return sincos2(self.PHI0)
227 @property_ROnce
228 def sincos2PHI0C(self): # 𝛷0
229 return sincos2(self.PHI0C)
231 def _toDict(self):
232 def _p(n): # lambda
233 return n.endswith('0') or n.startswith('R') or \
234 n.endswith('0C') # _0_
236 return self._preDict(_p, H0_ETRS=self.H0_ETRS)
238 @property_ROnce
239 def W0(self): # 2.4.1 p 15 w0
240 return self.log_tan(self.PHI0C) # 𝛷0
242# def _w1(self, sphi): # EPSG:9809
243# w1 = NAN
244# if _1_0 > sphi > _N_1_0:
245# e = self.E0.e
246# S = (_1_0 + sphi) / (_1_0 - sphi)
247# T = (_1_0 - sphi * e) / (_1_0 + sphi * e)
248# w1 = pow(pow(T, e) * S, self.N0)
249# return w1
251 # % python -c "import pyrdnap; print(pyrdnap.rd0._RD0.toStr())"
252 # D0=Datum(name='Bessel1841', ellipsoid=Ellipsoids.Bessel1841, transform=Transforms.Bessel1841),
253 # D80=Datum(name='GRS80', ellipsoid=Ellipsoids.GRS80, transform=Transforms.WGS84),
254 # E0=Ellipsoid(name='Bessel1841', a=6377397.155, f=0.00334277, f_=299.1528128, b=6356078.962818),
255 # H0=0.0, H0_ETRS=43.0, K0=0.9999079, LAM0=0.094032038, LAM0C=0.094032038,
256 # LAT0=52.156160556, LON0=5.387638889, M0=0.003773954, N0=1.000475857,
257 # PHI0=0.910296727, PHI0C=0.909684757, Q0=1.06531844,
258 # R=6382644.571035411, RK2=12764113.458940838, Rmn2=(2524.794785679199, 2527.9854850929623),
259 # sincos2PHI0=(0.7896858198001045, 0.6135114554811807),
260 # sincos2PHI0C=(0.7893102212553742, 0.6139946047171686),
261 # W0=1.069599332, X0=155000.0, Y0=463000.0
263_RD0 = _RD0() # PYCHOK singleton, in .test/testRndTrips
266class RDNAP7Tuple(_NamedTuple): # in .v_self
267 '''7-Tuple C{(RDx, RDy, NAPh, lat, lon, height, datum)} with I{local} C{RDx},
268 C{RDy} and C{NAPh} quasi-geoid_height, geodetic C{lat}, C{lon}, C{height}
269 and C{datum} with C{lat} and C{lon} in C{degrees} and with C{RDx}, C{RDy},
270 C{NAPh} and C{height} in C{meter}, conventionally.
272 @note: The C{lat} and {lon} are I{by default} B{GRS80 (ETRS89)} geodetic
273 coordinates L{RDNAP2018v1.reverse} but B{Bessel1841 (RD-Bessel)}
274 when returned from L{RDNAP2018v2.reverse}.
275 '''
276 _Names_ = ('RDx', 'RDy', 'NAPh', _lat_, _lon_, _height_, _datum_)
277 _Units_ = ( Meter, Meter, Meter, Lat, Lon, Height, _Pass)
279 @property_ROnce
280 def _datum_index(self):
281 return self._Names_.index(_datum_)
283 def diff(self, other, datum=None, **name):
284 '''Return the difference between this and an C{other} C{RDNAP7Tuple}.
286 @kwarg datum: Optional difference C{B{datum}=None} (C{Latum}).
287 @kwarg name: Optional name (C{str}).
289 @return: An L{RDNAP7Tuple} with the C{_diff} for each item, but
290 C{datum=B{datum}}.
291 '''
292 def _diff(a, b):
293 return fabs(a - b)
295 _xinstanceof(RDNAP7Tuple, other=other)
296 d = self._datum_index
297 t = map2(_diff, self[:d], other[:d])
298 return RDNAP7Tuple(t + (datum,), **name)
300 @Property_RO
301 def lam(self):
302 '''Get the longitude (B{C{radians}}).
303 '''
304 return Lamd(self.lon) # PYCHOK lon
306 @Property_RO
307 def latlon(self):
308 '''Get the lat-, longitude in C{degrees} (L{LatLon2Tuple}C{(lat, lon)}).
309 '''
310 return LatLon2Tuple(self.lat, self.lon, name=self.name)
312 @Property_RO
313 def latlonheight(self):
314 '''Get the lat-, longitude in C{degrees} and height (L{LatLon3Tuple}C{(lat, lon, height)}).
315 '''
316 return self.latlon.to3Tuple(self.height)
318 @Property_RO
319 def latlonheightdatum(self):
320 '''Get the lat-, longitude in C{degrees} with height and datum (L{LatLon4Tuple}C{(lat, lon, height, datum)}).
321 '''
322 return self.latlonheight.to4Tuple(self.datum)
324 @Property_RO
325 def phi(self):
326 '''Get the latitude (B{C{radians}}).
327 '''
328 return Phid(self.lat) # PYCHOK lat
330 @Property_RO
331 def philam(self):
332 '''Get the lat- and longitude in C{radians} (L{PhiLam2Tuple}C{(phi, lam)}).
333 '''
334 return PhiLam2Tuple(self.phi, self.lam, name=self.name) # PYCHOK lam, phi
336 @Property_RO
337 def philamheight(self):
338 '''Get the lat-, longitude in C{radians} and height (L{PhiLam3Tuple}C{(phi, lam, height)}).
339 '''
340 return self.philam.to3Tuple(self.height) # PYCHOK height
342 @Property_RO
343 def philamheightdatum(self):
344 '''Get the lat-, longitude in C{radians} with height and datum (L{PhiLamn4Tuple}C{(phi, lam, height, datum)}).
345 '''
346 return self.philamheight.to4Tuple(self.datum)
348 def toDatum(self, datum2, **name):
349 '''Convert this C{lat}, C{lon} and C{height} to B{C{datum2}}.
351 @arg datum2: Datum to convert I{to} (L{Datum}).
353 @return: An L{RDNAP7Tuple} with converted C{lat}, C{lon} and C{height}
354 or this L{RDNAP7Tuple} if this.datum is B{C{datum2}}.
355 '''
356 _xinstanceof(Datum, datum2=datum2)
357 if self.datum is datum2: # PYCHOK or self.datum == datum2
358 return self
359 g = self.toLatLon(_LLEB).toDatum(datum2)
360 h = NAN if _isNAN(self.height) else g.height # PYCHOK preserve height NAN
361 return self.dup(lat=g.lat, lon=g.lon, datum=g.datum, height=h,
362 **_xkwds(name, name=self.name))
364 def toLatLon(self, LatLon, **LatLon_kwds):
365 '''Return this C{lat}, C{lon}, C{datum} and C{height} as B{C{LatLon}}.
367 @arg LatLon: An ellipsodial C{LatLon} class (C{pygeodesy.ellipsoidal*}).
368 @kwarg LatLon_kwds: Optional, additional B{C{LatLon}} keyword arguments.
370 @return: An B{C{LatLon}} instance.
372 @raise TypeError: B{C{LatLon}} not ellipsoidal or an other issue.
373 '''
374 _xsubclassof(_LLEB, LatLon=LatLon)
375 h = _isNAN0(self.height) # PYCHOK height
376 kwds = _xkwds(LatLon_kwds, name=self.name, height=h)
377 return LatLon(self.lat, self.lon, datum=self.datum, **kwds) # PYCHOK datum
379 @Property_RO
380 def xy(self):
381 '''Get the I{local} C{(RDx, RDy)} coordinates (L{Vector2Tuple}C{(x, y)}).
382 '''
383 return Vector2Tuple(self.RDx, self.RDy, name=self.name)
385 @Property_RO
386 def xyz(self):
387 '''Get the I{local} C{(RDx, RDy, NAPh)} coordinates and height (L{Vector3Tuple}C{(x, y, z)}).
388 '''
389 return Vector3Tuple(self.RDx, self.RDy, self.NAPh, name=self.name)
392class LqRD(_LqRD):
393 '''Like U{pygeodesy.LqRD<https://mrJean1.GitHub.io/PyGeodesy/docs/pygeodesy.ltp.LqRD-class.html>}
394 but with methods C{forward} and C{reverse} returning an L{RDNAP7Tuple} with C{NAPh} replaced
395 by I{local} C{z}, the perpendicular distance to the local tangent plane (LTP).
397 This C{quasi-RD} transformer B{does not} implement any U{RD NAP<https://www.NSGI.NL/
398 coordinatenstelsels-en-transformaties/coordinatentransformaties/rdnap-etrs89-rdnaptrans>}
399 specification and B{does not} provide I{Netherlands}' C{B{N}ormaal B{A}msterdams B{P}eil
400 (NAP)} quasi-geodetic-height.
401 '''
402 if _FOR_DOCS:
403 __init__ = _LqRD.__init__
405 def forward(self, lat_latlonh, lon=None, height=0, **name): # PYCHOK signature
406 '''Convert I{geodetic} C{(lat, lon, height)} to I{local} C{quasi-RD (x, y, z)}.
408 @arg lat_latlonh: C{Scalar} (geodetic) latitude (C{degrees}) or a I{local}
409 C{quasi-RD} L{RDNAP7Tuple}.
410 @kwarg lon: C{Scalar} (geodetic) longitude (C{degrees}) iff B{C{lat_latlonh}}
411 is C{scalar}, ignored otherwise.
412 @kwarg height: Optional height (C{meter}, conventionally) perpendicular to and
413 above (or below) the ellipsoid's surface, iff B{C{lat_latlonh}}
414 is C{scalar}, ignored otherwise.
415 @kwarg name: Optional C{B{name}=NN} (C{str}).
417 @return: An L{RDNAP7Tuple}C{(RDx, RDy, NAPh, lat, lon, height, datum)} with
418 C{NAPh} set to I{local} C{z}.
420 @see: B{pygeodesy.LqRD.forward} for more information.
421 '''
422 t = _LqRD.forward(self, lat_latlonh, lon=lon, height=height)
423 return LqRD._l9t2r7t(t, **name)
425 def reverse(self, x_xyz, y=None, z=None, **name): # PYCHOK signature
426 '''Convert I{local} C{quasi-RD (x, y, z)} to I{geodetic} C{(lat, lon, height)}.
428 @arg x_xyz: Local C{quasi-RD x} coordinate (C{scalar}) or a I{local}
429 C{quasi-RD} L{RDNAP7Tuple}.
430 @kwarg y: Local C{quasi-RD y} coordinate (C{meter}) iff B{C{x_xyz}} is
431 C{scalar}, ignored otherwise.
432 @kwarg z: Local C{z} coordinate (C{meter}) iff B{C{x_xyz}} is C{scalar},
433 ignored otherwise.
434 @kwarg name: Optional C{B{name}=NN} (C{str}).
436 @return: An L{RDNAP7Tuple}C{(RDx, RDy, NAPh, lat, lon, height, datum)}
437 with C{NAPh} set to I{local} B{C{z}}.
439 @see: B{pygeodesy.LqRD.reverse} for more information.
440 '''
441 t = _LqRD.reverse(self, x_xyz, y=y, z=z)
442 return LqRD._l9t2r7t(t, **name)
444 @staticmethod
445 def _l9t2r7t(t, name=NN, **unused): # M=False
446 return RDNAP7Tuple(t.x, t.y, t.z, # NAPh = t.z
447 t.lat, t.lon, t.height, t.ecef.datum, name=name or t.name)
450__all__ += _ALL_OTHER(LqRD, RDNAP7Tuple, Bounds4Tuple, # passed along from PyGeodesy
451 Datums, LatLon2Tuple, PhiLam2Tuple, Similarity, Vector2Tuple, Vector3Tuple)
452del _LQRD
454# **) MIT License
455#
456# Copyright (C) 2026-2026 -- mrJean1 at Gmail -- All Rights Reserved.
457#
458# Permission is hereby granted, free of charge, to any person obtaining a
459# copy of this software and associated documentation files (the "Software"),
460# to deal in the Software without restriction, including without limitation
461# the rights to use, copy, modify, merge, publish, distribute, sublicense,
462# and/or sell copies of the Software, and to permit persons to whom the
463# Software is furnished to do so, subject to the following conditions:
464#
465# The above copyright notice and this permission notice shall be included
466# in all copies or substantial portions of the Software.
467#
468# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
469# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
470# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
471# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
472# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
473# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
474# OTHER DEALINGS IN THE SOFTWARE.