Coverage for pygeodesy/dms.py: 95%

283 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2024-02-25 12:57 -0500

1 

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

3 

4u'''Parsers and formatters of angles in degrees, minutes and seconds or radians. 

5 

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. 

9 

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. 

12 

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>}. 

16 

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}). 

28 

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}). 

40 

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}). 

52 

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} 

59 

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''' 

63 

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, _parseX, RangeError, rangerrors, _TypeError, \ 

68 _ValueError, _xkwds, _xkwds_get 

69from pygeodesy.interns import NN, _arg_, _COMMA_, _d_, _DASH_, _deg_, _degrees_, _DOT_, \ 

70 _0_, _e_, _E_, _EW_, _f_, _F_, _g_, _MINUS_, _N_, _NE_, _NS_, \ 

71 _NSEW_, _NW_, _of_, _PERCENTDOTSTAR_, _PLUS_, _PLUSMINUS_, \ 

72 _QUOTE1_, _QUOTE2_, _radians_, _S_, _SE_, _SPACE_, _SW_, _W_ 

73from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS 

74from pygeodesy.streprs import Fmt, fstr, fstrzs, _0wpF 

75# from pygeodesy.utily import _Wrap # _MODS 

76 

77from math import fabs, modf, radians 

78try: 

79 from string import letters as _LETTERS 

80except ImportError: # Python 3+ 

81 from string import ascii_letters as _LETTERS 

82 

83__all__ = _ALL_LAZY.dms 

84__version__ = '23.12.31' 

85 

86_beyond_ = 'beyond' 

87_DDDMMSS_ = 'DDDMMSS' 

88_deg_min_ = 'deg+min' 

89_keyword_ = 'keyword' 

90_SDIGITS_ = '-0123456789+' 

91_sexagecimal_ = 'sexagecimal' 

92_SEXAGECIMUL = 1.e4 # sexagecimal C{D.MMSSss} into decimal C{DMMSS.ss} 

93 

94F_D, F_DM, F_DMS, F_DEG, F_MIN, F_SEC, F_D60, F__E, F__F, F__G, F_RAD = _F_s = ( 

95 _d_, 'dm', 'dms', _deg_, 'min', 'sec', 'd60', _e_, _f_, _g_, 'rad') 

96F_D_, F_DM_, F_DMS_, F_DEG_, F_MIN_, F_SEC_, F_D60_, F__E_, F__F_, F__G_, F_RAD_ = (NN( 

97 _MINUS_, _) for _ in _F_s) 

98F_D__, F_DM__, F_DMS__, F_DEG__, F_MIN__, F_SEC__, F_D60__, F__E__, F__F__, F__G__, F_RAD__ = (NN( 

99 _PLUS_, _) for _ in _F_s) 

100del _F_s 

101 

102_F_case = {F_D: F_D, F_DEG: F_D, _degrees_: F_D, # unsigned _F_s 

103 F_DM: F_DM, F_MIN: F_DM, _deg_min_: F_DM, 

104 F_D60: F_D60, F_RAD: F_RAD, _radians_: F_RAD, 

105 F__E: F__E, F__F: F__F, F__G: F__G} # default F_DMS 

106 

107_F_prec = {F_D: 6, F_DM: 4, F_DMS: 2, # default precs 

108 F_DEG: 6, F_MIN: 4, F_SEC: 2, F_D60: 0, 

109 F__E: 8, F__F: 8, F__G: 8, F_RAD: 5} 

110 

111_F_symb = set((F_D, F_DM, F_DMS, _deg_min_)) # == {} pychok -Tb 

112 

113S_DEG = _DEGREES_ = '°' # ord() = 176 

114S_MIN = _MINUTES_ = '′' # PRIME 

115S_SEC = _SECONDS_ = '″' # DOUBLE_PRIME 

116S_RAD = _RADIANS_ = NN # PYCHOK radians symbol "" 

117S_DMS = True # include DMS symbols 

118S_SEP = NN # separator between deg|min|sec|suffix "" 

119S_NUL = NN # empty string, kept INTERNAL 

120 

121# note: ord(_DEGREES_) == ord('°') == 176, ord('˚') == 730 

122_S_norm = {S_DEG: _DEGREES_, '˚': _DEGREES_, '^': _DEGREES_, # _d_: _DEGREES_, 

123 S_MIN: _MINUTES_, '’': _MINUTES_, _QUOTE1_: _MINUTES_, # _r_: _RADIANS_ 

124 S_SEC: _SECONDS_, '”': _SECONDS_, _QUOTE2_: _SECONDS_} 

125 

126_WINDS = (_N_, 'NbE', 'NNE', 'NEbN', _NE_, 'NEbE', 'ENE', 'EbN', 

127 _E_, 'EbS', 'ESE', 'SEbE', _SE_, 'SEbS', 'SSE', 'SbE', 

128 _S_, 'SbW', 'SSW', 'SWbS', _SW_, 'SWbW', 'WSW', 'WbS', 

129 _W_, 'WbN', 'WNW', 'NWbW', _NW_, 'NWbN', 'NNW', 'NbW') 

130 

131 

132def _D603(sep, s_D=_DOT_, s_M=None, s_S=S_NUL, s_DMS=S_DMS, **unused): 

133 '''(INTERNAL) Get the overridden or default pseudo-C{DMS} symbols. 

134 ''' 

135 if s_DMS: 

136 M = sep if s_M is None else s_M 

137 return s_D, (M or S_NUL), s_S 

138 else: # no overriden symbols 

139 return _DOT_, sep, S_NUL 

140 

141 

142def _DMS3(form, s_D=S_DEG, s_M=S_MIN, s_S=S_SEC, s_DMS=S_DMS, **unused): 

143 '''(INTERNAL) Get the overridden or default C{DMS} symbols. 

144 ''' 

145 return (s_D, s_M, s_S) if s_DMS and form in _F_symb else (S_NUL, S_NUL, S_NUL) 

146 

147 

148def _dms3(d, ddd, p, w): 

149 '''(INTERNAL) Format C{d} as (deg, min, sec) C{str}s with leading zeros. 

150 ''' 

151 d, s = divmod(round(d * _3600_0, p), _3600_0) 

152 m, s = divmod(s, _60_0) 

153 return (_0wpF(ddd, 0, d), 

154 _0wpF( 2, 0, m), 

155 _0wpF(w+2, p, s)) 

156 

157 

158def _fstrzs(t, **unused): 

159 '''(INTERNAL) Pass-thru version of C{.streprs.fstrzs}. 

160 ''' 

