GeographicLib  1.46
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-2015) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * http://geographiclib.sourceforge.net/
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 string DMS::hemispheres_ = "SNWE";
23  const string DMS::signs_ = "-+";
24  const string DMS::digits_ = "0123456789";
25  const string DMS::dmsindicators_ = "D'\":";
26  const string DMS::components_[] = {"degrees", "minutes", "seconds"};
27 
28  Math::real DMS::Decode(const std::string& dms, flag& ind) {
29  string dmsa = dms;
30  replace(dmsa, "\xc2\xb0", 'd'); // U+00b0 degree symbol
31  replace(dmsa, "\xc2\xba", 'd'); // U+00ba alt symbol
32  replace(dmsa, "\xe2\x81\xb0", 'd'); // U+2070 sup zero
33  replace(dmsa, "\xcb\x9a", 'd'); // U+02da ring above
34  replace(dmsa, "\xe2\x80\xb2", '\''); // U+2032 prime
35  replace(dmsa, "\xc2\xb4", '\''); // U+00b4 acute accent
36  replace(dmsa, "\xe2\x80\x99", '\''); // U+2019 right single quote
37  replace(dmsa, "\xe2\x80\xb3", '"'); // U+2033 double prime
38  replace(dmsa, "\xe2\x80\x9d", '"'); // U+201d right double quote
39  replace(dmsa, "\xe2\x88\x92", '-'); // U+2212 minus sign
40  replace(dmsa, "\xb0", 'd'); // 0xb0 bare degree symbol
41  replace(dmsa, "\xba", 'd'); // 0xba bare alt symbol
42  replace(dmsa, "\xb4", '\''); // 0xb4 bare acute accent
43  replace(dmsa, "''", '"'); // '' -> "
44  string::size_type
45  beg = 0,
46  end = unsigned(dmsa.size());
47  while (beg < end && isspace(dmsa[beg]))
48  ++beg;
49  while (beg < end && isspace(dmsa[end - 1]))
50  --end;
51  // The trimmed string in [beg, end)
52  real v = 0;
53  int i = 0;
54  flag ind1 = NONE;
55  // p is pointer to the next piece that needs decoding
56  for (string::size_type p = beg, pb; p < end; p = pb, ++i) {
57  string::size_type pa = p;
58  // Skip over initial hemisphere letter (for i == 0)
59  if (i == 0 && Utility::lookup(hemispheres_, dmsa[pa]) >= 0)
60  ++pa;
61  // Skip over initial sign (checking for it if i == 0)
62  if (i > 0 || (pa < end && Utility::lookup(signs_, dmsa[pa]) >= 0))
63  ++pa;
64  // Find next sign
65  pb = min(dmsa.find_first_of(signs_, pa), end);
66  flag ind2 = NONE;
67  v += InternalDecode(dmsa.substr(p, pb - p), ind2);
68  if (ind1 == NONE)
69  ind1 = ind2;
70  else if (!(ind2 == NONE || ind1 == ind2))
71  throw GeographicErr("Incompatible hemisphere specifies in " +
72  dmsa.substr(beg, pb - beg));
73  }
74  if (i == 0)
75  throw GeographicErr("Empty or incomplete DMS string " +
76  dmsa.substr(beg, end - beg));
77  ind = ind1;
78  return v;
79  }
80 
81  Math::real DMS::InternalDecode(const std::string& dmsa, flag& ind) {
82  string errormsg;
83  do { // Executed once (provides the ability to break)
84  int sign = 1;
85  unsigned
86  beg = 0,
87  end = unsigned(dmsa.size());
88  flag ind1 = NONE;
89  int k = -1;
90  if (end > beg && (k = Utility::lookup(hemispheres_, dmsa[beg])) >= 0) {
91  ind1 = (k / 2) ? LONGITUDE : LATITUDE;
92  sign = k % 2 ? 1 : -1;
93  ++beg;
94  }
95  if (end > beg && (k = Utility::lookup(hemispheres_, dmsa[end-1])) >= 0) {
96  if (k >= 0) {
97  if (ind1 != NONE) {
98  if (toupper(dmsa[beg - 1]) == toupper(dmsa[end - 1]))
99  errormsg = "Repeated hemisphere indicators "
100  + Utility::str(dmsa[beg - 1])
101  + " in " + dmsa.substr(beg - 1, end - beg + 1);
102  else
103  errormsg = "Contradictory hemisphere indicators "
104  + Utility::str(dmsa[beg - 1]) + " and "
105  + Utility::str(dmsa[end - 1]) + " in "
106  + dmsa.substr(beg - 1, end - beg + 1);
107  break;
108  }
109  ind1 = (k / 2) ? LONGITUDE : LATITUDE;
110  sign = k % 2 ? 1 : -1;
111  --end;
112  }
113  }
114  if (end > beg && (k = Utility::lookup(signs_, dmsa[beg])) >= 0) {
115  if (k >= 0) {
116  sign *= k ? 1 : -1;
117  ++beg;
118  }
119  }
120  if (end == beg) {
121  errormsg = "Empty or incomplete DMS string " + dmsa;
122  break;
123  }
124  real ipieces[] = {0, 0, 0};
125  real fpieces[] = {0, 0, 0};
126  unsigned npiece = 0;
127  real icurrent = 0;
128  real fcurrent = 0;
129  unsigned ncurrent = 0, p = beg;
130  bool pointseen = false;
131  unsigned digcount = 0, intcount = 0;
132  while (p < end) {
133  char x = dmsa[p++];
134  if ((k = Utility::lookup(digits_, x)) >= 0) {
135  ++ncurrent;
136  if (digcount > 0)
137  ++digcount; // Count of decimal digits
138  else {
139  icurrent = 10 * icurrent + k;
140  ++intcount;
141  }
142  } else if (x == '.') {
143  if (pointseen) {
144  errormsg = "Multiple decimal points in "
145  + dmsa.substr(beg, end - beg);
146  break;
147  }
148  pointseen = true;
149  digcount = 1;
150  } else if ((k = Utility::lookup(dmsindicators_, x)) >= 0) {
151  if (k >= 3) {
152  if (p == end) {
153  errormsg = "Illegal for : to appear at the end of " +
154  dmsa.substr(beg, end - beg);
155  break;
156  }
157  k = npiece;
158  }
159  if (unsigned(k) == npiece - 1) {
160  errormsg = "Repeated " + components_[k] +
161  " component in " + dmsa.substr(beg, end - beg);
162  break;
163  } else if (unsigned(k) < npiece) {
164  errormsg = components_[k] + " component follows "
165  + components_[npiece - 1] + " component in "
166  + dmsa.substr(beg, end - beg);
167  break;
168  }
169  if (ncurrent == 0) {
170  errormsg = "Missing numbers in " + components_[k] +
171  " component of " + dmsa.substr(beg, end - beg);
172  break;
173  }
174  if (digcount > 0) {
175  istringstream s(dmsa.substr(p - intcount - digcount - 1,
176  intcount + digcount));
177  s >> fcurrent;
178  icurrent = 0;
179  }
180  ipieces[k] = icurrent;
181  fpieces[k] = icurrent + fcurrent;
182  if (p < end) {
183  npiece = k + 1;
184  icurrent = fcurrent = 0;
185  ncurrent = digcount = intcount = 0;
186  }
187  } else if (Utility::lookup(signs_, x) >= 0) {
188  errormsg = "Internal sign in DMS string "
189  + dmsa.substr(beg, end - beg);
190  break;
191  } else {
192  errormsg = "Illegal character " + Utility::str(x) + " in DMS string "
193  + dmsa.substr(beg, end - beg);
194  break;
195  }
196  }
197  if (!errormsg.empty())
198  break;
199  if (Utility::lookup(dmsindicators_, dmsa[p - 1]) < 0) {
200  if (npiece >= 3) {
201  errormsg = "Extra text following seconds in DMS string "
202  + dmsa.substr(beg, end - beg);
203  break;
204  }
205  if (ncurrent == 0) {
206  errormsg = "Missing numbers in trailing component of "
207  + dmsa.substr(beg, end - beg);
208  break;
209  }
210  if (digcount > 0) {
211  istringstream s(dmsa.substr(p - intcount - digcount,
212  intcount + digcount));
213  s >> fcurrent;
214  icurrent = 0;
215  }
216  ipieces[npiece] = icurrent;
217  fpieces[npiece] = icurrent + fcurrent;
218  }
219  if (pointseen && digcount == 0) {
220  errormsg = "Decimal point in non-terminal component of "
221  + dmsa.substr(beg, end - beg);
222  break;
223  }
224  // Note that we accept 59.999999... even though it rounds to 60.
225  if (ipieces[1] >= 60 || fpieces[1] > 60 ) {
226  errormsg = "Minutes " + Utility::str(fpieces[1])
227  + " not in range [0, 60)";
228  break;
229  }
230  if (ipieces[2] >= 60 || fpieces[2] > 60) {
231  errormsg = "Seconds " + Utility::str(fpieces[2])
232  + " not in range [0, 60)";
233  break;
234  }
235  ind = ind1;
236  // Assume check on range of result is made by calling routine (which
237  // might be able to offer a better diagnostic).
238  return real(sign) *
239  ( fpieces[2] ? (60*(60*fpieces[0] + fpieces[1]) + fpieces[2]) / 3600 :
240  ( fpieces[1] ? (60*fpieces[0] + fpieces[1]) / 60 : fpieces[0] ) );
241  } while (false);
242  real val = Utility::nummatch<real>(dmsa);
243  if (val == 0)
244  throw GeographicErr(errormsg);
245  else
246  ind = NONE;
247  return val;
248  }
249 
250  void DMS::DecodeLatLon(const std::string& stra, const std::string& strb,
251  real& lat, real& lon,
252  bool longfirst) {
253  real a, b;
254  flag ia, ib;
255  a = Decode(stra, ia);
256  b = Decode(strb, ib);
257  if (ia == NONE && ib == NONE) {
258  // Default to lat, long unless longfirst
259  ia = longfirst ? LONGITUDE : LATITUDE;
260  ib = longfirst ? LATITUDE : LONGITUDE;
261  } else if (ia == NONE)
262  ia = flag(LATITUDE + LONGITUDE - ib);
263  else if (ib == NONE)
264  ib = flag(LATITUDE + LONGITUDE - ia);
265  if (ia == ib)
266  throw GeographicErr("Both " + stra + " and "
267  + strb + " interpreted as "
268  + (ia == LATITUDE ? "latitudes" : "longitudes"));
269  real
270  lat1 = ia == LATITUDE ? a : b,
271  lon1 = ia == LATITUDE ? b : a;
272  if (abs(lat1) > 90)
273  throw GeographicErr("Latitude " + Utility::str(lat1)
274  + "d not in [-90d, 90d]");
275  lat = lat1;
276  lon = lon1;
277  }
278 
279  Math::real DMS::DecodeAngle(const std::string& angstr) {
280  flag ind;
281  real ang = Decode(angstr, ind);
282  if (ind != NONE)
283  throw GeographicErr("Arc angle " + angstr
284  + " includes a hemisphere, N/E/W/S");
285  return ang;
286  }
287 
288  Math::real DMS::DecodeAzimuth(const std::string& azistr) {
289  flag ind;
290  real azi = Decode(azistr, ind);
291  if (ind == LATITUDE)
292  throw GeographicErr("Azimuth " + azistr
293  + " has a latitude hemisphere, N/S");
294  return Math::AngNormalize(azi);
295  }
296 
297  string DMS::Encode(real angle, component trailing, unsigned prec, flag ind,
298  char dmssep) {
299  // Assume check on range of input angle has been made by calling
300  // routine (which might be able to offer a better diagnostic).
301  if (!Math::isfinite(angle))
302  return angle < 0 ? string("-inf") :
303  (angle > 0 ? string("inf") : string("nan"));
304 
305  // 15 - 2 * trailing = ceiling(log10(2^53/90/60^trailing)).
306  // This suffices to give full real precision for numbers in [-90,90]
307  prec = min(15 + Math::extra_digits() - 2 * unsigned(trailing), prec);
308  real scale = 1;
309  for (unsigned i = 0; i < unsigned(trailing); ++i)
310  scale *= 60;
311  for (unsigned i = 0; i < prec; ++i)
312  scale *= 10;
313  if (ind == AZIMUTH)
314  angle -= floor(angle/360) * 360;
315  int sign = angle < 0 ? -1 : 1;
316  angle *= sign;
317 
318  // Break off integer part to preserve precision in manipulation of
319  // fractional part.
320  real
321  idegree = floor(angle),
322  fdegree = (angle - idegree) * scale + real(0.5);
323  {
324  // Implement the "round ties to even" rule
325  real f = floor(fdegree);
326  fdegree = (f == fdegree && fmod(f, real(2)) == 1) ? f - 1 : f;
327  }
328  fdegree /= scale;
329  if (fdegree >= 1) {
330  idegree += 1;
331  fdegree -= 1;
332  }
333  real pieces[3] = {fdegree, 0, 0};
334  for (unsigned i = 1; i <= unsigned(trailing); ++i) {
335  real
336  ip = floor(pieces[i - 1]),
337  fp = pieces[i - 1] - ip;
338  pieces[i] = fp * 60;
339  pieces[i - 1] = ip;
340  }
341  pieces[0] += idegree;
342  ostringstream s;
343  s << fixed << setfill('0');
344  if (ind == NONE && sign < 0)
345  s << '-';
346  switch (trailing) {
347  case DEGREE:
348  if (ind != NONE)
349  s << setw(1 + min(int(ind), 2) + prec + (prec ? 1 : 0));
350  s << Utility::str(pieces[0], prec);
351  // Don't include degree designator (d) if it is the trailing component.
352  break;
353  default:
354  if (ind != NONE)
355  s << setw(1 + min(int(ind), 2));
356  s << int(pieces[0])
357  << (dmssep ? dmssep : char(tolower(dmsindicators_[0])));
358  switch (trailing) {
359  case MINUTE:
360  s << setw(2 + prec + (prec ? 1 : 0)) << Utility::str(pieces[1], prec);
361  if (!dmssep)
362  s << char(tolower(dmsindicators_[1]));
363  break;
364  case SECOND:
365  s << setw(2)
366  << int(pieces[1])
367  << (dmssep ? dmssep : char(tolower(dmsindicators_[1])))
368  << setw(2 + prec + (prec ? 1 : 0)) << Utility::str(pieces[2], prec);
369  if (!dmssep)
370  s << char(tolower(dmsindicators_[2]));
371  break;
372  default:
373  break;
374  }
375  }
376  if (ind != NONE && ind != AZIMUTH)
377  s << hemispheres_[(ind == LATITUDE ? 0 : 2) + (sign < 0 ? 0 : 1)];
378  return s.str();
379  }
380 
381 } // namespace GeographicLib
static T AngNormalize(T x)
Definition: Math.hpp:437
static Math::real DecodeAngle(const std::string &angstr)
Definition: DMS.cpp:279
GeographicLib::Math::real real
Definition: GeodSolve.cpp:31
Header for GeographicLib::Utility class.
static bool isfinite(T x)
Definition: Math.hpp:781
static int extra_digits()
Definition: Math.hpp:187
static std::string Encode(real angle, component trailing, unsigned prec, flag ind=NONE, char dmssep=char(0))
Definition: DMS.cpp:297
static Math::real DecodeAzimuth(const std::string &azistr)
Definition: DMS.cpp:288
static Math::real Decode(const std::string &dms, flag &ind)
Definition: DMS.cpp:28
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static std::string str(T x, int p=-1)
Definition: Utility.hpp:276
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool longfirst=false)
Definition: DMS.cpp:250
Exception handling for GeographicLib.
Definition: Constants.hpp:373
static int lookup(const std::string &s, char c)
Definition: Utility.hpp:424
Header for GeographicLib::DMS class.