15# pragma warning (disable: 4127 5055)
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"};
29 void DMS::replace(std::string& s,
const std::string& pat,
char c) {
30 string::size_type p = 0;
31 int count = c ? 1 : 0;
34 if (p == string::npos)
36 s.replace(p, pat.length(), count, c);
104 replace(dmsa,
"\xc2\xb0",
'd' );
105 replace(dmsa,
"\xc2\xba",
'd' );
106 replace(dmsa,
"\xe2\x81\xb0",
'd' );
107 replace(dmsa,
"\xcb\x9a",
'd' );
108 replace(dmsa,
"\xe2\x88\x98",
'd' );
110 replace(dmsa,
"\xe2\x80\xb2",
'\'');
111 replace(dmsa,
"\xe2\x80\xb5",
'\'');
112 replace(dmsa,
"\xc2\xb4",
'\'');
113 replace(dmsa,
"\xe2\x80\x98",
'\'');
114 replace(dmsa,
"\xe2\x80\x99",
'\'');
115 replace(dmsa,
"\xe2\x80\x9b",
'\'');
116 replace(dmsa,
"\xca\xb9",
'\'');
117 replace(dmsa,
"\xcb\x8a",
'\'');
118 replace(dmsa,
"\xcb\x8b",
'\'');
120 replace(dmsa,
"\xe2\x80\xb3",
'"' );
121 replace(dmsa,
"\xe2\x80\xb6",
'"' );
122 replace(dmsa,
"\xcb\x9d",
'"' );
123 replace(dmsa,
"\xe2\x80\x9c",
'"' );
124 replace(dmsa,
"\xe2\x80\x9d",
'"' );
125 replace(dmsa,
"\xe2\x80\x9f",
'"' );
126 replace(dmsa,
"\xca\xba",
'"' );
128 replace(dmsa,
"\xe2\x9e\x95",
'+' );
129 replace(dmsa,
"\xe2\x81\xa4",
'+' );
131 replace(dmsa,
"\xe2\x80\x90",
'-' );
132 replace(dmsa,
"\xe2\x80\x91",
'-' );
133 replace(dmsa,
"\xe2\x80\x93",
'-' );
134 replace(dmsa,
"\xe2\x80\x94",
'-' );
135 replace(dmsa,
"\xe2\x88\x92",
'-' );
136 replace(dmsa,
"\xe2\x9e\x96",
'-' );
138 replace(dmsa,
"\xc2\xa0",
'\0');
139 replace(dmsa,
"\xe2\x80\x87",
'\0');
140 replace(dmsa,
"\xe2\x80\x89",
'\0');
141 replace(dmsa,
"\xe2\x80\x8a",
'\0');
142 replace(dmsa,
"\xe2\x80\x8b",
'\0');
143 replace(dmsa,
"\xe2\x80\xaf",
'\0');
144 replace(dmsa,
"\xe2\x81\xa3",
'\0');
146 replace(dmsa,
"\xb0",
'd' );
147 replace(dmsa,
"\xba",
'd' );
148 replace(dmsa,
"*",
'd' );
149 replace(dmsa,
"`",
'\'');
150 replace(dmsa,
"\xb4",
'\'');
158 replace(dmsa,
"\xa0",
'\0');
159 replace(dmsa,
"''",
'"' );
162 end = unsigned(dmsa.size());
163 while (beg < end && isspace(dmsa[beg]))
165 while (beg < end && isspace(dmsa[end - 1]))
172 for (string::size_type p = beg, pb; p < end; p = pb, ++i) {
173 string::size_type pa = p;
181 pb = min(dmsa.find_first_of(signs_, pa), end);
183 v += InternalDecode(dmsa.substr(p, pb - p), ind2);
186 else if (!(ind2 ==
NONE || ind1 == ind2))
187 throw GeographicErr(
"Incompatible hemisphere specifier in " +
188 dmsa.substr(beg, pb - beg));
192 dmsa.substr(beg, end - beg));
197 Math::real DMS::InternalDecode(
const string& dmsa, flag& ind) {
203 end = unsigned(dmsa.size());
206 if (end > beg && (k =
Utility::lookup(hemispheres_, dmsa[beg])) >= 0) {
208 sign = k % 2 ? 1 : -1;
211 if (end > beg && (k =
Utility::lookup(hemispheres_, dmsa[end-1])) >= 0) {
214 if (toupper(dmsa[beg - 1]) == toupper(dmsa[end - 1]))
215 errormsg =
"Repeated hemisphere indicators "
217 +
" in " + dmsa.substr(beg - 1, end - beg + 1);
219 errormsg =
"Contradictory hemisphere indicators "
222 + dmsa.substr(beg - 1, end - beg + 1);
226 sign = k % 2 ? 1 : -1;
237 errormsg =
"Empty or incomplete DMS string " + dmsa;
240 real ipieces[] = {0, 0, 0};
241 real fpieces[] = {0, 0, 0};
245 unsigned ncurrent = 0, p = beg;
246 bool pointseen =
false;
247 unsigned digcount = 0, intcount = 0;
255 icurrent = 10 * icurrent + k;
258 }
else if (x ==
'.') {
260 errormsg =
"Multiple decimal points in "
261 + dmsa.substr(beg, end - beg);
269 errormsg =
"Illegal for : to appear at the end of " +
270 dmsa.substr(beg, end - beg);
275 if (
unsigned(k) == npiece - 1) {
276 errormsg =
"Repeated " + string(components_[k]) +
277 " component in " + dmsa.substr(beg, end - beg);
279 }
else if (
unsigned(k) < npiece) {
280 errormsg = string(components_[k]) +
" component follows "
281 + string(components_[npiece - 1]) +
" component in "
282 + dmsa.substr(beg, end - beg);
286 errormsg =
"Missing numbers in " + string(components_[k]) +
287 " component of " + dmsa.substr(beg, end - beg);
291 istringstream s(dmsa.substr(p - intcount - digcount - 1,
292 intcount + digcount));
296 ipieces[k] = icurrent;
297 fpieces[k] = icurrent + fcurrent;
300 icurrent = fcurrent = 0;
301 ncurrent = digcount = intcount = 0;
304 errormsg =
"Internal sign in DMS string "
305 + dmsa.substr(beg, end - beg);
308 errormsg =
"Illegal character " +
Utility::str(x) +
" in DMS string "
309 + dmsa.substr(beg, end - beg);
313 if (!errormsg.empty())
317 errormsg =
"Extra text following seconds in DMS string "
318 + dmsa.substr(beg, end - beg);
322 errormsg =
"Missing numbers in trailing component of "
323 + dmsa.substr(beg, end - beg);
327 istringstream s(dmsa.substr(p - intcount - digcount,
328 intcount + digcount));
332 ipieces[npiece] = icurrent;
333 fpieces[npiece] = icurrent + fcurrent;
335 if (pointseen && digcount == 0) {
336 errormsg =
"Decimal point in non-terminal component of "
337 + dmsa.substr(beg, end - beg);
343 +
" not in range [0, " + to_string(
Math::dm) +
")";
348 +
" not in range [0, " + to_string(
Math::ms) +
")";
360 real val = Utility::nummatch<real>(dmsa);
362 throw GeographicErr(errormsg);
369 real& lat, real& lon,
379 }
else if (ia ==
NONE)
385 + strb +
" interpreted as "
386 + (ia ==
LATITUDE ?
"latitudes" :
"longitudes"));
392 +
"d not in [-" + to_string(
Math::qd)
393 +
"d, " + to_string(
Math::qd) +
"d]");
400 real ang =
Decode(angstr, ind);
403 +
" includes a hemisphere, N/E/W/S");
409 real azi =
Decode(azistr, ind);
412 +
" has a latitude hemisphere, N/S");
420 if (!isfinite(angle))
421 return angle < 0 ? string(
"-inf") :
422 (angle > 0 ? string(
"inf") : string(
"nan"));
438 int sign = signbit(angle) ? -1 : 1;
444 idegree = trailing ==
DEGREE ? 0 : floor(angle),
445 fdegree = (angle - idegree) * scale;
446 string s =
Utility::str(fdegree, prec), degree, minute, second;
452 string::size_type p = s.find_first_of(
'.');
458 if (p == string::npos)
481 if (ind ==
NONE && sign < 0)
487 str << setw(1 + min(
int(ind), 2) + prec);
493 str << setw(1 + min(
int(ind), 2));
494 str << degree << (dmssep ? dmssep : char(tolower(dmsindicators_[0])))
495 << setw(2 + prec) << minute;
497 str << char(tolower(dmsindicators_[1]));
501 str << setw(1 + min(
int(ind), 2));
502 str << degree << (dmssep ? dmssep : char(tolower(dmsindicators_[0])))
504 << minute << (dmssep ? dmssep : char(tolower(dmsindicators_[1])))
505 << setw(2 + prec) << second;
507 str << char(tolower(dmsindicators_[2]));
511 str << hemispheres_[(ind ==
LATITUDE ? 0 : 2) + (sign < 0 ? 0 : 1)];
Header for GeographicLib::DMS class.
GeographicLib::Math::real real
Header for GeographicLib::Utility class.
static Math::real DecodeAzimuth(const std::string &azistr)
static Math::real DecodeAngle(const std::string &angstr)
static std::string Encode(real angle, component trailing, unsigned prec, flag ind=NONE, char dmssep=char(0))
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool longfirst=false)
static Math::real Decode(const std::string &dms, flag &ind)
Exception handling for GeographicLib.
static T AngNormalize(T x)
@ qd
degrees per quarter turn
static int extra_digits()
static int lookup(const std::string &s, char c)
static std::string str(T x, int p=-1)
Namespace for GeographicLib.