GeographicLib  1.51
DMS.cpp
Go to the documentation of this file.
1 /**
2  * \file DMS.cpp
3  * \brief Implementation for GeographicLib::DMS class
4  *
5  * Copyright (c) Charles Karney (2008-2020) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * https://geographiclib.sourceforge.io/
8  **********************************************************************/
9 
10 #include <GeographicLib/DMS.hpp>
12 
13 #if defined(_MSC_VER)
14 // Squelch warnings about constant conditional expressions
15 # pragma warning (disable: 4127)
16 #endif
17 
18 namespace GeographicLib {
19 
20  using namespace std;
21 
22  const char* const DMS::hemispheres_ = "SNWE";
23  const char* const DMS::signs_ = "-+";
24  const char* const DMS::digits_ = "0123456789";
25  const char* const DMS::dmsindicators_ = "D'\":";
26  const char* const DMS::components_[] = {"degrees", "minutes", "seconds"};
27 
28  Math::real DMS::Decode(const std::string& dms, flag& ind) {
29  // Here's a table of the allowed characters
30 
31  // S unicode dec UTF-8 descripton
32 
33  // DEGREE
34  // d U+0064 100 64 d
35  // D U+0044 68 44 D
36  // ° U+00b0 176 c2 b0 degree symbol
37  // º U+00ba 186 c2 ba alt symbol
38  // ⁰ U+2070 8304 e2 81 b0 sup zero
39  // ˚ U+02da 730 cb 9a ring above
40  // ∘ U+2218 8728 e2 88 98 compose function
41  // * U+002a 42 2a GRiD symbol for degrees
42 
43  // MINUTES
44  // ' U+0027 39 27 apostrophe
45  // ` U+0060 96 60 grave accent
46  // ′ U+2032 8242 e2 80 b2 prime
47  // ‵ U+2035 8245 e2 80 b5 back prime
48  // ´ U+00b4 180 c2 b4 acute accent
49  // ‘ U+2018 8216 e2 80 98 left single quote (also ext ASCII 0x91)
50  // ’ U+2019 8217 e2 80 99 right single quote (also ext ASCII 0x92)
51  // ‛ U+201b 8219 e2 80 9b reversed-9 single quote
52  // ʹ U+02b9 697 ca b9 modifier letter prime
53  // ˊ U+02ca 714 cb 8a modifier letter acute accent
54  // ˋ U+02cb 715 cb 8b modifier letter grave accent
55 
56  // SECONDS
57  // " U+0022 34 22 quotation mark
58  // ″ U+2033 8243 e2 80 b3 double prime
59  // ‶ U+2036 8246 e2 80 b6 reversed double prime
60  // ˝ U+02dd 733 cb 9d double acute accent
61  // “ U+201c 8220 e2 80 9c left double quote (also ext ASCII 0x93)
62  // ” U+201d 8221 e2 80 9d right double quote (also ext ASCII 0x94)
63  // ‟ U+201f 8223 e2 80 9f reversed-9 double quote
64  // ʺ U+02ba 698 ca ba modifier letter double prime
65 
66  // PLUS
67  // + U+002b 43 2b plus sign
68  // ➕ U+2795 10133 e2 9e 95 heavy plus
69  // U+2064 8292 e2 81 a4 invisible plus |⁤|
70 
71  // MINUS
72  // - U+002d 45 2d hyphen
73  // ‐ U+2010 8208 e2 80 90 dash
74  // ‑ U+2011 8209 e2 80 91 non-breaking hyphen
75  // – U+2013 8211 e2 80 93 en dash (also ext ASCII 0x96)
76  // — U+2014 8212 e2 80 94 em dash (also ext ASCII 0x97)
77  // − U+2212 8722 e2 88 92 minus sign
78  // ➖ U+2796 10134 e2 9e 96 heavy minus
79 
80  // IGNORED
81  //   U+00a0 160 c2 a0 non-breaking space
82  // U+2007 8199 e2 80 87 figure space | |
83  // U+2009 8201 e2 80 89 thin space | |
84  // U+200a 8202 e2 80 8a hair space | |
85  // U+200b 8203 e2 80 8b invisible space |​|
86  //   U+202f 8239 e2 80 af narrow space | |
87  // U+2063 8291 e2 81 a3 invisible separator |⁣|
88  // « U+00ab 171 c2 ab left guillemot (for cgi-bin)
89  // » U+00bb 187 c2 bb right guillemot (for cgi-bin)
90 
91  string dmsa = dms;
92  replace(dmsa, "\xc2\xb0", 'd' ); // U+00b0 degree symbol
93  replace(dmsa, "\xc2\xba", 'd' ); // U+00ba alt symbol
94  replace(dmsa, "\xe2\x81\xb0", 'd' ); // U+2070 sup zero
95  replace(dmsa, "\xcb\x9a", 'd' ); // U+02da ring above
96  replace(dmsa, "\xe2\x88\x98", 'd' ); // U+2218 compose function
97 
98  replace(dmsa, "\xe2\x80\xb2", '\''); // U+2032 prime
99  replace(dmsa, "\xe2\x80\xb5", '\''); // U+2035 back prime
100  replace(dmsa, "\xc2\xb4", '\''); // U+00b4 acute accent
101  replace(dmsa, "\xe2\x80\x98", '\''); // U+2018 left single quote
102  replace(dmsa, "\xe2\x80\x99", '\''); // U+2019 right single quote
103  replace(dmsa, "\xe2\x80\x9b", '\''); // U+201b reversed-9 single quote
104  replace(dmsa, "\xca\xb9", '\''); // U+02b9 modifier letter prime
105  replace(dmsa, "\xcb\x8a", '\''); // U+02ca modifier letter acute accent
106  replace(dmsa, "\xcb\x8b", '\''); // U+02cb modifier letter grave accent
107 
108  replace(dmsa, "\xe2\x80\xb3", '"' ); // U+2033 double prime
109  replace(dmsa, "\xe2\x80\xb6", '"' ); // U+2036 reversed double prime
110  replace(dmsa, "\xcb\x9d", '"' ); // U+02dd double acute accent
111  replace(dmsa, "\xe2\x80\x9c", '"' ); // U+201c left double quote
112  replace(dmsa, "\xe2\x80\x9d", '"' ); // U+201d right double quote
113  replace(dmsa, "\xe2\x80\x9f", '"' ); // U+201f reversed-9 double quote
114  replace(dmsa, "\xca\xba", '"' ); // U+02ba modifier letter double prime
115 
116  replace(dmsa, "\xe2\x9e\x95", '+' ); // U+2795 heavy plus
117  replace(dmsa, "\xe2\x81\xa4", '+' ); // U+2064 invisible plus
118 
119  replace(dmsa, "\xe2\x80\x90", '-' ); // U+2010 dash
120  replace(dmsa, "\xe2\x80\x91", '-' ); // U+2011 non-breaking hyphen
121  replace(dmsa, "\xe2\x80\x93", '-' ); // U+2013 en dash
122  replace(dmsa, "\xe2\x80\x94", '-' ); // U+2014 em dash
123  replace(dmsa, "\xe2\x88\x92", '-' ); // U+2212 minus sign
124  replace(dmsa, "\xe2\x9e\x96", '-' ); // U+2796 heavy minus
125 
126  replace(dmsa, "\xc2\xa0", '\0'); // U+00a0 non-breaking space
127  replace(dmsa, "\xe2\x80\x87", '\0'); // U+2007 figure space
128  replace(dmsa, "\xe2\x80\x89", '\0'); // U+2007 thin space
129  replace(dmsa, "\xe2\x80\x8a", '\0'); // U+200a hair space
130  replace(dmsa, "\xe2\x80\x8b", '\0'); // U+200b invisible space
131  replace(dmsa, "\xe2\x80\xaf", '\0'); // U+202f narrow space
132  replace(dmsa, "\xe2\x81\xa3", '\0'); // U+2063 invisible separator
133 
134  replace(dmsa, "\xb0", 'd' ); // 0xb0 bare degree symbol
135  replace(dmsa, "\xba", 'd' ); // 0xba bare alt symbol
136  replace(dmsa, "*", 'd' ); // GRiD symbol for degree
137  replace(dmsa, "`", '\''); // grave accent
138  replace(dmsa, "\xb4", '\''); // 0xb4 bare acute accent
139  // Don't implement these alternatives; they are only relevant for cgi-bin
140  // replace(dmsa, "\x91", '\''); // 0x91 ext ASCII left single quote
141  // replace(dmsa, "\x92", '\''); // 0x92 ext ASCII right single quote
142  // replace(dmsa, "\x93", '"' ); // 0x93 ext ASCII left double quote
143  // replace(dmsa, "\x94", '"' ); // 0x94 ext ASCII right double quote
144  // replace(dmsa, "\x96", '-' ); // 0x96 ext ASCII en dash
145  // replace(dmsa, "\x97", '-' ); // 0x97 ext ASCII em dash
146  replace(dmsa, "\xa0", '\0'); // 0xa0 bare non-breaking space
147  replace(dmsa, "''", '"' ); // '' -> "
148  string::size_type
149  beg = 0,
150  end = unsigned(dmsa.size());
151  while (beg < end && isspace(dmsa[beg]))
152  ++beg;
153  while (beg < end && isspace(dmsa[end - 1]))
154  --end;
155  // The trimmed string in [beg, end)
156  real v = 0;
157  int i = 0;
158  flag ind1 = NONE;
159  // p is pointer to the next piece that needs decoding
160  for (string::size_type p = beg, pb; p < end; p = pb, ++i) {
161  string::size_type pa = p;
162  // Skip over initial hemisphere letter (for i == 0)
163  if (i == 0 && Utility::lookup(hemispheres_, dmsa[pa]) >= 0)
164  ++pa;
165  // Skip over initial sign (checking for it if i == 0)
166  if (i > 0 || (pa < end && Utility::lookup(signs_, dmsa[pa]) >= 0))
167  ++pa;
168  // Find next sign
169  pb = min(dmsa.find_first_of(signs_, pa), end);
170  flag ind2 = NONE;
171  v += InternalDecode(dmsa.substr(p, pb - p), ind2);
172  if (ind1 == NONE)
173  ind1 = ind2;
174  else if (!(ind2 == NONE || ind1 == ind2))
175  throw GeographicErr("Incompatible hemisphere specifier in " +
176  dmsa.substr(beg, pb - beg));
177  }
178  if (i == 0)
179  throw GeographicErr("Empty or incomplete DMS string " +
180  dmsa.substr(beg, end - beg));
181  ind = ind1;
182  return v;
183  }
184 
185  Math::real DMS::InternalDecode(const string& dmsa, flag& ind) {
186  const int maxcomponents = 3;
187  string errormsg;
188  do { // Executed once (provides the ability to break)
189  int sign = 1;
190  unsigned
191  beg = 0,
192  end = unsigned(dmsa.size());
193  flag ind1 = NONE;
194  int k = -1;
195  if (end > beg && (k = Utility::lookup(hemispheres_, dmsa[beg])) >= 0) {
196  ind1 = (k / 2) ? LONGITUDE : LATITUDE;
197  sign = k % 2 ? 1 : -1;
198  ++beg;
199  }
200  if (end > beg && (k = Utility::lookup(hemispheres_, dmsa[end-1])) >= 0) {
201  if (k >= 0) {
202  if (ind1 != NONE) {
203  if (toupper(dmsa[beg - 1]) == toupper(dmsa[end - 1]))
204  errormsg = "Repeated hemisphere indicators "
205  + Utility::str(dmsa[beg - 1])
206  + " in " + dmsa.substr(beg - 1, end - beg + 1);
207  else
208  errormsg = "Contradictory hemisphere indicators "
209  + Utility::str(dmsa[beg - 1]) + " and "
210  + Utility::str(dmsa[end - 1]) + " in "
211  + dmsa.substr(beg - 1, end - beg + 1);
212  break;
213  }
214  ind1 = (k / 2) ? LONGITUDE : LATITUDE;
215  sign = k % 2 ? 1 : -1;
216  --end;
217  }
218  }
219  if (end > beg && (k = Utility::lookup(signs_, dmsa[beg])) >= 0) {
220  if (k >= 0) {
221  sign *= k ? 1 : -1;
222  ++beg;
223  }
224  }
225  if (end == beg) {
226  errormsg = "Empty or incomplete DMS string " + dmsa;
227  break;
228  }
229  real ipieces[maxcomponents] = {0, 0, 0};
230  real fpieces[maxcomponents] = {0, 0, 0};
231  unsigned npiece = 0;
232  real icurrent = 0;
233  real fcurrent = 0;
234  unsigned ncurrent = 0, p = beg;
235  bool pointseen = false;
236  unsigned digcount = 0, intcount = 0;
237  while (p < end) {
238  char x = dmsa[p++];
239  if ((k = Utility::lookup(digits_, x)) >= 0) {
240  ++ncurrent;
241  if (digcount > 0)
242  ++digcount; // Count of decimal digits
243  else {
244  icurrent = 10 * icurrent + k;
245  ++intcount;
246  }
247  } else if (x == '.') {
248  if (pointseen) {
249  errormsg = "Multiple decimal points in "
250  + dmsa.substr(beg, end - beg);
251  break;
252  }
253  pointseen = true;
254  digcount = 1;
255  } else if ((k = Utility::lookup(dmsindicators_, x)) >= 0) {
256  if (k >= maxcomponents) {
257  if (p == end) {
258  errormsg = "Illegal for : to appear at the end of " +
259  dmsa.substr(beg, end - beg);
260  break;
261  }
262  k = npiece;
263  }
264  if (unsigned(k) == npiece - 1) {
265  errormsg = "Repeated " + string(components_[k]) +
266  " component in " + dmsa.substr(beg, end - beg);
267  break;
268  } else if (unsigned(k) < npiece) {
269  errormsg = string(components_[k]) + " component follows "
270  + string(components_[npiece - 1]) + " component in "
271  + dmsa.substr(beg, end - beg);
272  break;
273  }
274  if (ncurrent == 0) {
275  errormsg = "Missing numbers in " + string(components_[k]) +
276  " component of " + dmsa.substr(beg, end - beg);
277  break;
278  }
279  if (digcount > 0) {
280  istringstream s(dmsa.substr(p - intcount - digcount - 1,
281  intcount + digcount));
282  s >> fcurrent;
283  icurrent = 0;
284  }
285  ipieces[k] = icurrent;
286  fpieces[k] = icurrent + fcurrent;
287  if (p < end) {
288  npiece = k + 1;
289  if (npiece >= maxcomponents) {
290  errormsg = "More than 3 DMS components in "
291  + dmsa.substr(beg, end - beg);
292  break;
293  }
294  icurrent = fcurrent = 0;
295  ncurrent = digcount = intcount = 0;
296  }
297  } else if (Utility::lookup(signs_, x) >= 0) {
298  errormsg = "Internal sign in DMS string "
299  + dmsa.substr(beg, end - beg);
300  break;
301  } else {
302  errormsg = "Illegal character " + Utility::str(x) + " in DMS string "
303  + dmsa.substr(beg, end - beg);
304  break;
305  }
306  }
307  if (!errormsg.empty())
308  break;
309  if (Utility::lookup(dmsindicators_, dmsa[p - 1]) < 0) {
310  if (npiece >= maxcomponents) {
311  errormsg = "Extra text following seconds in DMS string "
312  + dmsa.substr(beg, end - beg);
313  break;
314  }
315  if (ncurrent == 0) {
316  errormsg = "Missing numbers in trailing component of "
317  + dmsa.substr(beg, end - beg);
318  break;
319  }
320  if (digcount > 0) {
321  istringstream s(dmsa.substr(p - intcount - digcount,
322  intcount + digcount));
323  s >> fcurrent;
324  icurrent = 0;
325  }
326  ipieces[npiece] = icurrent;
327  fpieces[npiece] = icurrent + fcurrent;
328  }
329  if (pointseen && digcount == 0) {
330  errormsg = "Decimal point in non-terminal component of "
331  + dmsa.substr(beg, end - beg);
332  break;
333  }
334  // Note that we accept 59.999999... even though it rounds to 60.
335  if (ipieces[1] >= 60 || fpieces[1] > 60 ) {
336  errormsg = "Minutes " + Utility::str(fpieces[1])
337  + " not in range [0, 60)";
338  break;
339  }
340  if (ipieces[2] >= 60 || fpieces[2] > 60) {
341  errormsg = "Seconds " + Utility::str(fpieces[2])
342  + " not in range [0, 60)";
343  break;
344  }
345  ind = ind1;
346  // Assume check on range of result is made by calling routine (which
347  // might be able to offer a better diagnostic).
348  return real(sign) *
349  ( fpieces[2] != 0 ?
350  (60*(60*fpieces[0] + fpieces[1]) + fpieces[2]) / 3600 :
351  ( fpieces[1] != 0 ?
352  (60*fpieces[0] + fpieces[1]) / 60 : fpieces[0] ) );
353  } while (false);
354  real val = Utility::nummatch<real>(dmsa);
355  if (val == 0)
356  throw GeographicErr(errormsg);
357  else
358  ind = NONE;
359  return val;
360  }
361 
362  void DMS::DecodeLatLon(const string& stra, const string& strb,
363  real& lat, real& lon,
364  bool longfirst) {
365  real a, b;
366  flag ia, ib;
367  a = Decode(stra, ia);
368  b = Decode(strb, ib);
369  if (ia == NONE && ib == NONE) {
370  // Default to lat, long unless longfirst
371  ia = longfirst ? LONGITUDE : LATITUDE;
372  ib = longfirst ? LATITUDE : LONGITUDE;
373  } else if (ia == NONE)
374  ia = flag(LATITUDE + LONGITUDE - ib);
375  else if (ib == NONE)
376  ib = flag(LATITUDE + LONGITUDE - ia);
377  if (ia == ib)
378  throw GeographicErr("Both " + stra + " and "
379  + strb + " interpreted as "
380  + (ia == LATITUDE ? "latitudes" : "longitudes"));
381  real
382  lat1 = ia == LATITUDE ? a : b,
383  lon1 = ia == LATITUDE ? b : a;
384  if (abs(lat1) > 90)
385  throw GeographicErr("Latitude " + Utility::str(lat1)
386  + "d not in [-90d, 90d]");
387  lat = lat1;
388  lon = lon1;
389  }
390 
391  Math::real DMS::DecodeAngle(const string& angstr) {
392  flag ind;
393  real ang = Decode(angstr, ind);
394  if (ind != NONE)
395  throw GeographicErr("Arc angle " + angstr
396  + " includes a hemisphere, N/E/W/S");
397  return ang;
398  }
399 
400  Math::real DMS::DecodeAzimuth(const string& azistr) {
401  flag ind;
402  real azi = Decode(azistr, ind);
403  if (ind == LATITUDE)
404  throw GeographicErr("Azimuth " + azistr
405  + " has a latitude hemisphere, N/S");
406  return Math::AngNormalize(azi);
407  }
408 
409  string DMS::Encode(real angle, component trailing, unsigned prec, flag ind,
410  char dmssep) {
411  // Assume check on range of input angle has been made by calling
412  // routine (which might be able to offer a better diagnostic).
413  if (!isfinite(angle))
414  return angle < 0 ? string("-inf") :
415  (angle > 0 ? string("inf") : string("nan"));
416 
417  // 15 - 2 * trailing = ceiling(log10(2^53/90/60^trailing)).
418  // This suffices to give full real precision for numbers in [-90,90]
419  prec = min(15 + Math::extra_digits() - 2 * unsigned(trailing), prec);
420  real scale = 1;
421  for (unsigned i = 0; i < unsigned(trailing); ++i)
422  scale *= 60;
423  for (unsigned i = 0; i < prec; ++i)
424  scale *= 10;
425  if (ind == AZIMUTH)
426  angle -= floor(angle/360) * 360;
427  int sign = angle < 0 ? -1 : 1;
428  angle *= sign;
429 
430  // Break off integer part to preserve precision in manipulation of
431  // fractional part.
432  real
433  idegree = floor(angle),
434  fdegree = (angle - idegree) * scale + real(0.5);
435  {
436  // Implement the "round ties to even" rule
437  real f = floor(fdegree);
438  fdegree = (f == fdegree && fmod(f, real(2)) == 1) ? f - 1 : f;
439  }
440  fdegree /= scale;
441  if (fdegree >= 1) {
442  idegree += 1;
443  fdegree -= 1;
444  }
445  real pieces[3] = {fdegree, 0, 0};
446  for (unsigned i = 1; i <= unsigned(trailing); ++i) {
447  real
448  ip = floor(pieces[i - 1]),
449  fp = pieces[i - 1] - ip;
450  pieces[i] = fp * 60;
451  pieces[i - 1] = ip;
452  }
453  pieces[0] += idegree;
454  ostringstream s;
455  s << fixed << setfill('0');
456  if (ind == NONE && sign < 0)
457  s << '-';
458  switch (trailing) {
459  case DEGREE:
460  if (ind != NONE)
461  s << setw(1 + min(int(ind), 2) + prec + (prec ? 1 : 0));
462  s << Utility::str(pieces[0], prec);
463  // Don't include degree designator (d) if it is the trailing component.
464  break;
465  default:
466  if (ind != NONE)
467  s << setw(1 + min(int(ind), 2));
468  s << int(pieces[0])
469  << (dmssep ? dmssep : char(tolower(dmsindicators_[0])));
470  switch (trailing) {
471  case MINUTE:
472  s << setw(2 + prec + (prec ? 1 : 0)) << Utility::str(pieces[1], prec);
473  if (!dmssep)
474  s << char(tolower(dmsindicators_[1]));
475  break;
476  case SECOND:
477  s << setw(2)
478  << int(pieces[1])
479  << (dmssep ? dmssep : char(tolower(dmsindicators_[1])))
480  << setw(2 + prec + (prec ? 1 : 0)) << Utility::str(pieces[2], prec);
481  if (!dmssep)
482  s << char(tolower(dmsindicators_[2]));
483  break;
484  default:
485  break;
486  }
487  }
488  if (ind != NONE && ind != AZIMUTH)
489  s << hemispheres_[(ind == LATITUDE ? 0 : 2) + (sign < 0 ? 0 : 1)];
490  return s.str();
491  }
492 
493 } // namespace GeographicLib
Header for GeographicLib::DMS class.
GeographicLib::Math::real real
Definition: GeodSolve.cpp:31
Header for GeographicLib::Utility class.
static Math::real DecodeAzimuth(const std::string &azistr)
Definition: DMS.cpp:400
static Math::real DecodeAngle(const std::string &angstr)
Definition: DMS.cpp:391
static std::string Encode(real angle, component trailing, unsigned prec, flag ind=NONE, char dmssep=char(0))
Definition: DMS.cpp:409
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool longfirst=false)
Definition: DMS.cpp:362
static Math::real Decode(const std::string &dms, flag &ind)
Definition: DMS.cpp:28
Exception handling for GeographicLib.
Definition: Constants.hpp:315
static T AngNormalize(T x)
Definition: Math.hpp:405
static int extra_digits()
Definition: Math.cpp:51
static int lookup(const std::string &s, char c)
Definition: Utility.hpp:461
static std::string str(T x, int p=-1)
Definition: Utility.hpp:276
Namespace for GeographicLib.
Definition: Accumulator.cpp:12