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