161 return t 

162 

163 

164def _split3(strDMS, suffix=_NSEW_): 

165 '''(INTERNAL) Return sign, stripped B{C{strDMS}} and compass point. 

166 ''' 

167 t = strDMS.strip() 

168 s = t[:1] # sign or digit 

169 P = t[-1:] # compass point or digit or dot 

170 t = t.lstrip(_PLUSMINUS_).rstrip(suffix).strip() 

171 return s, t, P 

172 

173 

174def _toDMS(deg, form, prec, sep, ddd, suff, s_D_M_S): # MCCABE 13 in .units 

175 '''(INTERNAL) Convert C{deg} to C{str}, with/-out sign, DMS symbols and/or suffix. 

176 ''' 

177 f = Fmt.form or form 

178 try: 

179 deg = float(deg) 

180 except (TypeError, ValueError) as x: 

181 raise _ValueError(deg=deg, form=f, prec=prec, cause=x) 

182 

183 if f[:1] in _PLUSMINUS_: # signed 

184 sign = _MINUS_ if deg < 0 else ( 

185 _PLUS_ if deg > 0 and f[:1] == _PLUS_ else NN) 

186 f = f.lstrip(_PLUSMINUS_) 

187 suff = NN # no suffix if signed 

188 else: # suffixed 

189 sign = NN # no sign if suffixed 

190 if suff and sep: # no sep if no suffix 

191 suff = NN(sep, suff) 

192 try: 

193 F = _F_case[f] # .strip() 

194 except KeyError: 

195 f = f.lower() # .strip() 

196 F = _F_case.get(f, F_DMS) 

197 

198 if prec is None: 

199 z = p = _F_prec.get(F, 6) 

200 else: 

201 z = int(prec) 

202 p = abs(z) 

203 w = p + (1 if p else 0) 

204 z = fstrzs if z > 1 else _fstrzs 

205 d = fabs(deg) 

206 

207 if F is F_DMS: # 'deg+min+sec', default 

208 D, M, S = _DMS3(f, **s_D_M_S) 

209 d, m, s = _dms3(d, ddd, p, w) 

210 t = NN(sign, d, D, sep, 

211 m, M, sep, 

212 z(s), S, suff) 

213 

214 elif F is F_DM: # 'deg+min' 

215 D, M, _ = _DMS3(f, **s_D_M_S) 

216 d, m = divmod(round(d * _60_0, p), _60_0) 

217 t = NN(sign, _0wpF(ddd, 0, d), D, sep, 

218 z(_0wpF(w+2, p, m)), M, suff) 

219 

220 elif F is F_D: # 'deg' 

221 D, _, _ = _DMS3(f, **s_D_M_S) 

222 t = NN(sign, z(_0wpF(w+ddd, p, d)), D, suff) 

223 

224 elif F is F_D60: # 'deg.MM|SSss|' 

225 D, M, S = _D603(sep, **s_D_M_S) 

226 d, m, s = _dms3(d, ddd, p, w) 

227 t = z(s).split(_DOT_) + [S, suff] 

228 t = NN(sign, d, D, m, M, *t) 

229 

230 elif F is F_RAD: 

231 R = _xkwds_get(s_D_M_S, s_R=S_RAD) 

232 r = NN(_PERCENTDOTSTAR_, _F_) % (p, radians(d)) 

233 t = NN(sign, z(r), R, suff) 

234 

235 else: # F in (F__E, F__F, F__G) 

236 D = _xkwds_get(s_D_M_S, s_D=S_NUL) 

237 d = NN(_PERCENTDOTSTAR_, F) % (p, d) # XXX f? 

238 t = NN(sign, z(d, ap1z=F is F__G), D, suff) 

239 

240 return t # NOT unicode in Python 2- 

241 

242 

243def bearingDMS(bearing, form=F_D, prec=None, sep=S_SEP, **s_D_M_S): 

244 '''Convert bearing to a string (without compass point suffix). 

245 

246 @arg bearing: Bearing from North (compass C{degrees360}). 

247 @kwarg form: Format specifier for B{C{deg}} (C{str} or L{F_D}, 

248 L{F_DM}, L{F_DMS}, L{F_DEG}, L{F_MIN}, L{F_SEC}, 

249 L{F_D60}, L{F__E}, L{F__F}, L{F__G}, L{F_RAD}, 

250 L{F_D_}, L{F_DM_}, L{F_DMS_}, L{F_DEG_}, L{F_MIN_}, 

251 L{F_SEC_}, L{F_D60_}, L{F__E_}, L{F__F_}, L{F__G_}, 

252 L{F_RAD_}, L{F_D__}, L{F_DM__}, L{F_DMS__}, L{F_DEG__}, 

253 L{F_MIN__}, L{F_SEC__}, L{F_D60__}, L{F__E__}, 

254 L{F__F__}, L{F__G__} or L{F_RAD__}). 

255 @kwarg prec: Number of decimal digits (0..9 or C{None} for default). 

256 Trailing zero decimals are stripped for B{C{prec}} 

257 values of 1 and above, but kept for negative B{C{prec}}. 

258 @kwarg sep: Separator between degrees, minutes, seconds, suffix (C{str}). 

259 @kwarg s_D_M_S: Optional keyword arguments C{B{s_D}=str}, C{B{s_M}=str}, 

260 C{B{s_S}=str} and C{B{s_DMS}=True} to override any or 

261 cancel all DMS symbols, defaults L{S_DEG}, L{S_MIN} 

262 respectively L{S_SEC}. 

263 

264 @return: Compass degrees per the specified B{C{form}} (C{str}). 

265 

266 @see: Function L{pygeodesy.toDMS}. 

267 ''' 

268 return _toDMS(_umod_360(bearing), form, prec, sep, 1, NN, s_D_M_S) 

269 

270 

271def _clip(angle, limit, units): 

272 '''(INTERNAL) Helper for C{clipDegrees} and C{clipRadians}. 

273 ''' 

274 c = min(limit, max(-limit, angle)) 

275 if c != angle and rangerrors(): 

276 t = _SPACE_(fstr(angle, prec=6, ints=True), _beyond_, 

277 copysign0(limit, angle), units) 

278 raise RangeError(t, txt=None) 

279 return c 

280 

281 

282def clipDegrees(deg, limit): 

