GeographicLib: GeographicLib::MGRS Class Reference

GeographicLib 2.6

Loading...
Searching...
No Matches
GeographicLib::MGRS Class Reference

Convert between UTM/UPS and MGRS. More...

`#include <GeographicLib/MGRS.hpp>`

## Static Public Member Functions
static void | Forward (int zone, bool northp, real x, real y, int prec, std::string &mgrs)
static void | Forward (int zone, bool northp, real x, real y, real lat, int prec, std::string &mgrs)
static void | Reverse (const std::string &mgrs, int &zone, bool &northp, real &x, real &y, int &prec, bool centerp=true)
static void | Decode (const std::string &mgrs, std::string &gridzone, std::string &block, std::string &easting, std::string &northing)
static void | Check ()
Inspector functions
static Math::real | EquatorialRadius ()
static Math::real | Flattening ()

## Friends
class | UTMUPS

## Detailed Description

Convert between UTM/UPS and MGRS.

MGRS is defined in Chapter 3 of

- J. W. Hager, L. L. Fry, S. S. Jacks, D. R. Hill, Datums, Ellipsoids, Grids, and Grid Reference Systems, Defense Mapping Agency, Technical Manual TM8358.1 (1990).

This document has been updated by the two NGA documents

- Universal Grids and Grid Reference Systems, NGA.STND.0037 (2014).
- The Universal Grids and the Transverse Mercator and Polar Stereographic Map Projections, NGA.SIG.0012 (2014).

This implementation has the following properties:

- The conversions are closed, i.e., output from Forward is legal input for Reverse and vice versa. Conversion in both directions preserve the UTM/UPS selection and the UTM zone.
- Forward followed by Reverse and vice versa is approximately the identity. (This is affected in predictable ways by errors in determining the latitude band and by loss of precision in the MGRS coordinates.)
- The trailing digits produced by Forward are consistent as the precision is varied. Specifically, the digits are obtained by operating on the easting with ⌊106 x⌋ and extracting the required digits from the resulting number (and similarly for the northing).
- All MGRS coordinates truncate to legal 100 km blocks. All MGRS coordinates with a legal 100 km block prefix are legal (even though the latitude band letter may now belong to a neighboring band).
- The range of UTM/UPS coordinates allowed for conversion to MGRS coordinates is the maximum consistent with staying within the letter ranges of the MGRS scheme.
- All the transformations are implemented as static methods in the MGRS class.

The NGA software package geotrans also provides conversions to and from MGRS. Version 3.0 (and earlier) suffers from some drawbacks:

- Inconsistent rules are used to determine the whether a particular MGRS coordinate is legal. A more systematic approach is taken here.
- The underlying projections are not very accurately implemented.

Example of use:

// Example of using the GeographicLib::MGRS class
#include <iostream>
#include <exception>
#include <string>
#include <GeographicLib/UTMUPS.hpp>
#include <GeographicLib/MGRS.hpp>
using namespace std;
using namespace GeographicLib;
int main() {
try {
// See also example-GeoCoords.cpp
{
// Sample forward calculation
double lat = 33.3, lon = 44.4; // Baghdad
int zone;
bool northp;
double x, y;
UTMUPS::Forward(lat, lon, zone, northp, x, y);
string mgrs;
MGRS::Forward(zone, northp, x, y, lat, 5, mgrs);
cout << mgrs << "\n";
}
{
// Sample reverse calculation
string mgrs = "38SMB4488";
int zone, prec;
bool northp;
double x, y;
MGRS::Reverse(mgrs, zone, northp, x, y, prec);
double lat, lon;
UTMUPS::Reverse(zone, northp, x, y, lat, lon);
cout << prec << " " << lat << " " << lon << "\n";
}
}
catch (const exception& e) {
cerr << "Caught exception: " << e.what() << "\n";
return 1;
}
}
main
int main(int argc, const char *const argv[])
Definition Cart3Convert.cpp:52
MGRS.hpp
Header for GeographicLib::MGRS class.
UTMUPS.hpp
Header for GeographicLib::UTMUPS class.
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::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
Namespace for GeographicLib.
Definition Accumulator.cpp:12
std
Definition NearestNeighbor.hpp:806

Definition at line 68 of file MGRS.hpp.

## Member Function Documentation

## ◆ Forward() [1/2]

void GeographicLib::MGRS::Forward | ( | int | zone,
|  | bool | northp,
|  | real | x,
|  | real | y,
|  | int | prec,
|  | std::string & | mgrs )
| static

Convert UTM or UPS coordinate to an MGRS coordinate.

Parameters

  [in] | zone | UTM zone (zero means UPS).
  [in] | northp | hemisphere (true means north, false means south).
  [in] | x | easting of point (meters).
  [in] | y | northing of point (meters).
  [in] | prec | precision relative to 100 km.
  [out] | mgrs | MGRS string.

Exceptions

  GeographicErr | if zone, x, or y is outside its allowed range.
  GeographicErr | if the memory for the MGRS string can't be allocated.

prec specifies the precision of the MGRS string as follows:

