UTMUPS.hpp 20.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
/**
 * \file UTMUPS.hpp
 * \brief Header for GeographicLib::UTMUPS class
 *
 * Copyright (c) Charles Karney (2008-2019) <charles@karney.com> and licensed
 * under the MIT/X11 License.  For more information, see
 * https://geographiclib.sourceforge.io/
 **********************************************************************/

#if !defined(GEOGRAPHICLIB_UTMUPS_HPP)
#define GEOGRAPHICLIB_UTMUPS_HPP 1

#include "Constants.hpp"

namespace GeographicLib {

  /**
   * \brief Convert between geographic coordinates and UTM/UPS
   *
   * UTM and UPS are defined
   * - J. W. Hager, J. F. Behensky, and B. W. Drew,
   *   <a href="http://earth-info.nga.mil/GandG/publications/tm8358.2/TM8358_2.pdf">
   *   The Universal Grids: Universal Transverse Mercator (UTM) and Universal
   *   Polar Stereographic (UPS)</a>, Defense Mapping Agency, Technical Manual
   *   TM8358.2 (1989).
   * .
   * Section 2-3 defines UTM and section 3-2.4 defines UPS.  This document also
   * includes approximate algorithms for the computation of the underlying
   * transverse Mercator and polar stereographic projections.  Here we
   * substitute much more accurate algorithms given by
   * GeographicLib:TransverseMercator and GeographicLib:PolarStereographic.
   * These are the algorithms recommended by the NGA document
   * - <a href="http://earth-info.nga.mil/GandG/publications/NGA_SIG_0012_2_0_0_UTMUPS/NGA.SIG.0012_2.0.0_UTMUPS.pdf">
   *   The Universal Grids and the Transverse Mercator and Polar Stereographic
   *   Map Projections</a>, NGA.SIG.0012_2.0.0_UTMUPS (2014).
   *
   * In this implementation, the conversions are closed, i.e., output from
   * Forward is legal input for Reverse and vice versa.  The error is about 5nm
   * in each direction.  However, the conversion from legal UTM/UPS coordinates
   * to geographic coordinates and back might throw an error if the initial
   * point is within 5nm of the edge of the allowed range for the UTM/UPS
   * coordinates.
   *
   * The simplest way to guarantee the closed property is to define allowed
   * ranges for the eastings and northings for UTM and UPS coordinates.  The
   * UTM boundaries are the same for all zones.  (The only place the
   * exceptional nature of the zone boundaries is evident is when converting to
   * UTM/UPS coordinates requesting the standard zone.)  The MGRS lettering
   * scheme imposes natural limits on UTM/UPS coordinates which may be
   * converted into MGRS coordinates.  For the conversion to/from geographic
   * coordinates these ranges have been extended by 100km in order to provide a
   * generous overlap between UTM and UPS and between UTM zones.
   *
   * The <a href="http://www.nga.mil">NGA</a> software package
   * <a href="http://earth-info.nga.mil/GandG/geotrans/index.html">geotrans</a>
   * also provides conversions to and from UTM and UPS.  Version 2.4.2 (and
   * earlier) suffers from some drawbacks:
   * - Inconsistent rules are used to determine the whether a particular UTM or
   *   UPS coordinate is legal.  A more systematic approach is taken here.
   * - The underlying projections are not very accurately implemented.
   *
   * The GeographicLib::UTMUPS::EncodeZone encodes the UTM zone and hemisphere
   * to allow UTM/UPS coordinated to be displayed as, for example, "38N 444500
   * 3688500".  According to NGA.SIG.0012_2.0.0_UTMUPS the use of "N" to denote
   * "north" in the context is not allowed (since a upper case letter in this
   * context denotes the MGRS latitude band).  Consequently, as of version
   * 1.36, EncodeZone uses the lower case letters "n" and "s" to denote the
   * hemisphere.  In addition EncodeZone accepts an optional final argument \e
   * abbrev, which, if false, results in the hemisphere being spelled out as in
   * "38north".
   *
   * Example of use:
   * \include example-UTMUPS.cpp
   **********************************************************************/
  class GEOGRAPHICLIB_EXPORT UTMUPS {
  private:
    typedef Math::real real;
    static const int falseeasting_[4];
    static const int falsenorthing_[4];
    static const int mineasting_[4];
    static const int maxeasting_[4];
    static const int minnorthing_[4];
    static const int maxnorthing_[4];
    static const int epsg01N = 32601; // EPSG code for UTM 01N
    static const int epsg60N = 32660; // EPSG code for UTM 60N
    static const int epsgN   = 32661; // EPSG code for UPS   N
    static const int epsg01S = 32701; // EPSG code for UTM 01S
    static const int epsg60S = 32760; // EPSG code for UTM 60S
    static const int epsgS   = 32761; // EPSG code for UPS   S
    static real CentralMeridian(int zone)
    { return real(6 * zone - 183); }
    // Throw an error if easting or northing are outside standard ranges.  If
    // throwp = false, return bool instead.
    static bool CheckCoords(bool utmp, bool northp, real x, real y,
                            bool msgrlimits = false, bool throwp = true);
    UTMUPS();                   // Disable constructor