283 '''Clip a lat- or longitude to the given range. 

284 

285 @arg deg: Unclipped lat- or longitude (C{scalar degrees}). 

286 @arg limit: Valid C{-/+B{limit}} range (C{degrees}). 

287 

288 @return: Clipped value (C{degrees}). 

289 

290 @raise RangeError: If B{C{deg}} outside the valid C{-/+B{limit}} 

291 range and L{pygeodesy.rangerrors} set to C{True}. 

292 ''' 

293 return _clip(deg, limit, _degrees_) if limit and limit > 0 else deg 

294 

295 

296def clipRadians(rad, limit): 

297 '''Clip a lat- or longitude to the given range. 

298 

299 @arg rad: Unclipped lat- or longitude (C{radians}). 

300 @arg limit: Valid C{-/+B{limit}} range (C{radians}). 

301 

302 @return: Clipped value (C{radians}). 

303 

304 @raise RangeError: If B{C{rad}} outside the valid C{-/+B{limit}} 

305 range and L{pygeodesy.rangerrors} set to C{True}. 

306 ''' 

307 return _clip(rad, limit, _radians_) if limit and limit > 0 else rad 

308 

309 

310def compassDMS(bearing, form=F_D, prec=None, sep=S_SEP, **s_D_M_S): 

311 '''Convert bearing to a string suffixed with compass point. 

312 

313 @arg bearing: Bearing from North (compass C{degrees360}). 

314 @kwarg form: Format specifier for B{C{deg}} (C{str} or L{F_D}, 

315 L{F_DM}, L{F_DMS}, L{F_DEG}, L{F_MIN}, L{F_SEC}, 

316 L{F_D60}, L{F__E}, L{F__F}, L{F__G}, L{F_RAD}, 

317 L{F_D_}, L{F_DM_}, L{F_DMS_}, L{F_DEG_}, L{F_MIN_}, 

318 L{F_SEC_}, L{F_D60_}, L{F__E_}, L{F__F_}, L{F__G_}, 

319 L{F_RAD_}, L{F_D__}, L{F_DM__}, L{F_DMS__}, L{F_DEG__}, 

320 L{F_MIN__}, L{F_SEC__}, L{F_D60__}, L{F__E__}, 

321 L{F__F__}, L{F__G__} or L{F_RAD__}). 

322 @kwarg prec: Number of decimal digits (0..9 or C{None} for default). 

323 Trailing zero decimals are stripped for B{C{prec}} 

324 values of 1 and above, but kept for negative B{C{prec}}. 

325 @kwarg sep: Separator between degrees, minutes, seconds, suffix (C{str}). 

326 @kwarg s_D_M_S: Optional keyword arguments C{B{s_D}=str}, C{B{s_M}=str} 

327 C{B{s_S}=str} and C{B{s_DMS}=True} to override any or 

328 cancel all DMS symbols, defaults L{S_DEG}, L{S_MIN} 

329 respectively L{S_SEC}. 

330 

331 @return: Compass degrees and point in the specified form (C{str}). 

332 

333 @see: Function L{pygeodesy.toDMS}. 

334 ''' 

335 b = _umod_360(bearing) 

336 return _toDMS(b, form, prec, sep, 1, compassPoint(b), s_D_M_S) 

337 

338 

339def compassPoint(bearing, prec=3): 

340 '''Convert bearing to a compass point. 

341 

342 @arg bearing: Bearing from North (compass C{degrees360}). 

343 @kwarg prec: Precision, number of compass point characters: 

344 1 for cardinal or basic winds, 

345 2 for intercardinal or ordinal or principal winds, 

346 3 for secondary-intercardinal or half-winds or 

347 4 for quarter-winds). 

348 

349 @return: Compass point (1-, 2-, 3- or 4-letter C{str}). 

350 

351 @raise ValueError: Invalid B{C{prec}}. 

352 

353 @see: U{Dms.compassPoint 

354 <https://GitHub.com/ChrisVeness/geodesy/blob/master/dms.js>} 

355 and U{Compass rose<https://WikiPedia.org/wiki/Compass_rose>}. 

356 

357 @example: 

358 

359 >>> p = compassPoint(24, 1) # 'N' 

360 >>> p = compassPoint(24, 2) # 'NE' 

361 >>> p = compassPoint(24, 3) # 'NNE' 

362 >>> p = compassPoint(24) # 'NNE' 

363 >>> p = compassPoint(11, 4) # 'NbE' 

364 >>> p = compassPoint(30, 4) # 'NEbN' 

365 

366 >>> p = compassPoint(11.249) # 'N' 

367 >>> p = compassPoint(11.25) # 'NNE' 

368 >>> p = compassPoint(-11.25) # 'N' 

369 >>> p = compassPoint(348.749) # 'NNW' 

370 ''' 

371 try: # like .streprs.enstr2 

372 m = 2 << prec 

373 if m in (4, 8, 16, 32): 

374 w = 32 // m 

375 # not round(), i.e. half-even rounding in Python 3+, 

376 # but round-away-from-zero as int(b + 0.5) iff b is 

377 # non-negative, otherwise int(b + copysign0(_0_5, b)) 

378 w *= int(_umod_360(bearing) * m / _360_0 + _0_5) % m 

379 return _WINDS[w] 

380 

381 raise ValueError 

382 except (IndexError, TypeError, ValueError) as x: 

383 raise _ValueError(bearing=bearing, prec=prec, cause=x) 

384 

385 

386def degDMS(deg, prec=6, s_D=S_DEG, s_M=S_MIN, s_S=S_SEC, neg=_MINUS_, pos=NN): 

387 '''Convert degrees to a string in degrees, minutes I{or} seconds. 

388 

389 @arg deg: Value in degrees (C{scalar degrees}). 

390 @kwarg prec: Number of decimal digits (0..9 or C{None} for default). 

391 Trailing zero decimals are stripped for B{C{prec}} 

392 values of 1 and above, but kept for negative B{C{prec}}. 

393 @kwarg s_D: D symbol for degrees (C{str}). 

394 @kwarg s_M: M symbol for minutes (C{str}) or C{""}. 

395 @kwarg s_S: S symbol for seconds (C{str}) or C{""}. 

396 @kwarg neg: Optional sign for negative (C{'-'}). 

397 @kwarg pos: Optional sign for positive (C{''}). 

398 

399 @return: I{Either} degrees, minutes I{or} seconds (C{str}). 

400 

401 @see: Function L{pygeodesy.toDMS}. 

402 ''' 

403 try: 

404 deg = float(deg) 

405 except (TypeError, ValueError) as x: 

406 raise _ValueError(deg=deg, prec=prec, cause=x) 

407 

408 d, s = fabs(deg), s_D 

409 if d < 1: 

410 if s_M: 

