GeographicLib: GeoCoords.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
14
16
17 using namespace std;
18
19 void GeoCoords::Reset(const std::string& s, bool centerp, bool longfirst) {
20 vector sa;
21 const char* spaces = " \t\n\v\f\r,";
22 for (string::size_type pos0 = 0, pos1; pos0 != string::npos;) {
23 pos1 = s.find_first_not_of(spaces, pos0);
24 if (pos1 == string::npos)
25 break;
26 pos0 = s.find_first_of(spaces, pos1);
27 sa.push_back(s.substr(pos1, pos0 == string::npos ? pos0 : pos0 - pos1));
28 }
29 if (sa.size() == 1) {
30 int prec;
31 MGRS::Reverse(sa[0], _zone, _northp, _easting, _northing, prec, centerp);
33 _lat, _long, _gamma, _k);
34 } else if (sa.size() == 2) {
37 _zone, _northp, _easting, _northing, _gamma, _k);
38 } else if (sa.size() == 3) {
39 unsigned zoneind, coordind;
40 if (sa[0].size() > 0 && isalpha(sa[0][sa[0].size() - 1])) {
41 zoneind = 0;
42 coordind = 1;
43 } else if (sa[2].size() > 0 && isalpha(sa[2][sa[2].size() - 1])) {
44 zoneind = 2;
45 coordind = 0;
46 } else
47 throw GeographicErr("Neither " + sa[0] + " nor " + sa[2]
48 + " of the form UTM/UPS Zone + Hemisphere"
49 + " (ex: 38n, 09s, n)");
51 for (unsigned i = 0; i < 2; ++i)
52 (i ? _northing : _easting) = Utility::val(sa[coordind + i]);
54 _lat, _long, _gamma, _k);
55 FixHemisphere();
56 } else
57 throw GeographicErr("Coordinate requires 1, 2, or 3 elements");
58 CopyToAlt();
59 }
60
62 using std::isnan;
64 return Utility::str(longfirst ? _long : _lat, prec) +
65 " " + Utility::str(longfirst ? _lat : _long, prec);
66 }
67
69 char dmssep) const {
71 return DMS::Encode(longfirst ? _long : _lat, unsigned(prec),
73 " " + DMS::Encode(longfirst ? _lat : _long, unsigned(prec),
75 }
76
78
79 prec = max(-1, min(6, prec) + 5);
80 string mgrs;
81 MGRS::Forward(_zone, _northp, _easting, _northing, _lat, prec, mgrs);
82 return mgrs;
83 }
84
86
87 prec = max(-1, min(6, prec) + 5);
88 string mgrs;
89 MGRS::Forward(_alt_zone, _northp, _alt_easting, _alt_northing, _lat, prec,
90 mgrs);
91 return mgrs;
92 }
93
94 void GeoCoords::UTMUPSString(int zone, bool northp,
95 real easting, real northing, int prec,
96 bool abbrev, string& utm) {
97 ostringstream os;
99
100 real scale = prec < 0 ? real(pow(real(10), -prec)) : real(1);
101 os << UTMUPS::EncodeZone(zone, northp, abbrev) << fixed << setfill('0');
102 if (isfinite(easting)) {
103 os << " " << Utility::str(easting / scale, max(0, prec));
104 if (prec < 0 && fabs(easting / scale) > real(0.5))
105 os << setw(-prec) << 0;
106 } else
107 os << " nan";
108 if (isfinite(northing)) {
109 os << " " << Utility::str(northing / scale, max(0, prec));
110 if (prec < 0 && fabs(northing / scale) > real(0.5))
111 os << setw(-prec) << 0;
112 } else
113 os << " nan";
114 utm = os.str();
115 }
116
118 string utm;
119 UTMUPSString(_zone, _northp, _easting, _northing, prec, abbrev, utm);
120 return utm;
121 }
122
124 bool abbrev) const {
125 real e, n;
126 int z;
128 _zone, northp, e, n, z);
129 string utm;
130 UTMUPSString(_zone, northp, e, n, prec, abbrev, utm);
131 return utm;
132 }
133
135 string utm;
136 UTMUPSString(_alt_zone, _northp, _alt_easting, _alt_northing, prec,
137 abbrev, utm);
138 return utm;
139 }
140
142 bool abbrev) const {
143 real e, n;
144 int z;
145 UTMUPS::Transfer(_alt_zone, _northp, _alt_easting, _alt_northing,
146 _alt_zone, northp, e, n, z);
147 string utm;
148 UTMUPSString(_alt_zone, northp, e, n, prec, abbrev, utm);
149 return utm;
150 }
151
152 void GeoCoords::FixHemisphere() {
153 using std::isnan;
154 if (_lat == 0 || (_northp && _lat >= 0) || (!_northp && _lat < 0) ||
155 isnan(_lat))
156
157 return;
160 _northp = !_northp;
161 } else
162 throw GeographicErr("Hemisphere mixup");
163 }
164
165}
Header for GeographicLib::DMS class.
Header for GeographicLib::GeoCoords class.
GeographicLib::Math::real real
Header for GeographicLib::MGRS class.
Header for GeographicLib::Utility class.
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)
std::string DMSRepresentation(int prec=0, bool longfirst=false, char dmssep=char(0)) const
Definition GeoCoords.cpp:68
void Reset(const std::string &s, bool centerp=true, bool longfirst=false)
Definition GeoCoords.cpp:19
std::string AltMGRSRepresentation(int prec=0) const
Definition GeoCoords.cpp:85
std::string UTMUPSRepresentation(int prec=0, bool abbrev=true) const
Definition GeoCoords.cpp:117
std::string GeoRepresentation(int prec=0, bool longfirst=false) const
Definition GeoCoords.cpp:61
std::string AltUTMUPSRepresentation(int prec=0, bool abbrev=true) const
Definition GeoCoords.cpp:134
std::string MGRSRepresentation(int prec=0) const
Definition GeoCoords.cpp:77
Exception handling for GeographicLib.
static void Reverse(const std::string &mgrs, int &zone, bool &northp, real &x, real &y, int &prec, bool centerp=true)
static void Forward(int zone, bool northp, real x, real y, int prec, std::string &mgrs)
static int extra_digits()
static std::string EncodeZone(int zone, bool northp, bool abbrev=true)
static void Forward(real lat, real lon, int &zone, bool &northp, real &x, real &y, real &gamma, real &k, int setzone=STANDARD, bool mgrslimits=false)
static void Reverse(int zone, bool northp, real x, real y, real &lat, real &lon, real &gamma, real &k, bool mgrslimits=false)
static Math::real UTMShift()
static void Transfer(int zonein, bool northpin, real xin, real yin, int zoneout, bool northpout, real &xout, real &yout, int &zone)
static void DecodeZone(const std::string &zonestr, int &zone, bool &northp)
static std::string str(T x, int p=-1)
Namespace for GeographicLib.