  public:

    /**
     * In this class we bring together the UTM and UPS coordinates systems.
     * The UTM divides the earth between latitudes &minus;80&deg; and 84&deg;
     * into 60 zones numbered 1 thru 60.  Zone assign zone number 0 to the UPS
     * regions, covering the two poles.  Within UTMUPS, non-negative zone
     * numbers refer to one of the "physical" zones, 0 for UPS and [1, 60] for
     * UTM.  Negative "pseudo-zone" numbers are used to select one of the
     * physical zones.
     **********************************************************************/
    enum zonespec {
      /**
       * The smallest pseudo-zone number.
       **********************************************************************/
      MINPSEUDOZONE = -4,
      /**
       * A marker for an undefined or invalid zone.  Equivalent to NaN.
       **********************************************************************/
      INVALID = -4,
      /**
       * If a coordinate already include zone information (e.g., it is an MGRS
       * coordinate), use that, otherwise apply the UTMUPS::STANDARD rules.
       **********************************************************************/
      MATCH = -3,
      /**
       * Apply the standard rules for UTM zone assigment extending the UTM zone
       * to each pole to give a zone number in [1, 60].  For example, use UTM
       * zone 38 for longitude in [42&deg;, 48&deg;).  The rules include the
       * Norway and Svalbard exceptions.
       **********************************************************************/
      UTM = -2,
      /**
       * Apply the standard rules for zone assignment to give a zone number in
       * [0, 60].  If the latitude is not in [&minus;80&deg;, 84&deg;), then
       * use UTMUPS::UPS = 0, otherwise apply the rules for UTMUPS::UTM.  The
       * tests on latitudes and longitudes are all closed on the lower end open
       * on the upper.  Thus for UTM zone 38, latitude is in [&minus;80&deg;,
       * 84&deg;) and longitude is in [42&deg;, 48&deg;).
       **********************************************************************/
      STANDARD = -1,
      /**
       * The largest pseudo-zone number.
       **********************************************************************/
      MAXPSEUDOZONE = -1,
      /**
       * The smallest physical zone number.
       **********************************************************************/
      MINZONE = 0,
      /**
       * The zone number used for UPS
       **********************************************************************/
      UPS = 0,
      /**
       * The smallest UTM zone number.
       **********************************************************************/
      MINUTMZONE = 1,
      /**
       * The largest UTM zone number.
       **********************************************************************/
      MAXUTMZONE = 60,
      /**
       * The largest physical zone number.
       **********************************************************************/
      MAXZONE = 60,
    };

