GeographicLib: UTMUPS.cpp Source File

GeographicLib 2.7

Loading...
Searching...
No Matches
UTMUPS.cpp
Go to the documentation of this file.
1/**
2 * \file UTMUPS.cpp
3 * \brief Implementation for GeographicLib::UTMUPS 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/UTMUPS.hpp>
11#include <GeographicLib/MGRS.hpp>
12#include <GeographicLib/PolarStereographic.hpp>
13#include <GeographicLib/TransverseMercator.hpp>
14#include <GeographicLib/Utility.hpp>
15
16namespace GeographicLib {
17
18 using namespace std;
19
20 const int UTMUPS::falseeasting_[] =
21 { MGRS::upseasting_ * MGRS::tile_, MGRS::upseasting_ * MGRS::tile_,
22 MGRS::utmeasting_ * MGRS::tile_, MGRS::utmeasting_ * MGRS::tile_ };
23 const int UTMUPS::falsenorthing_[] =
24 { MGRS::upseasting_ * MGRS::tile_, MGRS::upseasting_ * MGRS::tile_,
25 MGRS::maxutmSrow_ * MGRS::tile_, MGRS::minutmNrow_ * MGRS::tile_ };
26 const int UTMUPS::mineasting_[] =
27 { MGRS::minupsSind_ * MGRS::tile_, MGRS::minupsNind_ * MGRS::tile_,
28 MGRS::minutmcol_ * MGRS::tile_, MGRS::minutmcol_ * MGRS::tile_ };
29 const int UTMUPS::maxeasting_[] =
30 { MGRS::maxupsSind_ * MGRS::tile_, MGRS::maxupsNind_ * MGRS::tile_,
31 MGRS::maxutmcol_ * MGRS::tile_, MGRS::maxutmcol_ * MGRS::tile_ };
32 const int UTMUPS::minnorthing_[] =
33 { MGRS::minupsSind_ * MGRS::tile_, MGRS::minupsNind_ * MGRS::tile_,
34 MGRS::minutmSrow_ * MGRS::tile_,
35 (MGRS::minutmNrow_ + MGRS::minutmSrow_ - MGRS::maxutmSrow_)
36 * MGRS::tile_ };
37 const int UTMUPS::maxnorthing_[] =
38 { MGRS::maxupsSind_ * MGRS::tile_, MGRS::maxupsNind_ * MGRS::tile_,
39 (MGRS::maxutmSrow_ + MGRS::maxutmNrow_ - MGRS::minutmNrow_) *
40 MGRS::tile_,
41 MGRS::maxutmNrow_ * MGRS::tile_ };
42
43 int UTMUPS::StandardZone(real lat, real lon, int setzone) {
44 if (!(setzone >= MINPSEUDOZONE && setzone <= MAXZONE))
45 throw GeographicErr("Illegal zone requested " + Utility::str(setzone));
46 if (setzone >= MINZONE || setzone == INVALID)
47 return setzone;
48 if (isnan(lat) || isnan(lon)) // Check if lat or lon is a NaN
49 return INVALID;
50 if (setzone == UTM || (lat >= -80 && lat < 84)) {
51 int ilon = int(floor(Math::AngNormalize(lon)));
52 if (ilon == Math::hd) ilon = -Math::hd; // ilon now in [-180,180)
53 int zone = (ilon + 186)/6;
54 int band = MGRS::LatitudeBand(lat);
55 if (band == 7 && zone == 31 && ilon >= 3) // The Norway exception
56 zone = 32;
57 else if (band == 9 && ilon >= 0 && ilon < 42) // The Svalbard exception
58 zone = 2 * ((ilon + 183)/12) + 1;
59 return zone;
60 } else
61 return UPS;
62 }
63
64 void UTMUPS::Forward(real lat, real lon,
65 int& zone, bool& northp, real& x, real& y,
66 real& gamma, real& k,
67 int setzone, bool mgrslimits) {
68 if (fabs(lat) > Math::qd)
69 throw GeographicErr("Latitude " + Utility::str(lat)
70 + "d not in [-" + to_string(Math::qd)
71 + "d, " + to_string(Math::qd) + "d]");
72 bool northp1 = !(signbit(lat));
73 int zone1 = StandardZone(lat, lon, setzone);
74 if (zone1 == INVALID) {
75 zone = zone1;
76 northp = northp1;
77 x = y = gamma = k = Math::NaN();
78 return;
79 }
80 real x1, y1, gamma1, k1;
81 bool utmp = zone1 != UPS;
82 if (utmp) {
83 real
84 lon0 = CentralMeridian(zone1),
85 dlon = Math::AngDiff(lon0, lon);
86 if (!(dlon <= 60))
87 // Check isn't really necessary because CheckCoords catches this case.
88 // But this allows a more meaningful error message to be given.
89 throw GeographicErr("Longitude " + Utility::str(lon)
90 + "d more than 60d from center of UTM zone "
91 + Utility::str(zone1));
92 TransverseMercator::UTM().Forward(lon0, lat, lon, x1, y1, gamma1, k1);
93 } else {
94 if (fabs(lat) < 70)
95 // Check isn't really necessary ... (see above).
96 throw GeographicErr("Latitude " + Utility::str(lat)
97 + "d more than 20d from "
98 + (northp1 ? "N" : "S") + " pole");
99 PolarStereographic::UPS().Forward(northp1, lat, lon, x1, y1, gamma1, k1);
100 }
101 int ind = (utmp ? 2 : 0) + (northp1 ? 1 : 0);
102 x1 += falseeasting_[ind];
103 y1 += falsenorthing_[ind];
104 if (! CheckCoords(zone1 != UPS, northp1, x1, y1, mgrslimits, false) )
105 throw GeographicErr("Latitude " + Utility::str(lat)
106 + ", longitude " + Utility::str(lon)
107 + " out of legal range for "
108 + (utmp ? "UTM zone " + Utility::str(zone1) :
109 "UPS"));
110 zone = zone1;
111 northp = northp1;
112 x = x1;
113 y = y1;
114 gamma = gamma1;
115 k = k1;
116 }
117
118 void UTMUPS::Reverse(int zone, bool northp, real x, real y,
119 real& lat, real& lon, real& gamma, real& k,
120 bool mgrslimits) {
121 if (zone == INVALID || isnan(x) || isnan(y)) {
122 lat = lon = gamma = k = Math::NaN();
123 return;
124 }
125 if (!(zone >= MINZONE && zone <= MAXZONE))
126 throw GeographicErr("Zone " + Utility::str(zone)
127 + " not in range [0, 60]");
128 bool utmp = zone != UPS;
129 CheckCoords(utmp, northp, x, y, mgrslimits);
130 int ind = (utmp ? 2 : 0) + (northp ? 1 : 0);
131 x -= falseeasting_[ind];
132 y -= falsenorthing_[ind];
133 if (utmp)
134 TransverseMercator::UTM().Reverse(CentralMeridian(zone),
135 x, y, lat, lon, gamma, k);
136 else
137 PolarStereographic::UPS().Reverse(northp, x, y, lat, lon, gamma, k);
138 }
139
140 bool UTMUPS::CheckCoords(bool utmp, bool northp, real x, real y,
141 bool mgrslimits, bool throwp) {
142 // Limits are all multiples of 100km and are all closed on the both ends.
143 // Failure tests are such that NaNs succeed.
144 real slop = mgrslimits ? 0 : MGRS::tile_;
145 int ind = (utmp ? 2 : 0) + (northp ? 1 : 0);
146 if (x < mineasting_[ind] - slop || x > maxeasting_[ind] + slop) {
147 if (!throwp) return false;
148 throw GeographicErr("Easting " + Utility::str(x/1000) + "km not in "
149 + (mgrslimits ? "MGRS/" : "")
150 + (utmp ? "UTM" : "UPS") + " range for "
151 + (northp ? "N" : "S" ) + " hemisphere ["
152 + Utility::str((mineasting_[ind] - slop)/1000)
153 + "km, "
154 + Utility::str((maxeasting_[ind] + slop)/1000)
155 + "km]");
156 }
157 if (y < minnorthing_[ind] - slop || y > maxnorthing_[ind] + slop) {
158 if (!throwp) return false;
159 throw GeographicErr("Northing " + Utility::str(y/1000) + "km not in "
160 + (mgrslimits ? "MGRS/" : "")
161 + (utmp ? "UTM" : "UPS") + " range for "
162 + (northp ? "N" : "S" ) + " hemisphere ["
163 + Utility::str((minnorthing_[ind] - slop)/1000)
164 + "km, "
165 + Utility::str((maxnorthing_[ind] + slop)/1000)
166 + "km]");
167 }
168 return true;
169 }
170
171 void UTMUPS::Transfer(int zonein, bool northpin, real xin, real yin,
172 int zoneout, bool northpout, real& xout, real& yout,
173 int& zone) {
174 bool northp = northpin;
175 if (zonein != zoneout) {
176 // Determine lat, lon
177 real lat, lon;
178 GeographicLib::UTMUPS::Reverse(zonein, northpin, xin, yin, lat, lon);
179 // Try converting to zoneout
180 real x, y;
181 int zone1;
182 GeographicLib::UTMUPS::Forward(lat, lon, zone1, northp, x, y,
183 zoneout == UTMUPS::MATCH
184 ? zonein : zoneout);
185 if (zone1 == 0 && northp != northpout)
186 throw GeographicErr
187 ("Attempt to transfer UPS coordinates between hemispheres");
188 zone = zone1;
189 xout = x;
190 yout = y;
191 } else {
192 if (zoneout == 0 && northp != northpout)
193 throw GeographicErr
194 ("Attempt to transfer UPS coordinates between hemispheres");
195 zone = zoneout;
196 xout = xin;
197 yout = yin;
198 }
199 if (northp != northpout)
200 // Can't get here if UPS
201 yout += (northpout ? -1 : 1) * MGRS::utmNshift_;
202 return;
203 }
204
205 void UTMUPS::DecodeZone(const string& zonestr, int& zone, bool& northp)
206 {
207 unsigned zlen = unsigned(zonestr.size());
208 if (zlen == 0)
209 throw GeographicErr("Empty zone specification");
210 // Longest zone spec is 32north, 42south, invalid = 7
211 if (zlen > 7)
212 throw GeographicErr("More than 7 characters in zone specification "
213 + zonestr);
214
215 const char* c = zonestr.c_str();
216 char* q;
217 int zone1 = strtol(c, &q, 10);
218 // if (zone1 == 0) zone1 = UPS; (not necessary)
219
220 if (zone1 == UPS) {
221 if (!(q == c))
222 // Don't allow 0n as an alternative to n for UPS coordinates
223 throw GeographicErr("Illegal zone 0 in " + zonestr +
224 ", use just the hemisphere for UPS");
225 } else if (!(zone1 >= MINUTMZONE && zone1 <= MAXUTMZONE))
226 throw GeographicErr("Zone " + Utility::str(zone1)
227 + " not in range [1, 60]");
228 else if (!isdigit(zonestr[0]))
229 throw GeographicErr("Must use unsigned number for zone "
230 + Utility::str(zone1));
231 else if (q - c > 2)
232 throw GeographicErr("More than 2 digits use to specify zone "
233 + Utility::str(zone1));
234
235 string hemi(zonestr, q - c);
236 for (string::iterator p = hemi.begin(); p != hemi.end(); ++p)
237 *p = char(tolower(*p));
238 if (q == c && (hemi == "inv" || hemi == "invalid")) {
239 zone = INVALID;
240 northp = false;
241 return;
242 }
243 bool northp1 = hemi == "north" || hemi == "n";
244 if (!(northp1 || hemi == "south" || hemi == "s"))
245 throw GeographicErr(string("Illegal hemisphere ") + hemi + " in "
246 + zonestr + ", specify north or south");
247 zone = zone1;
248 northp = northp1;
249 }
250
251 string UTMUPS::EncodeZone(int zone, bool northp, bool abbrev) {
252 if (zone == INVALID)
253 return string(abbrev ? "inv" : "invalid");
254 if (!(zone >= MINZONE && zone <= MAXZONE))
255 throw GeographicErr("Zone " + Utility::str(zone)
256 + " not in range [0, 60]");
257 ostringstream os;
258 if (zone != UPS)
259 os << setfill('0') << setw(2) << zone;
260 if (abbrev)
261 os << (northp ? 'n' : 's');
262 else
263 os << (northp ? "north" : "south");
264 return os.str();
265 }
266
267 void UTMUPS::DecodeEPSG(int epsg, int& zone, bool& northp) {
268 northp = false;
269 if (epsg >= epsg01N && epsg <= epsg60N) {
270 zone = (epsg - epsg01N) + MINUTMZONE;
271 northp = true;
272 } else if (epsg == epsgN) {
273 zone = UPS;
274 northp = true;
275 } else if (epsg >= epsg01S && epsg <= epsg60S) {
276 zone = (epsg - epsg01S) + MINUTMZONE;
277 } else if (epsg == epsgS) {
278 zone = UPS;
279 } else {
280 zone = INVALID;
281 }
282 }
283
284 int UTMUPS::EncodeEPSG(int zone, bool northp) {
285 int epsg = -1;
286 if (zone == UPS)
287 epsg = epsgS;
288 else if (zone >= MINUTMZONE && zone <= MAXUTMZONE)
289 epsg = (zone - MINUTMZONE) + epsg01S;
290 if (epsg >= 0 && northp)
291 epsg += epsgN - epsgS;
292 return epsg;
293 }
294
295 Math::real UTMUPS::UTMShift() { return real(MGRS::utmNshift_); }
296
297} // namespace GeographicLib
real
GeographicLib::Math::real real
Definition Geod3Solve.cpp:25
MGRS.hpp
Header for GeographicLib::MGRS class.
PolarStereographic.hpp
Header for GeographicLib::PolarStereographic class.
TransverseMercator.hpp
Header for GeographicLib::TransverseMercator class.
UTMUPS.hpp
Header for GeographicLib::UTMUPS class.
Utility.hpp
Header for GeographicLib::Utility class.
GeographicLib::GeographicErr
Exception handling for GeographicLib.
Definition Constants.hpp:344
GeographicLib::Math::qd
static constexpr int qd
degrees per quarter turn
Definition Math.hpp:142
GeographicLib::Math::AngNormalize
static T AngNormalize(T x)
Definition Math.cpp:69
GeographicLib::Math::NaN
static T NaN()
Definition Math.cpp:301
GeographicLib::Math::AngDiff
static T AngDiff(T x, T y, T &e)
Definition Math.cpp:80
GeographicLib::Math::hd
static constexpr int hd
degrees per half turn
Definition Math.hpp:145
GeographicLib::Math::real
double real
Definition Math.hpp:115
GeographicLib::PolarStereographic::Reverse
void Reverse(bool northp, real x, real y, real &lat, real &lon, real &gamma, real &k) const
Definition PolarStereographic.cpp:81
GeographicLib::PolarStereographic::UPS
static const PolarStereographic & UPS()
Definition PolarStereographic.cpp:33
GeographicLib::PolarStereographic::Forward
void Forward(bool northp, real lat, real lon, real &x, real &y, real &gamma, real &k) const
Definition PolarStereographic.cpp:61
GeographicLib::TransverseMercator::Reverse
void Reverse(real lon0, real x, real y, real &lat, real &lon, real &gamma, real &k) const
Definition TransverseMercator.cpp:523
GeographicLib::TransverseMercator::UTM
static const TransverseMercator & UTM()
Definition TransverseMercator.cpp:282
GeographicLib::TransverseMercator::Forward
void Forward(real lon0, real lat, real lon, real &x, real &y, real &gamma, real &k) const
Definition TransverseMercator.cpp:355
GeographicLib::UTMUPS::MINPSEUDOZONE
@ MINPSEUDOZONE
Definition UTMUPS.hpp:113
GeographicLib::UTMUPS::MAXZONE
@ MAXZONE
Definition UTMUPS.hpp:162
GeographicLib::UTMUPS::MINUTMZONE
@ MINUTMZONE
Definition UTMUPS.hpp:154
GeographicLib::UTMUPS::UPS
@ UPS
Definition UTMUPS.hpp:150
GeographicLib::UTMUPS::MATCH
@ MATCH
Definition UTMUPS.hpp:122
GeographicLib::UTMUPS::UTM
@ UTM
Definition UTMUPS.hpp:129
GeographicLib::UTMUPS::MAXUTMZONE
@ MAXUTMZONE
Definition UTMUPS.hpp:158
GeographicLib::UTMUPS::INVALID
@ INVALID
Definition UTMUPS.hpp:117
GeographicLib::UTMUPS::MINZONE
@ MINZONE
Definition UTMUPS.hpp:146
GeographicLib::UTMUPS::EncodeEPSG
static int EncodeEPSG(int zone, bool northp)
Definition UTMUPS.cpp:284
GeographicLib::UTMUPS::StandardZone
static int StandardZone(real lat, real lon, int setzone=STANDARD)
Definition UTMUPS.cpp:43
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::DecodeEPSG
static void DecodeEPSG(int epsg, int &zone, bool &northp)
Definition UTMUPS.cpp:267
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::str
static std::string str(T x, int p=-1)
Definition Utility.hpp:161
GeographicLib
Namespace for GeographicLib.
Definition Accumulator.cpp:12