GeographicLib 2.7

Loading...
Searching...
No Matches
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-2022) <karney@alum.mit.edu> and licensed
6 * under the MIT/X11 License. For more information, see
7 * https://geographiclib.sourceforge.io/
8 **********************************************************************/
9
10#include <GeographicLib/GeoCoords.hpp>
11#include <GeographicLib/MGRS.hpp>
12#include <GeographicLib/DMS.hpp>
13#include <GeographicLib/Utility.hpp>
14
15 namespace GeographicLib {
16
17 using namespace std;
18
19 void GeoCoords::Reset(const std::string& s, bool centerp, bool longfirst) {
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, longfirst);
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) = Utility::val<real>(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 longfirst) const {
62 prec = max(0, min(9 + Math::extra_digits(), prec) + 5);
63 return Utility::str(longfirst ? _long : _lat, prec) +
64 " " + Utility::str(longfirst ? _lat : _long, prec);
65 }
66
67 string GeoCoords::DMSRepresentation(int prec, bool longfirst,
68 char dmssep) const {
69 prec = max(0, min(10 + Math::extra_digits(), prec) + 5);
70 return DMS::Encode(longfirst ? _long : _lat, unsigned(prec),
71 longfirst ? DMS::LONGITUDE : DMS::LATITUDE, dmssep) +
72 " " + DMS::Encode(longfirst ? _lat : _long, unsigned(prec),
73 longfirst ? DMS::LATITUDE : DMS::LONGITUDE, dmssep);
74 }
75
76 string GeoCoords::MGRSRepresentation(int prec) const {
77 // Max precision is um
78 prec = max(-1, min(6, prec) + 5);
79 string mgrs;
80 MGRS::Forward(_zone, _northp, _easting, _northing, _lat, prec, mgrs);
81 return mgrs;
82 }
83
84 string GeoCoords::AltMGRSRepresentation(int prec) const {
85 // Max precision is um
86 prec = max(-1, min(6, prec) + 5);
87 string mgrs;
88 MGRS::Forward(_alt_zone, _northp, _alt_easting, _alt_northing, _lat, prec,
89 mgrs);
90 return mgrs;
91 }
92
93 void GeoCoords::UTMUPSString(int zone, bool northp,
94 real easting, real northing, int prec,
95 bool abbrev, string& utm) {
96 ostringstream os;
97 prec = max(-5, min(9 + Math::extra_digits(), prec));
98 // Need extra real because, since C++11, pow(float, int) returns double
99 real scale = prec < 0 ? real(pow(real(10), -prec)) : real(1);
100 os << UTMUPS::EncodeZone(zone, northp, abbrev) << fixed << setfill('0');
101 if (isfinite(easting)) {
102 os << " " << Utility::str(easting / scale, max(0, prec));
103 if (prec < 0 && fabs(easting / scale) > real(0.5))
104 os << setw(-prec) << 0;
105 } else
106 os << " nan";
107 if (isfinite(northing)) {
108 os << " " << Utility::str(northing / scale, max(0, prec));
109 if (prec < 0 && fabs(northing / scale) > real(0.5))
110 os << setw(-prec) << 0;
111 } else
112 os << " nan";
113 utm = os.str();
114 }
115
116 string GeoCoords::UTMUPSRepresentation(int prec, bool abbrev) const {
117 string utm;
118 UTMUPSString(_zone, _northp, _easting, _northing, prec, abbrev, utm);
119 return utm;
120 }
121
122 string GeoCoords::UTMUPSRepresentation(bool northp, int prec,
123 bool abbrev) const {
124 real e, n;
125 int z;
126 UTMUPS::Transfer(_zone, _northp, _easting, _northing,
127 _zone, northp, e, n, z);
128 string utm;
129 UTMUPSString(_zone, northp, e, n, prec, abbrev, utm);
130 return utm;
131 }
132
133 string GeoCoords::AltUTMUPSRepresentation(int prec, bool abbrev) const {
134 string utm;
135 UTMUPSString(_alt_zone, _northp, _alt_easting, _alt_northing, prec,
136 abbrev, utm);
137 return utm;
138 }
139
140 string GeoCoords::AltUTMUPSRepresentation(bool northp, int prec,
141 bool abbrev) const {
142 real e, n;
143 int z;
144 UTMUPS::Transfer(_alt_zone, _northp, _alt_easting, _alt_northing,
145 _alt_zone, northp, e, n, z);
146 string utm;
147 UTMUPSString(_alt_zone, northp, e, n, prec, abbrev, utm);
148 return utm;
149 }
150
151 void GeoCoords::FixHemisphere() {
152 if (_lat == 0 || (_northp && _lat >= 0) || (!_northp && _lat < 0) ||
153 isnan(_lat))
154 // Allow either hemisphere for equator
155 return;
156 if (_zone != UTMUPS::UPS) {
157 _northing += (_northp ? 1 : -1) * UTMUPS::UTMShift();
158 _northp = !_northp;
159 } else
160 throw GeographicErr("Hemisphere mixup");
161 }
162
163} // namespace GeographicLib
DMS.hpp
Header for GeographicLib::DMS class.
GeoCoords.hpp
Header for GeographicLib::GeoCoords class.
real
GeographicLib::Math::real real
Definition Geod3Solve.cpp:25
MGRS.hpp
Header for GeographicLib::MGRS class.
Utility.hpp
Header for GeographicLib::Utility class.
GeographicLib::DMS::LONGITUDE
@ LONGITUDE
Definition DMS.hpp:56
GeographicLib::DMS::LATITUDE
@ LATITUDE
Definition DMS.hpp:51
GeographicLib::DMS::Encode
static std::string Encode(real angle, component trailing, unsigned prec, flag ind=NONE, char dmssep=char(0))
Definition DMS.cpp:422
GeographicLib::DMS::DecodeLatLon
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool longfirst=false)
Definition DMS.cpp:374
GeographicLib::GeoCoords::DMSRepresentation
std::string DMSRepresentation(int prec=0, bool longfirst=false, char dmssep=char(0)) const
Definition GeoCoords.cpp:67
GeographicLib::GeoCoords::Reset
void Reset(const std::string &s, bool centerp=true, bool longfirst=false)
Definition GeoCoords.cpp:19
GeographicLib::GeoCoords::AltMGRSRepresentation
std::string AltMGRSRepresentation(int prec=0) const
Definition GeoCoords.cpp:84
GeographicLib::GeoCoords::UTMUPSRepresentation
std::string UTMUPSRepresentation(int prec=0, bool abbrev=true) const
Definition GeoCoords.cpp:116
GeographicLib::GeoCoords::GeoRepresentation
std::string GeoRepresentation(int prec=0, bool longfirst=false) const
Definition GeoCoords.cpp:61
GeographicLib::GeoCoords::AltUTMUPSRepresentation
std::string AltUTMUPSRepresentation(int prec=0, bool abbrev=true) const
Definition GeoCoords.cpp:133
GeographicLib::GeoCoords::MGRSRepresentation
std::string MGRSRepresentation(int prec=0) const
Definition GeoCoords.cpp:76
GeographicLib::GeographicErr
Exception handling for GeographicLib.
Definition Constants.hpp:344
GeographicLib::MGRS::Reverse
static void Reverse(const std::string &mgrs, int &zone, bool &northp, real &x, real &y, int &prec, bool centerp=true)
Definition MGRS.cpp:158
GeographicLib::MGRS::Forward
static void Forward(int zone, bool northp, real x, real y, int prec, std::string &mgrs)
Definition MGRS.cpp:123
GeographicLib::Math::extra_digits
static int extra_digits()
Definition Math.cpp:49
GeographicLib::UTMUPS::UPS
@ UPS
Definition UTMUPS.hpp:150
GeographicLib::UTMUPS::EncodeZone
static std::string EncodeZone(int zone, bool northp, bool abbrev=true)
Definition UTMUPS.cpp:251
GeographicLib::UTMUPS::Forward
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:64
GeographicLib::UTMUPS::Reverse
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
GeographicLib::UTMUPS::UTMShift
static Math::real UTMShift()
Definition UTMUPS.cpp:295
GeographicLib::UTMUPS::Transfer
static void Transfer(int zonein, bool northpin, real xin, real yin, int zoneout, bool northpout, real &xout, real &yout, int &zone)
Definition UTMUPS.cpp:171
GeographicLib::UTMUPS::DecodeZone
static void DecodeZone(const std::string &zonestr, int &zone, bool &northp)
Definition UTMUPS.cpp:205
GeographicLib::Utility::val
static T val(const std::string &s)
Definition Utility.hpp:225
GeographicLib::Utility::str
static std::string str(T x, int p=-1)
Definition Utility.hpp:161
GeographicLib
Namespace for GeographicLib.
Definition Accumulator.cpp:12