    /**
     * The standard zone.
     *
     * @param[in] lat latitude (degrees).
     * @param[in] lon longitude (degrees).
     * @param[in] setzone zone override (optional).  If omitted, use the
     *   standard rules for picking the zone.  If \e setzone is given then use
     *   that zone if it is non-negative, otherwise apply the rules given in
     *   UTMUPS::zonespec.
     * @exception GeographicErr if \e setzone is outside the range
     *   [UTMUPS::MINPSEUDOZONE, UTMUPS::MAXZONE] = [&minus;4, 60].
     *
     * This is exact.
     **********************************************************************/
    static int StandardZone(real lat, real lon, int setzone = STANDARD);

    /**
     * Forward projection, from geographic to UTM/UPS.
     *
     * @param[in] lat latitude of point (degrees).
     * @param[in] lon longitude of point (degrees).
     * @param[out] zone the UTM zone (zero means UPS).
     * @param[out] northp hemisphere (true means north, false means south).
     * @param[out] x easting of point (meters).
     * @param[out] y northing of point (meters).
     * @param[out] gamma meridian convergence at point (degrees).
     * @param[out] k scale of projection at point.
     * @param[in] setzone zone override (optional).
     * @param[in] mgrslimits if true enforce the stricter MGRS limits on the
     *   coordinates (default = false).
     * @exception GeographicErr if \e lat is not in [&minus;90&deg;,
     *   90&deg;].
     * @exception GeographicErr if the resulting \e x or \e y is out of allowed
     *   range (see Reverse); in this case, these arguments are unchanged.
     *
     * If \e setzone is omitted, use the standard rules for picking the zone.
     * If \e setzone is given then use that zone if it is non-negative,
     * otherwise apply the rules given in UTMUPS::zonespec.  The accuracy of
     * the conversion is about 5nm.
     *
     * The northing \e y jumps by UTMUPS::UTMShift() when crossing the equator
     * in the southerly direction.  Sometimes it is useful to remove this
     * discontinuity in \e y by extending the "northern" hemisphere using
     * UTMUPS::Transfer:
     * \code
     double lat = -1, lon = 123;
     int zone;
     bool northp;
     double x, y, gamma, k;
     GeographicLib::UTMUPS::Forward(lat, lon, zone, northp, x, y, gamma, k);
     GeographicLib::UTMUPS::Transfer(zone, northp, x, y,
                                     zone, true,   x, y, zone);
     northp = true;
     \endcode
     **********************************************************************/
    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);

    /**
     * Reverse projection, from  UTM/UPS to geographic.
     *
     * @param[in] zone the UTM zone (zero means UPS).
     * @param[in] northp hemisphere (true means north, false means south).
     * @param[in] x easting of point (meters).
     * @param[in] y northing of point (meters).
     * @param[out] lat latitude of point (degrees).
     * @param[out] lon longitude of point (degrees).
     * @param[out] gamma meridian convergence at point (degrees).
     * @param[out] k scale of projection at point.
     * @param[in] mgrslimits if true enforce the stricter MGRS limits on the
     *   coordinates (default = false).
     * @exception GeographicErr if \e zone, \e x, or \e y is out of allowed
     *   range; this this case the arguments are unchanged.
     *
     * The accuracy of the conversion is about 5nm.
     *
     * UTM eastings are allowed to be in the range [0km, 1000km], northings are
     * allowed to be in in [0km, 9600km] for the northern hemisphere and in
     * [900km, 10000km] for the southern hemisphere.  However UTM northings
     * can be continued across the equator.  So the actual limits on the
     * northings are [-9100km, 9600km] for the "northern" hemisphere and
     * [900km, 19600km] for the "southern" hemisphere.
     *
     * UPS eastings and northings are allowed to be in the range [1200km,
     * 2800km] in the northern hemisphere and in [700km, 3300km] in the
     * southern hemisphere.
     *
     * These ranges are 100km larger than allowed for the conversions to MGRS.
     * (100km is the maximum extra padding consistent with eastings remaining
     * non-negative.)  This allows generous overlaps between zones and UTM and
     * UPS.  If \e mgrslimits = true, then all the ranges are shrunk by 100km
     * so that they agree with the stricter MGRS ranges.  No checks are
     * performed besides these (e.g., to limit the distance outside the
     * standard zone boundaries).
     **********************************************************************/
    static void Reverse(int zone, bool northp, real x, real y,
                        real& lat, real& lon, real& gamma, real& k,
                        bool mgrslimits = false);

