16 namespace GeographicLib {
20 const int UTMUPS::falseeasting_[4] =
21 { MGRS::upseasting_ * MGRS::tile_, MGRS::upseasting_ * MGRS::tile_,
22 MGRS::utmeasting_ * MGRS::tile_, MGRS::utmeasting_ * MGRS::tile_ };
23 const int UTMUPS::falsenorthing_[4] =
24 { MGRS::upseasting_ * MGRS::tile_, MGRS::upseasting_ * MGRS::tile_,
25 MGRS::maxutmSrow_ * MGRS::tile_, MGRS::minutmNrow_ * MGRS::tile_ };
26 const int UTMUPS::mineasting_[4] =
27 { MGRS::minupsSind_ * MGRS::tile_, MGRS::minupsNind_ * MGRS::tile_,
28 MGRS::minutmcol_ * MGRS::tile_, MGRS::minutmcol_ * MGRS::tile_ };
29 const int UTMUPS::maxeasting_[4] =
30 { MGRS::maxupsSind_ * MGRS::tile_, MGRS::maxupsNind_ * MGRS::tile_,
31 MGRS::maxutmcol_ * MGRS::tile_, MGRS::maxutmcol_ * MGRS::tile_ };
32 const int UTMUPS::minnorthing_[4] =
33 { MGRS::minupsSind_ * MGRS::tile_, MGRS::minupsNind_ * MGRS::tile_,
34 MGRS::minutmSrow_ * MGRS::tile_,
35 (MGRS::minutmNrow_ + MGRS::minutmSrow_ - MGRS::maxutmSrow_)
37 const int UTMUPS::maxnorthing_[4] =
38 { MGRS::maxupsSind_ * MGRS::tile_, MGRS::maxupsNind_ * MGRS::tile_,
39 (MGRS::maxutmSrow_ + MGRS::maxutmNrow_ - MGRS::minutmNrow_) * MGRS::tile_,
40 MGRS::maxutmNrow_ * MGRS::tile_ };
43 if (!(setzone >= MINPSEUDOZONE && setzone <= MAXZONE))
45 if (setzone >= MINZONE || setzone == INVALID)
49 if (setzone == UTM || (lat >= -80 && lat < 84)) {
51 int ilon = int(floor(lon));
56 int zone = (ilon + 186)/6;
57 int band = MGRS::LatitudeBand(lat);
58 if (band == 7 && zone == 31 && ilon >= 3)
60 else if (band == 9 && ilon >= 0 && ilon < 42)
61 zone = 2 * ((ilon + 183)/12) + 1;
68 int& zone,
bool& northp, real& x, real& y,
70 int setzone,
bool mgrslimits) {
71 CheckLatLon(lat, lon);
72 bool northp1 = lat >= 0;
73 int zone1 = StandardZone(lat, lon, setzone);
74 if (zone1 == INVALID) {
80 real x1, y1, gamma1, k1;
81 bool utmp = zone1 != UPS;
84 lon0 = CentralMeridian(zone1),
86 dlon = abs(dlon - 360 * floor((dlon + 180)/360));
91 +
"d more than 60d from center of UTM zone "
98 +
"d more than 20d from "
99 + (northp1 ?
"N" :
"S") +
" pole");
102 int ind = (utmp ? 2 : 0) + (northp1 ? 1 : 0);
103 x1 += falseeasting_[ind];
104 y1 += falsenorthing_[ind];
105 if (! CheckCoords(zone1 != UPS, northp1, x1, y1, mgrslimits,
false) )
108 +
" out of legal range for "
119 real& lat, real& lon, real& gamma, real& k,
125 if (!(zone >= MINZONE && zone <= MAXZONE))
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];
135 x, y, lat, lon, gamma, k);
140 void UTMUPS::CheckLatLon(
real lat,
real lon) {
143 +
"d not in [-90d, 90d]");
144 if (lon < -540 || lon >= 540)
146 +
"d not in [-540d, 540d)");
149 bool UTMUPS::CheckCoords(
bool utmp,
bool northp,
real x,
real y,
150 bool mgrslimits,
bool throwp) {
153 real slop = mgrslimits ? 0 : MGRS::tile_;
154 int ind = (utmp ? 2 : 0) + (northp ? 1 : 0);
155 if (x < mineasting_[ind] - slop || x > maxeasting_[ind] + slop) {
156 if (!throwp)
return false;
157 throw GeographicErr(
"Easting " +
Utility::str(x/1000) +
"km not in "
158 + (mgrslimits ?
"MGRS/" :
"")
159 + (utmp ?
"UTM" :
"UPS") +
" range for "
160 + (northp ?
"N" :
"S" ) +
" hemisphere ["
166 if (y < minnorthing_[ind] - slop || y > maxnorthing_[ind] + slop) {
167 if (!throwp)
return false;
168 throw GeographicErr(
"Northing " +
Utility::str(y/1000) +
"km not in "
169 + (mgrslimits ?
"MGRS/" :
"")
170 + (utmp ?
"UTM" :
"UPS") +
" range for "
171 + (northp ?
"N" :
"S" ) +
" hemisphere ["
181 int zoneout,
bool northpout, real& xout, real& yout,
183 bool northp = northpin;
184 if (zonein != zoneout) {
194 if (zone1 == 0 && northp != northpout)
196 (
"Attempt to transfer UPS coordinates between hemispheres");
201 if (zoneout == 0 && northp != northpout)
203 (
"Attempt to transfer UPS coordinates between hemispheres");
208 if (northp != northpout)
210 yout += (northpout ? -1 : 1) * MGRS::utmNshift_;
215 unsigned zlen = unsigned(zonestr.size());
220 throw GeographicErr(
"More than 7 characters in zone specification "
223 const char* c = zonestr.c_str();
225 int zone1 = strtol(c, &q, 10);
232 ", use just the hemisphere for UPS");
233 }
else if (!(zone1 >= MINUTMZONE && zone1 <= MAXUTMZONE))
235 +
" not in range [1, 60]");
236 else if (!isdigit(zonestr[0]))
240 throw GeographicErr(
"More than 2 digits use to specify zone "
243 string hemi = zonestr.substr(q - c);
244 transform(hemi.begin(), hemi.end(), hemi.begin(), (int(*)(int))tolower);
245 if (q == c && (hemi ==
"inv" || hemi ==
"invalid")) {
250 bool northp1 = hemi ==
"north" || hemi ==
"n";
251 if (!(northp1 || hemi ==
"south" || hemi ==
"s"))
252 throw GeographicErr(
string(
"Illegal hemisphere ") + hemi +
" in "
253 + zonestr +
", specify north or south");
260 return string(abbrev ?
"inv" :
"invalid");
261 if (!(zone >= MINZONE && zone <= MAXZONE))
263 +
" not in range [0, 60]");
266 os << setfill(
'0') << setw(2) << zone;
268 os << (northp ?
'n' :
's');
270 os << (northp ?
"north" :
"south");
276 if (epsg >= epsg01N && epsg <= epsg60N) {
277 zone = (epsg - epsg01N) + MINUTMZONE;
279 }
else if (epsg == epsgN) {
282 }
else if (epsg >= epsg01S && epsg <= epsg60S) {
283 zone = (epsg - epsg01S) + MINUTMZONE;
284 }
else if (epsg == epsgS) {
295 else if (zone >= MINUTMZONE && zone <= MAXUTMZONE)
296 epsg = (zone - MINUTMZONE) + epsg01S;
297 if (epsg >= 0 && northp)
298 epsg += epsgN - epsgS;