411 d *= _60_0 

412 if d < 1 and s_S: 

413 d *= _60_0 

414 s = s_S 

415 else: 

416 s = s_M 

417 elif s_S: 

418 d *= _3600_0 

419 s = s_S 

420 

421 z = int(prec) 

422 t = Fmt.F(d, prec=abs(z)) 

423 if z > 1: 

424 t = fstrzs(t) 

425 n = neg if deg < 0 else pos 

426 return NN(n, t, s) # NOT unicode in Python 2- 

427 

428 

429def latDMS(deg, form=F_DMS, prec=None, sep=S_SEP, **s_D_M_S): 

430 '''Convert latitude to a string, optionally suffixed with N or S. 

431 

432 @arg deg: Latitude to be formatted (C{scalar degrees}). 

433 @kwarg form: Format specifier for B{C{deg}} (C{str} or L{F_D}, 

434 L{F_DM}, L{F_DMS}, L{F_DEG}, L{F_MIN}, L{F_SEC}, 

435 L{F_D60}, L{F__E}, L{F__F}, L{F__G}, L{F_RAD}, 

436 L{F_D_}, L{F_DM_}, L{F_DMS_}, L{F_DEG_}, L{F_MIN_}, 

437 L{F_SEC_}, L{F_D60_}, L{F__E_}, L{F__F_}, L{F__G_}, 

438 L{F_RAD_}, L{F_D__}, L{F_DM__}, L{F_DMS__}, L{F_DEG__}, 

439 L{F_MIN__}, L{F_SEC__}, L{F_D60__}, L{F__E__}, 

440 L{F__F__}, L{F__G__} or L{F_RAD__}). 

441 @kwarg prec: Number of decimal digits (0..9 or C{None} for default). 

442 Trailing zero decimals are stripped for B{C{prec}} 

443 values of 1 and above, but kept for negative B{C{prec}}. 

444 @kwarg sep: Separator between degrees, minutes, seconds, suffix (C{str}). 

445 @kwarg s_D_M_S: Optional keyword arguments C{B{s_D}=str}, C{B{s_M}=str} 

446 C{B{s_S}=str} and C{B{s_DMS}=True} to override any or 

447 cancel all DMS symbols, defaults L{S_DEG}, L{S_MIN} 

448 respectively L{S_SEC}. 

449 

450 @return: Degrees in the specified form (C{str}). 

451 

452 @see: Functions L{pygeodesy.toDMS} and L{pygeodesy.lonDMS}. 

453 ''' 

454 p = _S_ if deg < 0 else _N_ 

455 return _toDMS(deg, form, prec, sep, 2, p, s_D_M_S) 

456 

457 

458def latlonDMS(lls, **m_form_prec_sep_s_D_M_S): 

459 '''Convert one or more C{LatLon} instances to strings. 

460 

461 @arg lls: Single or list, sequence, tuple, etc. (C{LatLon}s). 

462 @kwarg m_form_prec_sep_s_D_M_S: Optional C{B{m}eter}, C{B{form}at}, 

463 C{B{prec}ision}, I{DEPRECATED} B{C{sep}}, B{C{s_D}}, B{C{s_M}}, 

464 B{C{s_S}} and B{C{s_DMS}} keyword arguments, see method 

465 C{LatLon.toStr} and functions L{pygeodesy.latDMS} and 

466 L{pygeodesy.lonDMS}. 

467 

468 @return: A C{tuple} of C{str}s if B{C{lls}} is a list, sequence, 

469 tuple, etc. of C{LatLon} instances or a single C{str} 

470 if B{C{lls}} is a single C{LatLon}. 

471 

472 @see: Functions L{pygeodesy.latDMS}, L{pygeodesy.latlonDMS_}, 

473 L{pygeodesy.lonDMS} and L{pygeodesy.toDMS} and method 

474 C{LatLon.toStr}. 

475 

476 @note: Keyword argument C{B{sep}=None} to return a C{str}ing 

477 instead of the C{tuple}, has been I{DEPRECATED}, use 

478 C{B{sep}.join(B{latlonDMS_}(...))}. 

479 ''' 

480 sep, kwds = _latlonDMS_sep2(latlonDMS, **m_form_prec_sep_s_D_M_S) 

481 if isLatLon(lls): 

482 t = lls.toStr(**kwds) 

483 elif issequence(lls): 

484 t = tuple(ll.toStr(**kwds) for ll in lls) 

485 if sep: # XXX TO BE REMOVED 

486 t = sep.join(t) 

487 else: 

488 raise _TypeError(lls=lls, **m_form_prec_sep_s_D_M_S) 

489 return t 

490 

491 

492def latlonDMS_(*lls, **m_form_prec_sep_s_D_M_S): 

493 '''Convert one or more C{LatLon} instances to strings. 

494 

495 @arg lls: The instances, all positional arguments (C{LatLon}s). 

496 @kwarg m_form_prec_sep_s_D_M_S: Optional C{B{m}eter}, C{B{form}at}, 

497 C{B{prec}ision}, I{DEPRECATED} B{C{sep}}, B{C{s_D}}, B{C{s_M}}, 

498 B{C{s_S}} and B{C{s_DMS}} keyword arguments, see method 

499 C{LatLon.toStr} and functions L{pygeodesy.latDMS} and 

500 L{pygeodesy.lonDMS}. 

501 

502 @return: A C{tuple} of C{str}s if 2 or more C{LatLon} instances 

503 or a single C{str} if only a single C{LatLon} instance 

504 is given in B{C{lls}}. 

505 

506 @see: Function L{pygeodesy.latlonDMS}. 

507 

508 @note: Keyword argument C{B{sep}=None} to return a C{str}ing 

509 instead of the C{tuple}, has been I{DEPRECATED}, use 

510 C{B{sep}.join(B{latlonDMS_}(...))}. 

511 ''' 

512 sep, kwds = _latlonDMS_sep2(latlonDMS, **m_form_prec_sep_s_D_M_S) 

513 if not lls: 

514 raise _ValueError(lls=lls, **m_form_prec_sep_s_D_M_S) 

515 elif len(lls) < 2: 

516 lls, sep = lls[0], None 

517 t = latlonDMS(lls, **kwds) 

518 return sep.join(t) if sep else t 

519 

520 

521def _latlonDMS_sep2(where, sep=None, **kwds): 

522 '''DEPRECATED, instead use: %r.join(%s(...))''' 

523 if sep: 

524 k = _SPACE_(_keyword_, _arg_, Fmt.EQUAL(sep=repr(sep)), _of_) 

