GeographicLib  1.44
GeoCoords.hpp
Go to the documentation of this file.
1 /**
2  * \file GeoCoords.hpp
3  * \brief Header for GeographicLib::GeoCoords class
4  *
5  * Copyright (c) Charles Karney (2008-2015) <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_GEOCOORDS_HPP)
11 #define GEOGRAPHICLIB_GEOCOORDS_HPP 1
12 
13 #include <GeographicLib/UTMUPS.hpp>
15 
16 namespace GeographicLib {
17 
18  /**
19  * \brief Conversion between geographic coordinates
20  *
21  * This class stores a geographic position which may be set via the
22  * constructors or Reset via
23  * - latitude and longitude
24  * - UTM or UPS coordinates
25  * - a string representation of these or an MGRS coordinate string
26  *
27  * The state consists of the latitude and longitude and the supplied UTM or
28  * UPS coordinates (possibly derived from the MGRS coordinates). If latitude
29  * and longitude were given then the UTM/UPS coordinates follows the standard
30  * conventions.
31  *
32  * The mutable state consists of the UTM or UPS coordinates for a alternate
33  * zone. A method SetAltZone is provided to set the alternate UPS/UTM zone.
34  *
35  * Methods are provided to return the geographic coordinates, the input UTM
36  * or UPS coordinates (and associated meridian convergence and scale), or
37  * alternate UTM or UPS coordinates (and their associated meridian
38  * convergence and scale).
39  *
40  * Once the input string has been parsed, you can print the result out in any
41  * of the formats, decimal degrees, degrees minutes seconds, MGRS, UTM/UPS.
42  *
43  * Example of use:
44  * \include example-GeoCoords.cpp
45  *
46  * <a href="GeoConvert.1.html">GeoConvert</a> is a command-line utility
47  * providing access to the functionality of GeoCoords.
48  **********************************************************************/
50  private:
51  typedef Math::real real;
52  real _lat, _long, _easting, _northing, _gamma, _k;
53  bool _northp;
54  int _zone; // See UTMUPS::zonespec
55  mutable real _alt_easting, _alt_northing, _alt_gamma, _alt_k;
56  mutable int _alt_zone;
57 
58  void CopyToAlt() const {
59  _alt_easting = _easting;
60  _alt_northing = _northing;
61  _alt_gamma = _gamma;
62  _alt_k = _k;
63  _alt_zone = _zone;
64  }
65  static void UTMUPSString(int zone, bool northp, real easting, real northing,
66  int prec, bool abbrev, std::string& utm);
67  void FixHemisphere();
68  public:
69 
70  /** \name Initializing the GeoCoords object
71  **********************************************************************/
72  ///@{
73  /**
74  * The default constructor sets the coordinate as undefined.
75  **********************************************************************/
77  : _lat(Math::NaN())
78  , _long(Math::NaN())
79  , _easting(Math::NaN())
80  , _northing(Math::NaN())
81  , _gamma(Math::NaN())
82  , _k(Math::NaN())
83  , _northp(false)
84  , _zone(UTMUPS::INVALID)
85  { CopyToAlt(); }
86 
87  /**
88  * Construct from a string.
89  *
90  * @param[in] s 1-element, 2-element, or 3-element string representation of
91  * the position.
92  * @param[in] centerp governs the interpretation of MGRS coordinates (see
93  * below).
94  * @param[in] longfirst governs the interpretation of geographic
95  * coordinates (see below).
96  * @exception GeographicErr if the \e s is malformed (see below).
97  *
98  * Parse as a string and interpret it as a geographic position. The input
99  * string is broken into space (or comma) separated pieces and Basic
100  * decision on which format is based on number of components
101  * -# MGRS
102  * -# "Lat Long" or "Long Lat"
103  * -# "Zone Easting Northing" or "Easting Northing Zone"
104  *
105  * The following inputs are approximately the same (Ar Ramadi Bridge, Iraq)
106  * - Latitude and Longitude
107  * - 33.44 43.27
108  * - N33d26.4' E43d16.2'
109  * - 43d16'12&quot;E 33d26'24&quot;N
110  * - 43:16:12E 33:26:24
111  * - MGRS
112  * - 38SLC30
113  * - 38SLC391014
114  * - 38SLC3918701405
115  * - 37SHT9708
116  * - UTM
117  * - 38n 339188 3701405
118  * - 897039 3708229 37n
119  *
120  * <b>Latitude and Longitude parsing</b>: Latitude precedes longitude,
121  * unless a N, S, E, W hemisphere designator is used on one or both
122  * coordinates. If \e longfirst = true (default is false), then
123  * longitude precedes latitude in the absence of a hemisphere designator.
124  * Thus (with \e longfirst = false)
125  * - 40 -75
126  * - N40 W75
127  * - -75 N40
128  * - 75W 40N
129  * - E-75 -40S
130  * .
131  * are all the same position. The coordinates may be given in
132  * decimal degrees, degrees and decimal minutes, degrees, minutes,
133  * seconds, etc. Use d, ', and &quot; to mark off the degrees,
134  * minutes and seconds. Various alternative symbols for degrees, minutes,
135  * and seconds are allowed. Alternatively, use : to separate these
136  * components. A single addition or subtraction is allowed. (See
137  * DMS::Decode for details.) Thus
138  * - 40d30'30&quot;
139  * - 40d30'30
140  * - 40&deg;30'30
141  * - 40d30.5'
142  * - 40d30.5
143  * - 40:30:30
144  * - 40:30.5
145  * - 40.508333333
146  * - 40:30+0:0:30
147  * - 40:31-0:0.5
148  * .
149  * all specify the same angle. The leading sign applies to the following
150  * components so -1d30 is -(1+30/60) = &minus;1.5. However, note
151  * that -1:30-0:0:15 is parsed as (-1:30) + (-0:0:15) = &minus;(1+30/60)
152  * &minus; (15/3600). Latitudes must be in the range [&minus;90&deg;,
153  * 90&deg;]. Internally longitudes are reduced to the range
154  * [&minus;180&deg;, 180&deg;).
155  *
156  * <b>UTM/UPS parsing</b>: For UTM zones (&minus;80&deg; &le; Lat <
157  * 84&deg;), the zone designator is made up of a zone number (for 1 to 60)
158  * and a hemisphere letter (n or s), e.g., 38n (38north can also be used).
159  * The latitude band designer ([C--M] in the southern hemisphere and [N--X]
160  * in the northern) should NOT be used. (This is part of the MGRS
161  * coordinate.) The zone designator for the poles (where UPS is employed)
162  * is a hemisphere letter by itself, i.e., n or s (north or south can also
163  * be used).
164  *
165  * <b>MGRS parsing</b> interprets the grid references as square area at the
166  * specified precision (1m, 10m, 100m, etc.). If \e centerp = true (the
167  * default), the center of this square is then taken to be the precise
168  * position; thus:
169  * - 38SMB = 38n 450000 3650000
170  * - 38SMB4484 = 38n 444500 3684500
171  * - 38SMB44148470 = 38n 444145 3684705
172  * .
173  * Otherwise, the "south-west" corner of the square is used, i.e.,
174  * - 38SMB = 38n 400000 3600000
175  * - 38SMB4484 = 38n 444000 3684000
176  * - 38SMB44148470 = 38n 444140 3684700
177  **********************************************************************/
178  explicit GeoCoords(const std::string& s,
179  bool centerp = true, bool longfirst = false)
180  { Reset(s, centerp, longfirst); }
181 
182  /**
183  * Construct from geographic coordinates.
184  *
185  * @param[in] latitude (degrees).
186  * @param[in] longitude (degrees).
187  * @param[in] zone if specified, force the UTM/UPS representation to use a
188  * specified zone using the rules given in UTMUPS::zonespec.
189  * @exception GeographicErr if \e latitude is not in [&minus;90&deg;,
190  * 90&deg;].
191  * @exception GeographicErr if \e zone cannot be used for this location.
192  **********************************************************************/
193  GeoCoords(real latitude, real longitude, int zone = UTMUPS::STANDARD) {
194  Reset(latitude, longitude, zone);
195  }
196 
197  /**
198  * Construct from UTM/UPS coordinates.
199  *
200  * @param[in] zone UTM zone (zero means UPS).
201  * @param[in] northp hemisphere (true means north, false means south).
202  * @param[in] easting (meters).
203  * @param[in] northing (meters).
204  * @exception GeographicErr if \e zone, \e easting, or \e northing is
205  * outside its allowed range.
206  **********************************************************************/
207  GeoCoords(int zone, bool northp, real easting, real northing) {
208  Reset(zone, northp, easting, northing);
209  }
210 
211  /**
212  * Reset the location from a string. See
213  * GeoCoords(const std::string& s, bool centerp, bool longfirst).
214  *
215  * @param[in] s 1-element, 2-element, or 3-element string representation of
216  * the position.
217  * @param[in] centerp governs the interpretation of MGRS coordinates.
218  * @param[in] longfirst governs the interpretation of geographic
219  * coordinates.
220  * @exception GeographicErr if the \e s is malformed.
221  **********************************************************************/
222  void Reset(const std::string& s,
223  bool centerp = true, bool longfirst = false);
224 
225  /**
226  * Reset the location in terms of geographic coordinates. See
227  * GeoCoords(real latitude, real longitude, int zone).
228  *
229  * @param[in] latitude (degrees).
230  * @param[in] longitude (degrees).
231  * @param[in] zone if specified, force the UTM/UPS representation to use a
232  * specified zone using the rules given in UTMUPS::zonespec.
233  * @exception GeographicErr if \e latitude is not in [&minus;90&deg;,
234  * 90&deg;].
235  * @exception GeographicErr if \e zone cannot be used for this location.
236  **********************************************************************/
237  void Reset(real latitude, real longitude, int zone = UTMUPS::STANDARD) {
238  UTMUPS::Forward(latitude, longitude,
239  _zone, _northp, _easting, _northing, _gamma, _k,
240  zone);
241  _lat = latitude;
242  _long = longitude;
243  if (_long >= 180) _long -= 360;
244  else if (_long < -180) _long += 360;
245  CopyToAlt();
246  }
247 
248  /**
249  * Reset the location in terms of UPS/UPS coordinates. See
250  * GeoCoords(int zone, bool northp, real easting, real northing).
251  *
252  * @param[in] zone UTM zone (zero means UPS).
253  * @param[in] northp hemisphere (true means north, false means south).
254  * @param[in] easting (meters).
255  * @param[in] northing (meters).
256  * @exception GeographicErr if \e zone, \e easting, or \e northing is
257  * outside its allowed range.
258  **********************************************************************/
259  void Reset(int zone, bool northp, real easting, real northing) {
260  UTMUPS::Reverse(zone, northp, easting, northing,
261  _lat, _long, _gamma, _k);
262  _zone = zone;
263  _northp = northp;
264  _easting = easting;
265  _northing = northing;
266  FixHemisphere();
267  CopyToAlt();
268  }
269  ///@}
270 
271  /** \name Querying the GeoCoords object
272  **********************************************************************/
273  ///@{
274  /**
275  * @return latitude (degrees)
276  **********************************************************************/
277  Math::real Latitude() const { return _lat; }
278 
279  /**
280  * @return longitude (degrees)
281  **********************************************************************/
282  Math::real Longitude() const { return _long; }
283 
284  /**
285  * @return easting (meters)
286  **********************************************************************/
287  Math::real Easting() const { return _easting; }
288 
289  /**
290  * @return northing (meters)
291  **********************************************************************/
292  Math::real Northing() const { return _northing; }
293 
294  /**
295  * @return meridian convergence (degrees) for the UTM/UPS projection.
296  **********************************************************************/
297  Math::real Convergence() const { return _gamma; }
298 
299  /**
300  * @return scale for the UTM/UPS projection.
301  **********************************************************************/
302  Math::real Scale() const { return _k; }
303 
304  /**
305  * @return hemisphere (false means south, true means north).
306  **********************************************************************/
307  bool Northp() const { return _northp; }
308 
309  /**
310  * @return hemisphere letter n or s.
311  **********************************************************************/
312  char Hemisphere() const { return _northp ? 'n' : 's'; }
313 
314  /**
315  * @return the zone corresponding to the input (return 0 for UPS).
316  **********************************************************************/
317  int Zone() const { return _zone; }
318 
319  ///@}
320 
321  /** \name Setting and querying the alternate zone
322  **********************************************************************/
323  ///@{
324  /**
325  * Specify alternate zone number.
326  *
327  * @param[in] zone zone number for the alternate representation.
328  * @exception GeographicErr if \e zone cannot be used for this location.
329  *
330  * See UTMUPS::zonespec for more information on the interpretation of \e
331  * zone. Note that \e zone == UTMUPS::STANDARD (the default) use the
332  * standard UPS or UTM zone, UTMUPS::MATCH does nothing retaining the
333  * existing alternate representation. Before this is called the alternate
334  * zone is the input zone.
335  **********************************************************************/
336  void SetAltZone(int zone = UTMUPS::STANDARD) const {
337  if (zone == UTMUPS::MATCH)
338  return;
339  zone = UTMUPS::StandardZone(_lat, _long, zone);
340  if (zone == _zone)
341  CopyToAlt();
342  else {
343  bool northp;
344  UTMUPS::Forward(_lat, _long,
345  _alt_zone, northp,
346  _alt_easting, _alt_northing, _alt_gamma, _alt_k,
347  zone);
348  }
349  }
350 
351  /**
352  * @return current alternate zone (return 0 for UPS).
353  **********************************************************************/
354  int AltZone() const { return _alt_zone; }
355 
356  /**
357  * @return easting (meters) for alternate zone.
358  **********************************************************************/
359  Math::real AltEasting() const { return _alt_easting; }
360 
361  /**
362  * @return northing (meters) for alternate zone.
363  **********************************************************************/
364  Math::real AltNorthing() const { return _alt_northing; }
365 
366  /**
367  * @return meridian convergence (degrees) for alternate zone.
368  **********************************************************************/
369  Math::real AltConvergence() const { return _alt_gamma; }
370 
371  /**
372  * @return scale for alternate zone.
373  **********************************************************************/
374  Math::real AltScale() const { return _alt_k; }
375  ///@}
376 
377  /** \name String representations of the GeoCoords object
378  **********************************************************************/
379  ///@{
380  /**
381  * String representation with latitude and longitude as signed decimal
382  * degrees.
383  *
384  * @param[in] prec precision (relative to about 1m).
385  * @param[in] longfirst if true give longitude first (default = false)
386  * @exception std::bad_alloc if memory for the string can't be allocated.
387  * @return decimal latitude/longitude string representation.
388  *
389  * Precision specifies accuracy of representation as follows:
390  * - prec = &minus;5 (min), 1&deg;
391  * - prec = 0, 10<sup>&minus;5</sup>&deg; (about 1m)
392  * - prec = 3, 10<sup>&minus;8</sup>&deg;
393  * - prec = 9 (max), 10<sup>&minus;14</sup>&deg;
394  **********************************************************************/
395  std::string GeoRepresentation(int prec = 0, bool longfirst = false) const;
396 
397  /**
398  * String representation with latitude and longitude as degrees, minutes,
399  * seconds, and hemisphere.
400  *
401  * @param[in] prec precision (relative to about 1m)
402  * @param[in] longfirst if true give longitude first (default = false)
403  * @param[in] dmssep if non-null, use as the DMS separator character
404  * (instead of d, ', &quot; delimiters).
405  * @exception std::bad_alloc if memory for the string can't be allocated.
406  * @return DMS latitude/longitude string representation.
407  *
408  * Precision specifies accuracy of representation as follows:
409  * - prec = &minus;5 (min), 1&deg;
410  * - prec = &minus;4, 0.1&deg;
411  * - prec = &minus;3, 1'
412  * - prec = &minus;2, 0.1'
413  * - prec = &minus;1, 1&quot;
414  * - prec = 0, 0.1&quot; (about 3m)
415  * - prec = 1, 0.01&quot;
416  * - prec = 10 (max), 10<sup>&minus;11</sup>&quot;
417  **********************************************************************/
418  std::string DMSRepresentation(int prec = 0, bool longfirst = false,
419  char dmssep = char(0))
420  const;
421 
422  /**
423  * MGRS string.
424  *
425  * @param[in] prec precision (relative to about 1m).
426  * @exception std::bad_alloc if memory for the string can't be allocated.
427  * @return MGRS string.
428  *
429  * This gives the coordinates of the enclosing grid square with size given
430  * by the precision. Thus 38n 444180 3684790 converted to a MGRS
431  * coordinate at precision &minus;2 (100m) is 38SMB441847 and not
432  * 38SMB442848. \e prec specifies the precision of the MGRS string as
433  * follows:
434  * - prec = &minus;5 (min), 100km
435  * - prec = &minus;4, 10km
436  * - prec = &minus;3, 1km
437  * - prec = &minus;2, 100m
438  * - prec = &minus;1, 10m
439  * - prec = 0, 1m
440  * - prec = 1, 0.1m
441  * - prec = 6 (max), 1&mu;m
442  **********************************************************************/
443  std::string MGRSRepresentation(int prec = 0) const;
444 
445  /**
446  * UTM/UPS string.
447  *
448  * @param[in] prec precision (relative to about 1m)
449  * @param[in] abbrev if true (the default) use abbreviated (n/s) notation
450  * for hemisphere; otherwise spell out the hemisphere (north/south)
451  * @exception std::bad_alloc if memory for the string can't be allocated.
452  * @return UTM/UPS string representation: zone designator, easting, and
453  * northing.
454  *
455  * Precision specifies accuracy of representation as follows:
456  * - prec = &minus;5 (min), 100km
457  * - prec = &minus;3, 1km
458  * - prec = 0, 1m
459  * - prec = 3, 1mm
460  * - prec = 6, 1&mu;m
461  * - prec = 9 (max), 1nm
462  **********************************************************************/
463  std::string UTMUPSRepresentation(int prec = 0, bool abbrev = true) const;
464 
465  /**
466  * UTM/UPS string with hemisphere override.
467  *
468  * @param[in] northp hemisphere override
469  * @param[in] prec precision (relative to about 1m)
470  * @param[in] abbrev if true (the default) use abbreviated (n/s) notation
471  * for hemisphere; otherwise spell out the hemisphere (north/south)
472  * @exception GeographicErr if the hemisphere override attempts to change
473  * UPS N to UPS S or vice versa.
474  * @exception std::bad_alloc if memory for the string can't be allocated.
475  * @return UTM/UPS string representation: zone designator, easting, and
476  * northing.
477  **********************************************************************/
478  std::string UTMUPSRepresentation(bool northp, int prec = 0,
479  bool abbrev = true) const;
480 
481  /**
482  * MGRS string for the alternate zone. See GeoCoords::MGRSRepresentation.
483  *
484  * @param[in] prec precision (relative to about 1m).
485  * @exception std::bad_alloc if memory for the string can't be allocated.
486  * @return MGRS string.
487  **********************************************************************/
488  std::string AltMGRSRepresentation(int prec = 0) const;
489 
490  /**
491  * UTM/UPS string for the alternate zone. See
492  * GeoCoords::UTMUPSRepresentation.
493  *
494  * @param[in] prec precision (relative to about 1m)
495  * @param[in] abbrev if true (the default) use abbreviated (n/s) notation
496  * for hemisphere; otherwise spell out the hemisphere (north/south)
497  * @exception std::bad_alloc if memory for the string can't be allocated.
498  * @return UTM/UPS string representation: zone designator, easting, and
499  * northing.
500  **********************************************************************/
501  std::string AltUTMUPSRepresentation(int prec = 0, bool abbrev = true) const;
502 
503  /**
504  * UTM/UPS string for the alternate zone, with hemisphere override.
505  *
506  * @param[in] northp hemisphere override
507  * @param[in] prec precision (relative to about 1m)
508  * @param[in] abbrev if true (the default) use abbreviated (n/s) notation
509  * for hemisphere; otherwise spell out the hemisphere (north/south)
510  * @exception GeographicErr if the hemisphere override attempts to change
511  * UPS n to UPS s or vice verse.
512  * @exception std::bad_alloc if memory for the string can't be allocated.
513  * @return UTM/UPS string representation: zone designator, easting, and
514  * northing.
515  **********************************************************************/
516  std::string AltUTMUPSRepresentation(bool northp, int prec = 0,
517  bool abbrev = true) const;
518  ///@}
519 
520  /** \name Inspector functions
521  **********************************************************************/
522  ///@{
523  /**
524  * @return \e a the equatorial radius of the WGS84 ellipsoid (meters).
525  *
526  * (The WGS84 value is returned because the UTM and UPS projections are
527  * based on this ellipsoid.)
528  **********************************************************************/
530 
531  /**
532  * @return \e f the flattening of the WGS84 ellipsoid.
533  *
534  * (The WGS84 value is returned because the UTM and UPS projections are
535  * based on this ellipsoid.)
536  **********************************************************************/
538  ///@}
539 
540  /// \cond SKIP
541  /**
542  * <b>DEPRECATED</b>
543  * @return \e r the inverse flattening of the ellipsoid.
544  **********************************************************************/
545  Math::real InverseFlattening() const
546  { return UTMUPS::InverseFlattening(); }
547  /// \endcond
548  };
549 
550 } // namespace GeographicLib
551 
552 #endif // GEOGRAPHICLIB_GEOCOORDS_HPP
Math::real Latitude() const
Definition: GeoCoords.hpp:277
#define GEOGRAPHICLIB_EXPORT
Definition: Constants.hpp:90
GeoCoords(int zone, bool northp, real easting, real northing)
Definition: GeoCoords.hpp:207
static Math::real Flattening()
Definition: UTMUPS.hpp:413
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
Math::real AltNorthing() const
Definition: GeoCoords.hpp:364
Header for GeographicLib::UTMUPS class.
Math::real MajorRadius() const
Definition: GeoCoords.hpp:529
GeoCoords(real latitude, real longitude, int zone=UTMUPS::STANDARD)
Definition: GeoCoords.hpp:193
Mathematical functions needed by GeographicLib.
Definition: Math.hpp:102
Conversion between geographic coordinates.
Definition: GeoCoords.hpp:49
Math::real Northing() const
Definition: GeoCoords.hpp:292
static Math::real MajorRadius()
Definition: UTMUPS.hpp:404
Math::real AltEasting() const
Definition: GeoCoords.hpp:359
void Reset(real latitude, real longitude, int zone=UTMUPS::STANDARD)
Definition: GeoCoords.hpp:237
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:66
Convert between geographic coordinates and UTM/UPS.
Definition: UTMUPS.hpp:75
Math::real Convergence() const
Definition: GeoCoords.hpp:297
Math::real Scale() const
Definition: GeoCoords.hpp:302
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
Math::real AltConvergence() const
Definition: GeoCoords.hpp:369
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:119
Header for GeographicLib::Constants class.
Math::real AltScale() const
Definition: GeoCoords.hpp:374
void Reset(int zone, bool northp, real easting, real northing)
Definition: GeoCoords.hpp:259
char Hemisphere() const
Definition: GeoCoords.hpp:312
Math::real Easting() const
Definition: GeoCoords.hpp:287
GeoCoords(const std::string &s, bool centerp=true, bool longfirst=false)
Definition: GeoCoords.hpp:178
void SetAltZone(int zone=UTMUPS::STANDARD) const
Definition: GeoCoords.hpp:336
Math::real Flattening() const
Definition: GeoCoords.hpp:537
static int StandardZone(real lat, real lon, int setzone=STANDARD)
Definition: UTMUPS.cpp:42
Math::real Longitude() const
Definition: GeoCoords.hpp:282