GeographicLib  1.40
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MGRS.hpp
Go to the documentation of this file.
1 /**
2  * \file MGRS.hpp
3  * \brief Header for GeographicLib::MGRS class
4  *
5  * Copyright (c) Charles Karney (2008-2014) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * http://geographiclib.sourceforge.net/
8  **********************************************************************/
9 
10 #if !defined(GEOGRAPHICLIB_MGRS_HPP)
11 #define GEOGRAPHICLIB_MGRS_HPP 1
12 
14 #include <GeographicLib/UTMUPS.hpp>
15 
16 #if defined(_MSC_VER)
17 // Squelch warnings about dll vs string
18 # pragma warning (push)
19 # pragma warning (disable: 4251)
20 #endif
21 
22 namespace GeographicLib {
23 
24  /**
25  * \brief Convert between UTM/UPS and %MGRS
26  *
27  * MGRS is defined in Chapter 3 of
28  * - J. W. Hager, L. L. Fry, S. S. Jacks, D. R. Hill,
29  * <a href="http://earth-info.nga.mil/GandG/publications/tm8358.1/pdf/TM8358_1.pdf">
30  * Datums, Ellipsoids, Grids, and Grid Reference Systems</a>,
31  * Defense Mapping Agency, Technical Manual TM8358.1 (1990).
32  * .
33  * This document has been updated by the two NGA documents
34  * - <a href="http://earth-info.nga.mil/GandG/publications/NGA_STND_0037_2_0_0_GRIDS/NGA.STND.0037_2.0.0_GRIDS.pdf">
35  * Universal Grids and Grid Reference Systems</a>,
36  * NGA.STND.0037_2.0.0_GRIDS (2014).
37  * - <a href="http://earth-info.nga.mil/GandG/publications/NGA_SIG_0012_2_0_0_UTMUPS/NGA.SIG.0012_2.0.0_UTMUPS.pdf">
38  * The Universal Grids and the Transverse Mercator and Polar Stereographic
39  * Map Projections</a>, NGA.SIG.0012_2.0.0_UTMUPS (2014).
40  *
41  * This implementation has the following properties:
42  * - The conversions are closed, i.e., output from Forward is legal input for
43  * Reverse and vice versa. Conversion in both directions preserve the
44  * UTM/UPS selection and the UTM zone.
45  * - Forward followed by Reverse and vice versa is approximately the
46  * identity. (This is affected in predictable ways by errors in
47  * determining the latitude band and by loss of precision in the MGRS
48  * coordinates.)
49  * - All MGRS coordinates truncate to legal 100 km blocks. All MGRS
50  * coordinates with a legal 100 km block prefix are legal (even though the
51  * latitude band letter may now belong to a neighboring band).
52  * - The range of UTM/UPS coordinates allowed for conversion to MGRS
53  * coordinates is the maximum consistent with staying within the letter
54  * ranges of the MGRS scheme.
55  * - All the transformations are implemented as static methods in the MGRS
56  * class.
57  *
58  * The <a href="http://www.nga.mil">NGA</a> software package
59  * <a href="http://earth-info.nga.mil/GandG/geotrans/index.html">geotrans</a>
60  * also provides conversions to and from MGRS. Version 3.0 (and earlier)
61  * suffers from some drawbacks:
62  * - Inconsistent rules are used to determine the whether a particular MGRS
63  * coordinate is legal. A more systematic approach is taken here.
64  * - The underlying projections are not very accurately implemented.
65  *
66  * Example of use:
67  * \include example-MGRS.cpp
68  **********************************************************************/
70  private:
71  typedef Math::real real;
72  // The smallest length s.t., 1.0e7 - eps() < 1.0e7 (approx 1.9 nm)
73  static inline real eps() {
74  using std::pow;
75  // 25 = ceil(log_2(2e7)) -- use half circumference here because
76  // northing 195e5 is a legal in the "southern" hemisphere.
77  static const real eps = pow(real(0.5), Math::digits() - 25);
78  return eps;
79  }
80  // The smallest angle s.t., 90 - angeps() < 90 (approx 50e-12 arcsec)
81  static inline real angeps() {
82  using std::pow;
83  // 7 = ceil(log_2(90))
84  static const real angeps = pow(real(0.5), Math::digits() - 7);
85  return angeps;
86  }
87  static const std::string hemispheres_;
88  static const std::string utmcols_[3];
89  static const std::string utmrow_;
90  static const std::string upscols_[4];
91  static const std::string upsrows_[2];
92  static const std::string latband_;
93  static const std::string upsband_;
94  static const std::string digits_;
95 
96  static const int mineasting_[4];
97  static const int maxeasting_[4];
98  static const int minnorthing_[4];
99  static const int maxnorthing_[4];
100  enum {
101  base_ = 10,
102  // Top-level tiles are 10^5 m = 100 km on a side
103  tilelevel_ = 5,
104  // Period of UTM row letters
105  utmrowperiod_ = 20,
106  // Row letters are shifted by 5 for even zones
107  utmevenrowshift_ = 5,
108  // Maximum precision is um
109  maxprec_ = 5 + 6,
110  };
111  static void CheckCoords(bool utmp, bool& northp, real& x, real& y);
112  static int UTMRow(int iband, int icol, int irow);
113 
114  friend class UTMUPS; // UTMUPS::StandardZone calls LatitudeBand
115  // Return latitude band number [-10, 10) for the given latitude (degrees).
116  // The bands are reckoned in include their southern edges.
117  static int LatitudeBand(real lat) {
118  using std::floor;
119  int ilat = int(floor(lat));
120  return (std::max)(-10, (std::min)(9, (ilat + 80)/8 - 10));
121  }
122  // Return approximate latitude band number [-10, 10) for the given northing
123  // (meters). With this rule, each 100km tile would have a unique band
124  // letter corresponding to the latitude at the center of the tile. This
125  // function isn't currently used.
126  static int ApproxLatitudeBand(real y) {
127  // northing at tile center in units of tile = 100km
128  using std::floor; using std::abs;
129  real ya = floor( (std::min)(real(88), abs(y/tile_)) ) +
130  real(0.5);
131  // convert to lat (mult by 90/100) and then to band (divide by 8)
132  // the +1 fine tunes the boundary between bands 3 and 4
133  int b = int(floor( ((ya * 9 + 1) / 10) / 8 ));
134  // For the northern hemisphere we have
135  // band rows num
136  // N 0 0:8 9
137  // P 1 9:17 9
138  // Q 2 18:26 9
139  // R 3 27:34 8
140  // S 4 35:43 9
141  // T 5 44:52 9
142  // U 6 53:61 9
143  // V 7 62:70 9
144  // W 8 71:79 9
145  // X 9 80:94 15
146  return y >= 0 ? b : -(b + 1);
147  }
148  // UTMUPS access these enums
149  enum {
150  tile_ = 100000, // Size MGRS blocks
151  minutmcol_ = 1,
152  maxutmcol_ = 9,
153  minutmSrow_ = 10,
154  maxutmSrow_ = 100, // Also used for UTM S false northing
155  minutmNrow_ = 0, // Also used for UTM N false northing
156  maxutmNrow_ = 95,
157  minupsSind_ = 8, // These 4 ind's apply to easting and northing
158  maxupsSind_ = 32,
159  minupsNind_ = 13,
160  maxupsNind_ = 27,
161  upseasting_ = 20, // Also used for UPS false northing
162  utmeasting_ = 5, // UTM false easting
163  // Difference between S hemisphere northing and N hemisphere northing
164  utmNshift_ = (maxutmSrow_ - minutmNrow_) * tile_
165  };
166  MGRS(); // Disable constructor
167 
168  public:
169 
170  /**
171  * Convert UTM or UPS coordinate to an MGRS coordinate.
172  *
173  * @param[in] zone UTM zone (zero means UPS).
174  * @param[in] northp hemisphere (true means north, false means south).
175  * @param[in] x easting of point (meters).
176  * @param[in] y northing of point (meters).
177  * @param[in] prec precision relative to 100 km.
178  * @param[out] mgrs MGRS string.
179  * @exception GeographicErr if \e zone, \e x, or \e y is outside its
180  * allowed range.
181  * @exception GeographicErr if the memory for the MGRS string can't be
182  * allocated.
183  *
184  * \e prec specifies the precision of the MGRS string as follows:
185  * - prec = &minus;1 (min), only the grid zone is returned
186  * - prec = 0 (min), 100 km
187  * - prec = 1, 10 km
188  * - prec = 2, 1 km
189  * - prec = 3, 100 m
190  * - prec = 4, 10 m
191  * - prec = 5, 1 m
192  * - prec = 6, 0.1 m
193  * - prec = 11 (max), 1 &mu;m
194  *
195  * UTM eastings are allowed to be in the range [100 km, 900 km], northings
196  * are allowed to be in in [0 km, 9500 km] for the northern hemisphere and
197  * in [1000 km, 10000 km] for the southern hemisphere. (However UTM
198  * northings can be continued across the equator. So the actual limits on
199  * the northings are [&minus;9000 km, 9500 km] for the "northern"
200  * hemisphere and [1000 km, 19500 km] for the "southern" hemisphere.)
201  *
202  * UPS eastings/northings are allowed to be in the range [1300 km, 2700 km]
203  * in the northern hemisphere and in [800 km, 3200 km] in the southern
204  * hemisphere.
205  *
206  * The ranges are 100 km more restrictive that for the conversion between
207  * geographic coordinates and UTM and UPS given by UTMUPS. These
208  * restrictions are dictated by the allowed letters in MGRS coordinates.
209  * The choice of 9500 km for the maximum northing for northern hemisphere
210  * and of 1000 km as the minimum northing for southern hemisphere provide
211  * at least 0.5 degree extension into standard UPS zones. The upper ends
212  * of the ranges for the UPS coordinates is dictated by requiring symmetry
213  * about the meridians 0E and 90E.
214  *
215  * All allowed UTM and UPS coordinates may now be converted to legal MGRS
216  * coordinates with the proviso that eastings and northings on the upper
217  * boundaries are silently reduced by about 4 nm (4 nanometers) to place
218  * them \e within the allowed range. (This includes reducing a southern
219  * hemisphere northing of 10000 km by 4 nm so that it is placed in latitude
220  * band M.) The UTM or UPS coordinates are truncated to requested
221  * precision to determine the MGRS coordinate. Thus in UTM zone 38n, the
222  * square area with easting in [444 km, 445 km) and northing in [3688 km,
223  * 3689 km) maps to MGRS coordinate 38SMB4488 (at \e prec = 2, 1 km),
224  * Khulani Sq., Baghdad.
225  *
226  * The UTM/UPS selection and the UTM zone is preserved in the conversion to
227  * MGRS coordinate. Thus for \e zone > 0, the MGRS coordinate begins with
228  * the zone number followed by one of [C--M] for the southern
229  * hemisphere and [N--X] for the northern hemisphere. For \e zone =
230  * 0, the MGRS coordinates begins with one of [AB] for the southern
231  * hemisphere and [XY] for the northern hemisphere.
232  *
233  * The conversion to the MGRS is exact for prec in [0, 5] except that a
234  * neighboring latitude band letter may be given if the point is within 5nm
235  * of a band boundary. For prec in [6, 11], the conversion is accurate to
236  * roundoff.
237  *
238  * If \e prec = &minus;1, then the "grid zone designation", e.g., 18T, is
239  * returned. This consists of the UTM zone number (absent for UPS) and the
240  * first letter of the MGRS string which labels the latitude band for UTM
241  * and the hemisphere for UPS.
242  *
243  * If \e x or \e y is NaN or if \e zone is UTMUPS::INVALID, the returned
244  * MGRS string is "INVALID".
245  *
246  * Return the result via a reference argument to avoid the overhead of
247  * allocating a potentially large number of small strings. If an error is
248  * thrown, then \e mgrs is unchanged.
249  **********************************************************************/
250  static void Forward(int zone, bool northp, real x, real y,
251  int prec, std::string& mgrs);
252 
253  /**
254  * Convert UTM or UPS coordinate to an MGRS coordinate when the latitude is
255  * known.
256  *
257  * @param[in] zone UTM zone (zero means UPS).
258  * @param[in] northp hemisphere (true means north, false means south).
259  * @param[in] x easting of point (meters).
260  * @param[in] y northing of point (meters).
261  * @param[in] lat latitude (degrees).
262  * @param[in] prec precision relative to 100 km.
263  * @param[out] mgrs MGRS string.
264  * @exception GeographicErr if \e zone, \e x, or \e y is outside its
265  * allowed range.
266  * @exception GeographicErr if \e lat is inconsistent with the given UTM
267  * coordinates.
268  * @exception std::bad_alloc if the memory for \e mgrs can't be allocated.
269  *
270  * The latitude is ignored for \e zone = 0 (UPS); otherwise the latitude is
271  * used to determine the latitude band and this is checked for consistency
272  * using the same tests as Reverse.
273  **********************************************************************/
274  static void Forward(int zone, bool northp, real x, real y, real lat,
275  int prec, std::string& mgrs);
276 
277  /**
278  * Convert a MGRS coordinate to UTM or UPS coordinates.
279  *
280  * @param[in] mgrs MGRS string.
281  * @param[out] zone UTM zone (zero means UPS).
282  * @param[out] northp hemisphere (true means north, false means south).
283  * @param[out] x easting of point (meters).
284  * @param[out] y northing of point (meters).
285  * @param[out] prec precision relative to 100 km.
286  * @param[in] centerp if true (default), return center of the MGRS square,
287  * else return SW (lower left) corner.
288  * @exception GeographicErr if \e mgrs is illegal.
289  *
290  * All conversions from MGRS to UTM/UPS are permitted provided the MGRS
291  * coordinate is a possible result of a conversion in the other direction.
292  * (The leading 0 may be dropped from an input MGRS coordinate for UTM
293  * zones 1--9.) In addition, MGRS coordinates with a neighboring
294  * latitude band letter are permitted provided that some portion of the
295  * 100 km block is within the given latitude band. Thus
296  * - 38VLS and 38WLS are allowed (latitude 64N intersects the square
297  * 38[VW]LS); but 38VMS is not permitted (all of 38VMS is north of 64N)
298  * - 38MPE and 38NPF are permitted (they straddle the equator); but 38NPE
299  * and 38MPF are not permitted (the equator does not intersect either
300  * block).
301  * - Similarly ZAB and YZB are permitted (they straddle the prime
302  * meridian); but YAB and ZZB are not (the prime meridian does not
303  * intersect either block).
304  *
305  * The UTM/UPS selection and the UTM zone is preserved in the conversion
306  * from MGRS coordinate. The conversion is exact for prec in [0, 5]. With
307  * centerp = true the conversion from MGRS to geographic and back is
308  * stable. This is not assured if \e centerp = false.
309  *
310  * If a "grid zone designation" (for example, 18T or A) is given, then some
311  * suitable (but essentially arbitrary) point within that grid zone is
312  * returned. The main utility of the conversion is to allow \e zone and \e
313  * northp to be determined. In this case, the \e centerp parameter is
314  * ignored and \e prec is set to &minus;1.
315  *
316  * If the first 3 characters of \e mgrs are "INV", then \e x and \e y are
317  * set to NaN, \e zone is set to UTMUPS::INVALID, and \e prec is set to
318  * &minus;2.
319  *
320  * If an exception is thrown, then the arguments are unchanged.
321  **********************************************************************/
322  static void Reverse(const std::string& mgrs,
323  int& zone, bool& northp, real& x, real& y,
324  int& prec, bool centerp = true);
325 
326  /** \name Inspector functions
327  **********************************************************************/
328  ///@{
329  /**
330  * @return \e a the equatorial radius of the WGS84 ellipsoid (meters).
331  *
332  * (The WGS84 value is returned because the UTM and UPS projections are
333  * based on this ellipsoid.)
334  **********************************************************************/
336 
337  /**
338  * @return \e f the flattening of the WGS84 ellipsoid.
339  *
340  * (The WGS84 value is returned because the UTM and UPS projections are
341  * based on this ellipsoid.)
342  **********************************************************************/
343  static Math::real Flattening() { return UTMUPS::Flattening(); }
344  ///@}
345 
346  /// \cond SKIP
347  /**
348  * <b>DEPRECATED</b>
349  * @return \e r the inverse flattening of the WGS84 ellipsoid.
350  **********************************************************************/
351  static Math::real InverseFlattening()
352  { return UTMUPS::InverseFlattening(); }
353  /// \endcond
354  };
355 
356 } // namespace GeographicLib
357 
358 #if defined(_MSC_VER)
359 # pragma warning (pop)
360 #endif
361 
362 #endif // GEOGRAPHICLIB_MGRS_HPP
#define GEOGRAPHICLIB_EXPORT
Definition: Constants.hpp:69
static Math::real Flattening()
Definition: UTMUPS.hpp:416
static Math::real MajorRadius()
Definition: MGRS.hpp:335
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
Header for GeographicLib::UTMUPS class.
static Math::real MajorRadius()
Definition: UTMUPS.hpp:407
Convert between geographic coordinates and UTM/UPS.
Definition: UTMUPS.hpp:75
Header for GeographicLib::Constants class.
static Math::real Flattening()
Definition: MGRS.hpp:343
Convert between UTM/UPS and MGRS.
Definition: MGRS.hpp:69
static int digits()
Definition: Math.hpp:145