525 n = where.__name__ 

526 t = _latlonDMS_sep2.__doc__ % (sep, n) 

527 _MODS.props._throwarning(k, n, t) 

528 return sep, kwds 

529 

530 

531def lonDMS(deg, form=F_DMS, prec=None, sep=S_SEP, **s_D_M_S): 

532 '''Convert longitude to a string, optionally suffixed with E or W. 

533 

534 @arg deg: Longitude to be formatted (C{scalar degrees}). 

535 @kwarg form: Format specifier for B{C{deg}} (C{str} or L{F_D}, 

536 L{F_DM}, L{F_DMS}, L{F_DEG}, L{F_MIN}, L{F_SEC}, 

537 L{F_D60}, L{F__E}, L{F__F}, L{F__G}, L{F_RAD}, 

538 L{F_D_}, L{F_DM_}, L{F_DMS_}, L{F_DEG_}, L{F_MIN_}, 

539 L{F_SEC_}, L{F_D60_}, L{F__E_}, L{F__F_}, L{F__G_}, 

540 L{F_RAD_}, L{F_D__}, L{F_DM__}, L{F_DMS__}, L{F_DEG__}, 

541 L{F_MIN__}, L{F_SEC__}, L{F_D60__}, L{F__E__}, 

542 L{F__F__}, L{F__G__} or L{F_RAD__}). 

543 @kwarg prec: Number of decimal digits (0..9 or C{None} for default). 

544 Trailing zero decimals are stripped for B{C{prec}} 

545 values of 1 and above, but kept for negative B{C{prec}}. 

546 @kwarg sep: Separator between degrees, minutes, seconds, suffix (C{str}). 

547 @kwarg s_D_M_S: Optional keyword arguments C{B{s_D}=str}, C{B{s_M}=str} 

548 C{B{s_S}=str} and C{B{s_DMS}=True} to override any or 

549 cancel all DMS symbols, defaults L{S_DEG}, L{S_MIN} 

550 respectively L{S_SEC}. 

551 

552 @return: Degrees in the specified form (C{str}). 

553 

554 @see: Functions L{pygeodesy.toDMS} and L{pygeodesy.latDMS}. 

555 ''' 

556 p = _W_ if deg < 0 else _E_ 

557 return _toDMS(deg, form, prec, sep, 3, p, s_D_M_S) 

558 

559 

560def normDMS(strDMS, norm=None, **s_D_M_S): 

561 '''Normalize all degrees, minutes and seconds (DMS) I{symbols} in 

562 a string to the default symbols L{S_DEG}, L{S_MIN}, L{S_SEC}. 

563 

564 @arg strDMS: Original DMS string (C{str}). 

565 @kwarg norm: Optional replacement symbol (C{str}) or C{None} for 

566 the default DMS symbols). Use C{B{norm}=""} to 

567 remove all DMS symbols. 

568 @kwarg s_D_M_S: Optional, alternate DMS symbols C{B{s_D}=str}, 

569 C{B{s_M}=str}, C{B{s_S}=str} and/or C{B{s_R}=str} 

570 for radians, each to be replaced by B{C{norm}}. 

571 

572 @return: Normalized DMS (C{str}). 

573 ''' 

574 def _s2S2(s_D=S_DEG, s_M=S_MIN, s_S=S_SEC, s_R=S_RAD): 

575 d = {s_D: S_DEG, s_M: S_MIN, s_S: S_SEC, s_R: S_RAD} 

576 for s, S in _xkwds(d, **_S_norm).items(): 

577 if s: 

578 yield s, S 

579 

580 # XXX strDMS isn't unicode in Python 2- and looping 

581 # thru strDMS will yield each byte, hence the loop 

582 # thru _s2S2 and replacing the DMS symbols in strDMS 

583 

584 if norm is None: # back to default DMS 

585 for s, S in _s2S2(**s_D_M_S): 

586 if s != S: 

587 strDMS = strDMS.replace(s, S) 

588 

589 else: # replace or remove all DMS 

590 n = norm or NN 

591 for s, _ in _s2S2(**s_D_M_S): 

592 if s != n: 

593 strDMS = strDMS.replace(s, n) 

594 if n: 

595 strDMS = strDMS.rstrip(n) # XXX not .strip? 

596 

597 return strDMS # NOT unicode in Python 2- 

598 

599 

600def parseDDDMMSS(strDDDMMSS, suffix=_NSEW_, sep=S_SEP, clip=0, sexagecimal=False): # MCCABE 14 

601 '''Parse a lat- or longitude represention forms as [D]DDMMSS in degrees. 

602 

603 @arg strDDDMMSS: Degrees in any of several forms (C{str}) and types (C{float}, 

604 C{int}, other). 

605 @kwarg suffix: Optional, valid compass points (C{str}, C{tuple}). 

606 @kwarg sep: Optional separator between "[D]DD", "MM", "SS", B{C{suffix}} (L{S_SEP}). 

607 @kwarg clip: Optionally, limit value to range C{-/+B{clip}} (C{degrees}). 

608 @kwarg sexagecimal: If C{True}, convert C{"D.MMSS"} or C{float(D.MMSS)} to 

609 C{base-60} "MM" and "SS" digits. See C{form}s L{F_D60}, 

610 L{F_D60_} and L{F_D60__}. 

611 

612 @return: Degrees (C{float}). 

613 

614 @raise ParseError: Invalid B{C{strDDDMMSS}} or B{C{clip}} or the form of 

615 B{C{strDDDMMSS}} is incompatible with the suffixed or 

616 B{C{suffix}} compass point. 

617 

618 @raise RangeError: Value of B{C{strDDDMMSS}} outside the valid C{-/+B{clip}} 

619 range and L{pygeodesy.rangerrors} set to C{True}. 

620 

621 @note: Type C{str} values "[D]DD", "[D]DDMM", "[D]DDMMSS" and "[D]DD.MMSS" 

622 for B{C{strDDDMMSS}} are parsed properly only if I{either} unsigned 

623 and suffixed with a valid, compatible, C{cardinal} L{compassPoint} 

624 I{or} signed I{or} unsigned, unsuffixed and with keyword argument 

625 B{C{suffix}="NS"}, B{C{suffix}="EW"} or a compatible L{compassPoint}. 

626 

627 @note: Unlike function L{parseDMS}, type C{float}, C{int} and other non-C{str} 

628 B{C{strDDDMMSS}} values are interpreted as C{form} [D]DDMMSS or 

629 [D]DD.MMSS. For example, C{int(1230)} is returned as 12.5 and I{not 

630 1230.0} degrees. However, C{int(345)} is considered C{form} "DDD" 

631 345 I{and not "DDMM" 0345}, unless B{C{suffix}} specifies the compass 

632 point. Also, C{float(15.0523)} is returned as 15.0523 decimal 

633 degrees and I{not 15°5′23″ sexagecimal}. To consider the latter, use 

634 C{float(15.0523)} or C{"15.0523"} and specify the keyword argument 

635 C{B{sexagecimal}=True}. 

636 

637 @see: Functions L{pygeodesy.parseDMS}, L{pygeodesy.parseDMS2} and 

638 L{pygeodesy.parse3llh}. 

639 ''' 

