GeographicLib: GeoCoords.cpp Source File

GeographicLib 2.5

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
15namespace 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 using std::isnan; // Needed for Centos 7, ubuntu 14
63 prec = max(0, min(9 + Math::extra_digits(), prec) + 5);
64 return Utility::str(longfirst ? _long : _lat, prec) +
65 " " + Utility::str(longfirst ? _lat : _long, prec);
66 }
67
68 string GeoCoords::DMSRepresentation(int prec, bool longfirst,
69 char dmssep) const {
70 prec = max(0, min(10 + Math::extra_digits(), prec) + 5);
71 return DMS::Encode(longfirst ? _long : _lat, unsigned(prec),
72 longfirst ? DMS::LONGITUDE : DMS::LATITUDE, dmssep) +
73 " " + DMS::Encode(longfirst ? _lat : _long, unsigned(prec),
74 longfirst ? DMS::LATITUDE : DMS::LONGITUDE, dmssep);
75 }
76
77 string GeoCoords::MGRSRepresentation(int prec) const {
78 // Max precision is um
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
85 string GeoCoords::AltMGRSRepresentation(int prec) const {
86 // Max precision is um
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;
98 prec = max(-5, min(9 + Math::extra_digits(), prec));
99 // Need extra real because, since C++11, pow(float, int) returns double
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
117 string GeoCoords::UTMUPSRepresentation(int prec, bool abbrev) const {
118 string utm;
119 UTMUPSString(_zone, _northp, _easting, _northing, prec, abbrev, utm);
120 return utm;
121 }
122
123 string GeoCoords::UTMUPSRepresentation(bool northp, int prec,
124 bool abbrev) const {
125 real e, n;
126 int z;
127 UTMUPS::Transfer(_zone, _northp, _easting, _northing,
128 _zone, northp, e, n, z);
129 string utm;
130 UTMUPSString(_zone, northp, e, n, prec, abbrev, utm);
131 return utm;
132 }
133
134 string GeoCoords::AltUTMUPSRepresentation(int prec, bool abbrev) const {
135 string utm;
136 UTMUPSString(_alt_zone, _northp, _alt_easting, _alt_northing, prec,
137 abbrev, utm);
138 return utm;
139 }
140
141 string GeoCoords::AltUTMUPSRepresentation(bool northp, int prec,
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; // Needed for Centos 7, ubuntu 14
154 if (_lat == 0 || (_northp && _lat >= 0) || (!_northp && _lat < 0) ||
155 isnan(_lat))
156 // Allow either hemisphere for equator
157 return;
158 if (_zone != UTMUPS::UPS) {
159 _northing += (_northp ? 1 : -1) * UTMUPS::UTMShift();
160 _northp = !_northp;
161 } else
162 throw GeographicErr("Hemisphere mixup");
163 }
164
165} // namespace GeographicLib
DMS.hpp
Header for GeographicLib::DMS class.
GeoCoords.hpp
Header for GeographicLib::GeoCoords class.
real
GeographicLib::Math::real real
Definition GeodSolve.cpp:28
MGRS.hpp
Header for GeographicLib::MGRS class.
Utility.hpp
Header for GeographicLib::Utility class.
GeographicLib::DMS::LONGITUDE
@ LONGITUDE
Definition DMS.hpp:50
GeographicLib::DMS::LATITUDE
@ LATITUDE
Definition DMS.hpp:45
GeographicLib::DMS::Encode
static std::string Encode(real angle, component trailing, unsigned prec, flag ind=NONE, char dmssep=char(0))
Definition DMS.cpp:411
GeographicLib::DMS::DecodeLatLon
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool longfirst=false)
Definition DMS.cpp:363
GeographicLib::GeoCoords::DMSRepresentation
std::string DMSRepresentation(int prec=0, bool longfirst=false, char dmssep=char(0)) const
Definition GeoCoords.cpp:68
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:85
GeographicLib::GeoCoords::UTMUPSRepresentation
std::string UTMUPSRepresentation(int prec=0, bool abbrev=true) const
Definition GeoCoords.cpp:117
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:134
GeographicLib::GeoCoords::MGRSRepresentation
std::string MGRSRepresentation(int prec=0) const
Definition GeoCoords.cpp:77
GeographicLib::GeographicErr
Exception handling for GeographicLib.
Definition Constants.hpp:316
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:159
GeographicLib::MGRS::Forward
static void Forward(int zone, bool northp, real x, real y, int prec, std::string &mgrs)
Definition MGRS.cpp:124
GeographicLib::Math::extra_digits
static int extra_digits()
Definition Math.cpp:46
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:253
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:65
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:119
GeographicLib::UTMUPS::UTMShift
static Math::real UTMShift()
Definition UTMUPS.cpp:297
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:173
GeographicLib::UTMUPS::DecodeZone
static void DecodeZone(const std::string &zonestr, int &zone, bool &northp)
Definition UTMUPS.cpp:207
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