- prec = −1 (min), only the grid zone is returned
- prec = 0, 100 km
- prec = 1, 10 km
- prec = 2, 1 km
- prec = 3, 100 m
- prec = 4, 10 m
- prec = 5, 1 m
- prec = 6, 0.1 m
- …
- prec = 11 (max), 1 μm

UTM eastings are allowed to be in the range [100 km, 900 km], northings are allowed to be in in [0 km, 9500 km] for the northern hemisphere and in [1000 km, 10000 km] for the southern hemisphere. (However UTM northings can be continued across the equator. So the actual limits on the northings are [−9000 km, 9500 km] for the "northern" hemisphere and [1000 km, 19500 km] for the "southern" hemisphere.)

UPS eastings/northings are allowed to be in the range [1300 km, 2700 km] in the northern hemisphere and in [800 km, 3200 km] in the southern hemisphere.

The ranges are 100 km more restrictive than for the conversion between geographic coordinates and UTM and UPS given by UTMUPS. These restrictions are dictated by the allowed letters in MGRS coordinates. The choice of 9500 km for the maximum northing for northern hemisphere and of 1000 km as the minimum northing for southern hemisphere provide at least 0.5 degree extension into standard UPS zones. The upper ends of the ranges for the UPS coordinates is dictated by requiring symmetry about the meridians 0E and 90E.

All allowed UTM and UPS coordinates may now be converted to legal MGRS coordinates with the proviso that eastings and northings on the upper boundaries are silently reduced by about 4 nm (4 nanometers) to place them within the allowed range. (This includes reducing a southern hemisphere northing of 10000 km by 4 nm so that it is placed in latitude band M.) The UTM or UPS coordinates are truncated to requested precision to determine the MGRS coordinate. Thus in UTM zone 38n, the square area with easting in [444 km, 445 km) and northing in [3688 km, 3689 km) maps to MGRS coordinate 38SMB4488 (at prec = 2, 1 km), Khulani Sq., Baghdad.

The UTM/UPS selection and the UTM zone is preserved in the conversion to MGRS coordinate. Thus for zone > 0, the MGRS coordinate begins with the zone number followed by one of [C–M] for the southern hemisphere and [N–X] for the northern hemisphere. For zone = 0, the MGRS coordinates begins with one of [AB] for the southern hemisphere and [XY] for the northern hemisphere.

The conversion to the MGRS is exact for prec in [0, 5] except that a neighboring latitude band letter may be given if the point is within 5nm of a band boundary. For prec in [6, 11], the conversion is accurate to roundoff.

If prec = −1, then the "grid zone designation", e.g., 18T, is returned. This consists of the UTM zone number (absent for UPS) and the first letter of the MGRS string which labels the latitude band for UTM and the hemisphere for UPS.

If x or y is NaN or if zone is UTMUPS::INVALID, the returned MGRS string is "INVALID".

Return the result via a reference argument to avoid the overhead of allocating a potentially large number of small strings. If an error is thrown, then mgrs is unchanged.

Definition at line 123 of file MGRS.cpp.

References Forward(), and GeographicLib::UTMUPS::Reverse().

Referenced by GeographicLib::GeoCoords::AltMGRSRepresentation(), Forward(), and GeographicLib::GeoCoords::MGRSRepresentation().

## ◆ Forward() [2/2]

void GeographicLib::MGRS::Forward | ( | int | zone,
|  | bool | northp,
|  | real | x,
|  | real | y,
|  | real | lat,
|  | int | prec,
|  | std::string & | mgrs )
| static

Convert UTM or UPS coordinate to an MGRS coordinate when the latitude is known.

Parameters

  [in] | zone | UTM zone (zero means UPS).
  [in] | northp | hemisphere (true means north, false means south).
  [in] | x | easting of point (meters).
  [in] | y | northing of point (meters).
  [in] | lat | latitude (degrees).
  [in] | prec | precision relative to 100 km.
  [out] | mgrs | MGRS string.

Exceptions

  GeographicErr | if zone, x, or y is outside its allowed range.
  GeographicErr | if lat is inconsistent with the given UTM coordinates.
  std::bad_alloc | if the memory for mgrs can't be allocated.

The latitude is ignored for zone = 0 (UPS); otherwise the latitude is used to determine the latitude band and this is checked for consistency using the same tests as Reverse.

Definition at line 41 of file MGRS.cpp.

References GeographicLib::Math::digits(), GEOGRAPHICLIB_VOLATILE, GeographicLib::UTMUPS::INVALID, GeographicLib::UTMUPS::MAXZONE, GeographicLib::UTMUPS::MINZONE, and GeographicLib::Utility::str().

## ◆ Reverse()

void GeographicLib::MGRS::Reverse | ( | const std::string & | mgrs,
|  | int & | zone,
|  | bool & | northp,
|  | real & | x,
|  | real & | y,
|  | int & | prec,
|  | bool | centerp = true )
| static

Convert a MGRS coordinate to UTM or UPS coordinates.

