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"};
30 replace(dmsa,
"\xc2\xb0",
'd');
31 replace(dmsa,
"\xc2\xba",
'd');
32 replace(dmsa,
"\xe2\x81\xb0",
'd');
33 replace(dmsa,
"\xcb\x9a",
'd');
34 replace(dmsa,
"\xe2\x80\xb2",
'\'');
35 replace(dmsa,
"\xc2\xb4",
'\'');
36 replace(dmsa,
"\xe2\x80\x99",
'\'');
37 replace(dmsa,
"\xe2\x80\xb3",
'"');
38 replace(dmsa,
"\xe2\x80\x9d",
'"');
39 replace(dmsa,
"\xe2\x88\x92",
'-');
40 replace(dmsa,
"\xb0",
'd');
41 replace(dmsa,
"\xba",
'd');
42 replace(dmsa,
"\xb4",
'\'');
43 replace(dmsa,
"''",
'"');
46 end = unsigned(dmsa.size());
47 while (beg < end && isspace(dmsa[beg]))
49 while (beg < end && isspace(dmsa[end - 1]))
56 for (string::size_type p = beg, pb; p < end; p = pb, ++i) {
57 string::size_type pa = p;
65 pb = min(dmsa.find_first_of(signs_, pa), end);
67 v += InternalDecode(dmsa.substr(p, pb - p), ind2);
70 else if (!(ind2 == NONE || ind1 == ind2))
72 dmsa.substr(beg, pb - beg));
76 dmsa.substr(beg, end - beg));
81 Math::real DMS::InternalDecode(
const std::string& dmsa, flag& ind) {
87 end = unsigned(dmsa.size());
91 ind1 = (k / 2) ? LONGITUDE : LATITUDE;
92 sign = k % 2 ? 1 : -1;
95 if (end > beg && (k =
Utility::lookup(hemispheres_, dmsa[end-1])) >= 0) {
98 if (toupper(dmsa[beg - 1]) == toupper(dmsa[end - 1]))
99 errormsg =
"Repeated hemisphere indicators "
101 +
" in " + dmsa.substr(beg - 1, end - beg + 1);
103 errormsg =
"Contradictory hemisphere indicators "
106 + dmsa.substr(beg - 1, end - beg + 1);
109 ind1 = (k / 2) ? LONGITUDE : LATITUDE;
110 sign = k % 2 ? 1 : -1;
121 errormsg =
"Empty or incomplete DMS string " + dmsa;
124 real ipieces[] = {0, 0, 0};
125 real fpieces[] = {0, 0, 0};
129 unsigned ncurrent = 0, p = beg;
130 bool pointseen =
false;
131 unsigned digcount = 0, intcount = 0;
139 icurrent = 10 * icurrent + k;
142 }
else if (x ==
'.') {
144 errormsg =
"Multiple decimal points in "
145 + dmsa.substr(beg, end - beg);
153 errormsg =
"Illegal for : to appear at the end of " +
154 dmsa.substr(beg, end - beg);
159 if (
unsigned(k) == npiece - 1) {
160 errormsg =
"Repeated " + components_[k] +
161 " component in " + dmsa.substr(beg, end - beg);
163 }
else if (
unsigned(k) < npiece) {
164 errormsg = components_[k] +
" component follows "
165 + components_[npiece - 1] +
" component in "
166 + dmsa.substr(beg, end - beg);
170 errormsg =
"Missing numbers in " + components_[k] +
171 " component of " + dmsa.substr(beg, end - beg);
175 istringstream s(dmsa.substr(p - intcount - digcount - 1,
176 intcount + digcount));
180 ipieces[k] = icurrent;
181 fpieces[k] = icurrent + fcurrent;
184 icurrent = fcurrent = 0;
185 ncurrent = digcount = intcount = 0;
188 errormsg =
"Internal sign in DMS string "
189 + dmsa.substr(beg, end - beg);
192 errormsg =
"Illegal character " +
Utility::str(x) +
" in DMS string "
193 + dmsa.substr(beg, end - beg);
197 if (!errormsg.empty())
201 errormsg =
"Extra text following seconds in DMS string "
202 + dmsa.substr(beg, end - beg);
206 errormsg =
"Missing numbers in trailing component of "
207 + dmsa.substr(beg, end - beg);
211 istringstream s(dmsa.substr(p - intcount - digcount,
212 intcount + digcount));
216 ipieces[npiece] = icurrent;
217 fpieces[npiece] = icurrent + fcurrent;
219 if (pointseen && digcount == 0) {
220 errormsg =
"Decimal point in non-terminal component of "
221 + dmsa.substr(beg, end - beg);
225 if (ipieces[1] >= 60 || fpieces[1] > 60 ) {
227 +
" not in range [0, 60)";
230 if (ipieces[2] >= 60 || fpieces[2] > 60) {
232 +
" not in range [0, 60)";
239 ( fpieces[2] ? (60*(60*fpieces[0] + fpieces[1]) + fpieces[2]) / 3600 :
240 ( fpieces[1] ? (60*fpieces[0] + fpieces[1]) / 60 : fpieces[0] ) );
242 real val = Utility::nummatch<real>(dmsa);
244 throw GeographicErr(errormsg);
251 real& lat, real& lon,
255 a = Decode(stra, ia);
256 b = Decode(strb, ib);
257 if (ia == NONE && ib == NONE) {
259 ia = longfirst ? LONGITUDE : LATITUDE;
260 ib = longfirst ? LATITUDE : LONGITUDE;
261 }
else if (ia == NONE)
262 ia =
flag(LATITUDE + LONGITUDE - ib);
264 ib =
flag(LATITUDE + LONGITUDE - ia);
267 + strb +
" interpreted as "
268 + (ia == LATITUDE ?
"latitudes" :
"longitudes"));
270 lat1 = ia == LATITUDE ? a : b,
271 lon1 = ia == LATITUDE ? b : a;
274 +
"d not in [-90d, 90d]");
281 real ang = Decode(angstr, ind);
284 +
" includes a hemisphere, N/E/W/S");
290 real azi = Decode(azistr, ind);
293 +
" has a latitude hemisphere, N/S");
302 return angle < 0 ? string(
"-inf") :
303 (angle > 0 ? string(
"inf") : string(
"nan"));
309 for (
unsigned i = 0; i < unsigned(trailing); ++i)
311 for (
unsigned i = 0; i < prec; ++i)
314 angle -= floor(angle/360) * 360;
315 int sign = angle < 0 ? -1 : 1;
321 idegree = floor(angle),
322 fdegree = (angle - idegree) * scale +
real(0.5);
325 real f = floor(fdegree);
326 fdegree = (f == fdegree && fmod(f,
real(2)) == 1) ? f - 1 : f;
333 real pieces[3] = {fdegree, 0, 0};
334 for (
unsigned i = 1; i <= unsigned(trailing); ++i) {
336 ip = floor(pieces[i - 1]),
337 fp = pieces[i - 1] - ip;
341 pieces[0] += idegree;
343 s << fixed << setfill(
'0');
344 if (ind == NONE && sign < 0)
349 s << setw(1 + min(
int(ind), 2) + prec + (prec ? 1 : 0));
355 s << setw(1 + min(
int(ind), 2));
357 << (dmssep ? dmssep : char(tolower(dmsindicators_[0])));
360 s << setw(2 + prec + (prec ? 1 : 0)) <<
Utility::str(pieces[1], prec);
362 s << char(tolower(dmsindicators_[1]));
367 << (dmssep ? dmssep : char(tolower(dmsindicators_[1])))
368 << setw(2 + prec + (prec ? 1 : 0)) << Utility::str(pieces[2], prec);
370 s << char(tolower(dmsindicators_[2]));
376 if (ind != NONE && ind != AZIMUTH)
377 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.