pygeodesy 24.3.24__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. PyGeodesy-24.3.24.dist-info/METADATA +272 -0
  2. PyGeodesy-24.3.24.dist-info/RECORD +115 -0
  3. PyGeodesy-24.3.24.dist-info/WHEEL +6 -0
  4. PyGeodesy-24.3.24.dist-info/top_level.txt +1 -0
  5. pygeodesy/LICENSE +21 -0
  6. pygeodesy/__init__.py +615 -0
  7. pygeodesy/__main__.py +103 -0
  8. pygeodesy/albers.py +867 -0
  9. pygeodesy/auxilats/_CX_4.py +218 -0
  10. pygeodesy/auxilats/_CX_6.py +314 -0
  11. pygeodesy/auxilats/_CX_8.py +475 -0
  12. pygeodesy/auxilats/__init__.py +54 -0
  13. pygeodesy/auxilats/__main__.py +86 -0
  14. pygeodesy/auxilats/auxAngle.py +548 -0
  15. pygeodesy/auxilats/auxDLat.py +302 -0
  16. pygeodesy/auxilats/auxDST.py +296 -0
  17. pygeodesy/auxilats/auxLat.py +848 -0
  18. pygeodesy/auxilats/auxily.py +272 -0
  19. pygeodesy/azimuthal.py +1150 -0
  20. pygeodesy/basics.py +892 -0
  21. pygeodesy/booleans.py +2031 -0
  22. pygeodesy/cartesianBase.py +1062 -0
  23. pygeodesy/clipy.py +704 -0
  24. pygeodesy/constants.py +516 -0
  25. pygeodesy/css.py +660 -0
  26. pygeodesy/datums.py +752 -0
  27. pygeodesy/deprecated/__init__.py +61 -0
  28. pygeodesy/deprecated/bases.py +40 -0
  29. pygeodesy/deprecated/classes.py +262 -0
  30. pygeodesy/deprecated/consterns.py +54 -0
  31. pygeodesy/deprecated/datum.py +40 -0
  32. pygeodesy/deprecated/functions.py +375 -0
  33. pygeodesy/deprecated/nvector.py +48 -0
  34. pygeodesy/deprecated/rhumbBase.py +32 -0
  35. pygeodesy/deprecated/rhumbaux.py +33 -0
  36. pygeodesy/deprecated/rhumbsolve.py +33 -0
  37. pygeodesy/deprecated/rhumbx.py +33 -0
  38. pygeodesy/dms.py +986 -0
  39. pygeodesy/ecef.py +1348 -0
  40. pygeodesy/elevations.py +279 -0
  41. pygeodesy/ellipsoidalBase.py +1224 -0
  42. pygeodesy/ellipsoidalBaseDI.py +913 -0
  43. pygeodesy/ellipsoidalExact.py +343 -0
  44. pygeodesy/ellipsoidalGeodSolve.py +343 -0
  45. pygeodesy/ellipsoidalKarney.py +403 -0
  46. pygeodesy/ellipsoidalNvector.py +685 -0
  47. pygeodesy/ellipsoidalVincenty.py +590 -0
  48. pygeodesy/ellipsoids.py +2476 -0
  49. pygeodesy/elliptic.py +1198 -0
  50. pygeodesy/epsg.py +243 -0
  51. pygeodesy/errors.py +804 -0
  52. pygeodesy/etm.py +1190 -0
  53. pygeodesy/fmath.py +1013 -0
  54. pygeodesy/formy.py +1818 -0
  55. pygeodesy/frechet.py +865 -0
  56. pygeodesy/fstats.py +760 -0
  57. pygeodesy/fsums.py +1898 -0
  58. pygeodesy/gars.py +358 -0
  59. pygeodesy/geodesicw.py +581 -0
  60. pygeodesy/geodesicx/_C4_24.py +1699 -0
  61. pygeodesy/geodesicx/_C4_27.py +2395 -0
  62. pygeodesy/geodesicx/_C4_30.py +3301 -0
  63. pygeodesy/geodesicx/__init__.py +48 -0
  64. pygeodesy/geodesicx/__main__.py +91 -0
  65. pygeodesy/geodesicx/gx.py +1382 -0
  66. pygeodesy/geodesicx/gxarea.py +535 -0
  67. pygeodesy/geodesicx/gxbases.py +154 -0
  68. pygeodesy/geodesicx/gxline.py +669 -0
  69. pygeodesy/geodsolve.py +426 -0
  70. pygeodesy/geohash.py +914 -0
  71. pygeodesy/geoids.py +1884 -0
  72. pygeodesy/hausdorff.py +892 -0
  73. pygeodesy/heights.py +1155 -0
  74. pygeodesy/interns.py +687 -0
  75. pygeodesy/iters.py +545 -0
  76. pygeodesy/karney.py +919 -0
  77. pygeodesy/ktm.py +633 -0
  78. pygeodesy/latlonBase.py +1766 -0
  79. pygeodesy/lazily.py +960 -0
  80. pygeodesy/lcc.py +684 -0
  81. pygeodesy/ltp.py +1107 -0
  82. pygeodesy/ltpTuples.py +1563 -0
  83. pygeodesy/mgrs.py +721 -0
  84. pygeodesy/named.py +1324 -0
  85. pygeodesy/namedTuples.py +683 -0
  86. pygeodesy/nvectorBase.py +695 -0
  87. pygeodesy/osgr.py +781 -0
  88. pygeodesy/points.py +1686 -0
  89. pygeodesy/props.py +628 -0
  90. pygeodesy/resections.py +1048 -0
  91. pygeodesy/rhumb/__init__.py +46 -0
  92. pygeodesy/rhumb/aux_.py +397 -0
  93. pygeodesy/rhumb/bases.py +1148 -0
  94. pygeodesy/rhumb/ekx.py +563 -0
  95. pygeodesy/rhumb/solve.py +572 -0
  96. pygeodesy/simplify.py +647 -0
  97. pygeodesy/solveBase.py +472 -0
  98. pygeodesy/sphericalBase.py +724 -0
  99. pygeodesy/sphericalNvector.py +1264 -0
  100. pygeodesy/sphericalTrigonometry.py +1447 -0
  101. pygeodesy/streprs.py +627 -0
  102. pygeodesy/trf.py +2079 -0
  103. pygeodesy/triaxials.py +1484 -0
  104. pygeodesy/units.py +969 -0
  105. pygeodesy/unitsBase.py +349 -0
  106. pygeodesy/ups.py +538 -0
  107. pygeodesy/utily.py +1231 -0
  108. pygeodesy/utm.py +762 -0
  109. pygeodesy/utmups.py +318 -0
  110. pygeodesy/utmupsBase.py +517 -0
  111. pygeodesy/vector2d.py +785 -0
  112. pygeodesy/vector3d.py +968 -0
  113. pygeodesy/vector3dBase.py +1049 -0
  114. pygeodesy/webmercator.py +383 -0
  115. pygeodesy/wgrs.py +439 -0