Parameters

  [in] | mgrs | MGRS string.
  [out] | zone | UTM zone (zero means UPS).
  [out] | northp | hemisphere (true means north, false means south).
  [out] | x | easting of point (meters).
  [out] | y | northing of point (meters).
  [out] | prec | precision relative to 100 km.
  [in] | centerp | if true (default), return center of the MGRS square, else return SW (lower left) corner.

Exceptions

  GeographicErr | if mgrs is illegal.

All conversions from MGRS to UTM/UPS are permitted provided the MGRS coordinate is a possible result of a conversion in the other direction. (The leading 0 may be dropped from an input MGRS coordinate for UTM zones 1–9.) In addition, MGRS coordinates with a neighboring latitude band letter are permitted provided that some portion of the 100 km block is within the given latitude band. Thus

- 38VLS and 38WLS are allowed (latitude 64N intersects the square 38[VW]LS); but 38VMS is not permitted (all of 38WMS is north of 64N)
- 38MPE and 38NPF are permitted (they straddle the equator); but 38NPE and 38MPF are not permitted (the equator does not intersect either block).
- Similarly ZAB and YZB are permitted (they straddle the prime meridian); but YAB and ZZB are not (the prime meridian does not intersect either block).

The UTM/UPS selection and the UTM zone is preserved in the conversion from MGRS coordinate. The conversion is exact for prec in [0, 5]. With centerp = true, the conversion from MGRS to geographic and back is stable. This is not assured if centerp = false.

If a "grid zone designation" (for example, 18T or A) is given, then some suitable (but essentially arbitrary) point within that grid zone is returned. The main utility of the conversion is to allow zone and northp to be determined. In this case, the centerp parameter is ignored and prec is set to −1.

If the first 3 characters of mgrs are "INV", then x and y are set to NaN, zone is set to UTMUPS::INVALID, and prec is set to −2.

If an exception is thrown, then the arguments are unchanged.

Definition at line 158 of file MGRS.cpp.

References GeographicLib::UTMUPS::INVALID, GeographicLib::Utility::lookup(), GeographicLib::UTMUPS::MAXUTMZONE, GeographicLib::UTMUPS::MINUTMZONE, GeographicLib::Math::NaN(), GeographicLib::Math::qd, GeographicLib::Utility::str(), and GeographicLib::UTMUPS::UPS.

Referenced by GeographicLib::GeoCoords::Reset().

## ◆ Decode()

void GeographicLib::MGRS::Decode | ( | const std::string & | mgrs,
|  | std::string & | gridzone,
|  | std::string & | block,
|  | std::string & | easting,
|  | std::string & | northing )
| static

Split a MGRS grid reference into its components.

Parameters

  [in] | mgrs | MGRS string, e.g., 38SMB4488.
  [out] | gridzone | the grid zone, e.g., 38S.
  [out] | block | the 100km block id, e.g., MB.
  [out] | easting | the leading digits of the block easting, e.g., 44.
  [out] | northing | the leading digits of the block easting, e.g., 88.

Exceptions

  GeographicErr | if mgrs is illegal.

Only the most rudimentary checking of MGRS grid ref is done: it is expected to consist of 0-2 digits followed by 1 or 3 letters, followed (in the case of 3 letters) by an even number (possibly 0) of digits. In reporting errors, the letters I and O (illegal in MSRS) are regarded as non-alphabetic. The returned gridzone will always be non-empty. The other output arguments may be empty strings.

If the first 3 characters of mgrs are "INV", then gridzone is set to those 3 characters and the other return arguments are set to empty strings..

If an exception is thrown, then the arguments are unchanged.

Definition at line 412 of file MGRS.cpp.

## ◆ EquatorialRadius()

Math::real GeographicLib::MGRS::EquatorialRadius | ( |  | ) |
| inlinestatic

Returns
  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.)

Definition at line 345 of file MGRS.hpp.

References GeographicLib::UTMUPS::EquatorialRadius().

## ◆ Flattening()

Math::real GeographicLib::MGRS::Flattening | ( |  | ) |
| inlinestatic

Returns
  f the flattening of the WGS84 ellipsoid.

(The WGS84 value is returned because the UTM and UPS projections are based on this ellipsoid.)

Definition at line 353 of file MGRS.hpp.

References GeographicLib::UTMUPS::Flattening().

## ◆ Check()

void GeographicLib::MGRS::Check | ( |  | ) |
| static

Perform some checks on the UTMUPS coordinates on this ellipsoid. Throw an error if any of the assumptions made in the MGRS class is not true. This check needs to be carried out if the ellipsoid parameters (or the UTM/UPS scales) are ever changed.

Definition at line 449 of file MGRS.cpp.

References GeographicLib::UTMUPS::Forward(), GeographicLib::UTMUPS::Reverse(), and GeographicLib::Utility::str().

Referenced by main().

## Friends And Related Symbol Documentation

## ◆ UTMUPS

friend class UTMUPS
| friend

Definition at line 99 of file MGRS.hpp.

References UTMUPS.

Referenced by UTMUPS.

The documentation for this class was generated from the following files:

- MGRS.hpp
- MGRS.cpp