    /**
     * UTMUPS::Forward without returning convergence and scale.
     **********************************************************************/
    static void Forward(real lat, real lon,
                        int& zone, bool& northp, real& x, real& y,
                        int setzone = STANDARD, bool mgrslimits = false) {
      real gamma, k;
      Forward(lat, lon, zone, northp, x, y, gamma, k, setzone, mgrslimits);
    }

    /**
     * UTMUPS::Reverse without returning convergence and scale.
     **********************************************************************/
    static void Reverse(int zone, bool northp, real x, real y,
                        real& lat, real& lon, bool mgrslimits = false) {
      real gamma, k;
      Reverse(zone, northp, x, y, lat, lon, gamma, k, mgrslimits);
    }

    /**
     * Transfer UTM/UPS coordinated from one zone to another.
     *
     * @param[in] zonein the UTM zone for \e xin and \e yin (or zero for UPS).
     * @param[in] northpin hemisphere for \e xin and \e yin (true means north,
     *   false means south).
     * @param[in] xin easting of point (meters) in \e zonein.
     * @param[in] yin northing of point (meters) in \e zonein.
     * @param[in] zoneout the requested UTM zone for \e xout and \e yout (or
     *   zero for UPS).
     * @param[in] northpout hemisphere for \e xout output and \e yout.
     * @param[out] xout easting of point (meters) in \e zoneout.
     * @param[out] yout northing of point (meters) in \e zoneout.
     * @param[out] zone the actual UTM zone for \e xout and \e yout (or zero
     *   for UPS); this equals \e zoneout if \e zoneout &ge; 0.
     * @exception GeographicErr if \e zonein is out of range (see below).
     * @exception GeographicErr if \e zoneout is out of range (see below).
     * @exception GeographicErr if \e xin or \e yin fall outside their allowed
     *   ranges (see UTMUPS::Reverse).
     * @exception GeographicErr if \e xout or \e yout fall outside their
     *   allowed ranges (see UTMUPS::Reverse).
     *
     * \e zonein must be in the range [UTMUPS::MINZONE, UTMUPS::MAXZONE] = [0,
     * 60] with \e zonein = UTMUPS::UPS, 0, indicating UPS.  \e zonein may
     * also be UTMUPS::INVALID.
     *
     * \e zoneout must be in the range [UTMUPS::MINPSEUDOZONE, UTMUPS::MAXZONE]
     * = [-4, 60].  If \e zoneout &lt; UTMUPS::MINZONE then the rules give in
     * the documentation of UTMUPS::zonespec are applied, and \e zone is set to
     * the actual zone used for output.
     *
     * (\e xout, \e yout) can overlap with (\e xin, \e yin).
     **********************************************************************/
    static void Transfer(int zonein, bool northpin, real xin, real yin,
                         int zoneout, bool northpout, real& xout, real& yout,
                         int& zone);

    /**
     * Decode a UTM/UPS zone string.
     *
     * @param[in] zonestr string representation of zone and hemisphere.
     * @param[out] zone the UTM zone (zero means UPS).
     * @param[out] northp hemisphere (true means north, false means south).
     * @exception GeographicErr if \e zonestr is malformed.
     *
     * For UTM, \e zonestr has the form of a zone number in the range
     * [UTMUPS::MINUTMZONE, UTMUPS::MAXUTMZONE] = [1, 60] followed by a
     * hemisphere letter, n or s (or "north" or "south" spelled out).  For UPS,
     * it consists just of the hemisphere letter (or the spelled out
     * hemisphere).  The returned value of \e zone is UTMUPS::UPS = 0 for UPS.
     * Note well that "38s" indicates the southern hemisphere of zone 38 and
     * not latitude band S, 32&deg; &le; \e lat &lt; 40&deg;.  n, 01s, 2n, 38s,
     * south, 3north are legal.  0n, 001s, +3n, 61n, 38P are illegal.  INV is a
     * special value for which the returned value of \e is UTMUPS::INVALID.
     **********************************************************************/
    static void DecodeZone(const std::string& zonestr,
                           int& zone, bool& northp);

