NETGeographicLib  1.38
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Pages
UTMUPS.h
Go to the documentation of this file.
1 #pragma once
2 /**
3  * \file NETGeographicLib/UTMUPS.h
4  * \brief Header for NETGeographicLib::UTMUPS class
5  *
6  * NETGeographicLib is copyright (c) Scott Heiman (2013)
7  * GeographicLib is Copyright (c) Charles Karney (2010-2012)
8  * <charles@karney.com> and licensed under the MIT/X11 License.
9  * For more information, see
10  * http://geographiclib.sourceforge.net/
11  **********************************************************************/
12 
13 namespace NETGeographicLib
14 {
15  /**
16  * \brief .NET wrapper for GeographicLib::UTMUPS.
17  *
18  * This class allows .NET applications to access GeographicLib::UTMUPS.
19  *
20  * UTM and UPS are defined
21  * - J. W. Hager, J. F. Behensky, and B. W. Drew,
22  * <a href="http://earth-info.nga.mil/GandG/publications/tm8358.2/TM8358_2.pdf">
23  * The Universal Grids: Universal Transverse Mercator (UTM) and Universal
24  * Polar Stereographic (UPS)</a>, Defense Mapping Agency, Technical Manual
25  * TM8358.2 (1989).
26  * .
27  * Section 2-3 defines UTM and section 3-2.4 defines UPS. This document also
28  * includes approximate algorithms for the computation of the underlying
29  * transverse Mercator and polar stereographic projections. Here we
30  * substitute much more accurate algorithms given by
31  * GeographicLib:TransverseMercator and GeographicLib:PolarStereographic.
32  *
33  * In this implementation, the conversions are closed, i.e., output from
34  * Forward is legal input for Reverse and vice versa. The error is about 5nm
35  * in each direction. However, the conversion from legal UTM/UPS coordinates
36  * to geographic coordinates and back might throw an error if the initial
37  * point is within 5nm of the edge of the allowed range for the UTM/UPS
38  * coordinates.
39  *
40  * The simplest way to guarantee the closed property is to define allowed
41  * ranges for the eastings and northings for UTM and UPS coordinates. The
42  * UTM boundaries are the same for all zones. (The only place the
43  * exceptional nature of the zone boundaries is evident is when converting to
44  * UTM/UPS coordinates requesting the standard zone.) The MGRS lettering
45  * scheme imposes natural limits on UTM/UPS coordinates which may be
46  * converted into MGRS coordinates. For the conversion to/from geographic
47  * coordinates these ranges have been extended by 100km in order to provide a
48  * generous overlap between UTM and UPS and between UTM zones.
49  *
50  * The <a href="http://www.nga.mil">NGA</a> software package
51  * <a href="http://earth-info.nga.mil/GandG/geotrans/index.html">geotrans</a>
52  * also provides conversions to and from UTM and UPS. Version 2.4.2 (and
53  * earlier) suffers from some drawbacks:
54  * - Inconsistent rules are used to determine the whether a particular UTM or
55  * UPS coordinate is legal. A more systematic approach is taken here.
56  * - The underlying projections are not very accurately implemented.
57  *
58  * C# Example:
59  * \include example-UTMUPS.cs
60  * Managed C++ Example:
61  * \include example-UTMUPS.cpp
62  * Visual Basic Example:
63  * \include example-UTMUPS.vb
64  *
65  **********************************************************************/
66  public ref class UTMUPS
67  {
68  private:
69  // hide the constructor since all members of the class are static.
70  UTMUPS() {}
71  public:
72  /**
73  * In this class we bring together the UTM and UPS coordinates systems.
74  * The UTM divides the earth between latitudes &minus;80&deg; and 84&deg;
75  * into 60 zones numbered 1 thru 60. Zone assign zone number 0 to the UPS
76  * regions, covering the two poles. Within UTMUPS, non-negative zone
77  * numbers refer to one of the "physical" zones, 0 for UPS and [1, 60] for
78  * UTM. Negative "pseudo-zone" numbers are used to select one of the
79  * physical zones.
80  **********************************************************************/
81  enum class ZoneSpec {
82  /**
83  * The smallest pseudo-zone number.
84  **********************************************************************/
85  MINPSEUDOZONE = -4,
86  /**
87  * A marker for an undefined or invalid zone. Equivalent to NaN.
88  **********************************************************************/
89  INVALID = -4,
90  /**
91  * If a coordinate already include zone information (e.g., it is an MGRS
92  * coordinate), use that, otherwise apply the UTMUPS::STANDARD rules.
93  **********************************************************************/
94  MATCH = -3,
95  /**
96  * Apply the standard rules for UTM zone assigment extending the UTM zone
97  * to each pole to give a zone number in [1, 60]. For example, use UTM
98  * zone 38 for longitude in [42&deg;, 48&deg;). The rules include the
99  * Norway and Svalbard exceptions.
100  **********************************************************************/
101  UTM = -2,
102  /**
103  * Apply the standard rules for zone assignment to give a zone number in
104  * [0, 60]. If the latitude is not in [&minus;80&deg;, 84&deg;), then
105  * use UTMUPS::UPS = 0, otherwise apply the rules for UTMUPS::UTM. The
106  * tests on latitudes and longitudes are all closed on the lower end open
107  * on the upper. Thus for UTM zone 38, latitude is in [&minus;80&deg;,
108  * 84&deg;) and longitude is in [42&deg;, 48&deg;).
109  **********************************************************************/
110  STANDARD = -1,
111  /**
112  * The largest pseudo-zone number.
113  **********************************************************************/
114  MAXPSEUDOZONE = -1,
115  /**
116  * The smallest physical zone number.
117  **********************************************************************/
118  MINZONE = 0,
119  /**
120  * The zone number used for UPS
121  **********************************************************************/
122  UPS = 0,
123  /**
124  * The smallest UTM zone number.
125  **********************************************************************/
126  MINUTMZONE = 1,
127  /**
128  * The largest UTM zone number.
129  **********************************************************************/
130  MAXUTMZONE = 60,
131  /**
132  * The largest physical zone number.
133  **********************************************************************/
134  MAXZONE = 60,
135  };
136 
137  /**
138  * The standard zone.
139  *
140  * @param[in] lat latitude (degrees).
141  * @param[in] lon longitude (degrees).
142  * @param[in] setzone zone override (use ZoneSpec.STANDARD as default). If
143  * omitted, use the standard rules for picking the zone. If \e setzone
144  * is given then use that zone if it is non-negative, otherwise apply the
145  * rules given in UTMUPS::zonespec.
146  * @exception GeographicErr if \e setzone is outside the range
147  * [UTMUPS::MINPSEUDOZONE, UTMUPS::MAXZONE] = [&minus;4, 60].
148  *
149  * This is exact.
150  **********************************************************************/
151  static int StandardZone(double lat, double lon, int setzone);
152 
153  /**
154  * Forward projection, from geographic to UTM/UPS.
155  *
156  * @param[in] lat latitude of point (degrees).
157  * @param[in] lon longitude of point (degrees).
158  * @param[out] zone the UTM zone (zero means UPS).
159  * @param[out] northp hemisphere (true means north, false means south).
160  * @param[out] x easting of point (meters).
161  * @param[out] y northing of point (meters).
162  * @param[out] gamma meridian convergence at point (degrees).
163  * @param[out] k scale of projection at point.
164  * @param[in] setzone zone override (use ZoneSpec.STANDARD as default).
165  * @param[in] mgrslimits if true enforce the stricter MGRS limits on the
166  * coordinates (default = false).
167  * @exception GeographicErr if \e lat is not in [&minus;90&deg;,
168  * 90&deg;].
169  * @exception GeographicErr if \e lon is not in [&minus;540&deg;,
170  * 540&deg;).
171  * @exception GeographicErr if the resulting \e x or \e y is out of allowed
172  * range (see Reverse); in this case, these arguments are unchanged.
173  *
174  * If \e setzone is omitted, use the standard rules for picking the zone.
175  * If \e setzone is given then use that zone if it is non-negative,
176  * otherwise apply the rules given in UTMUPS::zonespec. The accuracy of
177  * the conversion is about 5nm.
178  *
179  * The northing \e y jumps by UTMUPS::UTMShift() when crossing the equator
180  * in the southerly direction. Sometimes it is useful to remove this
181  * discontinuity in \e y by extending the "northern" hemisphere using
182  * UTMUPS::Transfer:
183  * \code
184  double lat = -1, lon = 123;
185  int zone;
186  bool northp;
187  double x, y, gamma, k;
188  GeographicLib::UTMUPS::Forward(lat, lon, zone, northp, x, y, gamma, k);
189  GeographicLib::UTMUPS::Transfer(zone, northp, x, y,
190  zone, true, x, y, zone);
191  northp = true;
192  \endcode
193  **********************************************************************/
194  static void Forward(double lat, double lon,
195  [System::Runtime::InteropServices::Out] int% zone,
196  [System::Runtime::InteropServices::Out] bool% northp,
197  [System::Runtime::InteropServices::Out] double% x,
198  [System::Runtime::InteropServices::Out] double% y,
199  [System::Runtime::InteropServices::Out] double% gamma,
200  [System::Runtime::InteropServices::Out] double% k,
201  int setzone, bool mgrslimits);
202 
203  /**
204  * Reverse projection, from UTM/UPS to geographic.
205  *
206  * @param[in] zone the UTM zone (zero means UPS).
207  * @param[in] northp hemisphere (true means north, false means south).
208  * @param[in] x easting of point (meters).
209  * @param[in] y northing of point (meters).
210  * @param[out] lat latitude of point (degrees).
211  * @param[out] lon longitude of point (degrees).
212  * @param[out] gamma meridian convergence at point (degrees).
213  * @param[out] k scale of projection at point.
214  * @param[in] mgrslimits if true enforce the stricter MGRS limits on the
215  * coordinates (default = false).
216  * @exception GeographicErr if \e zone, \e x, or \e y is out of allowed
217  * range; this this case the arguments are unchanged.
218  *
219  * The accuracy of the conversion is about 5nm.
220  *
221  * UTM eastings are allowed to be in the range [0km, 1000km], northings are
222  * allowed to be in in [0km, 9600km] for the northern hemisphere and in
223  * [900km, 10000km] for the southern hemisphere. However UTM northings
224  * can be continued across the equator. So the actual limits on the
225  * northings are [-9100km, 9600km] for the "northern" hemisphere and
226  * [900km, 19600km] for the "southern" hemisphere.
227  *
228  * UPS eastings and northings are allowed to be in the range [1200km,
229  * 2800km] in the northern hemisphere and in [700km, 3100km] in the
230  * southern hemisphere.
231  *
232  * These ranges are 100km larger than allowed for the conversions to MGRS.
233  * (100km is the maximum extra padding consistent with eastings remaining
234  * non-negative.) This allows generous overlaps between zones and UTM and
235  * UPS. If \e mgrslimits = true, then all the ranges are shrunk by 100km
236  * so that they agree with the stricter MGRS ranges. No checks are
237  * performed besides these (e.g., to limit the distance outside the
238  * standard zone boundaries).
239  **********************************************************************/
240  static void Reverse(int zone, bool northp, double x, double y,
241  [System::Runtime::InteropServices::Out] double% lat,
242  [System::Runtime::InteropServices::Out] double% lon,
243  [System::Runtime::InteropServices::Out] double% gamma,
244  [System::Runtime::InteropServices::Out] double% k,
245  bool mgrslimits);
246 
247  /**
248  * UTMUPS::Forward without returning convergence and scale.
249  **********************************************************************/
250  static void Forward(double lat, double lon,
251  [System::Runtime::InteropServices::Out] int% zone,
252  [System::Runtime::InteropServices::Out] bool% northp,
253  [System::Runtime::InteropServices::Out] double% x,
254  [System::Runtime::InteropServices::Out] double% y,
255  int setzone, bool mgrslimits );
256 
257  /**
258  * UTMUPS::Reverse without returning convergence and scale.
259  **********************************************************************/
260  static void Reverse(int zone, bool northp, double x, double y,
261  [System::Runtime::InteropServices::Out] double% lat,
262  [System::Runtime::InteropServices::Out] double% lon,
263  bool mgrslimits);
264 
265  /**
266  * Transfer UTM/UPS coordinated from one zone to another.
267  *
268  * @param[in] zonein the UTM zone for \e xin and \e yin (or zero for UPS).
269  * @param[in] northpin hemisphere for \e xin and \e yin (true means north,
270  * false means south).
271  * @param[in] xin easting of point (meters) in \e zonein.
272  * @param[in] yin northing of point (meters) in \e zonein.
273  * @param[in] zoneout the requested UTM zone for \e xout and \e yout (or
274  * zero for UPS).
275  * @param[in] northpout hemisphere for \e xout output and \e yout.
276  * @param[out] xout easting of point (meters) in \e zoneout.
277  * @param[out] yout northing of point (meters) in \e zoneout.
278  * @param[out] zone the actual UTM zone for \e xout and \e yout (or zero
279  * for UPS); this equals \e zoneout if \e zoneout &ge; 0.
280  * @exception GeographicErr if \e zonein is out of range (see below).
281  * @exception GeographicErr if \e zoneout is out of range (see below).
282  * @exception GeographicErr if \e xin or \e yin fall outside their allowed
283  * ranges (see UTMUPS::Reverse).
284  * @exception GeographicErr if \e xout or \e yout fall outside their
285  * allowed ranges (see UTMUPS::Reverse).
286  *
287  * \e zonein must be in the range [UTMUPS::MINZONE, UTMUPS::MAXZONE] = [0,
288  * 60] with \e zonein = UTMUPS::UPS, 0, indicating UPS. \e zonein may
289  * also be UTMUPS::INVALID.
290  *
291  * \e zoneout must be in the range [UTMUPS::MINPSEUDOZONE, UTMUPS::MAXZONE]
292  * = [-4, 60]. If \e zoneout &lt; UTMUPS::MINZONE then the rules give in
293  * the documentation of UTMUPS::zonespec are applied, and \e zone is set to
294  * the actual zone used for output.
295  *
296  * (\e xout, \e yout) can overlap with (\e xin, \e yin).
297  **********************************************************************/
298  static void Transfer(int zonein, bool northpin, double xin, double yin,
299  int zoneout, bool northpout,
300  [System::Runtime::InteropServices::Out] double% xout,
301  [System::Runtime::InteropServices::Out] double% yout,
302  [System::Runtime::InteropServices::Out] int% zone);
303 
304  /**
305  * Decode a UTM/UPS zone string.
306  *
307  * @param[in] zonestr string representation of zone and hemisphere.
308  * @param[out] zone the UTM zone (zero means UPS).
309  * @param[out] northp hemisphere (true means north, false means south).
310  * @exception GeographicErr if \e zonestr is malformed.
311  *
312  * For UTM, \e zonestr has the form of a zone number in the range
313  * [UTMUPS::MINUTMZONE, UTMUPS::MAXUTMZONE] = [1, 60] followed by a
314  * hemisphere letter, n or s (or "north" or "south" spelled out). For UPS,
315  * it consists just of the hemisphere letter (or the spelled out
316  * hemisphere). The returned value of \e zone is UTMUPS::UPS = 0 for UPS.
317  * Note well that "38s" indicates the southern hemisphere of zone 38 and
318  * not latitude band S, 32&deg; &le; \e lat &lt; 40&deg;. n, 01s, 2n, 38s,
319  * south, 3north are legal. 0n, 001s, +3n, 61n, 38P are illegal. INV is a
320  * special value for which the returned value of \e is UTMUPS::INVALID.
321  **********************************************************************/
322  static void DecodeZone(System::String^ zonestr,
323  [System::Runtime::InteropServices::Out] int% zone,
324  [System::Runtime::InteropServices::Out] bool% northp);
325 
326  /**
327  * Encode a UTM/UPS zone string.
328  *
329  * @param[in] zone the UTM zone (zero means UPS).
330  * @param[in] northp hemisphere (true means north, false means south).
331  * @param[in] abbrev if true (the default) use abbreviated (n/s) notation
332  * for hemisphere; otherwise spell out the hemisphere (north/south)
333  * @exception GeographicErr if \e zone is out of range (see below).
334  * @exception std::bad_alloc if memoy for the string can't be allocated.
335  * @return string representation of zone and hemisphere.
336  *
337  * \e zone must be in the range [UTMUPS::MINZONE, UTMUPS::MAXZONE] = [0,
338  * 60] with \e zone = UTMUPS::UPS, 0, indicating UPS (but the resulting
339  * string does not contain "0"). \e zone may also be UTMUPS::INVALID, in
340  * which case the returned string is "inv". This reverses
341  * UTMUPS::DecodeZone.
342  **********************************************************************/
343  static System::String^ EncodeZone(int zone, bool northp, bool abbrev);
344 
345  /**
346  * Decode EPSG.
347  *
348  * @param[in] epsg the EPSG code.
349  * @param[out] zone the UTM zone (zero means UPS).
350  * @param[out] northp hemisphere (true means north, false means south).
351  *
352  * EPSG (European Petroleum Survery Group) codes are a way to refer to many
353  * different projections. DecodeEPSG decodes those refering to UTM or UPS
354  * projections for the WGS84 ellipsoid. If the code does not refer to one
355  * of these projections, \e zone is set to UTMUPS::INVALID. See
356  * http://spatialreference.org/ref/epsg/
357  **********************************************************************/
358  static void DecodeEPSG(int epsg,
359  [System::Runtime::InteropServices::Out] int% zone,
360  [System::Runtime::InteropServices::Out] bool% northp);
361 
362  /**
363  * Encode zone as EPSG.
364  *
365  * @param[in] zone the UTM zone (zero means UPS).
366  * @param[in] northp hemisphere (true means north, false means south).
367  * @return EPSG code (or -1 if \e zone is not in the range
368  * [UTMUPS::MINZONE, UTMUPS::MAXZONE] = [0, 60])
369  *
370  * Convert \e zone and \e northp to the corresponding EPSG (European
371  * Petroleum Survery Group) codes
372  **********************************************************************/
373  static int EncodeEPSG(int zone, bool northp);
374 
375  /**
376  * @return shift (meters) necessary to align N and S halves of a UTM zone
377  * (10<sup>7</sup>).
378  **********************************************************************/
379  static double UTMShift();
380 
381  /** \name Inspector functions
382  **********************************************************************/
383  ///@{
384  /**
385  * @return \e a the equatorial radius of the WGS84 ellipsoid (meters).
386  *
387  * (The WGS84 value is returned because the UTM and UPS projections are
388  * based on this ellipsoid.)
389  **********************************************************************/
390  static double MajorRadius();
391 
392  /**
393  * @return \e f the flattening of the WGS84 ellipsoid.
394  *
395  * (The WGS84 value is returned because the UTM and UPS projections are
396  * based on this ellipsoid.)
397  **********************************************************************/
398  static double Flattening();
399  ///@}
400  };
401 } // namespace NETGeographicLib