640 def _DDDMMSS(strDDDMMSS, suffix, sep, clip, sexagecimal): 

641 S = suffix.upper() 

642 if isstr(strDDDMMSS): 

643 t = strDDDMMSS.replace(sep, NN) if sep else strDDDMMSS 

644 s, t, P = _split3(t, S) 

645 f = t.split(_DOT_) 

646 n = len(f[0]) 

647 f = NN.join(f) 

648 if 1 < n < 8 and f.isdigit() and ( # dddN/S/E/W or ddd or +/-ddd 

649 (P in S and s.isdigit()) or 

650 (P.isdigit() and s in _SDIGITS_ # PYCHOK indent 

651 and S in _WINDS)): 

652 # check [D]DDMMSS form and compass point 

653 X = _EW_ if isodd(n) else _NS_ 

654 if not (P in X or (S in X and (P.isdigit() or P == _DOT_))): 

655 t = _DDDMMSS_[int(X is _NS_):(n | 1)], _DASH_.join(X) 

656 raise ParseError('form %s applies %s' % t) 

657 elif not sexagecimal: # try other forms 

658 return _DMS2deg(strDDDMMSS, S, sep, clip, {}) 

659 

660 if sexagecimal: # move decimal dot from ... 

661 n += 4 # ... [D]DD.MMSSs to [D]DDMMSS.s 

662 if n < 6: 

663 raise ParseError('%s digits (%s)' % (_sexagecimal_, n)) 

664 z = n - len(f) # zeros to append 

665 t = (f + (_0_ * z)) if z > 0 else _DOT_(f[:n], f[n:]) 

666 f = _0_0 # fraction 

667 

668 else: # float or int to [D]DDMMSS[.fff] 

669 f, m = float(strDDDMMSS), 0 

670 if sexagecimal: 

671 f *= _SEXAGECIMUL 

672 m = 6 

673 s = P = _0_ # anything except NN, _S_, _SW_, _W_ 

674 if f < 0: 

675 f = -f 

676 s = _MINUS_ 

677 f, i = modf(f) # returns ... 

678 t = str(int(i)) # ... float(i) 

679 n = len(t) # number of digits to ... 

680 if n < m: # ... required min or ... 

681 t = (_0_ * (m - n)) + t 

682 # ... match the given compass point 

683 elif S in (_NS_ if isodd(n) else _EW_): 

684 t = _0_ + t 

685 # P = S 

686 # elif n > 1: 

687 # P = (_EW_ if isodd(n) else _NS_)[0] 

688 n = len(t) 

689 

690 if n < 4: # [D]DD[.ddd] 

691 t = (float(t) + f), 

692 else: 

693 f += float(t[n-2:]) 

694 if n < 6: # [D]DDMM[.mmm] 

695 t = float(t[:n-2]), f 

696 else: # [D]DDMMSS[.sss] 

697 t = float(t[:n-4]), float(t[n-4:n-2]), f 

698 d = _dms2deg(s, P, *t) 

699 return clipDegrees(d, float(clip)) if clip else d 

700 

701 return _parseX(_DDDMMSS, strDDDMMSS, suffix, sep, clip, sexagecimal, 

702 strDDDMMSS=strDDDMMSS, suffix=suffix, sexagecimal=sexagecimal) 

703 

704 

705def _dms2deg(s, P, deg, min=_0_0, sec=_0_0): 

706 '''(INTERNAL) Helper for C{parseDDDMMSS} and C{_DMS2deg}. 

707 ''' 

708 deg += (min + (sec / _60_0)) / _60_0 

709 if s == _MINUS_ or (P and P in _SW_): 

710 deg = _neg(deg) 

711 return deg 

712 

713 

714def _DMS2deg(strDMS, suffix, sep, clip, s_D_M_S): 

715 '''(INTERNAL) Helper for C{parseDDDMMSS} and C{parseDMS}. 

716 ''' 

717 try: 

718 d = float(strDMS) 

719 

720 except (TypeError, ValueError): 

721 s, t, P = _split3(strDMS, suffix.upper()) 

722 if sep: # remove all DMS symbols 

723 t = t.replace(sep, _SPACE_) 

724 t = normDMS(t, norm=NN, **s_D_M_S) 

725 else: # replace all DMS symbols 

726 t = normDMS(t, norm=_SPACE_, **s_D_M_S) 

727 t = map2(float, t.strip().split()) 

728 d = _dms2deg(s, P, *t[:3]) 

729 

730 return clipDegrees(d, float(clip)) if clip else d 

731 

732 

733def parseDMS(strDMS, suffix=_NSEW_, sep=S_SEP, clip=0, **s_D_M_S): # MCCABE 14 

734 '''Parse a lat- or longitude representation in C{degrees}. 

735 

736 This is very flexible on formats, allowing signed decimal 

737 degrees, degrees and minutes or degrees minutes and seconds 

738 optionally suffixed by a cardinal compass point. 

739 

740 A variety of symbols, separators and suffixes are accepted, 

741 for example "3°37′09″W". Minutes and seconds may be omitted. 

742 

743 @arg strDMS: Degrees in any of several forms (C{str}) and 

744 types (C{float}, C{int}, other). 

745 @kwarg suffix: Optional, valid compass points (C{str}, C{tuple}). 

746 @kwarg sep: Optional separator between deg°, min′, sec″, B{C{suffix}} (C{''}). 

747 @kwarg clip: Optionally, limit value to range C{-/+B{clip}} (C{degrees}). 

748 @kwarg s_D_M_S: Optional, alternate symbol for degrees C{B{s_D}=str}, 

749 minutes C{B{s_M}=str} and/or seconds C{B{s_S}=str}. 

750 

751 @return: Degrees (C{float}). 

752 

753 @raise ParseError: Invalid B{C{strDMS}} or B{C{clip}}. 

754 

755 @raise RangeError: Value of B{C{strDMS}} outside the valid C{-/+B{clip}} 

756 range and L{pygeodesy.rangerrors} set to C{True}. 

757 

758 @note: Unlike function L{parseDDDMMSS}, type C{float}, C{int} and other 

759 non-C{str} B{C{strDMS}} values are considered decimal (and not 

760 sexagecimal) degrees. For example, C{int(1230)} is returned 

761 as 1230.0 I{and not as 12.5} degrees and C{float(345)} as 345.0 

762 I{and not as 3.75} degrees! 

763 

764 @see: Functions L{pygeodesy.parseDDDMMSS}, L{pygeodesy.parseDMS2}, 

765 L{pygeodesy.parse3llh} and L{pygeodesy.toDMS}. 

766 ''' 