    /**
     * Encode a UTM/UPS zone string.
     *
     * @param[in] zone the UTM zone (zero means UPS).
     * @param[in] northp hemisphere (true means north, false means south).
     * @param[in] abbrev if true (the default) use abbreviated (n/s) notation
     *   for hemisphere; otherwise spell out the hemisphere (north/south)
     * @exception GeographicErr if \e zone is out of range (see below).
     * @exception std::bad_alloc if memoy for the string can't be allocated.
     * @return string representation of zone and hemisphere.
     *
     * \e zone must be in the range [UTMUPS::MINZONE, UTMUPS::MAXZONE] = [0,
     * 60] with \e zone = UTMUPS::UPS, 0, indicating UPS (but the resulting
     * string does not contain "0").  \e zone may also be UTMUPS::INVALID, in
     * which case the returned string is "inv".  This reverses
     * UTMUPS::DecodeZone.
     **********************************************************************/
    static std::string EncodeZone(int zone, bool northp, bool abbrev = true);

    /**
     * Decode EPSG.
     *
     * @param[in] epsg the EPSG code.
     * @param[out] zone the UTM zone (zero means UPS).
     * @param[out] northp hemisphere (true means north, false means south).
     *
     * EPSG (European Petroleum Survery Group) codes are a way to refer to many
     * different projections.  DecodeEPSG decodes those referring to UTM or UPS
     * projections for the WGS84 ellipsoid.  If the code does not refer to one
     * of these projections, \e zone is set to UTMUPS::INVALID.  See
     * https://www.spatialreference.org/ref/epsg/
     **********************************************************************/
    static void DecodeEPSG(int epsg, int& zone, bool& northp);

    /**
     * Encode zone as EPSG.
     *
     * @param[in] zone the UTM zone (zero means UPS).
     * @param[in] northp hemisphere (true means north, false means south).
     * @return EPSG code (or -1 if \e zone is not in the range
     *   [UTMUPS::MINZONE, UTMUPS::MAXZONE] = [0, 60])
     *
     * Convert \e zone and \e northp to the corresponding EPSG (European
     * Petroleum Survery Group) codes
     **********************************************************************/
    static int EncodeEPSG(int zone, bool northp);

    /**
     * @return shift (meters) necessary to align north and south halves of a
     * UTM zone (10<sup>7</sup>).
     **********************************************************************/
    static Math::real UTMShift();

    /** \name Inspector functions
     **********************************************************************/
    ///@{
    /**
     * @return \e a the equatorial radius of the WGS84 ellipsoid (meters).
     *
     * (The WGS84 value is returned because the UTM and UPS projections are
     * based on this ellipsoid.)
     **********************************************************************/
    static Math::real EquatorialRadius()
    { return Constants::WGS84_a(); }

    /**
     * @return \e f the flattening of the WGS84 ellipsoid.
     *
     * (The WGS84 value is returned because the UTM and UPS projections are
     * based on this ellipsoid.)
     **********************************************************************/
    static Math::real Flattening()
    { return Constants::WGS84_f(); }

    /**
      * \deprecated An old name for EquatorialRadius().
      **********************************************************************/
    // GEOGRAPHICLIB_DEPRECATED("Use EquatorialRadius()")
    static Math::real MajorRadius() { return EquatorialRadius(); }
    ///@}

  };

} // namespace GeographicLib

#endif  // GEOGRAPHICLIB_UTMUPS_HPP