pygeodesy/osgr.py ADDED
@@ -0,0 +1,781 @@
1
+
2
+ # -*- coding: utf-8 -*-
3
+
4
+ u'''Ordnance Survey Grid References (OSGR) references on the UK U{National Grid
5
+ <https://www.OrdnanceSurvey.co.UK/documents/resources/guide-to-nationalgrid.pdf>}.
6
+
7
+ Classes L{Osgr} and L{OSGRError} and functions L{parseOSGR} and L{toOsgr}.
8
+
9
+ A pure Python implementation, transcoded from I{Chris Veness}' JavaScript originals U{OS National Grid
10
+ <https://www.Movable-Type.co.UK/scripts/latlong-os-gridref.html>} and U{Module osgridref
11
+ <https://www.Movable-Type.co.UK/scripts/geodesy/docs/module-osgridref.html>} and I{Charles Karney}'s
12
+ C++ class U{OSGB<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1OSGB.html>}.
13
+
14
+ OSGR provides geocoordinate references for UK mapping purposes, converted in 2015 to work with the C{WGS84}
15
+ or the original C{OSGB36} datum. In addition, this implementation includes both the OS recommended and the
16
+ Krüger-based method to convert between OSGR and geodetic coordinates (with keyword argument C{kTM} of
17
+ function L{toOsgr}, method L{Osgr.toLatLon} and method C{toOsgr} of any ellipsoidal C{LatLon} class).
18
+
19
+ See U{Transverse Mercator: Redfearn series<https://WikiPedia.org/wiki/Transverse_Mercator:_Redfearn_series>},
20
+ Karney's U{"Transverse Mercator with an accuracy of a few nanometers", 2011<https://ArXiv.org/pdf/1002.1417v3.pdf>}
21
+ (building on U{"Konforme Abbildung des Erdellipsoids in der Ebene", 1912<https://bib.GFZ-Potsdam.DE/pub/digi/krueger2.pdf>},
22
+ U{"Die Mathematik der Gauß-Krueger-Abbildung", 2006<https://DE.WikiPedia.org/wiki/Gauß-Krüger-Koordinatensystem>},
23
+ U{A Guide<https://www.OrdnanceSurvey.co.UK/documents/resources/guide-coordinate-systems-great-britain.pdf>}
24
+ and U{Ordnance Survey National Grid<https://WikiPedia.org/wiki/Ordnance_Survey_National_Grid>}.
25
+ '''
26
+ # make sure int/int division yields float quotient, see .basics
27
+ from __future__ import division as _; del _ # PYCHOK semicolon
28
+
29
+ from pygeodesy.basics import halfs2, isbool, isfloat, map1, \
30
+ _splituple, _xsubclassof
31
+ from pygeodesy.constants import _1_0, _10_0, _N_2_0 # PYCHOK used!
32
+ from pygeodesy.datums import Datums, _ellipsoidal_datum, _WGS84
33
+ # from pygeodesy.dms import parseDMS2 # _MODS
34
+ from pygeodesy.ellipsoidalBase import LatLonEllipsoidalBase as _LLEB
35
+ from pygeodesy.errors import _parseX, _TypeError, _ValueError, \
36
+ _xkwds, _xkwds_get
37
+ from pygeodesy.fmath import Fdot, fpowers, Fsum
38
+ # from pygeodesy.fsums import Fsum # from .fmath
39
+ from pygeodesy.interns import MISSING, NN, _A_, _COLON_, _COMMA_, \
40
+ _COMMASPACE_, _DOT_, _ellipsoidal_, \
41
+ _latlon_, _not_, _SPACE_
42
+ from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS
43
+ from pygeodesy.named import _NamedBase, nameof, _xnamed
44
+ from pygeodesy.namedTuples import EasNor2Tuple, LatLon2Tuple, \
45
+ LatLonDatum3Tuple
46
+ from pygeodesy.props import Property_RO, property_RO
47
+ from pygeodesy.streprs import _EN_WIDE, enstr2, _enstr2m3, Fmt, \
48
+ _resolution10, unstr, _xzipairs
49
+ from pygeodesy.units import Easting, Lam_, Lat, Lon, Northing, \
50
+ Phi_, Scalar, _10um, _100km
51
+ from pygeodesy.utily import degrees90, degrees180, sincostan3, truncate
52
+
53
+ from math import cos, fabs, radians, sin, sqrt
54
+
55
+ __all__ = _ALL_LAZY.osgr
56
+ __version__ = '23.12.03'
57
+
58
+ _equivalent_ = 'equivalent'
59
+ _OSGR_ = 'OSGR'
60
+ _ord_A = ord(_A_)
61
+ _TRIPS = 33 # .toLatLon convergence
62
+
63
+
64
+ class _NG(object):
65
+ '''Ordnance Survey National Grid parameters.
66
+ '''
67
+ @Property_RO
68
+ def a0(self): # equatoradius, scaled
69
+ return self.ellipsoid.a * self.k0
70
+
71
+ @Property_RO
72
+ def b0(self): # polaradius, scaled
73
+ return self.ellipsoid.b * self.k0
74
+
75
+ @Property_RO
76
+ def datum(self): # datum, Airy130 ellipsoid
77
+ return Datums.OSGB36
78
+
79
+ @Property_RO
80
+ def eas0(self): # False origin easting (C{meter})
81
+ return Easting(4 * _100km)
82
+
83
+ @Property_RO
84
+ def easX(self): # easting [0..extent] (C{meter})
85
+ return Easting(7 * _100km)
86
+
87
+ @Property_RO
88
+ def ellipsoid(self): # ellipsoid, Airy130
89
+ return self.datum.ellipsoid
90
+
91
+ def forward2(self, latlon): # convert C{latlon} to (easting, norting), as I{Karney}'s
92
+ # U{Forward<https://GeographicLib.SourceForge.io/C++/doc/OSGB_8hpp_source.html>}
93
+ t = self.kTM.forward(latlon.lat, latlon.lon, lon0=self.lon0)
94
+ e = t.easting + self.eas0
95
+ n = t.northing + self.nor0ffset
96
+ return e, n
97
+
98
+ @Property_RO
99
+ def k0(self): # central scale (C{float}), like I{Karney}'s CentralScale
100
+ # <https://GeographicLib.SourceForge.io/C++/doc/OSGB_8hpp_source.html>
101
+ _0_9998268 = (9998268 - 10000000) / 10000000
102
+ return Scalar(_10_0**_0_9998268) # 0.9996012717...
103
+
104
+ @Property_RO
105
+ def kTM(self): # the L{KTransverseMercator} instance, like I{Karney}'s OSGBTM
106
+ # <https://GeographicLib.SourceForge.io/C++/doc/OSGB_8cpp_source.html>
107
+ return _MODS.ktm.KTransverseMercator(self.datum, lon0=0, k0=self.k0)
108
+
109
+ @Property_RO
110
+ def lam0(self): # True origin longitude C{radians}
111
+ return Lam_(self.lon0)
112
+
113
+ @Property_RO
114
+ def lat0(self): # True origin latitude, 49°N
115
+ return Lat(49.0)
116
+
117
+ @Property_RO
118
+ def lon0(self): # True origin longitude, 2°W
119
+ return Lon(_N_2_0)
120
+
121
+ @Property_RO
122
+ def Mabcd(self): # meridional coefficients (a, b, c, d)
123
+ n, n2, n3 = fpowers(self.ellipsoid.n, 3)
124
+ M = (Fsum(4, 4 * n, 5 * n2, 5 * n3) / 4,
125
+ Fsum( 24 * n, 24 * n2, 21 * n3) / 8,
126
+ Fsum( 15 * n2, 15 * n3) / 8,
127
+ (35 * n3 / 24))
128
+ return M
129
+
130
+ def Mabcd0(self, a): # meridional arc, scaled
131
+ c = a + self.phi0
132
+ s = a - self.phi0
133
+ R = Fdot(self.Mabcd, s, -sin(s) * cos(c),
134
+ sin(s * 2) * cos(c * 2),
135
+ -sin(s * 3) * cos(c * 3))
136
+ return float(R * self.b0)
137
+
138
+ @Property_RO
139
+ def nor0(self): # False origin northing (C{meter})
140
+ return Northing(-_100km)
141
+
142
+ @Property_RO
143
+ def nor0ffset(self): # like I{Karney}'s computenorthoffset
144
+ # <https://GeographicLib.SourceForge.io/C++/doc/OSGB_8cpp_source.html>
145
+ return self.nor0 - self.kTM.forward(self.lat0, 0).northing
146
+
147
+ @Property_RO
148
+ def norX(self): # northing [0..extent] (C{meter})
149
+ return Northing(13 * _100km)
150
+
151
+ def nu_rho_eta3(self, sa): # 3-tuple (nu, nu / rho, eta2)
152
+ E = self.ellipsoid # rho, nu = E.roc2_(sa) # .k0?
153
+ s = E.e2s2(sa) # == 1 - E.e2 * sa**2
154
+ v = self.a0 / sqrt(s) # == nu, transverse roc
155
+ # rho = .a0 * E.e21 / s**1.5 == v * E.e21 / s
156
+ # r = v * E.e21 / s # == rho, meridional roc
157
+ # nu / rho == v / (v * E.e21 / s) == s / E.e21 == ...
158
+ s *= E._1_e21 # ... s * E._1_e21 == s * E.a2_b2
159
+ return v, s, (s - _1_0) # η2 = nu / rho - 1
160
+
161
+ @Property_RO
162
+ def phi0(self): # True origin latitude C{radians}
163
+ return Phi_(self.lat0)
164
+
165
+ def reverse(self, osgr): # convert C{osgr} to (ellipsoidal} LatLon, as I{Karney}'s
166
+ # U{Reverse<https://GeographicLib.SourceForge.io/C++/doc/OSGB_8hpp_source.html>}
167
+ r = osgr._latlonTM
168
+ if r is None:
169
+ x = osgr.easting - self.eas0
170
+ y = osgr.northing - self.nor0ffset
171
+ t = self.kTM.reverse(x, y, lon0=self.lon0)
172
+ r = _LLEB(t.lat, t.lon, datum=self.datum, name=osgr.name)
173
+ osgr._latlonTM = r
174
+ return r
175
+
176
+ _NG = _NG() # PYCHOK singleton
177
+
178
+
179
+ class OSGRError(_ValueError):
180
+ '''Error raised for a L{parseOSGR}, L{Osgr} or other OSGR issue.
181
+ '''
182
+ pass
183
+
184
+
185
+ class Osgr(_NamedBase):
186
+ '''Ordnance Survey Grid References (OSGR) coordinates on
187
+ the U{National Grid<https://www.OrdnanceSurvey.co.UK/
188
+ documents/resources/guide-to-nationalgrid.pdf>}.
189
+ '''
190
+ _datum = _NG.datum # default datum (L{Datums.OSGB36})
191
+ _easting = 0 # Easting (C{meter})
192
+ _latlon = None # cached B{C{_toLatlon}}
193
+ _latlonTM = None # cached B{C{_toLatlon kTM}}
194
+ _northing = 0 # Nothing (C{meter})
195
+ _resolution = 0 # from L{parseOSGR} (C{meter})
196
+
197
+ def __init__(self, easting, northing, datum=None, name=NN,
198
+ resolution=0):
199
+ '''New L{Osgr} coordinate.
200
+
201
+ @arg easting: Easting from the OS C{National Grid}
202
+ origin (C{meter}).
203
+ @arg northing: Northing from the OS C{National Grid}
204
+ origin (C{meter}).
205
+ @kwarg datum: Override default datum (C{Datums.OSGB36}).
206
+ @kwarg name: Optional name (C{str}).
207
+ @kwarg resolution: Optional resolution (C{meter}),
208
+ C{0} for default.
209
+
210
+ @raise OSGRError: Invalid or negative B{C{easting}} or
211
+ B{C{northing}} or B{C{datum}} not an
212
+ C{Datums.OSGB36} equivalent.
213
+ '''
214
+ if datum: # PYCHOK no cover
215
+ try:
216
+ self._datum = _ellipsoidal_datum(datum)
217
+ if self.datum != _NG.datum:
218
+ raise ValueError(_not_(_NG.datum.name, _equivalent_))
219
+ except (TypeError, ValueError) as x:
220
+ raise OSGRError(datum=datum, cause=x)
221
+
222
+ self._easting = Easting( easting, Error=OSGRError, high=_NG.easX)
223
+ self._northing = Northing(northing, Error=OSGRError, high=_NG.norX)
224
+
225
+ if name:
226
+ self.name = name
227
+ if resolution:
228
+ self._resolution = _resolution10(resolution, Error=OSGRError)
229
+
230
+ def __str__(self):
231
+ return self.toStr(GD=True, sep=_SPACE_)
232
+
233
+ @Property_RO
234
+ def datum(self):
235
+ '''Get the datum (L{Datum}).
236
+ '''
237
+ return self._datum
238
+
239
+ @Property_RO
240
+ def easting(self):
241
+ '''Get the easting (C{meter}).
242
+ '''
243
+ return self._easting
244
+
245
+ @Property_RO
246
+ def falsing0(self):
247
+ '''Get the C{OS National Grid} falsing (L{EasNor2Tuple}).
248
+ '''
249
+ return EasNor2Tuple(_NG.eas0, _NG.nor0, name=_OSGR_)
250
+
251
+ @property_RO
252
+ def iteration(self):
253
+ '''Get the most recent C{Osgr.toLatLon} iteration number
254
+ (C{int}) or C{None} if not available/applicable.
255
+ '''
256
+ return self._iteration
257
+
258
+ @Property_RO
259
+ def latlon0(self):
260
+ '''Get the C{OS National Grid} origin (L{LatLon2Tuple}).
261
+ '''
262
+ return LatLon2Tuple(_NG.lat, _NG.lon0, name=_OSGR_)
263
+
264
+ @Property_RO
265
+ def northing(self):
266
+ '''Get the northing (C{meter}).
267
+ '''
268
+ return self._northing
269
+
270
+ def parse(self, strOSGR, name=NN):
271
+ '''Parse an OSGR reference to a similar L{Osgr} instance.
272
+
273
+ @arg strOSGR: The OSGR reference (C{str}), see function L{parseOSGR}.
274
+ @kwarg name: Optional instance name (C{str}), overriding this name.
275
+
276
+ @return: The similar instance (L{Osgr})
277
+
278
+ @raise OSGRError: Invalid B{C{strOSGR}}.
279
+ '''
280
+ return parseOSGR(strOSGR, Osgr=self.classof, name=name or self.name)
281
+
282
+ @property_RO
283
+ def resolution(self):
284
+ '''Get the OSGR resolution (C{meter}, power of 10) or C{0} if undefined.
285
+ '''
286
+ return self._resolution
287
+
288
+ def toLatLon(self, LatLon=None, datum=_WGS84, kTM=False, eps=_10um, **LatLon_kwds):
289
+ '''Convert this L{Osgr} coordinate to an (ellipsoidal) geodetic
290
+ point.
291
+
292
+ @kwarg LatLon: Optional ellipsoidal class to return the
293
+ geodetic point (C{LatLon}) or C{None}.
294
+ @kwarg datum: Optional datum to convert to (L{Datum},
295
+ L{Ellipsoid}, L{Ellipsoid2}, L{Ellipsoid2}
296
+ or L{a_f2Tuple}).
297
+ @kwarg kTM: If C{True} use I{Karney}'s Krüger method from
298
+ module L{ktm}, otherwise use the Ordnance
299
+ Survey formulation (C{bool}).
300
+ @kwarg eps: Tolerance for OS convergence (C{meter}).
301
+ @kwarg LatLon_kwds: Optional, additional B{C{LatLon}} keyword
302
+ arguments, ignored if C{B{LatLon} is None}.
303
+
304
+ @return: A B{C{LatLon}} instance or if B{C{LatLon}} is C{None}
305
+ a L{LatLonDatum3Tuple}C{(lat, lon, datum)}.
306
+
307
+ @note: While OS grid references are based on the OSGB36 datum,
308
+ the Ordnance Survey have deprecated the use of OSGB36 for
309
+ lat-/longitude coordinates (in favour of WGS84). Hence,
310
+ this method returns WGS84 by default with OSGB36 as an
311
+ option, U{see<https://www.OrdnanceSurvey.co.UK/blog/2014/12/2>}.
312
+
313
+ @note: The formulation implemented here due to Thomas, Redfearn,
314
+ etc. is as published by the Ordnance Survey, but is
315
+ inferior to Krüger as used by e.g. Karney 2011.
316
+
317
+ @raise OSGRError: No convergence.
318
+
319
+ @raise TypeError: If B{C{LatLon}} is not ellipsoidal or B{C{datum}}
320
+ is invalid or conversion to B{C{datum}} failed.
321
+ '''
322
+ NG = _NG
323
+ if kTM:
324
+ r = NG.reverse(self)
325
+
326
+ elif self._latlon is None:
327
+ e0 = self.easting - NG.eas0
328
+ n0 = m = self.northing - NG.nor0
329
+
330
+ _M = NG.Mabcd0
331
+ a0 = NG.a0
332
+ a = NG.phi0
333
+ _A = Fsum(a).fsum_
334
+ for self._iteration in range(1, _TRIPS):
335
+ a = _A(m / a0)
336
+ m = n0 - _M(a) # meridional arc
337
+ if fabs(m) < eps:
338
+ break
339
+ else: # PYCHOK no cover
340
+ t = str(self)
341
+ t = Fmt.PAREN(self.classname, repr(t))
342
+ t = _DOT_(t, self.toLatLon.__name__)
343
+ t = unstr(t, eps=eps, kTM=kTM)
344
+ raise OSGRError(Fmt.no_convergence(m), txt=t)
345
+
346
+ sa, ca, ta = sincostan3(a)
347
+ v, v_r, n2 = NG.nu_rho_eta3(sa)
348
+
349
+ ta2 = ta**2
350
+ ta4 = ta2**2
351
+
352
+ ta *= v_r / 2
353
+ d = e0 / v
354
+ d2 = d**2
355
+
356
+ a = (d2 * ta * (-1 + # Horner-like
357
+ d2 / 12 * (Fsum( 5, 3 * ta2, -9 * ta2 * n2, n2) -
358
+ d2 / 30 * Fsum(61, 90 * ta2, 45 * ta4)))).fsum_(a)
359
+
360
+ b = (d / ca * (1 - # Horner-like
361
+ d2 / 6 * (Fsum(v_r, 2 * ta2) -
362
+ d2 / 20 * (Fsum( 5, 28 * ta2, 24 * ta4) +
363
+ d2 / 42 * Fsum(61, 662 * ta2, 1320 * ta4,
364
+ 720 * ta2 * ta4))))).fsum_(NG.lam0)
365
+
366
+ r = _LLEB(degrees90(a), degrees180(b), datum=self.datum, name=self.name)
367
+ r._iteration = self._iteration # only ellipsoidal LatLon
368
+ self._latlon = r
369
+ else:
370
+ r = self._latlon
371
+
372
+ return _ll2LatLon3(r, LatLon, datum, LatLon_kwds)
373
+
374
+ @Property_RO
375
+ def scale0(self):
376
+ '''Get the C{OS National Grid} central scale (C{scalar}).
377
+ '''
378
+ return _NG.k0
379
+
380
+ def toRepr(self, GD=None, fmt=Fmt.SQUARE, sep=_COMMASPACE_, **prec): # PYCHOK expected
381
+ '''Return a string representation of this L{Osgr} coordinate.
382
+
383
+ @kwarg GD: If C{bool}, in- or exclude the 2-letter grid designation and get
384
+ the new B{C{prec}} behavior, otherwise if C{None}, default to the
385
+ DEPRECATED definition C{B{prec}=5} I{for backward compatibility}.
386
+ @kwarg fmt: Enclosing backets format (C{str}).
387
+ @kwarg sep: Separator to join (C{str}) or C{None} to return an unjoined 2- or
388
+ 3-C{tuple} of C{str}s.
389
+ @kwarg prec: Precison C{B{prec}=0}, the number of I{decimal} digits (C{int}) or
390
+ if negative, the number of I{units to drop}, like MGRS U{PRECISION
391
+ <https://GeographicLib.SourceForge.io/C++/doc/GeoConvert.1.html#PRECISION>}.
392
+
393
+ @return: This OSGR as (C{str}), C{"[G:GD, E:meter, N:meter]"} or if C{B{GD}=False}
394
+ C{"[OSGR:easting,northing]"} or C{B{GD}=False} and C{B{prec} > 0} if
395
+ C{"[OSGR:easting.d,northing.d]"}.
396
+
397
+ @note: OSGR easting and northing values are truncated, not rounded.
398
+
399
+ @raise OSGRError: If C{B{GD} not in (None, True, False)} or if C{B{prec} < -4}
400
+ and C{B{GD}=False}.
401
+
402
+ @raise ValueError: Invalid B{C{prec}}.
403
+ '''
404
+ GD, prec = _GD_prec2(GD, fmt=fmt, sep=sep, **prec)
405
+
406
+ if GD:
407
+ t = self.toStr(GD=True, prec=prec, sep=None)
408
+ t = _xzipairs('GEN', t, sep=sep, fmt=fmt)
409
+ else:
410
+ t = _COLON_(_OSGR_, self.toStr(GD=False, prec=prec))
411
+ if fmt:
412
+ t = fmt % (t,)
413
+ return t
414
+
415
+ def toStr(self, GD=None, sep=NN, **prec): # PYCHOK expected
416
+ '''Return this L{Osgr} coordinate as a string.
417
+
418
+ @kwarg GD: If C{bool}, in- or exclude the 2-letter grid designation and get
419
+ the new B{C{prec}} behavior, otherwise if C{None}, default to the
420
+ DEPRECATED definition C{B{prec}=5} I{for backward compatibility}.
421
+ @kwarg sep: Separator to join (C{str}) or C{None} to return an unjoined 2- or
422
+ 3-C{tuple} of C{str}s.
423
+ @kwarg prec: Precison C{B{prec}=0}, the number of I{decimal} digits (C{int}) or
424
+ if negative, the number of I{units to drop}, like MGRS U{PRECISION
425
+ <https://GeographicLib.SourceForge.io/C++/doc/GeoConvert.1.html#PRECISION>}.
426
+
427
+ @return: This OSGR as (C{str}), C{"GD meter meter"} or if C{B{GD}=False}
428
+ C{"easting,northing"} or if C{B{GD}=False} and C{B{prec} > 0}
429
+ C{"easting.d,northing.d"}
430
+
431
+ @note: OSGR easting and northing values are truncated, not rounded.
432
+
433
+ @raise OSGRError: If C{B{GD} not in (None, True, False)} or if C{B{prec}
434
+ < -4} and C{B{GD}=False}.
435
+
436
+ @raise ValueError: Invalid B{C{prec}}.
437
+ '''
438
+ def _i2c(i):
439
+ if i > 7:
440
+ i += 1
441
+ return chr(_ord_A + i)
442
+
443
+ GD, prec = _GD_prec2(GD, sep=sep, **prec)
444
+
445
+ if GD:
446
+ E, e = divmod(self.easting, _100km)
447
+ N, n = divmod(self.northing, _100km)
448
+ E, N = int(E), int(N)
449
+ if 0 > E or E > 6 or \
450
+ 0 > N or N > 12:
451
+ raise OSGRError(E=E, e=e, N=N, n=n, prec=prec, sep=sep)
452
+ N = 19 - N
453
+ EN = _i2c( N - (N % 5) + (E + 10) // 5) + \
454
+ _i2c((N * 5) % 25 + (E % 5))
455
+ t = enstr2(e, n, prec, EN)
456
+ s = sep
457
+
458
+ elif prec <= -_EN_WIDE:
459
+ raise OSGRError(GD=GD, prec=prec, sep=sep)
460
+ else:
461
+ t = enstr2(self.easting, self.northing, prec, dot=True,
462
+ wide=_EN_WIDE + 1)
463
+ s = sep if sep is None else (sep or _COMMA_)
464
+
465
+ return t if s is None else s.join(t)
466
+
467
+
468
+ def _GD_prec2(GD, **prec_et_al):
469
+ '''(INTERNAL) Handle C{prec} backward compatibility.
470
+ '''
471
+ if GD is None: # old C{prec} 5+ or neg
472
+ prec = _xkwds_get(prec_et_al, prec=_EN_WIDE)
473
+ GD = prec > 0
474
+ prec = (prec - _EN_WIDE) if GD else -prec
475
+ elif isbool(GD):
476
+ prec = _xkwds_get(prec_et_al, prec=0)
477
+ else:
478
+ raise OSGRError(GD=GD, **prec_et_al)
479
+ return GD, prec
480
+
481
+
482
+ def _ll2datum(ll, datum, name):
483
+ '''(INTERNAL) Convert datum if needed.
484
+ '''
485
+ if datum:
486
+ try:
487
+ if ll.datum != datum:
488
+ ll = ll.toDatum(datum)
489
+ except (AttributeError, TypeError, ValueError) as x:
490
+ raise _TypeError(cause=x, datum=datum.name, **{name: ll})
491
+ return ll
492
+
493
+
494
+ def _ll2LatLon3(ll, LatLon, datum, LatLon_kwds):
495
+ '''(INTERNAL) Convert C{ll} to C{LatLon}
496
+ '''
497
+ if LatLon is None:
498
+ r = _ll2datum(ll, datum, LatLonDatum3Tuple.__name__)
499
+ r = LatLonDatum3Tuple(r.lat, r.lon, r.datum)
500
+ else: # must be ellipsoidal
501
+ _xsubclassof(_LLEB, LatLon=LatLon)
502
+ r = _ll2datum(ll, datum, LatLon.__name__)
503
+ r = LatLon(r.lat, r.lon, datum=r.datum, **LatLon_kwds)
504
+ if r._iteration != ll._iteration:
505
+ r._iteration = ll._iteration
506
+ return _xnamed(r, nameof(ll))
507
+
508
+
509
+ def parseOSGR(strOSGR, Osgr=Osgr, name=NN, **Osgr_kwds):
510
+ '''Parse a string representing an OS Grid Reference, consisting
511
+ of C{"[GD] easting northing"}.
512
+
513
+ Accepts standard OS Grid References like "SU 387 148", with
514
+ or without whitespace separators, from 2- up to 22-digit
515
+ references, or all-numeric, comma-separated references in
516
+ meters, for example "438700,114800".
517
+
518
+ @arg strOSGR: An OSGR coordinate (C{str}).
519
+ @kwarg Osgr: Optional class to return the OSGR coordinate
520
+ (L{Osgr}) or C{None}.
521
+ @kwarg name: Optional B{C{Osgr}} name (C{str}).
522
+ @kwarg Osgr_kwds: Optional, additional B{C{Osgr}} keyword
523
+ arguments, ignored if C{B{Osgr} is None}.
524
+
525
+ @return: An (B{C{Osgr}}) instance or if B{C{Osgr}} is
526
+ C{None} an L{EasNor2Tuple}C{(easting, northing)}.
527
+
528
+ @raise OSGRError: Invalid B{C{strOSGR}}.
529
+ '''
530
+ def _c2i(G):
531
+ g = ord(G.upper()) - _ord_A
532
+ if g > 7:
533
+ g -= 1
534
+ if g < 0 or g > 25:
535
+ raise ValueError
536
+ return g
537
+
538
+ def _OSGR(strOSGR, Osgr, name):
539
+ s = _splituple(strOSGR.strip())
540
+ p = len(s)
541
+ if not p:
542
+ raise ValueError
543
+ g = s[0]
544
+ if p == 2 and isfloat(g): # "easting,northing"
545
+ e, n, m = _enstr2m3(*s, wide=_EN_WIDE + 1)
546
+
547
+ else:
548
+ if p == 1: # "GReastingnorthing"
549
+ s = halfs2(g[2:])
550
+ g = g[:2]
551
+ elif p == 2: # "GReasting northing"
552
+ s = g[2:], s[1] # for backward ...
553
+ g = g[:2] # ... compatibility
554
+ elif p != 3:
555
+ raise ValueError
556
+ else: # "GR easting northing"
557
+ s = s[1:]
558
+
559
+ e, n = map(_c2i, g)
560
+ n, m = divmod(n, 5)
561
+ E = ((e - 2) % 5) * 5 + m
562
+ N = 19 - (e // 5) * 5 - n
563
+ if 0 > E or E > 6 or \
564
+ 0 > N or N > 12:
565
+ raise ValueError
566
+
567
+ e, n, m = _enstr2m3(*s, wide=_EN_WIDE)
568
+ e += E * _100km
569
+ n += N * _100km
570
+
571
+ if Osgr is None:
572
+ _ = _MODS.osgr.Osgr(e, n, resolution=m) # validate
573
+ r = EasNor2Tuple(e, n, name=name)
574
+ else:
575
+ r = Osgr(e, n, name=name,
576
+ **_xkwds(Osgr_kwds, resolution=m))
577
+ return r
578
+
579
+ return _parseX(_OSGR, strOSGR, Osgr, name,
580
+ strOSGR=strOSGR, Error=OSGRError)
581
+
582
+
583
+ def toOsgr(latlon, lon=None, kTM=False, datum=_WGS84, Osgr=Osgr, name=NN, # MCCABE 14
584
+ **prec_Osgr_kwds):
585
+ '''Convert a lat-/longitude point to an OSGR coordinate.
586
+
587
+ @arg latlon: Latitude (C{degrees}) or an (ellipsoidal) geodetic
588
+ C{LatLon} point.
589
+ @kwarg lon: Optional longitude in degrees (scalar or C{None}).
590
+ @kwarg kTM: If C{True} use I{Karney}'s Krüger method from
591
+ module L{ktm}, otherwise use the Ordnance
592
+ Survey formulation (C{bool}).
593
+ @kwarg datum: Optional datum to convert B{C{lat, lon}} from
594
+ (L{Datum}, L{Ellipsoid}, L{Ellipsoid2} or
595
+ L{a_f2Tuple}).
596
+ @kwarg Osgr: Optional class to return the OSGR coordinate
597
+ (L{Osgr}) or C{None}.
598
+ @kwarg name: Optional B{C{Osgr}} name (C{str}).
599
+ @kwarg prec_Osgr_kwds: Optional L{truncate} precision
600
+ C{B{prec}=ndigits} and/or additional
601
+ B{C{Osgr}} keyword arguments, ignored
602
+ if C{B{Osgr} is None}.
603
+
604
+ @return: An (B{C{Osgr}}) instance or if B{C{Osgr}} is C{None}
605
+ an L{EasNor2Tuple}C{(easting, northing)}.
606
+
607
+ @note: If L{isint}C{(B{prec})} both easting and northing are
608
+ L{truncate}d to the given number of digits.
609
+
610
+ @raise OSGRError: Invalid B{C{latlon}} or B{C{lon}}.
611
+
612
+ @raise TypeError: Non-ellipsoidal B{C{latlon}} or invalid
613
+ B{C{datum}}, B{C{Osgr}}, B{C{Osgr_kwds}}
614
+ or conversion to C{Datums.OSGB36} failed.
615
+ '''
616
+ def _prec_kwds2(prec=MISSING, **kwds):
617
+ return prec, kwds
618
+
619
+ if lon is not None:
620
+ try:
621
+ lat, lon = _MODS.dms.parseDMS2(latlon, lon)
622
+ latlon = _LLEB(lat, lon, datum=datum)
623
+ except Exception as x:
624
+ raise OSGRError(latlon=latlon, lon=lon, datum=datum, cause=x)
625
+ elif not isinstance(latlon, _LLEB):
626
+ raise _TypeError(latlon=latlon, txt=_not_(_ellipsoidal_))
627
+ elif not name: # use latlon.name
628
+ name = nameof(latlon)
629
+
630
+ NG = _NG
631
+ # convert latlon to OSGB36 first
632
+ ll = _ll2datum(latlon, NG.datum, _latlon_)
633
+
634
+ if kTM:
635
+ e, n = NG.forward2(ll)
636
+
637
+ else:
638
+ try:
639
+ a, b = ll.philam
640
+ except AttributeError:
641
+ a, b = map1(radians, ll.lat, ll.lon)
642
+
643
+ sa, ca, ta = sincostan3(a)
644
+ v, v_r, n2 = NG.nu_rho_eta3(sa)
645
+
646
+ m0 = NG.Mabcd0(a)
647
+ b -= NG.lam0
648
+ t = b * sa * v / 2
649
+ d = b * ca
650
+ d2 = d**2
651
+
652
+ ta2 = -(ta**2)
653
+ ta4 = ta2**2
654
+
655
+ e = (d * v * (1 + # Horner-like
656
+ d2 / 6 * (Fsum(v_r, ta2) +
657
+ d2 / 20 * Fsum(5, 18 * ta2, ta4, 14 * n2,
658
+ 58 * n2 * ta2)))).fsum_(NG.eas0)
659
+
660
+ n = (d * t * (1 + # Horner-like
661
+ d2 / 12 * (Fsum( 5, ta2, 9 * n2) +
662
+ d2 / 30 * Fsum(61, ta4, 58 * ta2)))).fsum_(m0, NG.nor0)
663
+
664
+ p, kwds = _prec_kwds2(**prec_Osgr_kwds)
665
+ if p is not MISSING:
666
+ e = truncate(e, p)
667
+ n = truncate(n, p)
668
+
669
+ if Osgr is None:
670
+ _ = _MODS.osgr.Osgr(e, n) # validate
671
+ r = EasNor2Tuple(e, n)
672
+ else:
673
+ r = Osgr(e, n, **kwds) # datum=NG.datum
674
+ if lon is None and isinstance(latlon, _LLEB):
675
+ if kTM:
676
+ r._latlonTM = latlon # XXX weakref(latlon)?
677
+ else:
678
+ r._latlon = latlon # XXX weakref(latlon)?
679
+ return _xnamed(r, name or nameof(latlon))
680
+
681
+
682
+ if __name__ == '__main__':
683
+
684
+ from pygeodesy.lazily import printf
685
+ from random import random, seed
686
+ from time import localtime
687
+
688
+ seed(localtime().tm_yday)
689
+
690
+ def _rnd(X, n):
691
+ X -= 2
692
+ d = set()
693
+ while len(d) < n:
694
+ r = 1 + int(random() * X)
695
+ if r not in d:
696
+ d.add(r)
697
+ yield r
698
+
699
+ D = _NG.datum
700
+ i = t = 0
701
+ t1 = t2 = 0, 0, 0, 0
702
+ for e in _rnd(_NG.easX, 256):
703
+ for n in _rnd(_NG.norX, 512):
704
+ p = False
705
+ t += 1
706
+
707
+ g = Osgr(e, n)
708
+ v = g.toLatLon(kTM=False, datum=D)
709
+ k = g.toLatLon(kTM=True, datum=D)
710
+ d = max(fabs(v.lat - k.lat), fabs(v.lon - k.lon))
711
+ if d > t1[2]:
712
+ t1 = e, n, d, t
713
+ p = True
714
+
715
+ ll = _LLEB((v.lat + k.lat) / 2,
716
+ (v.lon + k.lon) / 2, datum=D)
717
+ v = ll.toOsgr(kTM=False)
718
+ k = ll.toOsgr(kTM=True)
719
+ d = max(fabs(v.easting - k.easting),
720
+ fabs(v.northing - k.northing))
721
+ if d > t2[2]:
722
+ t2 = ll.lat, ll.lon, d, t
723
+ p = True
724
+
725
+ if p:
726
+ i += 1
727
+ printf('%5d: %s %s', i,
728
+ 'll(%.2f, %.2f) %.3e %d' % t2,
729
+ 'en(%d, %d) %.3e %d' % t1)
730
+ printf('%d total %s', t, D.name)
731
+
732
+ # **) MIT License
733
+ #
734
+ # Copyright (C) 2016-2024 -- mrJean1 at Gmail -- All Rights Reserved.
735
+ #
736
+ # Permission is hereby granted, free of charge, to any person obtaining a
737
+ # copy of this software and associated documentation files (the "Software"),
738
+ # to deal in the Software without restriction, including without limitation
739
+ # the rights to use, copy, modify, merge, publish, distribute, sublicense,
740
+ # and/or sell copies of the Software, and to permit persons to whom the
741
+ # Software is furnished to do so, subject to the following conditions:
742
+ #
743
+ # The above copyright notice and this permission notice shall be included
744
+ # in all copies or substantial portions of the Software.
745
+ #
746
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
747
+ # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
748
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
749
+ # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
750
+ # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
751
+ # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
752
+ # OTHER DEALINGS IN THE SOFTWARE.
753
+
754
+ # % python3 -m pygeodesy.osgr
755
+ # 1: ll(53.42, -0.59) 4.672e-07 1 en(493496, 392519) 2.796e-11 1
756
+ # 2: ll(60.86, -0.28) 2.760e-05 2 en(493496, 1220986) 2.509e-10 2
757
+ # 3: ll(61.41, -0.25) 3.045e-05 13 en(493496, 1281644) 2.774e-10 13
758
+ # 4: ll(61.41, -0.25) 3.045e-05 13 en(493496, 1192797) 3.038e-10 20
759
+ # 5: ll(61.41, -0.25) 3.045e-05 13 en(493496, 1192249) 3.073e-10 120
760
+ # 6: ll(61.55, -0.24) 3.120e-05 160 en(493496, 1192249) 3.073e-10 120
761
+ # 7: ll(61.55, -0.24) 3.122e-05 435 en(493496, 1192249) 3.073e-10 120
762
+ # 8: ll(61.57, -0.24) 3.130e-05 473 en(493496, 1192249) 3.073e-10 120
763
+ # 9: ll(58.66, -8.56) 8.084e-04 513 en(19711, 993800) 3.020e-06 513
764
+ # 10: ll(52.83, -7.65) 8.156e-04 518 en(19711, 993800) 3.020e-06 513
765
+ # 11: ll(51.55, -7.49) 8.755e-04 519 en(19711, 993800) 3.020e-06 513
766
+ # 12: ll(60.20, -8.87) 9.439e-04 521 en(19711, 1165686) 4.318e-06 521
767
+ # 13: ll(60.45, -8.92) 9.668e-04 532 en(19711, 1194002) 4.588e-06 532
768
+ # 14: ll(61.17, -9.08) 1.371e-03 535 en(19711, 1274463) 5.465e-06 535
769
+ # 15: ll(61.31, -9.11) 1.463e-03 642 en(19711, 1290590) 5.663e-06 642
770
+ # 16: ll(61.35, -9.12) 1.488e-03 807 en(19711, 1294976) 5.718e-06 807
771
+ # 17: ll(61.38, -9.13) 1.510e-03 929 en(19711, 1298667) 5.765e-06 929
772
+ # 18: ll(61.11, -9.24) 1.584e-03 11270 en(10307, 1268759) 6.404e-06 11270
773
+ # 19: ll(61.20, -9.26) 1.650e-03 11319 en(10307, 1278686) 6.545e-06 11319
774
+ # 20: ll(61.23, -9.27) 1.676e-03 11383 en(10307, 1282514) 6.600e-06 11383
775
+ # 21: ll(61.36, -9.30) 1.776e-03 11437 en(10307, 1297037) 6.816e-06 11437
776
+ # 22: ll(61.38, -9.30) 1.789e-03 11472 en(10307, 1298889) 6.844e-06 11472
777
+ # 23: ll(61.25, -9.39) 1.885e-03 91137 en(4367, 1285831) 7.392e-06 91137
778
+ # 24: ll(61.32, -9.40) 1.944e-03 91207 en(4367, 1293568) 7.519e-06 91207
779
+ # 25: ll(61.34, -9.41) 1.963e-03 91376 en(4367, 1296061) 7.561e-06 91376
780
+ # 26: ll(61.37, -9.41) 1.986e-03 91595 en(4367, 1298908) 7.608e-06 91595
781
+ # 131072 total OSGB36