767 return _parseX(_DMS2deg, strDMS, suffix, sep, clip, s_D_M_S, strDMS=strDMS, suffix=suffix) 

768 

769 

770def parseDMS2(strLat, strLon, sep=S_SEP, clipLat=90, clipLon=180, wrap=False, **s_D_M_S): 

771 '''Parse a lat- and a longitude representions C{"lat, lon"} in C{degrees}. 

772 

773 @arg strLat: Latitude in any of several forms (C{str} or C{degrees}). 

774 @arg strLon: Longitude in any of several forms (C{str} or C{degrees}). 

775 @kwarg sep: Optional separator between deg°, min′, sec″, suffix (C{''}). 

776 @kwarg clipLat: Limit latitude to range C{-/+B{clipLat}} (C{degrees}). 

777 @kwarg clipLon: Limit longitude to range C{-/+B{clipLon}} (C{degrees}). 

778 @kwarg wrap: If C{True}, wrap or I{normalize} the lat- and longitude, 

779 overriding B{C{clipLat}} and B{C{clipLon}} (C{bool}). 

780 @kwarg s_D_M_S: Optional, alternate symbol for degrees C{B{s_D}=str}, 

781 minutes C{B{s_M}=str} and/or seconds C{B{s_S}=str}. 

782 

783 @return: A L{LatLon2Tuple}C{(lat, lon)} in C{degrees}. 

784 

785 @raise ParseError: Invalid B{C{strLat}} or B{C{strLon}}. 

786 

787 @raise RangeError: Value of B{C{strLat}} or B{C{strLon}} outside the 

788 valid C{-/+B{clipLat}} or C{-/+B{clipLon}} range 

789 and L{pygeodesy.rangerrors} set to C{True}. 

790 

791 @note: See the B{Notes} at function L{parseDMS}. 

792 

793 @see: Functions L{pygeodesy.parseDDDMMSS}, L{pygeodesy.parseDMS}, 

794 L{pygeodesy.parse3llh} and L{pygeodesy.toDMS}. 

795 ''' 

796 return _2Tuple(strLat, strLon, clipLat, clipLon, wrap, sep=sep, **s_D_M_S) 

797 

798 

799def _2Tuple(strLat, strLon, clipLat, clipLon, wrap, **kwds): 

800 '''(INTERNAL) Helper for C{parseDMS2} and C{parse3llh}. 

801 ''' 

802 if wrap: 

803 _W = _MODS.utily._Wrap 

804 lat, lon = _W.latlon(parseDMS(strLat, suffix=_NS_, **kwds), 

805 parseDMS(strLon, suffix=_EW_, **kwds)) 

806 else: 

807 # if wrap is None: 

808 # clipLat = clipLon = 0 

809 lat = parseDMS(strLat, suffix=_NS_, clip=clipLat, **kwds) 

810 lon = parseDMS(strLon, suffix=_EW_, clip=clipLon, **kwds) 

811 return _MODS.namedTuples.LatLon2Tuple(lat, lon) 

812 

813 

814def parse3llh(strllh, height=0, sep=_COMMA_, clipLat=90, clipLon=180, wrap=False, **s_D_M_S): 

815 '''Parse a string C{"lat, lon [, h]"} representing lat-, longitude in 

816 C{degrees} and optional height in C{meter}. 

817 

818 The lat- and longitude value must be separated by a separator 

819 character. If height is present it must follow, separated by 

820 another separator. 

821 

822 The lat- and longitude values may be swapped, provided at least 

823 one ends with the proper compass point. 

824 

825 @arg strllh: Latitude, longitude[, height] (C{str}, ...). 

826 @kwarg height: Optional, default height (C{meter}) or C{None}. 

827 @kwarg sep: Optional separator between C{"lat lon [h] suffix"} (C{str}). 

828 @kwarg clipLat: Limit latitude to range C{-/+B{clipLat}} (C{degrees}). 

829 @kwarg clipLon: Limit longitude to range C{-/+B{clipLon}} (C{degrees}). 

830 @kwarg wrap: If C{True}, wrap or I{normalize} the lat- and longitude, 

831 overriding B{C{clipLat}} and B{C{clipLon}} (C{bool}). 

832 @kwarg s_D_M_S: Optional, alternate symbol for degrees C{B{s_D}=str}, 

833 minutes C{B{s_M}=str} and/or seconds C{B{s_S}=str}. 

834 

835 @return: A L{LatLon3Tuple}C{(lat, lon, height)} in C{degrees}, 

836 C{degrees} and C{float}. 

837 

838 @raise RangeError: Lat- or longitude value of B{C{strllh}} outside 

839 the valid C{-/+B{clipLat}} or C{-/+B{clipLon}} 

840 range and L{pygeodesy.rangerrors} set to C{True}. 

841 

842 @raise ValueError: Invalid B{C{strllh}} or B{C{height}}. 

843 

844 @note: See the B{Notes} at function L{parseDMS}. 

845 

846 @see: Functions L{pygeodesy.parseDDDMMSS}, L{pygeodesy.parseDMS}, 

847 L{pygeodesy.parseDMS2} and L{pygeodesy.toDMS}. 

848 ''' 

849 

850 def _3llh(strllh, height, sep, wrap): 

851 ll = strllh.strip().split(sep) 

852 if len(ll) > 2: # XXX interpret height unit 

853 h = float(ll.pop(2).rstrip(_LETTERS + _SPACE_)) 

854 else: 

855 h = height # None from wgrs.Georef.__new__ 

856 if len(ll) != 2: 

857 raise ValueError 

858 

859 a, b = [_.strip() for _ in ll] # PYCHOK false 

860 if a[-1:] in _EW_ or b[-1:] in _NS_: 

861 a, b = b, a 

