GeographicLib  1.42
GeoCoords.cpp
Go to the documentation of this file.
1 /**
2  * \file GeoCoords.cpp
3  * \brief Implementation for GeographicLib::GeoCoords class
4  *
5  * Copyright (c) Charles Karney (2008-2011) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * http://geographiclib.sourceforge.net/
8  **********************************************************************/
9 
11 #include <GeographicLib/MGRS.hpp>
12 #include <GeographicLib/DMS.hpp>
14 
15 namespace GeographicLib {
16 
17  using namespace std;
18 
19  void GeoCoords::Reset(const std::string& s, bool centerp, bool swaplatlong) {
20  vector<string> sa;
21  const char* spaces = " \t\n\v\f\r,"; // Include comma as a space
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);
32  UTMUPS::Reverse(_zone, _northp, _easting, _northing,
33  _lat, _long, _gamma, _k);
34  } else if (sa.size() == 2) {
35  DMS::DecodeLatLon(sa[0], sa[1], _lat, _long, swaplatlong);
36  UTMUPS::Forward( _lat, _long,
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)");
50  UTMUPS::DecodeZone(sa[zoneind], _zone, _northp);
51  for (unsigned i = 0; i < 2; ++i)
52  (i ? _northing : _easting) = DMS::Decode(sa[coordind + i]);
53  UTMUPS::Reverse(_zone, _northp, _easting, _northing,
54  _lat, _long, _gamma, _k);
55  FixHemisphere();
56  } else
57  throw GeographicErr("Coordinate requires 1, 2, or 3 elements");
58  CopyToAlt();
59  }
60 
61  string GeoCoords::GeoRepresentation(int prec, bool swaplatlong) const {
62  prec = max(0, min(9 + Math::extra_digits(), prec) + 5);
63  ostringstream os;
64  os << fixed << setprecision(prec);
65  real a = swaplatlong ? _long : _lat;
66  real b = swaplatlong ? _lat : _long;
67  if (!Math::isnan(a))
68  os << a;
69  else
70  os << "nan";
71  os << " ";
72  if (!Math::isnan(b))
73  os << b;
74  else
75  os << "nan";
76  return os.str();
77  }
78 
79  string GeoCoords::DMSRepresentation(int prec, bool swaplatlong,
80  char dmssep) const {
81  prec = max(0, min(10 + Math::extra_digits(), prec) + 5);
82  return DMS::Encode(swaplatlong ? _long : _lat, unsigned(prec),
83  swaplatlong ? DMS::LONGITUDE : DMS::LATITUDE, dmssep) +
84  " " + DMS::Encode(swaplatlong ? _lat : _long, unsigned(prec),
85  swaplatlong ? DMS::LATITUDE : DMS::LONGITUDE, dmssep);
86  }
87 
88  string GeoCoords::MGRSRepresentation(int prec) const {
89  // Max precision is um
90  prec = max(-1, min(6, prec) + 5);
91  string mgrs;
92  MGRS::Forward(_zone, _northp, _easting, _northing, _lat, prec, mgrs);
93  return mgrs;
94  }
95 
96  string GeoCoords::AltMGRSRepresentation(int prec) const {
97  // Max precision is um
98  prec = max(-1, min(6, prec) + 5);
99  string mgrs;
100  MGRS::Forward(_alt_zone, _northp, _alt_easting, _alt_northing, _lat, prec,
101  mgrs);
102  return mgrs;
103  }
104 
105  void GeoCoords::UTMUPSString(int zone, bool northp,
106  real easting, real northing, int prec,
107  bool abbrev, std::string& utm) {
108  ostringstream os;
109  prec = max(-5, min(9 + Math::extra_digits(), prec));
110  real scale = prec < 0 ? pow(real(10), -prec) : real(1);
111  os << UTMUPS::EncodeZone(zone, northp, abbrev) << fixed << setfill('0');
112  if (Math::isfinite(easting)) {
113  os << " " << Utility::str(easting / scale, max(0, prec));
114  if (prec < 0 && abs(easting / scale) > real(0.5))
115  os << setw(-prec) << 0;
116  } else
117  os << " nan";
118  if (Math::isfinite(northing)) {
119  os << " " << Utility::str(northing / scale, max(0, prec));
120  if (prec < 0 && abs(northing / scale) > real(0.5))
121  os << setw(-prec) << 0;
122  } else
123  os << " nan";
124  utm = os.str();
125  }
126 
127  string GeoCoords::UTMUPSRepresentation(int prec, bool abbrev) const {
128  string utm;
129  UTMUPSString(_zone, _northp, _easting, _northing, prec, abbrev, utm);
130  return utm;
131  }
132 
133  string GeoCoords::UTMUPSRepresentation(bool northp, int prec, bool abbrev)
134  const {
135  real e, n;
136  int z;
137  UTMUPS::Transfer(_zone, _northp, _easting, _northing,
138  _zone, northp, e, n, z);
139  string utm;
140  UTMUPSString(_zone, northp, e, n, prec, abbrev, utm);
141  return utm;
142  }
143 
144  string GeoCoords::AltUTMUPSRepresentation(int prec, bool abbrev) const {
145  string utm;
146  UTMUPSString(_alt_zone, _northp, _alt_easting, _alt_northing, prec,
147  abbrev, utm);
148  return utm;
149  }
150 
151  string GeoCoords::AltUTMUPSRepresentation(bool northp, int prec, bool abbrev)
152  const {
153  real e, n;
154  int z;
155  UTMUPS::Transfer(_alt_zone, _northp, _alt_easting, _alt_northing,
156  _alt_zone, northp, e, n, z);
157  string utm;
158  UTMUPSString(_alt_zone, northp, e, n, prec, abbrev, utm);
159  return utm;
160  }
161 
162  void GeoCoords::FixHemisphere() {
163  if (_lat == 0 || (_northp && _lat >= 0) || (!_northp && _lat < 0) ||
164  Math::isnan(_lat))
165  // Allow either hemisphere for equator
166  return;
167  if (_zone != UTMUPS::UPS) {
168  _northing += (_northp ? 1 : -1) * UTMUPS::UTMShift();
169  _northp = !_northp;
170  } else
171  throw GeographicErr("Hemisphere mixup");
172  }
173 
174 } // namespace GeographicLib
static std::string EncodeZone(int zone, bool northp, bool abbrev=true)
Definition: UTMUPS.cpp:258
std::string GeoRepresentation(int prec=0, bool swaplatlong=false) const
Definition: GeoCoords.cpp:61
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
Header for GeographicLib::Utility class.
static bool isfinite(T x)
Definition: Math.hpp:596
static bool isnan(T x)
Definition: Math.hpp:628
static void Transfer(int zonein, bool northpin, real xin, real yin, int zoneout, bool northpout, real &xout, real &yout, int &zone)
Definition: UTMUPS.cpp:180
std::string AltUTMUPSRepresentation(int prec=0, bool abbrev=true) const
Definition: GeoCoords.cpp:144
std::string DMSRepresentation(int prec=0, bool swaplatlong=false, char dmssep=char(0)) const
Definition: GeoCoords.cpp:79
Header for GeographicLib::GeoCoords class.
Header for GeographicLib::MGRS class.
static int extra_digits()
Definition: Math.hpp:185
void Reset(const std::string &s, bool centerp=true, bool swaplatlong=false)
Definition: GeoCoords.cpp:19
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)
Definition: UTMUPS.cpp:67
static std::string Encode(real angle, component trailing, unsigned prec, flag ind=NONE, char dmssep=char(0))
Definition: DMS.cpp:298
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool swaplatlong=false)
Definition: DMS.cpp:246
static Math::real Decode(const std::string &dms, flag &ind)
Definition: DMS.cpp:28
std::string AltMGRSRepresentation(int prec=0) const
Definition: GeoCoords.cpp:96
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static void DecodeZone(const std::string &zonestr, int &zone, bool &northp)
Definition: UTMUPS.cpp:214
static std::string str(T x, int p=-1)
Definition: Utility.hpp:276
static void Reverse(int zone, bool northp, real x, real y, real &lat, real &lon, real &gamma, real &k, bool mgrslimits=false)
Definition: UTMUPS.cpp:118
static void Reverse(const std::string &mgrs, int &zone, bool &northp, real &x, real &y, int &prec, bool centerp=true)
Definition: MGRS.cpp:158
Exception handling for GeographicLib.
Definition: Constants.hpp:382
std::string MGRSRepresentation(int prec=0) const
Definition: GeoCoords.cpp:88
static Math::real UTMShift()
Definition: UTMUPS.cpp:302
static void Forward(int zone, bool northp, real x, real y, int prec, std::string &mgrs)
Definition: MGRS.cpp:123
std::string UTMUPSRepresentation(int prec=0, bool abbrev=true) const
Definition: GeoCoords.cpp:127
Header for GeographicLib::DMS class.