Coverage for pygeodesy/dms.py: 95%
284 statements
« prev ^ index » next coverage.py v7.6.0, created at 2024-08-02 18:24 -0400
« prev ^ index » next coverage.py v7.6.0, created at 2024-08-02 18:24 -0400
2# -*- coding: utf-8 -*-
4u'''Parsers and formatters of angles in degrees, minutes and seconds or radians.
6Functions to parse and format bearing, compass, lat- and longitudes in various forms of
7degrees, minutes and seconds with or without degrees, minute and second symbols plus a
8compass point suffix, including parsing of C{decimal} and C{sexagecimal} degrees.
10Set env variable C{PYGEODESY_FMT_FORM} to any C{F_...} form to override default C{F_DMS}
11formatting of lat- and longitudes or to an empty string to restore the default.
13After I{(C) Chris Veness 2011-2015} published under the same MIT Licence**, see
14U{Latitude/Longitude<https://www.Movable-Type.co.UK/scripts/latlong.html>} and
15U{Vector-based geodesy<https://www.Movable-Type.co.UK/scripts/latlong-vectors.html>}.
17@var F_D: Format degrees as unsigned "deg°" with symbol, plus compass point suffix C{N, S, E} or C{W} (C{str}).
18@var F_DM: Format degrees as unsigned "deg°min′" with symbols, plus suffix (C{str}).
19@var F_DMS: Format degrees as unsigned "deg°min′sec″" with symbols, plus suffix (C{str}).
20@var F_DEG: Format degrees as unsigned "[D]DD" I{without} symbol, plus suffix (C{str}).
21@var F_MIN: Format degrees as unsigned "[D]DDMM" I{without} symbols, plus suffix (C{str}).
22@var F_SEC: Format degrees as unsigned "[D]DDMMSS" I{without} symbols, plus suffix (C{str}).
23@var F_D60: Format degrees as unsigned "[D]DD.MMSS" C{sexagecimal} I{without} symbols, plus suffix (C{str}).
24@var F__E: Format degrees as unsigned "%E" I{without} symbols, plus suffix (C{str}).
25@var F__F: Format degrees as unsigned "%F" I{without} symbols, plus suffix (C{str}).
26@var F__G: Format degrees as unsigned "%G" I{without} symbols, plus suffix (C{str}).
27@var F_RAD: Convert degrees to radians and format as unsigned "RR" with symbol, plus suffix (C{str}).
29@var F_D_: Format degrees as signed "-/deg°" with symbol, I{without} suffix (C{str}).
30@var F_DM_: Format degrees as signed "-/deg°min′" with symbols, I{without} suffix (C{str}).
31@var F_DMS_: Format degrees as signed "-/deg°min′sec″" with symbols, I{without} suffix (C{str}).
32@var F_DEG_: Format degrees as signed "-/[D]DD" I{without} symbol, I{without} suffix (C{str}).
33@var F_MIN_: Format degrees as signed "-/[D]DDMM" I{without} symbols, I{without} suffix (C{str}).
34@var F_SEC_: Format degrees as signed "-/[D]DDMMSS" I{without} symbols, I{without} suffix (C{str}).
35@var F_D60_: Format degrees as signed "-/[D]DD.MMSS" C{sexagecimal} I{without} symbols, I{without} suffix (C{str}).
36@var F__E_: Format degrees as signed "-/%E" I{without} symbols, I{without} suffix (C{str}).
37@var F__F_: Format degrees as signed "-/%F" I{without} symbols, I{without} suffix (C{str}).
38@var F__G_: Format degrees as signed "-/%G" I{without} symbols, I{without} suffix (C{str}).
39@var F_RAD_: Convert degrees to radians and format as signed "-/RR" I{without} symbol, I{without} suffix (C{str}).
41@var F_D__: Format degrees as signed "-/+deg°" with symbol, I{without} suffix (C{str}).
42@var F_DM__: Format degrees as signed "-/+deg°min′" with symbols, I{without} suffix (C{str}).
43@var F_DMS__: Format degrees as signed "-/+deg°min′sec″" with symbols, I{without} suffix (C{str}).
44@var F_DEG__: Format degrees as signed "-/+[D]DD" I{without} symbol, I{without} suffix (C{str}).
45@var F_MIN__: Format degrees as signed "-/+[D]DDMM" I{without} symbols, without suffix (C{str}).
46@var F_SEC__: Format degrees as signed "-/+[D]DDMMSS" I{without} symbols, I{without} suffix (C{str}).
47@var F_D60__: Format degrees as signed "-/+[D]DD.MMSS" C{sexagecimal} I{without} symbols, I{without} suffix (C{str}).
48@var F__E__: Format degrees as signed "-/+%E" I{without} symbols, I{without} suffix (C{str}).
49@var F__F__: Format degrees as signed "-/+%F" I{without} symbols, I{without} suffix (C{str}).
50@var F__G__: Format degrees as signed "-/+%G" I{without} symbols, I{without} suffix (C{str}).
51@var F_RAD__: Convert degrees to radians and format as signed "-/+RR" I{without} symbol, I{without} suffix (C{str}).
53@var S_DEG: Degrees symbol, default C{"°"}
54@var S_MIN: Minutes symbol, default C{"′"} aka I{PRIME}
55@var S_SEC: Seconds symbol, default C{"″"} aka I{DOUBLE_PRIME}
56@var S_RAD: Radians symbol, default C{""} aka L{pygeodesy.NN}
57@var S_DMS: If C{True} include, otherwise cancel all DMS symbols, default C{True}.
58@var S_SEP: Separator between C{deg°|min′|sec″|suffix}, default C{""} aka L{pygeodesy.NN}
60@note: In Python 2-, L{S_DEG}, L{S_MIN}, L{S_SEC}, L{S_RAD} and L{S_SEP} may be multi-byte,
61 non-ascii characters and if so, I{not} C{unicode}.
62'''
64from pygeodesy.basics import copysign0, isLatLon, isodd, issequence, isstr, map2, \
65 neg as _neg # in .ups
66from pygeodesy.constants import _umod_360, _0_0, _0_5, _60_0, _360_0, _3600_0
67from pygeodesy.errors import ParseError, RangeError, _TypeError, _ValueError,\
68 _parseX, rangerrors, _xkwds, _xkwds_get
69from pygeodesy.interns import NN, _arg_, _COMMA_, _d_, _DASH_, _deg_, _degrees_, \
70 _DOT_, _0_, _e_, _E_, _EW_, _f_, _F_, _g_, _keyword_, \
71 _MINUS_, _N_, _NE_, _NS_, _NSEW_, _NW_, _of_, \
72 _PERCENTDOTSTAR_, _PLUS_, _PLUSMINUS_, _QUOTE1_, \
73 _QUOTE2_, _radians_, _S_, _SE_, _SPACE_, _SW_, _W_
74from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS, _getenv
75# from pygeodesy.namedTuples import LatLon2Tuple # _MODS
76# from pygeodesy.props import _throwarning # _MODS
77from pygeodesy.streprs import Fmt, fstr, fstrzs, _0wpF
78# from pygeodesy.units import Precision_ # _MODS
79# from pygeodesy.utily import _Wrap # _MODS
81from math import fabs, modf, radians
82try:
83 from string import letters as _LETTERS
84except ImportError: # Python 3+
85 from string import ascii_letters as _LETTERS
87__all__ = _ALL_LAZY.dms
88__version__ = '24.06.11'
90_beyond_ = 'beyond'
91_DDDMMSS_ = 'DDDMMSS'
92_deg_min_ = 'deg+min'
93_SDIGITS_ = '-0123456789+'
94_sexagecimal_ = 'sexagecimal'
95_SEXAGECIMUL = 1.e4 # sexagecimal C{D.MMSSss} into decimal C{DMMSS.ss}
97F_D, F_DM, F_DMS, F_DEG, F_MIN, F_SEC, F_D60, F__E, F__F, F__G, F_RAD = _F_s = (
98 _d_, 'dm', 'dms', _deg_, 'min', 'sec', 'd60', _e_, _f_, _g_, 'rad')
99F_D_, F_DM_, F_DMS_, F_DEG_, F_MIN_, F_SEC_, F_D60_, F__E_, F__F_, F__G_, F_RAD_ = (NN(
100 _MINUS_, _) for _ in _F_s)
101F_D__, F_DM__, F_DMS__, F_DEG__, F_MIN__, F_SEC__, F_D60__, F__E__, F__F__, F__G__, F_RAD__ = (NN(
102 _PLUS_, _) for _ in _F_s)
103del _F_s
104_F_DMS = _getenv('PYGEODESY_FMT_FORM', NN) or F_DMS
106_F_case = {F_D: F_D, F_DEG: F_D, _degrees_: F_D, # unsigned _F_s
107 F_DM: F_DM, F_MIN: F_DM, _deg_min_: F_DM,
108 F_D60: F_D60, F_RAD: F_RAD, _radians_: F_RAD,
109 F__E: F__E, F__F: F__F, F__G: F__G} # default F_DMS
110_F_prec = {F_D: 6, F_DM: 4, F_DMS: 2, # default precs
111 F_DEG: 6, F_MIN: 4, F_SEC: 2, F_D60: 0,
112 F__E: 8, F__F: 8, F__G: 8, F_RAD: 5}
113_F_symb = set((F_D, F_DM, F_DMS, _deg_min_)) # == {} pychok -Tb
115S_DEG = _DEGREES_ = '°' # ord() = 176
116S_MIN = _MINUTES_ = '′' # PRIME
117S_SEC = _SECONDS_ = '″' # DOUBLE_PRIME
118S_RAD = _RADIANS_ = NN # PYCHOK radians symbol ""
119S_DMS = True # include DMS symbols
120S_SEP = NN # separator between deg|min|sec|suffix ""
121S_NUL = NN # empty string, kept INTERNAL
123# note: ord(_DEGREES_) == ord('°') == 176, ord('˚') == 730
124_S_norm = {S_DEG: _DEGREES_, '˚': _DEGREES_, '^': _DEGREES_, # _d_: _DEGREES_,
125 S_MIN: _MINUTES_, '’': _MINUTES_, _QUOTE1_: _MINUTES_, # _r_: _RADIANS_
126 S_SEC: _SECONDS_, '”': _SECONDS_, _QUOTE2_: _SECONDS_}
128_WINDS = (_N_, 'NbE', 'NNE', 'NEbN', _NE_, 'NEbE', 'ENE', 'EbN',
129 _E_, 'EbS', 'ESE', 'SEbE', _SE_, 'SEbS', 'SSE', 'SbE',
130 _S_, 'SbW', 'SSW', 'SWbS', _SW_, 'SWbW', 'WSW', 'WbS',
131 _W_, 'WbN', 'WNW', 'NWbW', _NW_, 'NWbN', 'NNW', 'NbW')
134def _D603(sep, s_D=_DOT_, s_M=None, s_S=S_NUL, s_DMS=S_DMS, **unused):
135 '''(INTERNAL) Get the overridden or default pseudo-C{DMS} symbols.
136 '''
137 if s_DMS:
138 M = sep if s_M is None else s_M
139 return s_D, (M or S_NUL), s_S
140 else: # no overriden symbols
141 return _DOT_, sep, S_NUL
144def _DMS3(form, s_D=S_DEG, s_M=S_MIN, s_S=S_SEC, s_DMS=S_DMS, **unused):
145 '''(INTERNAL) Get the overridden or default C{DMS} symbols.
146 '''
147 return (s_D, s_M, s_S) if s_DMS and form in _F_symb else (S_NUL, S_NUL, S_NUL)
150def _dms3(d, ddd, p, w):
151 '''(INTERNAL) Format C{d} as (deg, min, sec) C{str}s with leading zeros.
152 '''
153 d, s = divmod(round(d * _3600_0, p), _3600_0)
154 m, s = divmod(s, _60_0)
155 return (_0wpF(ddd, 0, d),
156 _0wpF( 2, 0, m),
157 _0wpF(w+2, p, s))
160def _fstrzs(t, **unused):
161 '''(INTERNAL) Pass-thru version of C{.streprs.fstrzs}.
162 '''
163 return t
166def _split3(strDMS, suffix=_NSEW_):
167 '''(INTERNAL) Return sign, stripped B{C{strDMS}} and compass point.
168 '''
169 t = strDMS.strip()
170 s = t[:1] # sign or digit
171 P = t[-1:] # compass point or digit or dot
172 t = t.lstrip(_PLUSMINUS_).rstrip(suffix).strip()
173 return s, t, P
176def _toDMS(deg, form, prec, sep, ddd, suff, s_D_M_S): # MCCABE 13 in .units
177 '''(INTERNAL) Convert C{deg} to C{str}, with/-out sign, DMS symbols and/or suffix.
178 '''
179 f = form
180 try:
181 deg = float(deg)
182 except (TypeError, ValueError) as x:
183 raise _ValueError(deg=deg, form=f, prec=prec, cause=x)
185 if f[:1] in _PLUSMINUS_: # signed
186 sign = _MINUS_ if deg < 0 else (
187 _PLUS_ if deg > 0 and f[:1] == _PLUS_ else NN)
188 f = f.lstrip(_PLUSMINUS_)
189 suff = NN # no suffix if signed
190 else: # suffixed
191 sign = NN # no sign if suffixed
192 if suff and sep: # no sep if no suffix
193 suff = NN(sep, suff)
194 try:
195 F = _F_case[f] # .strip()
196 except KeyError:
197 f = f.lower() # .strip()
198 F = _F_case.get(f, F_DMS)
200 if prec is None:
201 z = p = _F_prec.get(F, 6)
202 else:
203 z = int(prec)
204 p = abs(z)
205 w = p + (1 if p else 0)
206 z = fstrzs if z > 1 else _fstrzs
207 d = fabs(deg)
209 try:
210 if F is F_DMS: # 'deg+min+sec', default
211 D, M, S = _DMS3(f, **s_D_M_S)
212 d, m, s = _dms3(d, ddd, p, w)
213 t = NN(sign, d, D, sep,
214 m, M, sep,
215 z(s), S, suff)
217 elif F is F_DM: # 'deg+min'
218 D, M, _ = _DMS3(f, **s_D_M_S)
219 d, m = divmod(round(d * _60_0, p), _60_0)
220 t = NN(sign, _0wpF(ddd, 0, d), D, sep,
221 z(_0wpF(w+2, p, m)), M, suff)
223 elif F is F_D: # 'deg'
224 D, _, _ = _DMS3(f, **s_D_M_S)
225 t = NN(sign, z(_0wpF(w+ddd, p, d)), D, suff)
227 elif F is F_D60: # 'deg.MM|SSss|'
228 D, M, S = _D603(sep, **s_D_M_S)
229 d, m, s = _dms3(d, ddd, p, w)
230 t = z(s).split(_DOT_) + [S, suff]
231 t = NN(sign, d, D, m, M, *t)
233 elif F is F_RAD:
234 R = _xkwds_get(s_D_M_S, s_R=S_RAD)
235 r = NN(_PERCENTDOTSTAR_, _F_) % (p, radians(d))
236 t = NN(sign, z(r), R, suff)
238 else: # F in (F__E, F__F, F__G)
239 D = _xkwds_get(s_D_M_S, s_D=S_NUL)
240 d = NN(_PERCENTDOTSTAR_, F) % (p, d) # XXX f?
241 t = NN(sign, z(d, ap1z=F is F__G), D, suff)
243 except Exception as x:
244 raise _ValueError(deg=deg, form=form, F=F, prec=prec, suff=suff, cause=x)
246 return t # NOT unicode in Python 2-
249def bearingDMS(bearing, form=F_D, prec=None, sep=S_SEP, **s_D_M_S):
250 '''Convert bearing to a string (without compass point suffix).
252 @arg bearing: Bearing from North (compass C{degrees360}).
253 @kwarg form: Format specifier for B{C{deg}} (C{str} or L{F_D},
254 L{F_DM}, L{F_DMS}, L{F_DEG}, L{F_MIN}, L{F_SEC},
255 L{F_D60}, L{F__E}, L{F__F}, L{F__G}, L{F_RAD},
256 L{F_D_}, L{F_DM_}, L{F_DMS_}, L{F_DEG_}, L{F_MIN_},
257 L{F_SEC_}, L{F_D60_}, L{F__E_}, L{F__F_}, L{F__G_},
258 L{F_RAD_}, L{F_D__}, L{F_DM__}, L{F_DMS__}, L{F_DEG__},
259 L{F_MIN__}, L{F_SEC__}, L{F_D60__}, L{F__E__},
260 L{F__F__}, L{F__G__} or L{F_RAD__}).
261 @kwarg prec: Number of decimal digits (0..9 or C{None} for default).
262 Trailing zero decimals are stripped for B{C{prec}}
263 values of 1 and above, but kept for negative B{C{prec}}.
264 @kwarg sep: Separator between degrees, minutes, seconds, suffix (C{str}).
265 @kwarg s_D_M_S: Optional keyword arguments C{B{s_D}=str}, C{B{s_M}=str},
266 C{B{s_S}=str} and C{B{s_DMS}=True} to override any or
267 cancel all DMS symbols, defaults L{S_DEG}, L{S_MIN}
268 respectively L{S_SEC}.
270 @return: Compass degrees per the specified B{C{form}} (C{str}).
272 @see: Function L{pygeodesy.toDMS}.
273 '''
274 return _toDMS(_umod_360(bearing), form, prec, sep, 1, NN, s_D_M_S)
277def _clip(angle, limit, units):
278 '''(INTERNAL) Helper for C{clipDegrees} and C{clipRadians}.
279 '''
280 c = min(limit, max(-limit, angle))
281 if c != angle and rangerrors():
282 t = _SPACE_(fstr(angle, prec=6, ints=True), _beyond_,
283 copysign0(limit, angle), units)
284 raise RangeError(t, txt=None)
285 return c
288def clipDegrees(deg, limit):
289 '''Clip a lat- or longitude to the given range.
291 @arg deg: Unclipped lat- or longitude (C{scalar degrees}).
292 @arg limit: Valid C{-/+B{limit}} range (C{degrees}).
294 @return: Clipped value (C{degrees}).
296 @raise RangeError: If B{C{deg}} outside the valid C{-/+B{limit}} range
297 and L{rangerrors<pygeodesy.rangerrors>} is C{True}.
298 '''
299 return _clip(deg, limit, _degrees_) if limit and limit > 0 else deg
302def clipRadians(rad, limit):
303 '''Clip a lat- or longitude to the given range.
305 @arg rad: Unclipped lat- or longitude (C{radians}).
306 @arg limit: Valid C{-/+B{limit}} range (C{radians}).
308 @return: Clipped value (C{radians}).
310 @raise RangeError: If B{C{rad}} outside the valid C{-/+B{limit}} range
311 and L{rangerrors<pygeodesy.rangerrors>} is C{True}.
312 '''
313 return _clip(rad, limit, _radians_) if limit and limit > 0 else rad
316def compassDMS(bearing, form=F_D, prec=None, sep=S_SEP, **s_D_M_S):
317 '''Convert bearing to a string suffixed with compass point.
319 @arg bearing: Bearing from North (compass C{degrees360}).
320 @kwarg form: Format specifier for B{C{deg}} (C{str} or L{F_D},
321 L{F_DM}, L{F_DMS}, L{F_DEG}, L{F_MIN}, L{F_SEC},
322 L{F_D60}, L{F__E}, L{F__F}, L{F__G}, L{F_RAD},
323 L{F_D_}, L{F_DM_}, L{F_DMS_}, L{F_DEG_}, L{F_MIN_},
324 L{F_SEC_}, L{F_D60_}, L{F__E_}, L{F__F_}, L{F__G_},
325 L{F_RAD_}, L{F_D__}, L{F_DM__}, L{F_DMS__}, L{F_DEG__},
326 L{F_MIN__}, L{F_SEC__}, L{F_D60__}, L{F__E__},
327 L{F__F__}, L{F__G__} or L{F_RAD__}).
328 @kwarg prec: Number of decimal digits (0..9 or C{None} for default).
329 Trailing zero decimals are stripped for B{C{prec}}
330 values of 1 and above, but kept for negative B{C{prec}}.
331 @kwarg sep: Separator between degrees, minutes, seconds, suffix (C{str}).
332 @kwarg s_D_M_S: Optional keyword arguments C{B{s_D}=str}, C{B{s_M}=str}
333 C{B{s_S}=str} and C{B{s_DMS}=True} to override any or
334 cancel all DMS symbols, defaults L{S_DEG}, L{S_MIN}
335 respectively L{S_SEC}.
337 @return: Compass degrees and point in the specified form (C{str}).
339 @see: Function L{pygeodesy.toDMS}.
340 '''
341 b = _umod_360(bearing)
342 return _toDMS(b, form, prec, sep, 1, compassPoint(b), s_D_M_S)
345def compassPoint(bearing, prec=3):
346 '''Convert a C{bearing} from North to a compass point.
348 @arg bearing: Bearing (compass C{degrees360}).
349 @kwarg prec: Precision, number of compass point characters:
350 1 for cardinal or basic winds,
351 2 for intercardinal or ordinal or principal winds,
352 3 for secondary-intercardinal or half-winds or
353 4 for quarter-winds).
355 @return: Compass point (1-, 2-, 3- or 4-letter C{str}).
357 @raise ValueError: Invalid B{C{bearing}} or B{C{prec}}.
359 @see: U{Dms.compassPoint
360 <https://GitHub.com/ChrisVeness/geodesy/blob/master/dms.js>}
361 and U{Compass rose<https://WikiPedia.org/wiki/Compass_rose>}.
362 '''
363 try: # like .streprs.enstr2
364 b = _umod_360(bearing)
365 p = _MODS.units.Precision_(prec, low=1, high=4) \
366 if prec != 3 else int(prec)
367 m = 2 << p
368 w = 32 // m # if m in (4, 8, 16, 32)
369 # not round(b), half-even rounding in Python 3+, but
370 # round-away-from-zero as int(b + copysign0(_0_5, b))
371 w *= int(b * m / _360_0 + _0_5) % m
372 return _WINDS[w]
373 except Exception as x:
374 raise _ValueError(bearing=bearing, prec=prec, cause=x)
377def degDMS(deg, prec=6, s_D=S_DEG, s_M=S_MIN, s_S=S_SEC, neg=_MINUS_, pos=NN):
378 '''Convert degrees to a string in degrees, minutes I{or} seconds.
380 @arg deg: Value in degrees (C{scalar degrees}).
381 @kwarg prec: Number of decimal digits (0..9 or C{None} for default).
382 Trailing zero decimals are stripped for B{C{prec}}
383 values of 1 and above, but kept for negative B{C{prec}}.
384 @kwarg s_D: D symbol for degrees (C{str}).
385 @kwarg s_M: M symbol for minutes (C{str}) or C{""}.
386 @kwarg s_S: S symbol for seconds (C{str}) or C{""}.
387 @kwarg neg: Optional sign for negative (C{'-'}).
388 @kwarg pos: Optional sign for positive (C{''}).
390 @return: I{Either} degrees, minutes I{or} seconds (C{str}).
392 @see: Function L{pygeodesy.toDMS}.
393 '''
394 try:
395 deg = float(deg)
396 except (TypeError, ValueError) as x:
397 raise _ValueError(deg=deg, prec=prec, cause=x)
399 d, s = fabs(deg), s_D
400 if d < 1:
401 if s_M:
402 d *= _60_0
403 if d < 1 and s_S:
404 d *= _60_0
405 s = s_S
406 else:
407 s = s_M
408 elif s_S:
409 d *= _3600_0
410 s = s_S
412 z = int(prec)
413 t = Fmt.F(d, prec=abs(z))
414 if z > 1:
415 t = fstrzs(t)
416 n = neg if deg < 0 else pos
417 return NN(n, t, s) # NOT unicode in Python 2-
420def latDMS(deg, form=_F_DMS, prec=None, sep=S_SEP, **s_D_M_S):
421 '''Convert latitude to a string, optionally suffixed with N or S.
423 @arg deg: Latitude to be formatted (C{scalar degrees}).
424 @kwarg form: Format specifier for B{C{deg}} (C{str} or L{F_D},
425 L{F_DM}, L{F_DMS}, L{F_DEG}, L{F_MIN}, L{F_SEC},
426 L{F_D60}, L{F__E}, L{F__F}, L{F__G}, L{F_RAD},
427 L{F_D_}, L{F_DM_}, L{F_DMS_}, L{F_DEG_}, L{F_MIN_},
428 L{F_SEC_}, L{F_D60_}, L{F__E_}, L{F__F_}, L{F__G_},
429 L{F_RAD_}, L{F_D__}, L{F_DM__}, L{F_DMS__}, L{F_DEG__},
430 L{F_MIN__}, L{F_SEC__}, L{F_D60__}, L{F__E__},
431 L{F__F__}, L{F__G__} or L{F_RAD__}).
432 @kwarg prec: Number of decimal digits (0..9 or C{None} for default).
433 Trailing zero decimals are stripped for B{C{prec}}
434 values of 1 and above, but kept for negative B{C{prec}}.
435 @kwarg sep: Separator between degrees, minutes, seconds, suffix (C{str}).
436 @kwarg s_D_M_S: Optional keyword arguments C{B{s_D}=str}, C{B{s_M}=str}
437 C{B{s_S}=str} and C{B{s_DMS}=True} to override any or
438 cancel all DMS symbols, defaults L{S_DEG}, L{S_MIN}
439 respectively L{S_SEC}.
441 @return: Degrees in the specified form (C{str}).
443 @see: Functions L{pygeodesy.toDMS} and L{pygeodesy.lonDMS}.
444 '''
445 p = _S_ if deg < 0 else _N_
446 return _toDMS(deg, form, prec, sep, 2, p, s_D_M_S)
449def latlonDMS(lls, **m_form_prec_sep_s_D_M_S):
450 '''Convert one or more C{LatLon} instances to strings.
452 @arg lls: Single (C{LatLon}) or list, sequence, tuple, etc. (C{LatLon}s).
453 @kwarg m_form_prec_sep_s_D_M_S: Optional keyword arguments C{B{m}eter},
454 C{B{form}at}, C{B{prec}ision}, B{C{s_D}}, B{C{s_M}}, B{C{s_S}},
455 B{C{s_DMS}} and I{DEPRECATED} C{B{sep}=None}, see method
456 C{LatLon.toStr} and functions L{pygeodesy.latDMS} and
457 L{pygeodesy.lonDMS} for more details.
459 @return: A C{tuple} of C{str}s if B{C{lls}} is a list, sequence, tuple, etc.
460 of C{LatLon}s or a single C{str} if B{C{lls}} is a single C{LatLon}.
462 @see: Functions L{pygeodesy.latlonDMS_}, L{pygeodesy.latDMS}, L{pygeodesy.lonDMS}
463 and L{pygeodesy.toDMS} and method C{LatLon.toStr}.
465 @note: Keyword argument C{B{sep}=None} to join a C{str}ing from the returned C{tuple}
466 has been I{DEPRECATED}, use C{B{sep}.join(B{latlonDMS_}(...))} instead.
467 '''
468 sep, kwds = _latlonDMS_sep2(latlonDMS, **m_form_prec_sep_s_D_M_S)
469 if isLatLon(lls):
470 t = lls.toStr(**kwds)
471 elif issequence(lls):
472 t = tuple(ll.toStr(**kwds) for ll in lls)
473 if sep: # XXX TO BE REMOVED
474 t = sep.join(t)
475 else:
476 raise _TypeError(lls=lls, **m_form_prec_sep_s_D_M_S)
477 return t
480def latlonDMS_(*lls, **m_form_prec_sep_s_D_M_S):
481 '''Convert one or more C{LatLon} instances to strings.
483 @arg lls: The instances (C{LatLon}s), all positional arguments.
484 @kwarg m_form_prec_sep_s_D_M_S: Optional keyword arguments
485 C{B{m}eter}, C{B{form}at}, C{B{prec}ision}, B{C{s_D}},
486 B{C{s_M}}, B{C{s_S}}, B{C{s_DMS}} and I{DEPRECATED}
487 C{B{sep}=None}, see method C{LatLon.toStr} and
488 functions L{pygeodesy.latDMS} and L{pygeodesy.lonDMS}
489 for more details.
491 @return: A C{tuple} of C{str}s if 2 or more C{LatLon} instances
492 or a single C{str} if only a single C{LatLon} instance
493 is given in B{C{lls}}.
495 @see: Functions L{pygeodesy.latlonDMS}, L{pygeodesy.latDMS} and
496 L{pygeodesy.lonDMS} and L{pygeodesy.toDMS} and method
497 C{LatLon.toStr}.
499 @note: Keyword argument C{B{sep}=None} to join a C{str}ing
500 from the returned C{tuple} has been I{DEPRECATED},
501 use C{B{sep}.join(B{latlonDMS_}(...))} instead.
502 '''
503 sep, kwds = _latlonDMS_sep2(latlonDMS, **m_form_prec_sep_s_D_M_S)
504 if not lls:
505 raise _ValueError(lls=lls, **m_form_prec_sep_s_D_M_S)
506 elif len(lls) < 2:
507 lls, sep = lls[0], None
508 t = latlonDMS(lls, **kwds)
509 return sep.join(t) if sep else t
512def _latlonDMS_sep2(where, sep=None, **kwds):
513 '''DEPRECATED, instead use: %r.join(%s(...))'''
514 if sep:
515 k = _SPACE_(_keyword_, _arg_, Fmt.EQUAL(sep=repr(sep)), _of_)
516 n = where.__name__
517 t = _latlonDMS_sep2.__doc__ % (sep, n)
518 _MODS.props._throwarning(k, n, t)
519 return sep, kwds
522def lonDMS(deg, form=_F_DMS, prec=None, sep=S_SEP, **s_D_M_S):
523 '''Convert longitude to a string, optionally suffixed with E or W.
525 @arg deg: Longitude to be formatted (C{scalar degrees}).
526 @kwarg form: Format specifier for B{C{deg}} (C{str} or L{F_D},
527 L{F_DM}, L{F_DMS}, L{F_DEG}, L{F_MIN}, L{F_SEC},
528 L{F_D60}, L{F__E}, L{F__F}, L{F__G}, L{F_RAD},
529 L{F_D_}, L{F_DM_}, L{F_DMS_}, L{F_DEG_}, L{F_MIN_},
530 L{F_SEC_}, L{F_D60_}, L{F__E_}, L{F__F_}, L{F__G_},
531 L{F_RAD_}, L{F_D__}, L{F_DM__}, L{F_DMS__}, L{F_DEG__},
532 L{F_MIN__}, L{F_SEC__}, L{F_D60__}, L{F__E__},
533 L{F__F__}, L{F__G__} or L{F_RAD__}).
534 @kwarg prec: Number of decimal digits (0..9 or C{None} for default).
535 Trailing zero decimals are stripped for B{C{prec}}
536 values of 1 and above, but kept for negative B{C{prec}}.
537 @kwarg sep: Separator between degrees, minutes, seconds, suffix (C{str}).
538 @kwarg s_D_M_S: Optional keyword arguments C{B{s_D}=str}, C{B{s_M}=str}
539 C{B{s_S}=str} and C{B{s_DMS}=True} to override any or
540 cancel all DMS symbols, defaults L{S_DEG}, L{S_MIN}
541 respectively L{S_SEC}.
543 @return: Degrees in the specified form (C{str}).
545 @see: Functions L{pygeodesy.toDMS} and L{pygeodesy.latDMS}.
546 '''
547 p = _W_ if deg < 0 else _E_
548 return _toDMS(deg, form, prec, sep, 3, p, s_D_M_S)
551def normDMS(strDMS, norm=None, **s_D_M_S):
552 '''Normalize all degrees, minutes and seconds (DMS) I{symbols} in
553 a string to the default symbols L{S_DEG}, L{S_MIN}, L{S_SEC}.
555 @arg strDMS: Original DMS string (C{str}).
556 @kwarg norm: Optional replacement symbol (C{str}) or C{None} for
557 the default DMS symbols). Use C{B{norm}=""} to
558 remove all DMS symbols.
559 @kwarg s_D_M_S: Optional, alternate DMS symbols C{B{s_D}=str},
560 C{B{s_M}=str}, C{B{s_S}=str} and/or C{B{s_R}=str}
561 for radians, each to be replaced by B{C{norm}}.
563 @return: Normalized DMS (C{str}).
564 '''
565 def _s2S2(s_D=S_DEG, s_M=S_MIN, s_S=S_SEC, s_R=S_RAD):
566 d = {s_D: S_DEG, s_M: S_MIN, s_S: S_SEC, s_R: S_RAD}
567 for s, S in _xkwds(d, **_S_norm).items():
568 if s:
569 yield s, S
571 # XXX strDMS isn't unicode in Python 2- and looping
572 # thru strDMS will yield each byte, hence the loop
573 # thru _s2S2 and replacing the DMS symbols in strDMS
575 if norm is None: # back to default DMS
576 for s, S in _s2S2(**s_D_M_S):
577 if s != S:
578 strDMS = strDMS.replace(s, S)
580 else: # replace or remove all DMS
581 n = norm or NN
582 for s, _ in _s2S2(**s_D_M_S):
583 if s != n:
584 strDMS = strDMS.replace(s, n)
585 if n:
586 strDMS = strDMS.rstrip(n) # XXX not .strip?
588 return strDMS # NOT unicode in Python 2-
591def parseDDDMMSS(strDDDMMSS, suffix=_NSEW_, sep=S_SEP, clip=0, sexagecimal=False): # MCCABE 14
592 '''Parse a lat- or longitude represention forms as [D]DDMMSS in degrees.
594 @arg strDDDMMSS: Degrees in any of several forms (C{str}) and types (C{float},
595 C{int}, other).
596 @kwarg suffix: Optional, valid compass points (C{str}, C{tuple}).
597 @kwarg sep: Optional separator between "[D]DD", "MM", "SS", B{C{suffix}} (L{S_SEP}).
598 @kwarg clip: Optionally, limit value to range C{-/+B{clip}} (C{degrees}).
599 @kwarg sexagecimal: If C{True}, convert C{"D.MMSS"} or C{float(D.MMSS)} to
600 C{base-60} "MM" and "SS" digits. See C{form}s L{F_D60},
601 L{F_D60_} and L{F_D60__}.
603 @return: Degrees (C{float}).
605 @raise ParseError: Invalid B{C{strDDDMMSS}} or B{C{clip}} or the form of
606 B{C{strDDDMMSS}} is incompatible with the suffixed or
607 B{C{suffix}} compass point.
609 @raise RangeError: Value of B{C{strDDDMMSS}} outside the valid C{-/+B{clip}}
610 range and L{rangerrors<pygeodesy.rangerrors>} is C{True}.
612 @note: Type C{str} values "[D]DD", "[D]DDMM", "[D]DDMMSS" and "[D]DD.MMSS"
613 for B{C{strDDDMMSS}} are parsed properly only if I{either} unsigned
614 and suffixed with a valid, compatible, C{cardinal} L{compassPoint}
615 I{or} signed I{or} unsigned, unsuffixed and with keyword argument
616 B{C{suffix}="NS"}, B{C{suffix}="EW"} or a compatible L{compassPoint}.
618 @note: Unlike function L{parseDMS}, type C{float}, C{int} and other non-C{str}
619 B{C{strDDDMMSS}} values are interpreted as C{form} [D]DDMMSS or
620 [D]DD.MMSS. For example, C{int(1230)} is returned as 12.5 and I{not
621 1230.0} degrees. However, C{int(345)} is considered C{form} "DDD"
622 345 I{and not "DDMM" 0345}, unless B{C{suffix}} specifies the compass
623 point. Also, C{float(15.0523)} is returned as 15.0523 decimal
624 degrees and I{not 15°5′23″ sexagecimal}. To consider the latter, use
625 C{float(15.0523)} or C{"15.0523"} and specify the keyword argument
626 C{B{sexagecimal}=True}.
628 @see: Functions L{pygeodesy.parseDMS}, L{pygeodesy.parseDMS2} and
629 L{pygeodesy.parse3llh}.
630 '''
631 def _DDDMMSS(strDDDMMSS, suffix, sep, clip, sexagecimal):
632 S = suffix.upper()
633 if isstr(strDDDMMSS):
634 t = strDDDMMSS.replace(sep, NN) if sep else strDDDMMSS
635 s, t, P = _split3(t, S)
636 f = t.split(_DOT_)
637 n = len(f[0])
638 f = NN.join(f)
639 if 1 < n < 8 and f.isdigit() and ( # dddN/S/E/W or ddd or +/-ddd
640 (P in S and s.isdigit()) or
641 (P.isdigit() and s in _SDIGITS_ # PYCHOK indent
642 and S in _WINDS)):
643 # check [D]DDMMSS form and compass point
644 X = _EW_ if isodd(n) else _NS_
645 if not (P in X or (S in X and (P.isdigit() or P == _DOT_))):
646 t = _DDDMMSS_[int(X is _NS_):(n | 1)], _DASH_.join(X)
647 raise ParseError('form %s applies %s' % t)
648 elif not sexagecimal: # try other forms
649 return _DMS2deg(strDDDMMSS, S, sep, clip, {})
651 if sexagecimal: # move decimal dot from ...
652 n += 4 # ... [D]DD.MMSSs to [D]DDMMSS.s
653 if n < 6:
654 raise ParseError('%s digits (%s)' % (_sexagecimal_, n))
655 z = n - len(f) # zeros to append
656 t = (f + (_0_ * z)) if z > 0 else _DOT_(f[:n], f[n:])
657 f = _0_0 # fraction
659 else: # float or int to [D]DDMMSS[.fff]
660 f, m = float(strDDDMMSS), 0
661 if sexagecimal:
662 f *= _SEXAGECIMUL
663 m = 6
664 s = P = _0_ # anything except NN, _S_, _SW_, _W_
665 if f < 0:
666 f = -f
667 s = _MINUS_
668 f, i = modf(f) # returns ...
669 t = str(int(i)) # ... float(i)
670 n = len(t) # number of digits to ...
671 if n < m: # ... required min or ...
672 t = (_0_ * (m - n)) + t
673 # ... match the given compass point
674 elif S in (_NS_ if isodd(n) else _EW_):
675 t = _0_ + t
676 # P = S
677 # elif n > 1:
678 # P = (_EW_ if isodd(n) else _NS_)[0]
679 n = len(t)
681 if n < 4: # [D]DD[.ddd]
682 t = (float(t) + f),
683 else:
684 f += float(t[n-2:])
685 if n < 6: # [D]DDMM[.mmm]
686 t = float(t[:n-2]), f
687 else: # [D]DDMMSS[.sss]
688 t = float(t[:n-4]), float(t[n-4:n-2]), f
689 d = _dms2deg(s, P, *t)
690 return clipDegrees(d, float(clip)) if clip else d
692 return _parseX(_DDDMMSS, strDDDMMSS, suffix, sep, clip, sexagecimal,
693 strDDDMMSS=strDDDMMSS, suffix=suffix, sexagecimal=sexagecimal)
696def _dms2deg(s, P, deg, min=_0_0, sec=_0_0):
697 '''(INTERNAL) Helper for C{parseDDDMMSS} and C{_DMS2deg}.
698 '''
699 deg += (min + (sec / _60_0)) / _60_0
700 if s == _MINUS_ or (P and P in _SW_):
701 deg = _neg(deg)
702 return deg
705def _DMS2deg(strDMS, suffix, sep, clip, s_D_M_S):
706 '''(INTERNAL) Helper for C{parseDDDMMSS} and C{parseDMS}.
707 '''
708 try:
709 d = float(strDMS)
711 except (TypeError, ValueError):
712 s, t, P = _split3(strDMS, suffix.upper())
713 if sep: # remove all DMS symbols
714 t = t.replace(sep, _SPACE_)
715 t = normDMS(t, norm=NN, **s_D_M_S)
716 else: # replace all DMS symbols
717 t = normDMS(t, norm=_SPACE_, **s_D_M_S)
718 t = map2(float, t.strip().split())
719 d = _dms2deg(s, P, *t[:3])
721 return clipDegrees(d, float(clip)) if clip else d
724def parseDMS(strDMS, suffix=_NSEW_, sep=S_SEP, clip=0, **s_D_M_S): # MCCABE 14
725 '''Parse a lat- or longitude representation in C{degrees}.
727 This is very flexible on formats, allowing signed decimal
728 degrees, degrees and minutes or degrees minutes and seconds
729 optionally suffixed by a cardinal compass point.
731 A variety of symbols, separators and suffixes are accepted,
732 for example "3°37′09″W". Minutes and seconds may be omitted.
734 @arg strDMS: Degrees in any of several forms (C{str}) and
735 types (C{float}, C{int}, other).
736 @kwarg suffix: Optional, valid compass points (C{str}, C{tuple}).
737 @kwarg sep: Optional separator between deg°, min′, sec″, B{C{suffix}} (C{''}).
738 @kwarg clip: Optionally, limit value to range C{-/+B{clip}} (C{degrees}).
739 @kwarg s_D_M_S: Optional, alternate symbol for degrees C{B{s_D}=str},
740 minutes C{B{s_M}=str} and/or seconds C{B{s_S}=str}.
742 @return: Degrees (C{float}).
744 @raise ParseError: Invalid B{C{strDMS}} or B{C{clip}}.
746 @raise RangeError: Value of B{C{strDMS}} outside the valid C{-/+B{clip}} range
747 and L{rangerrors<pygeodesy.rangerrors>} is C{True}.
749 @note: Unlike function L{parseDDDMMSS}, type C{float}, C{int} and other non-C{str}
750 B{C{strDMS}} values are considered decimal (and not sexagecimal) degrees.
751 For example, C{int(1230)} is returned as 1230.0 I{and not as 12.5} degrees
752 and C{float(345)} as 345.0 I{and not as 3.75} degrees!
754 @see: Functions L{pygeodesy.parseDDDMMSS}, L{pygeodesy.parseDMS2},
755 L{pygeodesy.parse3llh} and L{pygeodesy.toDMS}.
756 '''
757 return _parseX(_DMS2deg, strDMS, suffix, sep, clip, s_D_M_S, strDMS=strDMS, suffix=suffix)
760def parseDMS2(strLat, strLon, sep=S_SEP, clipLat=90, clipLon=180, wrap=False, **s_D_M_S):
761 '''Parse a lat- and a longitude representions C{"lat, lon"} in C{degrees}.
763 @arg strLat: Latitude in any of several forms (C{str} or C{degrees}).
764 @arg strLon: Longitude in any of several forms (C{str} or C{degrees}).
765 @kwarg sep: Optional separator between deg°, min′, sec″, suffix (C{''}).
766 @kwarg clipLat: Limit latitude to range C{-/+B{clipLat}} (C{degrees}).
767 @kwarg clipLon: Limit longitude to range C{-/+B{clipLon}} (C{degrees}).
768 @kwarg wrap: If C{True}, wrap or I{normalize} the lat- and longitude,
769 overriding B{C{clipLat}} and B{C{clipLon}} (C{bool}).
770 @kwarg s_D_M_S: Optional, alternate symbol for degrees C{B{s_D}=str},
771 minutes C{B{s_M}=str} and/or seconds C{B{s_S}=str}.
773 @return: A L{LatLon2Tuple}C{(lat, lon)} in C{degrees}.
775 @raise ParseError: Invalid B{C{strLat}} or B{C{strLon}}.
777 @raise RangeError: Value of B{C{strLat}} or B{C{strLon}} outside the
778 valid C{-/+B{clipLat}} or C{-/+B{clipLon}} range
779 and L{rangerrors<pygeodesy.rangerrors>} is C{True}.
781 @note: See the B{Notes} at function L{parseDMS}.
783 @see: Functions L{pygeodesy.parseDDDMMSS}, L{pygeodesy.parseDMS},
784 L{pygeodesy.parse3llh} and L{pygeodesy.toDMS}.
785 '''
786 return _2Tuple(strLat, strLon, clipLat, clipLon, wrap, sep=sep, **s_D_M_S)
789def _2Tuple(strLat, strLon, clipLat, clipLon, wrap, **kwds):
790 '''(INTERNAL) Helper for C{parseDMS2} and C{parse3llh}.
791 '''
792 if wrap:
793 _W = _MODS.utily._Wrap
794 lat, lon = _W.latlon(parseDMS(strLat, suffix=_NS_, **kwds),
795 parseDMS(strLon, suffix=_EW_, **kwds))
796 else:
797 # if wrap is None:
798 # clipLat = clipLon = 0
799 lat = parseDMS(strLat, suffix=_NS_, clip=clipLat, **kwds)
800 lon = parseDMS(strLon, suffix=_EW_, clip=clipLon, **kwds)
801 return _MODS.namedTuples.LatLon2Tuple(lat, lon)
804def parse3llh(strllh, height=0, sep=_COMMA_, clipLat=90, clipLon=180, wrap=False, **s_D_M_S):
805 '''Parse a string C{"lat, lon [, h]"} representing lat-, longitude in
806 C{degrees} and optional height in C{meter}.
808 The lat- and longitude value must be separated by a separator
809 character. If height is present it must follow, separated by
810 another separator.
812 The lat- and longitude values may be swapped, provided at least
813 one ends with the proper compass point.
815 @arg strllh: Latitude, longitude[, height] (C{str}, ...).
816 @kwarg height: Optional, default height (C{meter}) or C{None}.
817 @kwarg sep: Optional separator between C{"lat lon [h] suffix"} (C{str}).
818 @kwarg clipLat: Limit latitude to range C{-/+B{clipLat}} (C{degrees}).
819 @kwarg clipLon: Limit longitude to range C{-/+B{clipLon}} (C{degrees}).
820 @kwarg wrap: If C{True}, wrap or I{normalize} the lat- and longitude,
821 overriding B{C{clipLat}} and B{C{clipLon}} (C{bool}).
822 @kwarg s_D_M_S: Optional, alternate symbol for degrees C{B{s_D}=str},
823 minutes C{B{s_M}=str} and/or seconds C{B{s_S}=str}.
825 @return: A L{LatLon3Tuple}C{(lat, lon, height)} in C{degrees},
826 C{degrees} and C{float}.
828 @raise RangeError: Lat- or longitude value of B{C{strllh}} outside the
829 valid C{-/+B{clipLat}} or C{-/+B{clipLon}} range
830 and L{rangerrors<pygeodesy.rangerrors>} is C{True}.
832 @raise ValueError: Invalid B{C{strllh}} or B{C{height}}.
834 @note: See the B{Notes} at function L{parseDMS}.
836 @see: Functions L{pygeodesy.parseDDDMMSS}, L{pygeodesy.parseDMS},
837 L{pygeodesy.parseDMS2} and L{pygeodesy.toDMS}.
838 '''
840 def _3llh(strllh, height, sep, wrap):
841 ll = strllh.strip().split(sep)
842 if len(ll) > 2: # XXX interpret height unit
843 h = float(ll.pop(2).rstrip(_LETTERS + _SPACE_))
844 else:
845 h = height # None from wgrs.Georef.__new__
846 if len(ll) != 2:
847 raise ValueError
849 a, b = [_.strip() for _ in ll] # PYCHOK false
850 if a[-1:] in _EW_ or b[-1:] in _NS_:
851 a, b = b, a
852 return _2Tuple(a, b, clipLat, clipLon, wrap, **s_D_M_S).to3Tuple(h)
854 return _parseX(_3llh, strllh, height, sep, wrap, strllh=strllh)
857def parseRad(strRad, suffix=_NSEW_, clip=0):
858 '''Parse a string representing angle in C{radians}.
860 @arg strRad: Degrees in any of several forms (C{str} or C{radians}).
861 @kwarg suffix: Optional, valid compass points (C{str}, C{tuple}).
862 @kwarg clip: Optionally, limit value to range C{-/+B{clip}} (C{radians}).
864 @return: Radians (C{float}).
866 @raise ParseError: Invalid B{C{strRad}} or B{C{clip}}.
868 @raise RangeError: Value of B{C{strRad}} outside the valid C{-/+B{clip}}
869 range and L{rangerrors<pygeodesy.rangerrors>} is C{True}.
870 '''
871 def _Rad(strRad, suffix, clip):
872 try:
873 r = float(strRad)
875 except (TypeError, ValueError):
876 s, t, P = _split3(strRad, suffix.upper())
877 r = _dms2deg(s, P, float(t))
879 return clipRadians(r, float(clip)) if clip else r
881 return _parseX(_Rad, strRad, suffix, clip, strRad=strRad, suffix=suffix)
884def precision(form, prec=None):
885 '''Set the default precison for a given F_ form.
887 @arg form: L{F_D}, L{F_DM}, L{F_DMS}, L{F_DEG}, L{F_MIN},
888 L{F_SEC}, L{F_D60}, L{F__E}, L{F__F}, L{F__G}
889 or L{F_RAD} (C{str}).
890 @kwarg prec: Number of decimal digits (0..9 or C{None} for
891 default). Trailing zero decimals are stripped
892 for B{C{prec}} values of 1 and above, but kept
893 for negative B{C{prec}}.
895 @return: Previous precision for the B{C{form}} (C{int}).
897 @raise ValueError: Invalid B{C{form}} or B{C{prec}} or B{C{prec}}
898 outside range C{-9..+9}.
899 '''
900 try:
901 p = _F_prec[form]
902 except KeyError:
903 raise _ValueError(form=form)
905 if prec is not None: # set as default
906 _F_prec[form] = _MODS.units.Precision_(prec, low=-9, high=9)
908 return p
911def toDMS(deg, form=_F_DMS, prec=2, sep=S_SEP, ddd=2, neg=_MINUS_, pos=_PLUS_, **s_D_M_S):
912 '''Convert I{signed} C{degrees} to string, without suffix.
914 @arg deg: Degrees to be formatted (C{scalar degrees}).
915 @kwarg form: Format specifier for B{C{deg}} (C{str} or L{F_D},
916 L{F_DM}, L{F_DMS}, L{F_DEG}, L{F_MIN}, L{F_SEC},
917 L{F_D60}, L{F__E}, L{F__F}, L{F__G}, L{F_RAD},
918 L{F_D_}, L{F_DM_}, L{F_DMS_}, L{F_DEG_}, L{F_MIN_},
919 L{F_SEC_}, L{F_D60_}, L{F__E_}, L{F__F_}, L{F__G_},
920 L{F_RAD_}, L{F_D__}, L{F_DM__}, L{F_DMS__}, L{F_DEG__},
921 L{F_MIN__}, L{F_SEC__}, L{F_D60__}, L{F__E__},
922 L{F__F__}, L{F__G__} or L{F_RAD__}).
923 @kwarg prec: Number of decimal digits (0..9 or C{None} for default).
924 Trailing zero decimals are stripped for B{C{prec}}
925 values of 1 and above, but kept for negative B{C{prec}}.
926 @kwarg sep: Separator between degrees, minutes, seconds, suffix (C{str}).
927 @kwarg ddd: Number of digits for B{C{deg}°} (2 or 3).
928 @kwarg neg: Prefix for negative B{C{deg}} (C{'-'}).
929 @kwarg pos: Prefix for positive B{C{deg}} and signed B{C{form}} (C{'+'}).
930 @kwarg s_D_M_S: Optional keyword arguments C{B{s_D}=str}, C{B{s_M}=str}
931 C{B{s_S}=str} and C{B{s_DMS}=True} to override any or
932 cancel all DMS symbols, defaults L{S_DEG}, L{S_MIN}
933 respectively L{S_SEC}. See B{Notes} below.
935 @return: Degrees in the specified form (C{str}).
937 @note: The degrees, minutes and seconds (DMS) symbol can be overridden in
938 this and other C{*DMS} functions by using optional keyword argments
939 C{B{s_D}="d"}, C{B{s_M}="'"} respectively C{B{s_S}='"'}. Using
940 keyword argument B{C{s_DMS}=None} cancels all C{DMS} symbols to
941 C{B{S_NUL}=NN}.
943 @note: Sexagecimal format B{C{F_D60}} supports overridable pseudo-DMS symbols
944 positioned at C{"[D]DD<B{s_D}>MM<B{s_M}>SS<B{s_S}>"} with defaults
945 C{B{s_D}="."}, C{B{s_M}=B{sep}} and C{B{s_S}=}L{pygeodesy.NN}.
947 @note: Formats B{C{F__E}}, B{C{F__F}} and B{C{F__G}} can be extended with
948 a C{D}-only symbol if defined with keyword argument C{B{s_D}=str}.
949 Likewise for B{C{F_RAD}} formats with keyword argument C{B{s_R}=str}.
951 @see: Function L{pygeodesy.degDMS}
952 '''
953 s = form[:1]
954 f = form[1:] if s in _PLUSMINUS_ else form
955 t = _toDMS(deg, f, prec, sep, ddd, NN, s_D_M_S) # unsigned and -suffixed
956 if deg < 0 and neg:
957 t = neg + t
958 elif deg > 0 and s == _PLUS_ and pos:
959 t = pos + t
960 return t
962# **) MIT License
963#
964# Copyright (C) 2016-2024 -- mrJean1 at Gmail -- All Rights Reserved.
965#
966# Permission is hereby granted, free of charge, to any person obtaining a
967# copy of this software and associated documentation files (the "Software"),
968# to deal in the Software without restriction, including without limitation
969# the rights to use, copy, modify, merge, publish, distribute, sublicense,
970# and/or sell copies of the Software, and to permit persons to whom the
971# Software is furnished to do so, subject to the following conditions:
972#
973# The above copyright notice and this permission notice shall be included
974# in all copies or substantial portions of the Software.
975#
976# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
977# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
978# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
979# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
980# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
981# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
982# OTHER DEALINGS IN THE SOFTWARE.