862 return _2Tuple(a, b, clipLat, clipLon, wrap, **s_D_M_S).to3Tuple(h) 

863 

864 return _parseX(_3llh, strllh, height, sep, wrap, strllh=strllh) 

865 

866 

867def parseRad(strRad, suffix=_NSEW_, clip=0): 

868 '''Parse a string representing angle in C{radians}. 

869 

870 @arg strRad: Degrees in any of several forms (C{str} or C{radians}). 

871 @kwarg suffix: Optional, valid compass points (C{str}, C{tuple}). 

872 @kwarg clip: Optionally, limit value to range C{-/+B{clip}} (C{radians}). 

873 

874 @return: Radians (C{float}). 

875 

876 @raise ParseError: Invalid B{C{strRad}} or B{C{clip}}. 

877 

878 @raise RangeError: Value of B{C{strRad}} outside the valid C{-/+B{clip}} 

879 range and L{pygeodesy.rangerrors} set to C{True}. 

880 ''' 

881 def _Rad(strRad, suffix, clip): 

882 try: 

883 r = float(strRad) 

884 

885 except (TypeError, ValueError): 

886 s, t, P = _split3(strRad, suffix.upper()) 

887 r = _dms2deg(s, P, float(t)) 

888 

889 return clipRadians(r, float(clip)) if clip else r 

890 

891 return _parseX(_Rad, strRad, suffix, clip, strRad=strRad, suffix=suffix) 

892 

893 

894def precision(form, prec=None): 

895 '''Set the default precison for a given F_ form. 

896 

897 @arg form: L{F_D}, L{F_DM}, L{F_DMS}, L{F_DEG}, L{F_MIN}, 

898 L{F_SEC}, L{F_D60}, L{F__E}, L{F__F}, L{F__G} 

899 or L{F_RAD} (C{str}). 

900 @kwarg prec: Number of decimal digits (0..9 or C{None} for 

901 default). Trailing zero decimals are stripped 

902 for B{C{prec}} values of 1 and above, but kept 

903 for negative B{C{prec}}. 

904 

905 @return: Previous precision for the B{C{form}} (C{int}). 

906 

907 @raise ValueError: Invalid B{C{form}} or B{C{prec}} or B{C{prec}} 

908 outside the valid range C{-/+9}. 

909 ''' 

910 try: 

911 p = _F_prec[form] 

912 except KeyError: 

913 raise _ValueError(form=form) 

914 

915 if prec is not None: # set as default 

916 _F_prec[form] = _MODS.units.Precision_(prec=prec, low=-9, high=9) 

917 

918 return p 

919 

920 

921def toDMS(deg, form=F_DMS, prec=2, sep=S_SEP, ddd=2, neg=_MINUS_, pos=_PLUS_, **s_D_M_S): 

922 '''Convert I{signed} C{degrees} to string, without suffix. 

923 

924 @arg deg: Degrees to be formatted (C{scalar degrees}). 

925 @kwarg form: Format specifier for B{C{deg}} (C{str} or L{F_D}, 

926 L{F_DM}, L{F_DMS}, L{F_DEG}, L{F_MIN}, L{F_SEC}, 

927 L{F_D60}, L{F__E}, L{F__F}, L{F__G}, L{F_RAD}, 

928 L{F_D_}, L{F_DM_}, L{F_DMS_}, L{F_DEG_}, L{F_MIN_}, 

929 L{F_SEC_}, L{F_D60_}, L{F__E_}, L{F__F_}, L{F__G_}, 

930 L{F_RAD_}, L{F_D__}, L{F_DM__}, L{F_DMS__}, L{F_DEG__}, 

931 L{F_MIN__}, L{F_SEC__}, L{F_D60__}, L{F__E__}, 

932 L{F__F__}, L{F__G__} or L{F_RAD__}). 

933 @kwarg prec: Number of decimal digits (0..9 or C{None} for default). 

934 Trailing zero decimals are stripped for B{C{prec}} 

935 values of 1 and above, but kept for negative B{C{prec}}. 

936 @kwarg sep: Separator between degrees, minutes, seconds, suffix (C{str}). 

937 @kwarg ddd: Number of digits for B{C{deg}°} (2 or 3). 

938 @kwarg neg: Prefix for negative B{C{deg}} (C{'-'}). 

939 @kwarg pos: Prefix for positive B{C{deg}} and signed B{C{form}} (C{'+'}). 

940 @kwarg s_D_M_S: Optional keyword arguments C{B{s_D}=str}, C{B{s_M}=str} 

941 C{B{s_S}=str} and C{B{s_DMS}=True} to override any or 

942 cancel all DMS symbols, defaults L{S_DEG}, L{S_MIN} 

943 respectively L{S_SEC}. See B{Notes} below. 

944 

945 @return: Degrees in the specified form (C{str}). 

946 

947 @note: The degrees, minutes and seconds (DMS) symbol can be overridden in 

948 this and other C{*DMS} functions by using optional keyword argments 

949 C{B{s_D}="d"}, C{B{s_M}="'"} respectively C{B{s_S}='"'}. Using 

950 keyword argument B{C{s_DMS}=None} cancels all C{DMS} symbols to 

951 C{B{S_NUL}=NN}. 

952 

953 @note: Sexagecimal format B{C{F_D60}} supports overridable pseudo-DMS symbols 

954 positioned at C{"[D]DD<B{s_D}>MM<B{s_M}>SS<B{s_S}>"} with defaults 

955 C{B{s_D}="."}, C{B{s_M}=B{sep}} and C{B{s_S}=}L{pygeodesy.NN}. 

956 

957 @note: Formats B{C{F__E}}, B{C{F__F}} and B{C{F__G}} can be extended with 

958 a C{D}-only symbol if defined with keyword argument C{B{s_D}=str}. 

959 Likewise for B{C{F_RAD}} formats with keyword argument C{B{s_R}=str}. 

960 

961 @see: Function L{pygeodesy.degDMS} 

962 ''' 

963 s = form[:1] 

964 f = form[1:] if s in _PLUSMINUS_ else form 

965 t = _toDMS(deg, f, prec, sep, ddd, NN, s_D_M_S) # unsigned and -suffixed 

966 if deg < 0 and neg: 

967 t = neg + t 

968 elif deg > 0 and s == _PLUS_ and pos: 

969 t = pos + t 

970 return t 

971 

972# **) MIT License 

973# 

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

975# 

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

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

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

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

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

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

982# 

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

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

985# 

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

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

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

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

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

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

992# OTHER DEALINGS IN THE SOFTWARE.