GeographicLib: DMS.hpp Source File

GeographicLib 2.5.2

Loading...
Searching...
No Matches
DMS.hpp
Go to the documentation of this file.
1/**
2 * \file DMS.hpp
3 * \brief Header for GeographicLib::DMS class
4 *
5 * Copyright (c) Charles Karney (2008-2022) <karney@alum.mit.edu> and licensed
6 * under the MIT/X11 License. For more information, see
7 * https://geographiclib.sourceforge.io/
8 **********************************************************************/
9
10#if !defined(GEOGRAPHICLIB_DMS_HPP)
11#define GEOGRAPHICLIB_DMS_HPP 1
12
13#include <GeographicLib/Constants.hpp>
14#include <GeographicLib/Utility.hpp>
15
16namespace GeographicLib {
17
18 /**
19 * \brief Convert between degrees and the %DMS representation
20 *
21 * Parse a string representing degree, minutes, and seconds and return the
22 * angle in degrees and format an angle in degrees as degree, minutes, and
23 * seconds. In addition, handle NANs and infinities on input and output.
24 *
25 * Example of use:
26 * \include example-DMS.cpp
27 **********************************************************************/
28 class GEOGRAPHICLIB_EXPORT DMS {
29 public:
30
31 /**
32 * Indicator for presence of hemisphere indicator (N/S/E/W) on latitudes
33 * and longitudes.
34 **********************************************************************/
35 enum flag {
36 /**
37 * No indicator present.
38 * @hideinitializer
39 **********************************************************************/
40 NONE = 0,
41 /**
42 * Latitude indicator (N/S) present.
43 * @hideinitializer
44 **********************************************************************/
45 LATITUDE = 1,
46 /**
47 * Longitude indicator (E/W) present.
48 * @hideinitializer
49 **********************************************************************/
50 LONGITUDE = 2,
51 /**
52 * Used in Encode to indicate output of an azimuth in [000, 360) with no
53 * letter indicator.
54 * @hideinitializer
55 **********************************************************************/
56 AZIMUTH = 3,
57 /**
58 * Used in Encode to indicate output of a plain number.
59 * @hideinitializer
60 **********************************************************************/
61 NUMBER = 4,
62 };
63
64 /**
65 * Indicator for trailing units on an angle.
66 **********************************************************************/
67 enum component {
68 /**
69 * Trailing unit is degrees.
70 * @hideinitializer
71 **********************************************************************/
72 DEGREE = 0,
73 /**
74 * Trailing unit is arc minutes.
75 * @hideinitializer
76 **********************************************************************/
77 MINUTE = 1,
78 /**
79 * Trailing unit is arc seconds.
80 * @hideinitializer
81 **********************************************************************/
82 SECOND = 2,
83 };
84
85 private:
86 typedef Math::real real;
87 // Replace all occurrences of pat by c. If c is NULL remove pat.
88 static void replace(std::string& s, const std::string& pat, char c);
89 static const char* const hemispheres_;
90 static const char* const signs_;
91 static const char* const digits_;
92 static const char* const dmsindicators_;
93 static const char* const components_[3];
94 static Math::real NumMatch(const std::string& s);
95 static Math::real InternalDecode(const std::string& dmsa, flag& ind);
96 DMS() = delete; // Disable constructor
97
98 public:
99
100 /**
101 * Convert a string in DMS to an angle.
102 *
103 * @param[in] dms string input.
104 * @param[out] ind a DMS::flag value signaling the presence of a
105 * hemisphere indicator.
106 * @exception GeographicErr if \e dms is malformed (see below).
107 * @return angle (degrees).
108 *
109 * Degrees, minutes, and seconds are indicated by the characters d, '
110 * (single quote), &quot; (double quote), and these components may only be
111 * given in this order. Any (but not all) components may be omitted and
112 * other symbols (e.g., the &deg; symbol for degrees and the unicode prime
113 * and double prime symbols for minutes and seconds) may be substituted;
114 * two single quotes can be used instead of &quot;. The last component
115 * indicator may be omitted and is assumed to be the next smallest unit
116 * (thus 33d10 is interpreted as 33d10'). The final component may be a
117 * decimal fraction but the non-final components must be integers. Instead
118 * of using d, ', and &quot; to indicate degrees, minutes, and seconds, :
119 * (colon) may be used to <i>separate</i> these components (numbers must
120 * appear before and after each colon); thus 50d30'10.3&quot; may be
121 * written as 50:30:10.3, 5.5' may be written 0:5.5, and so on. The
122 * integer parts of the minutes and seconds components must be less
123 * than 60. A single leading sign is permitted. A hemisphere designator
124 * (N, E, W, S) may be added to the beginning or end of the string. The
125 * result is multiplied by the implied sign of the hemisphere designator
126 * (negative for S and W). In addition \e ind is set to DMS::LATITUDE if N
127 * or S is present, to DMS::LONGITUDE if E or W is present, and to
128 * DMS::NONE otherwise. Leading and trailing whitespace is removed from
129 * the string before processing. This routine throws an error on a
130 * malformed string. No check is performed on the range of the result.
131 * Examples of legal and illegal strings are
132 * - <i>LEGAL</i> (all the entries on each line are equivalent)
133 * - -20.51125, 20d30'40.5&quot;S, -20&deg;30'40.5, -20d30.675,
134 * N-20d30'40.5&quot;, -20:30:40.5
135 * - 4d0'9, 4d9&quot;, 4d9'', 4:0:9, 004:00:09, 4.0025, 4.0025d, 4d0.15,
136 * 04:.15
137 * - 4:59.99999999999999, 4:60.0, 4:59:59.9999999999999, 4:59:60.0, 5
138 * - <i>ILLEGAL</i> (the exception thrown explains the problem)
139 * - 4d5&quot;4', 4::5, 4:5:, :4:5, 4d4.5'4&quot;, -N20.5, 1.8e2d, 4:60,
140 * 4:59:60
141 *
142 * The decoding operation can also perform addition and subtraction
143 * operations. If the string includes <i>internal</i> signs (i.e., not at
144 * the beginning nor immediately after an initial hemisphere designator),
145 * then the string is split immediately before such signs and each piece is
146 * decoded according to the above rules and the results added; thus
147 * <code>S3-2.5+4.1N</code> is parsed as the sum of <code>S3</code>,
148 * <code>-2.5</code>, <code>+4.1N</code>. Any piece can include a
149 * hemisphere designator; however, if multiple designators are given, they
150 * must compatible; e.g., you cannot mix N and E. In addition, the
151 * designator can appear at the beginning or end of the first piece, but
152 * must be at the end of all subsequent pieces (a hemisphere designator is
153 * not allowed after the initial sign). Examples of legal and illegal
154 * combinations are
155 * - <i>LEGAL</i> (these are all equivalent)
156 * - -070:00:45, 70:01:15W+0:0.5, 70:01:15W-0:0:30W, W70:01:15+0:0:30E
157 * - <i>ILLEGAL</i> (the exception thrown explains the problem)
158 * - 70:01:15W+0:0:15N, W70:01:15+W0:0:15
159 *
160 * \warning The "exponential" notation is not recognized. Thus
161 * <code>7.0E1</code> is illegal, while <code>7.0E+1</code> is parsed as
162 * <code>(7.0E) + (+1)</code>, yielding the same result as
163 * <code>8.0E</code>.
164 *
165 * \note At present, all the string handling in the C++ implementation of
166 * %GeographicLib is with 8-bit characters. The support for unicode
167 * symbols for degrees, minutes, and seconds is therefore via the
168 * <a href="https://en.wikipedia.org/wiki/UTF-8">UTF-8</a> encoding. (The
169 * JavaScript implementation of this class uses unicode natively, of
170 * course.)
171 *
172 * Here is the list of Unicode symbols supported for degrees, minutes,
173 * seconds, and the plus and minus signs; various symbols denoting variants
174 * of a space, which may separate the components of a DMS string, are
175 * removed:
176 * - degrees:
177 * - d, D lower and upper case letters
178 * - U+00b0 degree symbol (&deg;)
179 * - U+00ba masculine ordinal indicator (&ordm;)
180 * - U+2070 superscript zero (⁰)
181 * - U+02da ring above (˚)
182 * - U+2218 compose function (∘)
183 * - * the <a href="https://grid.nga.mil">GRiD</a> symbol for degrees
184 * - minutes:
185 * - ' apostrophe
186 * - ` grave accent
187 * - U+2032 prime (&prime;)
188 * - U+2035 back prime (‵)
189 * - U+00b4 acute accent (&acute;)
190 * - U+2018 left single quote (&lsquo;)
191 * - U+2019 right single quote (&rsquo;)
192 * - U+201b reversed-9 single quote (‛)
193 * - U+02b9 modifier letter prime (ʹ)
194 * - U+02ca modifier letter acute accent (ˊ)
195 * - U+02cb modifier letter grave accent (ˋ)
196 * - seconds:
197 * - &quot; quotation mark
198 * - U+2033 double prime (&Prime;)
199 * - U+2036 reversed double prime (‶)
200 * + U+02dd double acute accent (˝)
201 * - U+201c left double quote (&ldquo;)
202 * - U+201d right double quote (&rdquo;)
203 * - U+201f reversed-9 double quote (‟)
204 * - U+02ba modifier letter double prime (ʺ)
205 * - '&thinsp;' any two consecutive symbols for minutes
206 * - plus sign:
207 * - + plus
208 * - U+2795 heavy plus (➕)
209 * - U+2064 invisible plus (|⁤|)
210 * - minus sign:
211 * - - hyphen
212 * - U+2010 dash (‐)
213 * - U+2011 non-breaking hyphen (‑)
214 * - U+2013 en dash (&ndash;)
215 * - U+2014 em dash (&mdash;)
216 * - U+2212 minus sign (&minus;)
217 * - U+2796 heavy minus (➖)
218 * - ignored spaces:
219 * - U+00a0 non-breaking space
220 * - U+2007 figure space (| |)
221 * - U+2009 thin space (|&thinsp;|)
222 * - U+200a hair space ( | |)
223 * - U+200b invisible space (|​|)
224 * - U+202f narrow space ( | |)
225 * - U+2063 invisible separator (|⁣|)
226 * .
227 * The codes with a leading zero byte, e.g., U+00b0, are accepted in their
228 * UTF-8 coded form 0xc2 0xb0 and as a single byte 0xb0.
229 **********************************************************************/
230 static Math::real Decode(const std::string& dms, flag& ind);
231
232 /**
233 * Convert DMS to an angle.
234 *
235 * @param[in] d degrees.
236 * @param[in] m arc minutes.
237 * @param[in] s arc seconds.
238 * @return angle (degrees)
239 *
240 * This does not propagate the sign on \e d to the other components,
241 * so -3d20' would need to be represented as - DMS::Decode(3.0, 20.0) or
242 * DMS::Decode(-3.0, -20.0).
243 **********************************************************************/
244 static Math::real Decode(real d, real m = 0, real s = 0)
245 { return d + (m + s / real(Math::ms)) / real(Math::dm); }
246
247 /**
248 * Convert a pair of strings to latitude and longitude.
249 *
250 * @param[in] dmsa first string.
251 * @param[in] dmsb second string.
252 * @param[out] lat latitude (degrees).
253 * @param[out] lon longitude (degrees).
254 * @param[in] longfirst if true assume longitude is given before latitude
255 * in the absence of hemisphere designators (default false).
256 * @exception GeographicErr if \e dmsa or \e dmsb is malformed.
257 * @exception GeographicErr if \e dmsa and \e dmsb are both interpreted as
258 * latitudes.
259 * @exception GeographicErr if \e dmsa and \e dmsb are both interpreted as
260 * longitudes.
261 * @exception GeographicErr if decoded latitude is not in [&minus;90&deg;,
262 * 90&deg;].
263 *
264 * By default, the \e lat (resp., \e lon) is assigned to the results of
265 * decoding \e dmsa (resp., \e dmsb). However this is overridden if either
266 * \e dmsa or \e dmsb contain a latitude or longitude hemisphere designator
267 * (N, S, E, W). If an exception is thrown, \e lat and \e lon are
268 * unchanged.
269 **********************************************************************/
270 static void DecodeLatLon(const std::string& dmsa, const std::string& dmsb,
271 real& lat, real& lon,
272 bool longfirst = false);
273
274 /**
275 * Convert a string to an angle in degrees.
276 *
277 * @param[in] angstr input string.
278 * @exception GeographicErr if \e angstr is malformed.
279 * @exception GeographicErr if \e angstr includes a hemisphere designator.
280 * @return angle (degrees)
281 *
282 * No hemisphere designator is allowed and no check is done on the range of
283 * the result.
284 **********************************************************************/
285 static Math::real DecodeAngle(const std::string& angstr);
286
287 /**
288 * Convert a string to an azimuth in degrees.
289 *
290 * @param[in] azistr input string.
291 * @exception GeographicErr if \e azistr is malformed.
292 * @exception GeographicErr if \e azistr includes a N/S designator.
293 * @return azimuth (degrees) reduced to the range [&minus;180&deg;,
294 * 180&deg;].
295 *
296 * A hemisphere designator E/W can be used; the result is multiplied by
297 * &minus;1 if W is present.
298 **********************************************************************/
299 static Math::real DecodeAzimuth(const std::string& azistr);
300
301 /**
302 * Convert angle (in degrees) into a DMS string (using d, ', and &quot;).
303 *
304 * @param[in] angle input angle (degrees)
305 * @param[in] trailing DMS::component value indicating the trailing units
306 * of the string (this component is given as a decimal number if
307 * necessary).
308 * @param[in] prec the number of digits after the decimal point for the
309 * trailing component.
310 * @param[in] ind DMS::flag value indicating additional formatting.
311 * @param[in] dmssep if non-null, use as the DMS separator character
312 * (instead of d, ', &quot; delimiters).
313 * @exception std::bad_alloc if memory for the string can't be allocated.
314 * @return formatted string
315 *
316 * The interpretation of \e ind is as follows:
317 * - ind == DMS::NONE, signed result no leading zeros on degrees except in
318 * the units place, e.g., -8d03'.
319 * - ind == DMS::LATITUDE, trailing N or S hemisphere designator, no sign,
320 * pad degrees to 2 digits, e.g., 08d03'S.
321 * - ind == DMS::LONGITUDE, trailing E or W hemisphere designator, no
322 * sign, pad degrees to 3 digits, e.g., 008d03'W.
323 * - ind == DMS::AZIMUTH, convert to the range [0, 360&deg;), no
324 * sign, pad degrees to 3 digits, e.g., 351d57'.
325 * .
326 * The integer parts of the minutes and seconds components are always given
327 * with 2 digits.
328 **********************************************************************/
329 static std::string Encode(real angle, component trailing, unsigned prec,
330 flag ind = NONE, char dmssep = char(0));
331
332 /**
333 * Convert angle into a DMS string (using d, ', and &quot;) selecting the
334 * trailing component based on the precision.
335 *
336 * @param[in] angle input angle (degrees)
337 * @param[in] prec the precision relative to 1 degree.
338 * @param[in] ind DMS::flag value indicated additional formatting.
339 * @param[in] dmssep if non-null, use as the DMS separator character
340 * (instead of d, ', &quot; delimiters).
341 * @exception std::bad_alloc if memory for the string can't be allocated.
342 * @return formatted string
343 *
344 * \e prec indicates the precision relative to 1 degree, e.g., \e prec = 3
345 * gives a result accurate to 0.1' and \e prec = 4 gives a result accurate
346 * to 1&quot;. \e ind is interpreted as in DMS::Encode with the additional
347 * facility that DMS::NUMBER represents \e angle as a number in fixed
348 * format with precision \e prec.
349 **********************************************************************/
350 static std::string Encode(real angle, unsigned prec, flag ind = NONE,
351 char dmssep = char(0)) {
352 return ind == NUMBER ? Utility::str(angle, int(prec)) :
353 Encode(angle,
354 prec < 2 ? DEGREE : (prec < 4 ? MINUTE : SECOND),
355 prec < 2 ? prec : (prec < 4 ? prec - 2 : prec - 4),
356 ind, dmssep);
357 }
358
359 /**
360 * Split angle into degrees and minutes
361 *
362 * @param[in] ang angle (degrees)
363 * @param[out] d degrees (an integer returned as a real)
364 * @param[out] m arc minutes.
365 **********************************************************************/
366 static void Encode(real ang, real& d, real& m) {
367 d = int(ang); m = real(Math::dm) * (ang - d);
368 }
369
370 /**
371 * Split angle into degrees and minutes and seconds.
372 *
373 * @param[in] ang angle (degrees)
374 * @param[out] d degrees (an integer returned as a real)
375 * @param[out] m arc minutes (an integer returned as a real)
376 * @param[out] s arc seconds.
377 **********************************************************************/
378 static void Encode(real ang, real& d, real& m, real& s) {
379 d = int(ang); ang = real(Math::dm) * (ang - d);
380 m = int(ang); s = real(Math::ms) * (ang - m);
381 }
382
383 };
384
385} // namespace GeographicLib
386
387#endif // GEOGRAPHICLIB_DMS_HPP
Constants.hpp
Header for GeographicLib::Constants class.
GEOGRAPHICLIB_EXPORT
#define GEOGRAPHICLIB_EXPORT
Definition Constants.hpp:67
real
GeographicLib::Math::real real
Definition GeodSolve.cpp:28
Utility.hpp
Header for GeographicLib::Utility class.
GeographicLib::DMS
Convert between degrees and the DMS representation.
Definition DMS.hpp:28
GeographicLib::DMS::Encode
static void Encode(real ang, real &d, real &m)
Definition DMS.hpp:366
GeographicLib::DMS::Decode
static Math::real Decode(real d, real m=0, real s=0)
Definition DMS.hpp:244
GeographicLib::DMS::flag
flag
Definition DMS.hpp:35
GeographicLib::DMS::LONGITUDE
@ LONGITUDE
Definition DMS.hpp:50
GeographicLib::DMS::NUMBER
@ NUMBER
Definition DMS.hpp:61
GeographicLib::DMS::LATITUDE
@ LATITUDE
Definition DMS.hpp:45
GeographicLib::DMS::AZIMUTH
@ AZIMUTH
Definition DMS.hpp:56
GeographicLib::DMS::NONE
@ NONE
Definition DMS.hpp:40
GeographicLib::DMS::component
component
Definition DMS.hpp:67
GeographicLib::DMS::SECOND
@ SECOND
Definition DMS.hpp:82
GeographicLib::DMS::DEGREE
@ DEGREE
Definition DMS.hpp:72
GeographicLib::DMS::MINUTE
@ MINUTE
Definition DMS.hpp:77
GeographicLib::DMS::Encode
static std::string Encode(real angle, component trailing, unsigned prec, flag ind=NONE, char dmssep=char(0))
Definition DMS.cpp:417
GeographicLib::DMS::Encode
static std::string Encode(real angle, unsigned prec, flag ind=NONE, char dmssep=char(0))
Definition DMS.hpp:350
GeographicLib::DMS::Encode
static void Encode(real ang, real &d, real &m, real &s)
Definition DMS.hpp:378
GeographicLib::Math::dm
static constexpr int dm
minutes per degree
Definition Math.hpp:151
GeographicLib::Math::real
double real
Definition Math.hpp:115
GeographicLib::Math::ms
static constexpr int ms
seconds per minute
Definition Math.hpp:152
GeographicLib::Utility::str
static std::string str(T x, int p=-1)
Definition Utility.hpp:161
GeographicLib
Namespace for GeographicLib.
Definition Accumulator.cpp:12