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"};
30 replace(dmsa,
"\xe2\x88\x92",
'-');
31 replace(dmsa,
"\xc2\xb0",
'd');
32 replace(dmsa,
"\xb0",
'd');
33 replace(dmsa,
"\xc2\xba",
'd');
34 replace(dmsa,
"\xba",
'd');
35 replace(dmsa,
"\xe2\x81\xb0",
'd');
36 replace(dmsa,
"\xcb\x9a",
'd');
37 replace(dmsa,
"\xe2\x80\xb2",
'\'');
38 replace(dmsa,
"\xc2\xb4",
'\'');
39 replace(dmsa,
"\xb4",
'\'');
40 replace(dmsa,
"\xe2\x80\x99",
'\'');
41 replace(dmsa,
"\xe2\x80\xb3",
'"');
42 replace(dmsa,
"\xe2\x80\x9d",
'"');
43 replace(dmsa,
"\xc2\xa0",
'\0');
44 replace(dmsa,
"\xa0",
'\0');
45 replace(dmsa,
"\xe2\x80\xaf",
'\0');
46 replace(dmsa,
"\xe2\x80\x87",
'\0');
47 replace(dmsa,
"''",
'"');
50 end = unsigned(dmsa.size());
51 while (beg < end && isspace(dmsa[beg]))
53 while (beg < end && isspace(dmsa[end - 1]))
60 for (string::size_type p = beg, pb; p < end; p = pb, ++i) {
61 string::size_type pa = p;
69 pb = min(dmsa.find_first_of(signs_, pa), end);
71 v += InternalDecode(dmsa.substr(p, pb - p), ind2);
74 else if (!(ind2 == NONE || ind1 == ind2))
76 dmsa.substr(beg, pb - beg));
80 dmsa.substr(beg, end - beg));
85 Math::real DMS::InternalDecode(
const std::string& dmsa, flag& ind) {
91 end = unsigned(dmsa.size());
95 ind1 = (k / 2) ? LONGITUDE : LATITUDE;
96 sign = k % 2 ? 1 : -1;
99 if (end > beg && (k =
Utility::lookup(hemispheres_, dmsa[end-1])) >= 0) {
102 if (toupper(dmsa[beg - 1]) == toupper(dmsa[end - 1]))
103 errormsg =
"Repeated hemisphere indicators "
105 +
" in " + dmsa.substr(beg - 1, end - beg + 1);
107 errormsg =
"Contradictory hemisphere indicators "
110 + dmsa.substr(beg - 1, end - beg + 1);
113 ind1 = (k / 2) ? LONGITUDE : LATITUDE;
114 sign = k % 2 ? 1 : -1;
125 errormsg =
"Empty or incomplete DMS string " + dmsa;
128 real ipieces[] = {0, 0, 0};
129 real fpieces[] = {0, 0, 0};
133 unsigned ncurrent = 0, p = beg;
134 bool pointseen =
false;
135 unsigned digcount = 0, intcount = 0;
143 icurrent = 10 * icurrent + k;
146 }
else if (x ==
'.') {
148 errormsg =
"Multiple decimal points in "
149 + dmsa.substr(beg, end - beg);
157 errormsg =
"Illegal for : to appear at the end of " +
158 dmsa.substr(beg, end - beg);
163 if (
unsigned(k) == npiece - 1) {
164 errormsg =
"Repeated " + string(components_[k]) +
165 " component in " + dmsa.substr(beg, end - beg);
167 }
else if (
unsigned(k) < npiece) {
168 errormsg = string(components_[k]) +
" component follows "
169 + string(components_[npiece - 1]) +
" component in "
170 + dmsa.substr(beg, end - beg);
174 errormsg =
"Missing numbers in " + string(components_[k]) +
175 " component of " + dmsa.substr(beg, end - beg);
179 istringstream s(dmsa.substr(p - intcount - digcount - 1,
180 intcount + digcount));
184 ipieces[k] = icurrent;
185 fpieces[k] = icurrent + fcurrent;
188 icurrent = fcurrent = 0;
189 ncurrent = digcount = intcount = 0;
192 errormsg =
"Internal sign in DMS string "
193 + dmsa.substr(beg, end - beg);
196 errormsg =
"Illegal character " +
Utility::str(x) +
" in DMS string "
197 + dmsa.substr(beg, end - beg);
201 if (!errormsg.empty())
205 errormsg =
"Extra text following seconds in DMS string "
206 + dmsa.substr(beg, end - beg);
210 errormsg =
"Missing numbers in trailing component of "
211 + dmsa.substr(beg, end - beg);
215 istringstream s(dmsa.substr(p - intcount - digcount,
216 intcount + digcount));
220 ipieces[npiece] = icurrent;
221 fpieces[npiece] = icurrent + fcurrent;
223 if (pointseen && digcount == 0) {
224 errormsg =
"Decimal point in non-terminal component of "
225 + dmsa.substr(beg, end - beg);
229 if (ipieces[1] >= 60 || fpieces[1] > 60 ) {
231 +
" not in range [0, 60)";
234 if (ipieces[2] >= 60 || fpieces[2] > 60) {
236 +
" not in range [0, 60)";
244 (60*(60*fpieces[0] + fpieces[1]) + fpieces[2]) / 3600 :
246 (60*fpieces[0] + fpieces[1]) / 60 : fpieces[0] ) );
248 real val = Utility::nummatch<real>(dmsa);
250 throw GeographicErr(errormsg);
257 real& lat, real& lon,
261 a = Decode(stra, ia);
262 b = Decode(strb, ib);
263 if (ia == NONE && ib == NONE) {
265 ia = longfirst ? LONGITUDE : LATITUDE;
266 ib = longfirst ? LATITUDE : LONGITUDE;
267 }
else if (ia == NONE)
268 ia =
flag(LATITUDE + LONGITUDE - ib);
270 ib =
flag(LATITUDE + LONGITUDE - ia);
273 + strb +
" interpreted as "
274 + (ia == LATITUDE ?
"latitudes" :
"longitudes"));
276 lat1 = ia == LATITUDE ? a : b,
277 lon1 = ia == LATITUDE ? b : a;
280 +
"d not in [-90d, 90d]");
287 real ang = Decode(angstr, ind);
290 +
" includes a hemisphere, N/E/W/S");
296 real azi = Decode(azistr, ind);
299 +
" has a latitude hemisphere, N/S");
308 return angle < 0 ? string(
"-inf") :
309 (angle > 0 ? string(
"inf") : string(
"nan"));
315 for (
unsigned i = 0; i < unsigned(trailing); ++i)
317 for (
unsigned i = 0; i < prec; ++i)
320 angle -= floor(angle/360) * 360;
321 int sign = angle < 0 ? -1 : 1;
327 idegree = floor(angle),
328 fdegree = (angle - idegree) * scale +
real(0.5);
331 real f = floor(fdegree);
332 fdegree = (f == fdegree && fmod(f,
real(2)) == 1) ? f - 1 : f;
339 real pieces[3] = {fdegree, 0, 0};
340 for (
unsigned i = 1; i <= unsigned(trailing); ++i) {
342 ip = floor(pieces[i - 1]),
343 fp = pieces[i - 1] - ip;
347 pieces[0] += idegree;
349 s << fixed << setfill(
'0');
350 if (ind == NONE && sign < 0)
355 s << setw(1 + min(
int(ind), 2) + prec + (prec ? 1 : 0));
361 s << setw(1 + min(
int(ind), 2));
363 << (dmssep ? dmssep : char(tolower(dmsindicators_[0])));
366 s << setw(2 + prec + (prec ? 1 : 0)) <<
Utility::str(pieces[1], prec);
368 s << char(tolower(dmsindicators_[1]));
373 << (dmssep ? dmssep : char(tolower(dmsindicators_[1])))
374 << setw(2 + prec + (prec ? 1 : 0)) <<
Utility::str(pieces[2], prec);
376 s << char(tolower(dmsindicators_[2]));
382 if (ind != NONE && ind != AZIMUTH)
383 s << hemispheres_[(ind == LATITUDE ? 0 : 2) + (sign < 0 ? 0 : 1)];