15# pragma warning (disable: 4127)
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"};
92 replace(dmsa,
"\xc2\xb0",
'd' );
93 replace(dmsa,
"\xc2\xba",
'd' );
94 replace(dmsa,
"\xe2\x81\xb0",
'd' );
95 replace(dmsa,
"\xcb\x9a",
'd' );
96 replace(dmsa,
"\xe2\x88\x98",
'd' );
98 replace(dmsa,
"\xe2\x80\xb2",
'\'');
99 replace(dmsa,
"\xe2\x80\xb5",
'\'');
100 replace(dmsa,
"\xc2\xb4",
'\'');
101 replace(dmsa,
"\xe2\x80\x98",
'\'');
102 replace(dmsa,
"\xe2\x80\x99",
'\'');
103 replace(dmsa,
"\xe2\x80\x9b",
'\'');
104 replace(dmsa,
"\xca\xb9",
'\'');
105 replace(dmsa,
"\xcb\x8a",
'\'');
106 replace(dmsa,
"\xcb\x8b",
'\'');
108 replace(dmsa,
"\xe2\x80\xb3",
'"' );
109 replace(dmsa,
"\xe2\x80\xb6",
'"' );
110 replace(dmsa,
"\xcb\x9d",
'"' );
111 replace(dmsa,
"\xe2\x80\x9c",
'"' );
112 replace(dmsa,
"\xe2\x80\x9d",
'"' );
113 replace(dmsa,
"\xe2\x80\x9f",
'"' );
114 replace(dmsa,
"\xca\xba",
'"' );
116 replace(dmsa,
"\xe2\x9e\x95",
'+' );
117 replace(dmsa,
"\xe2\x81\xa4",
'+' );
119 replace(dmsa,
"\xe2\x80\x90",
'-' );
120 replace(dmsa,
"\xe2\x80\x91",
'-' );
121 replace(dmsa,
"\xe2\x80\x93",
'-' );
122 replace(dmsa,
"\xe2\x80\x94",
'-' );
123 replace(dmsa,
"\xe2\x88\x92",
'-' );
124 replace(dmsa,
"\xe2\x9e\x96",
'-' );
126 replace(dmsa,
"\xc2\xa0",
'\0');
127 replace(dmsa,
"\xe2\x80\x87",
'\0');
128 replace(dmsa,
"\xe2\x80\x89",
'\0');
129 replace(dmsa,
"\xe2\x80\x8a",
'\0');
130 replace(dmsa,
"\xe2\x80\x8b",
'\0');
131 replace(dmsa,
"\xe2\x80\xaf",
'\0');
132 replace(dmsa,
"\xe2\x81\xa3",
'\0');
134 replace(dmsa,
"\xb0",
'd' );
135 replace(dmsa,
"\xba",
'd' );
136 replace(dmsa,
"*",
'd' );
137 replace(dmsa,
"`",
'\'');
138 replace(dmsa,
"\xb4",
'\'');
146 replace(dmsa,
"\xa0",
'\0');
147 replace(dmsa,
"''",
'"' );
150 end = unsigned(dmsa.size());
151 while (beg < end && isspace(dmsa[beg]))
153 while (beg < end && isspace(dmsa[end - 1]))
160 for (string::size_type p = beg, pb; p < end; p = pb, ++i) {
161 string::size_type pa = p;
169 pb = min(dmsa.find_first_of(signs_, pa), end);
171 v += InternalDecode(dmsa.substr(p, pb - p), ind2);
174 else if (!(ind2 ==
NONE || ind1 == ind2))
175 throw GeographicErr(
"Incompatible hemisphere specifier in " +
176 dmsa.substr(beg, pb - beg));
180 dmsa.substr(beg, end - beg));
185 Math::real DMS::InternalDecode(
const string& dmsa, flag& ind) {
191 end = unsigned(dmsa.size());
194 if (end > beg && (k =
Utility::lookup(hemispheres_, dmsa[beg])) >= 0) {
196 sign = k % 2 ? 1 : -1;
199 if (end > beg && (k =
Utility::lookup(hemispheres_, dmsa[end-1])) >= 0) {
202 if (toupper(dmsa[beg - 1]) == toupper(dmsa[end - 1]))
203 errormsg =
"Repeated hemisphere indicators "
205 +
" in " + dmsa.substr(beg - 1, end - beg + 1);
207 errormsg =
"Contradictory hemisphere indicators "
210 + dmsa.substr(beg - 1, end - beg + 1);
214 sign = k % 2 ? 1 : -1;
225 errormsg =
"Empty or incomplete DMS string " + dmsa;
228 real ipieces[] = {0, 0, 0};
229 real fpieces[] = {0, 0, 0};
233 unsigned ncurrent = 0, p = beg;
234 bool pointseen =
false;
235 unsigned digcount = 0, intcount = 0;
243 icurrent = 10 * icurrent + k;
246 }
else if (x ==
'.') {
248 errormsg =
"Multiple decimal points in "
249 + dmsa.substr(beg, end - beg);
257 errormsg =
"Illegal for : to appear at the end of " +
258 dmsa.substr(beg, end - beg);
263 if (
unsigned(k) == npiece - 1) {
264 errormsg =
"Repeated " + string(components_[k]) +
265 " component in " + dmsa.substr(beg, end - beg);
267 }
else if (
unsigned(k) < npiece) {
268 errormsg = string(components_[k]) +
" component follows "
269 + string(components_[npiece - 1]) +
" component in "
270 + dmsa.substr(beg, end - beg);
274 errormsg =
"Missing numbers in " + string(components_[k]) +
275 " component of " + dmsa.substr(beg, end - beg);
279 istringstream s(dmsa.substr(p - intcount - digcount - 1,
280 intcount + digcount));
284 ipieces[k] = icurrent;
285 fpieces[k] = icurrent + fcurrent;
288 icurrent = fcurrent = 0;
289 ncurrent = digcount = intcount = 0;
292 errormsg =
"Internal sign in DMS string "
293 + dmsa.substr(beg, end - beg);
296 errormsg =
"Illegal character " +
Utility::str(x) +
" in DMS string "
297 + dmsa.substr(beg, end - beg);
301 if (!errormsg.empty())
305 errormsg =
"Extra text following seconds in DMS string "
306 + dmsa.substr(beg, end - beg);
310 errormsg =
"Missing numbers in trailing component of "
311 + dmsa.substr(beg, end - beg);
315 istringstream s(dmsa.substr(p - intcount - digcount,
316 intcount + digcount));
320 ipieces[npiece] = icurrent;
321 fpieces[npiece] = icurrent + fcurrent;
323 if (pointseen && digcount == 0) {
324 errormsg =
"Decimal point in non-terminal component of "
325 + dmsa.substr(beg, end - beg);
329 if (ipieces[1] >= 60 || fpieces[1] > 60 ) {
331 +
" not in range [0, 60)";
334 if (ipieces[2] >= 60 || fpieces[2] > 60) {
336 +
" not in range [0, 60)";
344 (60*(60*fpieces[0] + fpieces[1]) + fpieces[2]) / 3600 :
346 (60*fpieces[0] + fpieces[1]) / 60 : fpieces[0] ) );
348 real val = Utility::nummatch<real>(dmsa);
350 throw GeographicErr(errormsg);
357 real& lat, real& lon,
367 }
else if (ia ==
NONE)
373 + strb +
" interpreted as "
374 + (ia ==
LATITUDE ?
"latitudes" :
"longitudes"));
380 +
"d not in [-90d, 90d]");
387 real ang =
Decode(angstr, ind);
390 +
" includes a hemisphere, N/E/W/S");
396 real azi =
Decode(azistr, ind);
399 +
" has a latitude hemisphere, N/S");
407 if (!isfinite(angle))
408 return angle < 0 ? string(
"-inf") :
409 (angle > 0 ? string(
"inf") : string(
"nan"));
415 for (
unsigned i = 0; i < unsigned(trailing); ++i)
417 for (
unsigned i = 0; i < prec; ++i)
420 angle -= floor(angle/360) * 360;
421 int sign = angle < 0 ? -1 : 1;
427 idegree = floor(angle),
428 fdegree = (angle - idegree) * scale + real(0.5);
431 real f = floor(fdegree);
432 fdegree = (f == fdegree && fmod(f, real(2)) == 1) ? f - 1 : f;
439 real pieces[3] = {fdegree, 0, 0};
440 for (
unsigned i = 1; i <= unsigned(trailing); ++i) {
442 ip = floor(pieces[i - 1]),
443 fp = pieces[i - 1] - ip;
447 pieces[0] += idegree;
449 s << fixed << setfill(
'0');
450 if (ind ==
NONE && sign < 0)
455 s << setw(1 + min(
int(ind), 2) + prec + (prec ? 1 : 0));
461 s << setw(1 + min(
int(ind), 2));
463 << (dmssep ? dmssep : char(tolower(dmsindicators_[0])));
466 s << setw(2 + prec + (prec ? 1 : 0)) <<
Utility::str(pieces[1], prec);
468 s << char(tolower(dmsindicators_[1]));
473 << (dmssep ? dmssep : char(tolower(dmsindicators_[1])))
474 << setw(2 + prec + (prec ? 1 : 0)) <<
Utility::str(pieces[2], prec);
476 s << char(tolower(dmsindicators_[2]));
483 s << 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)
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.