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,
"''",
'"');
49 end = unsigned(dmsa.size());
50 while (beg < end && isspace(dmsa[beg]))
52 while (beg < end && isspace(dmsa[end - 1]))
57 ind1 = (k / 2) ? LONGITUDE : LATITUDE;
58 sign = k % 2 ? 1 : -1;
61 if (end > beg && (k =
Utility::lookup(hemispheres_, dmsa[end-1])) >= 0) {
64 if (toupper(dmsa[beg - 1]) == toupper(dmsa[end - 1]))
65 errormsg =
"Repeated hemisphere indicators "
67 +
" in " + dmsa.substr(beg - 1, end - beg + 1);
69 errormsg =
"Contradictory hemisphere indicators "
72 + dmsa.substr(beg - 1, end - beg + 1);
75 ind1 = (k / 2) ? LONGITUDE : LATITUDE;
76 sign = k % 2 ? 1 : -1;
87 errormsg =
"Empty or incomplete DMS string " + dmsa;
90 real ipieces[] = {0, 0, 0};
91 real fpieces[] = {0, 0, 0};
95 unsigned ncurrent = 0, p = beg;
96 bool pointseen =
false;
97 unsigned digcount = 0, intcount = 0;
105 icurrent = 10 * icurrent + k;
108 }
else if (x ==
'.') {
110 errormsg =
"Multiple decimal points in "
111 + dmsa.substr(beg, end - beg);
119 errormsg =
"Illegal for : to appear at the end of " +
120 dmsa.substr(beg, end - beg);
125 if (
unsigned(k) == npiece - 1) {
126 errormsg =
"Repeated " + components_[k] +
127 " component in " + dmsa.substr(beg, end - beg);
129 }
else if (
unsigned(k) < npiece) {
130 errormsg = components_[k] +
" component follows "
131 + components_[npiece - 1] +
" component in "
132 + dmsa.substr(beg, end - beg);
136 errormsg =
"Missing numbers in " + components_[k] +
137 " component of " + dmsa.substr(beg, end - beg);
141 istringstream s(dmsa.substr(p - intcount - digcount - 1,
142 intcount + digcount));
146 ipieces[k] = icurrent;
147 fpieces[k] = icurrent + fcurrent;
150 icurrent = fcurrent = 0;
151 ncurrent = digcount = intcount = 0;
154 errormsg =
"Internal sign in DMS string "
155 + dmsa.substr(beg, end - beg);
158 errormsg =
"Illegal character " +
Utility::str(x) +
" in DMS string "
159 + dmsa.substr(beg, end - beg);
163 if (!errormsg.empty())
167 errormsg =
"Extra text following seconds in DMS string "
168 + dmsa.substr(beg, end - beg);
172 errormsg =
"Missing numbers in trailing component of "
173 + dmsa.substr(beg, end - beg);
177 istringstream s(dmsa.substr(p - intcount - digcount,
178 intcount + digcount));
182 ipieces[npiece] = icurrent;
183 fpieces[npiece] = icurrent + fcurrent;
185 if (pointseen && digcount == 0) {
186 errormsg =
"Decimal point in non-terminal component of "
187 + dmsa.substr(beg, end - beg);
191 if (ipieces[1] >= 60) {
193 +
" not in range [0, 60)";
196 if (ipieces[2] >= 60) {
198 +
" not in range [0, 60)";
204 return real(sign) * (fpieces[0] + (fpieces[1] + fpieces[2] / 60) / 60);
206 real val = Utility::nummatch<real>(dmsa);
215 real& lat, real& lon,
bool swaplatlong) {
218 a = Decode(stra, ia);
219 b = Decode(strb, ib);
220 if (ia == NONE && ib == NONE) {
222 ia = swaplatlong ? LONGITUDE : LATITUDE;
223 ib = swaplatlong ? LATITUDE : LONGITUDE;
224 }
else if (ia == NONE)
225 ia =
flag(LATITUDE + LONGITUDE - ib);
227 ib =
flag(LATITUDE + LONGITUDE - ia);
230 + strb +
" interpreted as "
231 + (ia == LATITUDE ?
"latitudes" :
"longitudes"));
233 lat1 = ia == LATITUDE ? a : b,
234 lon1 = ia == LATITUDE ? b : a;
237 +
"d not in [-90d, 90d]");
238 if (lon1 < -540 || lon1 >= 540)
240 +
"d not in [-540d, 540d)");
248 real ang = Decode(angstr, ind);
251 +
" includes a hemisphere, N/E/W/S");
257 real azi = Decode(azistr, ind);
260 +
" has a latitude hemisphere, N/S");
261 if (azi < -540 || azi >= 540)
262 throw GeographicErr(
"Azimuth " + azistr +
" not in range [-540d, 540d)");
271 return angle < 0 ? string(
"-inf") :
272 (angle > 0 ? string(
"inf") : string(
"nan"));
278 for (
unsigned i = 0; i < unsigned(trailing); ++i)
280 for (
unsigned i = 0; i < prec; ++i)
283 angle -= floor(angle/360) * 360;
284 int sign = angle < 0 ? -1 : 1;
290 idegree = floor(angle),
291 fdegree = floor((angle - idegree) * scale +
real(0.5)) / scale;
296 real pieces[3] = {fdegree, 0, 0};
297 for (
unsigned i = 1; i <= unsigned(trailing); ++i) {
299 ip = floor(pieces[i - 1]),
300 fp = pieces[i - 1] - ip;
304 pieces[0] += idegree;
306 s << fixed << setfill(
'0');
307 if (ind == NONE && sign < 0)
312 s << setw(1 + min(
int(ind), 2) + prec + (prec ? 1 : 0));
313 s << setprecision(prec) << pieces[0];
318 s << setw(1 + min(
int(ind), 2));
319 s << setprecision(0) << pieces[0]
320 << (dmssep ? dmssep : char(tolower(dmsindicators_[0])));
323 s << setw(2 + prec + (prec ? 1 : 0)) << setprecision(prec) << pieces[1];
325 s << char(tolower(dmsindicators_[1]));
329 << pieces[1] << (dmssep ? dmssep : char(tolower(dmsindicators_[1])))
330 << setw(2 + prec + (prec ? 1 : 0)) << setprecision(prec) << pieces[2];
332 s << char(tolower(dmsindicators_[2]));
338 if (ind != NONE && ind != AZIMUTH)
339 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 void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool swaplatlong=false)
static Math::real Decode(const std::string &dms, flag &ind)
Namespace for GeographicLib.
static std::string str(T x, int p=-1)
Exception handling for GeographicLib.
static int lookup(const std::string &s, char c)
Header for GeographicLib::DMS class.