15 # pragma warning (disable: 4127)
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"};
31 replace(dmsa,
"\xc2\xb0",
'd');
32 replace(dmsa,
"\xc2\xba",
'd');
33 replace(dmsa,
"\xe2\x81\xb0",
'd');
34 replace(dmsa,
"\xcb\x9a",
'd');
35 replace(dmsa,
"\xe2\x80\xb2",
'\'');
36 replace(dmsa,
"\xc2\xb4",
'\'');
37 replace(dmsa,
"\xe2\x80\x99",
'\'');
38 replace(dmsa,
"\xe2\x80\xb3",
'"');
39 replace(dmsa,
"\xe2\x80\x9d",
'"');
40 replace(dmsa,
"\xe2\x88\x92",
'-');
41 replace(dmsa,
"\xb0",
'd');
42 replace(dmsa,
"\xba",
'd');
43 replace(dmsa,
"\xb4",
'\'');
44 replace(dmsa,
"''",
'"');
47 end = unsigned(dmsa.size());
48 while (beg < end && isspace(dmsa[beg]))
50 while (beg < end && isspace(dmsa[end - 1]))
57 for (string::size_type p = beg, pb; p < end; p = pb, ++i) {
58 string::size_type pa = p;
66 pb = min(dmsa.find_first_of(signs_, pa), end);
68 v += InternalDecode(dmsa.substr(p, pb - p), ind2);
71 else if (!(ind2 == NONE || ind1 == ind2))
73 dmsa.substr(beg, pb - beg));
77 dmsa.substr(beg, end - beg));
82 Math::real DMS::InternalDecode(
const std::string& dmsa, flag& ind) {
88 end = unsigned(dmsa.size());
92 ind1 = (k / 2) ? LONGITUDE : LATITUDE;
93 sign = k % 2 ? 1 : -1;
96 if (end > beg && (k =
Utility::lookup(hemispheres_, dmsa[end-1])) >= 0) {
99 if (toupper(dmsa[beg - 1]) == toupper(dmsa[end - 1]))
100 errormsg =
"Repeated hemisphere indicators "
102 +
" in " + dmsa.substr(beg - 1, end - beg + 1);
104 errormsg =
"Contradictory hemisphere indicators "
107 + dmsa.substr(beg - 1, end - beg + 1);
110 ind1 = (k / 2) ? LONGITUDE : LATITUDE;
111 sign = k % 2 ? 1 : -1;
122 errormsg =
"Empty or incomplete DMS string " + dmsa;
125 real ipieces[] = {0, 0, 0};
126 real fpieces[] = {0, 0, 0};
130 unsigned ncurrent = 0, p = beg;
131 bool pointseen =
false;
132 unsigned digcount = 0, intcount = 0;
140 icurrent = 10 * icurrent + k;
143 }
else if (x ==
'.') {
145 errormsg =
"Multiple decimal points in "
146 + dmsa.substr(beg, end - beg);
154 errormsg =
"Illegal for : to appear at the end of " +
155 dmsa.substr(beg, end - beg);
160 if (
unsigned(k) == npiece - 1) {
161 errormsg =
"Repeated " + components_[k] +
162 " component in " + dmsa.substr(beg, end - beg);
164 }
else if (
unsigned(k) < npiece) {
165 errormsg = components_[k] +
" component follows "
166 + components_[npiece - 1] +
" component in "
167 + dmsa.substr(beg, end - beg);
171 errormsg =
"Missing numbers in " + components_[k] +
172 " component of " + dmsa.substr(beg, end - beg);
176 istringstream s(dmsa.substr(p - intcount - digcount - 1,
177 intcount + digcount));
181 ipieces[k] = icurrent;
182 fpieces[k] = icurrent + fcurrent;
185 icurrent = fcurrent = 0;
186 ncurrent = digcount = intcount = 0;
189 errormsg =
"Internal sign in DMS string "
190 + dmsa.substr(beg, end - beg);
193 errormsg =
"Illegal character " +
Utility::str(x) +
" in DMS string "
194 + dmsa.substr(beg, end - beg);
198 if (!errormsg.empty())
202 errormsg =
"Extra text following seconds in DMS string "
203 + dmsa.substr(beg, end - beg);
207 errormsg =
"Missing numbers in trailing component of "
208 + dmsa.substr(beg, end - beg);
212 istringstream s(dmsa.substr(p - intcount - digcount,
213 intcount + digcount));
217 ipieces[npiece] = icurrent;
218 fpieces[npiece] = icurrent + fcurrent;
220 if (pointseen && digcount == 0) {
221 errormsg =
"Decimal point in non-terminal component of "
222 + dmsa.substr(beg, end - beg);
226 if (ipieces[1] >= 60 || fpieces[1] > 60 ) {
228 +
" not in range [0, 60)";
231 if (ipieces[2] >= 60 || fpieces[2] > 60) {
233 +
" not in range [0, 60)";
240 ( fpieces[2] ? (60*(60*fpieces[0] + fpieces[1]) + fpieces[2]) / 3600 :
241 ( fpieces[1] ? (60*fpieces[0] + fpieces[1]) / 60 : fpieces[0] ) );
243 real val = Utility::nummatch<real>(dmsa);
245 throw GeographicErr(errormsg);
252 real& lat, real& lon,
256 a = Decode(stra, ia);
257 b = Decode(strb, ib);
258 if (ia == NONE && ib == NONE) {
260 ia = longfirst ? LONGITUDE : LATITUDE;
261 ib = longfirst ? LATITUDE : LONGITUDE;
262 }
else if (ia == NONE)
263 ia =
flag(LATITUDE + LONGITUDE - ib);
265 ib =
flag(LATITUDE + LONGITUDE - ia);
268 + strb +
" interpreted as "
269 + (ia == LATITUDE ?
"latitudes" :
"longitudes"));
271 lat1 = ia == LATITUDE ? a : b,
272 lon1 = ia == LATITUDE ? b : a;
275 +
"d not in [-90d, 90d]");
282 real ang = Decode(angstr, ind);
285 +
" includes a hemisphere, N/E/W/S");
291 real azi = Decode(azistr, ind);
294 +
" has a latitude hemisphere, N/S");
303 return angle < 0 ? string(
"-inf") :
304 (angle > 0 ? string(
"inf") : string(
"nan"));
310 for (
unsigned i = 0; i < unsigned(trailing); ++i)
312 for (
unsigned i = 0; i < prec; ++i)
315 angle -= floor(angle/360) * 360;
316 int sign = angle < 0 ? -1 : 1;
322 idegree = floor(angle),
323 fdegree = (angle - idegree) * scale +
real(0.5);
326 real f = floor(fdegree);
327 fdegree = (f == fdegree && fmod(f,
real(2)) == 1) ? f - 1 : f;
334 real pieces[3] = {fdegree, 0, 0};
335 for (
unsigned i = 1; i <= unsigned(trailing); ++i) {
337 ip = floor(pieces[i - 1]),
338 fp = pieces[i - 1] - ip;
342 pieces[0] += idegree;
344 s << fixed << setfill(
'0');
345 if (ind == NONE && sign < 0)
350 s << setw(1 + min(
int(ind), 2) + prec + (prec ? 1 : 0));
356 s << setw(1 + min(
int(ind), 2));
358 << (dmssep ? dmssep : char(tolower(dmsindicators_[0])));
361 s << setw(2 + prec + (prec ? 1 : 0)) <<
Utility::str(pieces[1], prec);
363 s << char(tolower(dmsindicators_[1]));
368 << (dmssep ? dmssep : char(tolower(dmsindicators_[1])))
369 << setw(2 + prec + (prec ? 1 : 0)) << Utility::str(pieces[2], prec);
371 s << char(tolower(dmsindicators_[2]));
377 if (ind != NONE && ind != AZIMUTH)
378 s << hemispheres_[(ind == LATITUDE ? 0 : 2) + (sign < 0 ? 0 : 1)];
static T AngNormalize(T x)
static Math::real DecodeAngle(const std::string &angstr)
GeographicLib::Math::real real
Header for GeographicLib::Utility class.
static bool isfinite(T x)
static int extra_digits()
static std::string Encode(real angle, component trailing, unsigned prec, flag ind=NONE, char dmssep=char(0))
static Math::real DecodeAzimuth(const std::string &azistr)
static Math::real Decode(const std::string &dms, flag &ind)
Namespace for GeographicLib.
static std::string str(T x, int p=-1)
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool longfirst=false)
Exception handling for GeographicLib.
static int lookup(const std::string &s, char c)
Header for GeographicLib::DMS class.