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