17 const char*
const DMS::hemispheres_ =
"SNWE";
18 const char*
const DMS::signs_ =
"-+";
19 const char*
const DMS::digits_ =
"0123456789";
20 const char*
const DMS::dmsindicators_ =
"D'\":";
21 const char*
const DMS::components_[] = {
"degrees",
"minutes",
"seconds"};
24 void DMS::replace(std::string& s,
const std::string& pat,
char c) {
25 string::size_type p = 0;
26 int count = c ? 1 : 0;
29 if (p == string::npos)
31 s.replace(p, pat.length(), count, c);
99 replace(dmsa,
"\xc2\xb0",
'd' );
100 replace(dmsa,
"\xc2\xba",
'd' );
101 replace(dmsa,
"\xe2\x81\xb0",
'd' );
102 replace(dmsa,
"\xcb\x9a",
'd' );
103 replace(dmsa,
"\xe2\x88\x98",
'd' );
105 replace(dmsa,
"\xe2\x80\xb2",
'\'');
106 replace(dmsa,
"\xe2\x80\xb5",
'\'');
107 replace(dmsa,
"\xc2\xb4",
'\'');
108 replace(dmsa,
"\xe2\x80\x98",
'\'');
109 replace(dmsa,
"\xe2\x80\x99",
'\'');
110 replace(dmsa,
"\xe2\x80\x9b",
'\'');
111 replace(dmsa,
"\xca\xb9",
'\'');
112 replace(dmsa,
"\xcb\x8a",
'\'');
113 replace(dmsa,
"\xcb\x8b",
'\'');
115 replace(dmsa,
"\xe2\x80\xb3",
'"' );
116 replace(dmsa,
"\xe2\x80\xb6",
'"' );
117 replace(dmsa,
"\xcb\x9d",
'"' );
118 replace(dmsa,
"\xe2\x80\x9c",
'"' );
119 replace(dmsa,
"\xe2\x80\x9d",
'"' );
120 replace(dmsa,
"\xe2\x80\x9f",
'"' );
121 replace(dmsa,
"\xca\xba",
'"' );
123 replace(dmsa,
"\xe2\x9e\x95",
'+' );
124 replace(dmsa,
"\xe2\x81\xa4",
'+' );
126 replace(dmsa,
"\xe2\x80\x90",
'-' );
127 replace(dmsa,
"\xe2\x80\x91",
'-' );
128 replace(dmsa,
"\xe2\x80\x93",
'-' );
129 replace(dmsa,
"\xe2\x80\x94",
'-' );
130 replace(dmsa,
"\xe2\x88\x92",
'-' );
131 replace(dmsa,
"\xe2\x9e\x96",
'-' );
133 replace(dmsa,
"\xc2\xa0",
'\0');
134 replace(dmsa,
"\xe2\x80\x87",
'\0');
135 replace(dmsa,
"\xe2\x80\x89",
'\0');
136 replace(dmsa,
"\xe2\x80\x8a",
'\0');
137 replace(dmsa,
"\xe2\x80\x8b",
'\0');
138 replace(dmsa,
"\xe2\x80\xaf",
'\0');
139 replace(dmsa,
"\xe2\x81\xa3",
'\0');
141 replace(dmsa,
"\xb0",
'd' );
142 replace(dmsa,
"\xba",
'd' );
143 replace(dmsa,
"*",
'd' );
144 replace(dmsa,
"`",
'\'');
145 replace(dmsa,
"\xb4",
'\'');
153 replace(dmsa,
"\xa0",
'\0');
154 replace(dmsa,
"''",
'"' );
157 end = unsigned(dmsa.size());
158 while (beg < end && isspace(dmsa[beg]))
160 while (beg < end && isspace(dmsa[end - 1]))
167 for (string::size_type p = beg, pb; p < end; p = pb, ++i) {
168 string::size_type pa = p;
176 pb = min(dmsa.find_first_of(signs_, pa), end);
178 v += InternalDecode(dmsa.substr(p, pb - p), ind2);
181 else if (!(ind2 ==
NONE || ind1 == ind2))
182 throw GeographicErr(
"Incompatible hemisphere specifier in " +
183 dmsa.substr(beg, pb - beg));
187 dmsa.substr(beg, end - beg));
192 Math::real DMS::InternalDecode(
const string& dmsa, flag& ind) {
198 end = unsigned(dmsa.size());
201 if (end > beg && (k =
Utility::lookup(hemispheres_, dmsa[beg])) >= 0) {
203 sign = k % 2 ? 1 : -1;
206 if (end > beg && (k =
Utility::lookup(hemispheres_, dmsa[end-1])) >= 0) {
209 if (toupper(dmsa[beg - 1]) == toupper(dmsa[end - 1]))
210 errormsg =
"Repeated hemisphere indicators "
212 +
" in " + dmsa.substr(beg - 1, end - beg + 1);
214 errormsg =
"Contradictory hemisphere indicators "
217 + dmsa.substr(beg - 1, end - beg + 1);
221 sign = k % 2 ? 1 : -1;
232 errormsg =
"Empty or incomplete DMS string " + dmsa;
235 real ipieces[] = {0, 0, 0};
236 real fpieces[] = {0, 0, 0};
240 unsigned ncurrent = 0, p = beg;
241 bool pointseen =
false;
242 unsigned digcount = 0, intcount = 0;
250 icurrent = 10 * icurrent + k;
253 }
else if (x ==
'.') {
255 errormsg =
"Multiple decimal points in "
256 + dmsa.substr(beg, end - beg);
264 errormsg =
"Illegal for : to appear at the end of " +
265 dmsa.substr(beg, end - beg);
270 if (
unsigned(k) == npiece - 1) {
271 errormsg =
"Repeated " + string(components_[k]) +
272 " component in " + dmsa.substr(beg, end - beg);
274 }
else if (
unsigned(k) < npiece) {
275 errormsg = string(components_[k]) +
" component follows "
276 + string(components_[npiece - 1]) +
" component in "
277 + dmsa.substr(beg, end - beg);
281 errormsg =
"Missing numbers in " + string(components_[k]) +
282 " component of " + dmsa.substr(beg, end - beg);
286 istringstream s(dmsa.substr(p - intcount - digcount - 1,
287 intcount + digcount));
291 ipieces[k] = icurrent;
292 fpieces[k] = icurrent + fcurrent;
295 icurrent = fcurrent = 0;
296 ncurrent = digcount = intcount = 0;
299 errormsg =
"Internal sign in DMS string "
300 + dmsa.substr(beg, end - beg);
303 errormsg =
"Illegal character " +
Utility::str(x) +
" in DMS string "
304 + dmsa.substr(beg, end - beg);
308 if (!errormsg.empty())
312 errormsg =
"Extra text following seconds in DMS string "
313 + dmsa.substr(beg, end - beg);
317 errormsg =
"Missing numbers in trailing component of "
318 + dmsa.substr(beg, end - beg);
322 istringstream s(dmsa.substr(p - intcount - digcount,
323 intcount + digcount));
327 ipieces[npiece] = icurrent;
328 fpieces[npiece] = icurrent + fcurrent;
330 if (pointseen && digcount == 0) {
331 errormsg =
"Decimal point in non-terminal component of "
332 + dmsa.substr(beg, end - beg);
338 +
" not in range [0, " + to_string(
Math::dm) +
")";
343 +
" not in range [0, " + to_string(
Math::ms) +
")";
355 real val = Utility::nummatch<real>(dmsa);
357 throw GeographicErr(errormsg);
364 real& lat, real& lon,
374 }
else if (ia ==
NONE)
380 + strb +
" interpreted as "
381 + (ia ==
LATITUDE ?
"latitudes" :
"longitudes"));
387 +
"d not in [-" + to_string(
Math::qd)
388 +
"d, " + to_string(
Math::qd) +
"d]");
395 real ang =
Decode(angstr, ind);
398 +
" includes a hemisphere, N/E/W/S");
404 real azi =
Decode(azistr, ind);
407 +
" has a latitude hemisphere, N/S");
415 if (!isfinite(angle))
416 return angle < 0 ? string(
"-inf") :
417 (angle > 0 ? string(
"inf") : string(
"nan"));
433 int sign = signbit(angle) ? -1 : 1;
439 idegree = trailing ==
DEGREE ? 0 : floor(angle),
440 fdegree = (angle - idegree) * scale;
441 string s =
Utility::str(fdegree, prec), degree, minute, second;
447 string::size_type p = s.find_first_of(
'.');
453 if (p == string::npos)
476 if (ind ==
NONE && sign < 0)
482 str << setw(1 + min(
int(ind), 2) + prec);
488 str << setw(1 + min(
int(ind), 2));
489 str << degree << (dmssep ? dmssep : char(tolower(dmsindicators_[0])))
490 << setw(2 + prec) << minute;
492 str << char(tolower(dmsindicators_[1]));
496 str << setw(1 + min(
int(ind), 2));
497 str << degree << (dmssep ? dmssep : char(tolower(dmsindicators_[0])))
499 << minute << (dmssep ? dmssep : char(tolower(dmsindicators_[1])))
500 << setw(2 + prec) << second;
502 str << char(tolower(dmsindicators_[2]));
506 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 constexpr int dm
minutes per degree
static constexpr int ds
seconds per degree
static constexpr int qd
degrees per quarter turn
static T AngNormalize(T x)
static constexpr int td
degrees per turn
static constexpr int ms
seconds per minute
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.