GeographicLib  1.42
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-2011) <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] swaplatlong 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 swaplatlong = true (default is false), then
123  * longitude precedes latitude in the absence of a hemisphere designator.
124  * Thus (with \e swaplatlong = 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;] and longitudes in the range [&minus;540&deg;, 540&deg;).
154  * Internally longitudes are reduced to the range [&minus;180&deg;,
155  * 180&deg;).
156  *
157  * <b>UTM/UPS parsing</b>: For UTM zones (&minus;80&deg; &le; Lat <
158  * 84&deg;), the zone designator is made up of a zone number (for 1 to 60)
159  * and a hemisphere letter (n or s), e.g., 38n (38north can also be used).
160  * The latitude band designer ([C--M] in the southern hemisphere and [N--X]
161  * in the northern) should NOT be used. (This is part of the MGRS
162  * coordinate.) The zone designator for the poles (where UPS is employed)
163  * is a hemisphere letter by itself, i.e., n or s (north or south can also
164  * be used).
165  *
166  * <b>MGRS parsing</b> interprets the grid references as square area at the
167  * specified precision (1m, 10m, 100m, etc.). If \e centerp = true (the
168  * default), the center of this square is then taken to be the precise
169  * position; thus:
170  * - 38SMB = 38n 450000 3650000
171  * - 38SMB4484 = 38n 444500 3684500
172  * - 38SMB44148470 = 38n 444145 3684705
173  * .
174  * Otherwise, the "south-west" corner of the square is used, i.e.,
175  * - 38SMB = 38n 400000 3600000
176  * - 38SMB4484 = 38n 444000 3684000
177  * - 38SMB44148470 = 38n 444140 3684700
178  **********************************************************************/
179  explicit GeoCoords(const std::string& s,
180  bool centerp = true, bool swaplatlong = false)
181  { Reset(s, centerp, swaplatlong); }
182 
183  /**
184  * Construct from geographic coordinates.
185  *
186  * @param[in] latitude (degrees).
187  * @param[in] longitude (degrees).
188  * @param[in] zone if specified, force the UTM/UPS representation to use a
189  * specified zone using the rules given in UTMUPS::zonespec.
190  * @exception GeographicErr if \e latitude is not in [&minus;90&deg;,
191  * 90&deg;].
192  * @exception GeographicErr if \e longitude is not in [&minus;540&deg;,
193  * 540&deg;).
194  * @exception GeographicErr if \e zone cannot be used for this location.
195  **********************************************************************/
196  GeoCoords(real latitude, real longitude, int zone = UTMUPS::STANDARD) {
197  Reset(latitude, longitude, zone);
198  }
199 
200  /**
201  * Construct from UTM/UPS coordinates.
202  *
203  * @param[in] zone UTM zone (zero means UPS).
204  * @param[in] northp hemisphere (true means north, false means south).
205  * @param[in] easting (meters).
206  * @param[in] northing (meters).
207  * @exception GeographicErr if \e zone, \e easting, or \e northing is
208  * outside its allowed range.
209  **********************************************************************/
210  GeoCoords(int zone, bool northp, real easting, real northing) {
211  Reset(zone, northp, easting, northing);
212  }
213 
214  /**
215  * Reset the location from a string. See
216  * GeoCoords(const std::string& s, bool centerp, bool swaplatlong).
217  *
218  * @param[in] s 1-element, 2-element, or 3-element string representation of
219  * the position.
220  * @param[in] centerp governs the interpretation of MGRS coordinates.
221  * @param[in] swaplatlong governs the interpretation of geographic
222  * coordinates.
223  * @exception GeographicErr if the \e s is malformed.
224  **********************************************************************/
225  void Reset(const std::string& s,
226  bool centerp = true, bool swaplatlong = false);
227 
228  /**
229  * Reset the location in terms of geographic coordinates. See
230  * GeoCoords(real latitude, real longitude, int zone).
231  *
232  * @param[in] latitude (degrees).
233  * @param[in] longitude (degrees).
234  * @param[in] zone if specified, force the UTM/UPS representation to use a
235  * specified zone using the rules given in UTMUPS::zonespec.
236  * @exception GeographicErr if \e latitude is not in [&minus;90&deg;,
237  * 90&deg;].
238  * @exception GeographicErr if \e longitude is not in [&minus;540&deg;,
239  * 540&deg;).
240  * @exception GeographicErr if \e zone cannot be used for this location.
241  **********************************************************************/
242  void Reset(real latitude, real longitude, int zone = UTMUPS::STANDARD) {
243  UTMUPS::Forward(latitude, longitude,
244  _zone, _northp, _easting, _northing, _gamma, _k,
245  zone);
246  _lat = latitude;
247  _long = longitude;
248  if (_long >= 180) _long -= 360;
249  else if (_long < -180) _long += 360;
250  CopyToAlt();
251  }
252 
253  /**
254  * Reset the location in terms of UPS/UPS coordinates. See
255  * GeoCoords(int zone, bool northp, real easting, real northing).
256  *
257  * @param[in] zone UTM zone (zero means UPS).
258  * @param[in] northp hemisphere (true means north, false means south).
259  * @param[in] easting (meters).
260  * @param[in] northing (meters).
261  * @exception GeographicErr if \e zone, \e easting, or \e northing is
262  * outside its allowed range.
263  **********************************************************************/
264  void Reset(int zone, bool northp, real easting, real northing) {
265  UTMUPS::Reverse(zone, northp, easting, northing,
266  _lat, _long, _gamma, _k);
267  _zone = zone;
268  _northp = northp;
269  _easting = easting;
270  _northing = northing;
271  FixHemisphere();
272  CopyToAlt();
273  }
274  ///@}
275 
276  /** \name Querying the GeoCoords object
277  **********************************************************************/
278  ///@{
279  /**
280  * @return latitude (degrees)
281  **********************************************************************/
282  Math::real Latitude() const { return _lat; }
283 
284  /**
285  * @return longitude (degrees)
286  **********************************************************************/
287  Math::real Longitude() const { return _long; }
288 
289  /**
290  * @return easting (meters)
291  **********************************************************************/
292  Math::real Easting() const { return _easting; }
293 
294  /**
295  * @return northing (meters)
296  **********************************************************************/
297  Math::real Northing() const { return _northing; }
298 
299  /**
300  * @return meridian convergence (degrees) for the UTM/UPS projection.
301  **********************************************************************/
302  Math::real Convergence() const { return _gamma; }
303 
304  /**
305  * @return scale for the UTM/UPS projection.
306  **********************************************************************/
307  Math::real Scale() const { return _k; }
308 
309  /**
310  * @return hemisphere (false means south, true means north).
311  **********************************************************************/
312  bool Northp() const { return _northp; }
313 
314  /**
315  * @return hemisphere letter n or s.
316  **********************************************************************/
317  char Hemisphere() const { return _northp ? 'n' : 's'; }
318 
319  /**
320  * @return the zone corresponding to the input (return 0 for UPS).
321  **********************************************************************/
322  int Zone() const { return _zone; }
323 
324  ///@}
325 
326  /** \name Setting and querying the alternate zone
327  **********************************************************************/
328  ///@{
329  /**
330  * Specify alternate zone number.
331  *
332  * @param[in] zone zone number for the alternate representation.
333  * @exception GeographicErr if \e zone cannot be used for this location.
334  *
335  * See UTMUPS::zonespec for more information on the interpretation of \e
336  * zone. Note that \e zone == UTMUPS::STANDARD (the default) use the
337  * standard UPS or UTM zone, UTMUPS::MATCH does nothing retaining the
338  * existing alternate representation. Before this is called the alternate
339  * zone is the input zone.
340  **********************************************************************/
341  void SetAltZone(int zone = UTMUPS::STANDARD) const {
342  if (zone == UTMUPS::MATCH)
343  return;
344  zone = UTMUPS::StandardZone(_lat, _long, zone);
345  if (zone == _zone)
346  CopyToAlt();
347  else {
348  bool northp;
349  UTMUPS::Forward(_lat, _long,
350  _alt_zone, northp,
351  _alt_easting, _alt_northing, _alt_gamma, _alt_k,
352  zone);
353  }
354  }
355 
356  /**
357  * @return current alternate zone (return 0 for UPS).
358  **********************************************************************/
359  int AltZone() const { return _alt_zone; }
360 
361  /**
362  * @return easting (meters) for alternate zone.
363  **********************************************************************/
364  Math::real AltEasting() const { return _alt_easting; }
365 
366  /**
367  * @return northing (meters) for alternate zone.
368  **********************************************************************/
369  Math::real AltNorthing() const { return _alt_northing; }
370 
371  /**
372  * @return meridian convergence (degrees) for alternate zone.
373  **********************************************************************/
374  Math::real AltConvergence() const { return _alt_gamma; }
375 
376  /**
377  * @return scale for alternate zone.
378  **********************************************************************/
379  Math::real AltScale() const { return _alt_k; }
380  ///@}
381 
382  /** \name String representations of the GeoCoords object
383  **********************************************************************/
384  ///@{
385  /**
386  * String representation with latitude and longitude as signed decimal
387  * degrees.
388  *
389  * @param[in] prec precision (relative to about 1m).
390  * @param[in] swaplatlong if true give longitude first (default = false)
391  * @exception std::bad_alloc if memory for the string can't be allocated.
392  * @return decimal latitude/longitude string representation.
393  *
394  * Precision specifies accuracy of representation as follows:
395  * - prec = &minus;5 (min), 1&deg;
396  * - prec = 0, 10<sup>&minus;5</sup>&deg; (about 1m)
397  * - prec = 3, 10<sup>&minus;8</sup>&deg;
398  * - prec = 9 (max), 10<sup>&minus;14</sup>&deg;
399  **********************************************************************/
400  std::string GeoRepresentation(int prec = 0, bool swaplatlong = false) const;
401 
402  /**
403  * String representation with latitude and longitude as degrees, minutes,
404  * seconds, and hemisphere.
405  *
406  * @param[in] prec precision (relative to about 1m)
407  * @param[in] swaplatlong if true give longitude first (default = false)
408  * @param[in] dmssep if non-null, use as the DMS separator character
409  * (instead of d, ', &quot; delimiters).
410  * @exception std::bad_alloc if memory for the string can't be allocated.
411  * @return DMS latitude/longitude string representation.
412  *
413  * Precision specifies accuracy of representation as follows:
414  * - prec = &minus;5 (min), 1&deg;
415  * - prec = &minus;4, 0.1&deg;
416  * - prec = &minus;3, 1'
417  * - prec = &minus;2, 0.1'
418  * - prec = &minus;1, 1&quot;
419  * - prec = 0, 0.1&quot; (about 3m)
420  * - prec = 1, 0.01&quot;
421  * - prec = 10 (max), 10<sup>&minus;11</sup>&quot;
422  **********************************************************************/
423  std::string DMSRepresentation(int prec = 0, bool swaplatlong = false,
424  char dmssep = char(0))
425  const;
426 
427  /**
428  * MGRS string.
429  *
430  * @param[in] prec precision (relative to about 1m).
431  * @exception std::bad_alloc if memory for the string can't be allocated.
432  * @return MGRS string.
433  *
434  * This gives the coordinates of the enclosing grid square with size given
435  * by the precision. Thus 38n 444180 3684790 converted to a MGRS
436  * coordinate at precision &minus;2 (100m) is 38SMB441847 and not
437  * 38SMB442848. \e prec specifies the precision of the MGRS string as
438  * follows:
439  * - prec = &minus;5 (min), 100km
440  * - prec = &minus;4, 10km
441  * - prec = &minus;3, 1km
442  * - prec = &minus;2, 100m
443  * - prec = &minus;1, 10m
444  * - prec = 0, 1m
445  * - prec = 1, 0.1m
446  * - prec = 6 (max), 1&mu;m
447  **********************************************************************/
448  std::string MGRSRepresentation(int prec = 0) const;
449 
450  /**
451  * UTM/UPS string.
452  *
453  * @param[in] prec precision (relative to about 1m)
454  * @param[in] abbrev if true (the default) use abbreviated (n/s) notation
455  * for hemisphere; otherwise spell out the hemisphere (north/south)
456  * @exception std::bad_alloc if memory for the string can't be allocated.
457  * @return UTM/UPS string representation: zone designator, easting, and
458  * northing.
459  *
460  * Precision specifies accuracy of representation as follows:
461  * - prec = &minus;5 (min), 100km
462  * - prec = &minus;3, 1km
463  * - prec = 0, 1m
464  * - prec = 3, 1mm
465  * - prec = 6, 1&mu;m
466  * - prec = 9 (max), 1nm
467  **********************************************************************/
468  std::string UTMUPSRepresentation(int prec = 0, bool abbrev = true) const;
469 
470  /**
471  * UTM/UPS string with hemisphere override.
472  *
473  * @param[in] northp hemisphere override
474  * @param[in] prec precision (relative to about 1m)
475  * @param[in] abbrev if true (the default) use abbreviated (n/s) notation
476  * for hemisphere; otherwise spell out the hemisphere (north/south)
477  * @exception GeographicErr if the hemisphere override attempts to change
478  * UPS N to UPS S or vice versa.
479  * @exception std::bad_alloc if memory for the string can't be allocated.
480  * @return UTM/UPS string representation: zone designator, easting, and
481  * northing.
482  **********************************************************************/
483  std::string UTMUPSRepresentation(bool northp, int prec = 0,
484  bool abbrev = true) const;
485 
486  /**
487  * MGRS string for the alternate zone. See GeoCoords::MGRSRepresentation.
488  *
489  * @param[in] prec precision (relative to about 1m).
490  * @exception std::bad_alloc if memory for the string can't be allocated.
491  * @return MGRS string.
492  **********************************************************************/
493  std::string AltMGRSRepresentation(int prec = 0) const;
494 
495  /**
496  * UTM/UPS string for the alternate zone. See
497  * GeoCoords::UTMUPSRepresentation.
498  *
499  * @param[in] prec precision (relative to about 1m)
500  * @param[in] abbrev if true (the default) use abbreviated (n/s) notation
501  * for hemisphere; otherwise spell out the hemisphere (north/south)
502  * @exception std::bad_alloc if memory for the string can't be allocated.
503  * @return UTM/UPS string representation: zone designator, easting, and
504  * northing.
505  **********************************************************************/
506  std::string AltUTMUPSRepresentation(int prec = 0, bool abbrev = true) const;
507 
508  /**
509  * UTM/UPS string for the alternate zone, with hemisphere override.
510  *
511  * @param[in] northp hemisphere override
512  * @param[in] prec precision (relative to about 1m)
513  * @param[in] abbrev if true (the default) use abbreviated (n/s) notation
514  * for hemisphere; otherwise spell out the hemisphere (north/south)
515  * @exception GeographicErr if the hemisphere override attempts to change
516  * UPS n to UPS s or vice verse.
517  * @exception std::bad_alloc if memory for the string can't be allocated.
518  * @return UTM/UPS string representation: zone designator, easting, and
519  * northing.
520  **********************************************************************/
521  std::string AltUTMUPSRepresentation(bool northp, int prec = 0,
522  bool abbrev = true) const;
523  ///@}
524 
525  /** \name Inspector functions
526  **********************************************************************/
527  ///@{
528  /**
529  * @return \e a the equatorial radius of the WGS84 ellipsoid (meters).
530  *
531  * (The WGS84 value is returned because the UTM and UPS projections are
532  * based on this ellipsoid.)
533  **********************************************************************/
535 
536  /**
537  * @return \e f the flattening of the WGS84 ellipsoid.
538  *
539  * (The WGS84 value is returned because the UTM and UPS projections are
540  * based on this ellipsoid.)
541  **********************************************************************/
543  ///@}
544 
545  /// \cond SKIP
546  /**
547  * <b>DEPRECATED</b>
548  * @return \e r the inverse flattening of the ellipsoid.
549  **********************************************************************/
550  Math::real InverseFlattening() const
551  { return UTMUPS::InverseFlattening(); }
552  /// \endcond
553  };
554 
555 } // namespace GeographicLib
556 
557 #endif // GEOGRAPHICLIB_GEOCOORDS_HPP
Math::real Latitude() const
Definition: GeoCoords.hpp:282
#define GEOGRAPHICLIB_EXPORT
Definition: Constants.hpp:90
GeoCoords(int zone, bool northp, real easting, real northing)
Definition: GeoCoords.hpp:210
static Math::real Flattening()
Definition: UTMUPS.hpp:416
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
Math::real AltNorthing() const
Definition: GeoCoords.hpp:369
Header for GeographicLib::UTMUPS class.
Math::real MajorRadius() const
Definition: GeoCoords.hpp:534
GeoCoords(real latitude, real longitude, int zone=UTMUPS::STANDARD)
Definition: GeoCoords.hpp:196
Mathematical functions needed by GeographicLib.
Definition: Math.hpp:102
Conversion between geographic coordinates.
Definition: GeoCoords.hpp:49
Math::real Northing() const
Definition: GeoCoords.hpp:297
GeoCoords(const std::string &s, bool centerp=true, bool swaplatlong=false)
Definition: GeoCoords.hpp:179
static Math::real MajorRadius()
Definition: UTMUPS.hpp:407
Math::real AltEasting() const
Definition: GeoCoords.hpp:364
void Reset(real latitude, real longitude, int zone=UTMUPS::STANDARD)
Definition: GeoCoords.hpp:242
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:67
Convert between geographic coordinates and UTM/UPS.
Definition: UTMUPS.hpp:75
Math::real Convergence() const
Definition: GeoCoords.hpp:302
Math::real Scale() const
Definition: GeoCoords.hpp:307
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
Math::real AltConvergence() const
Definition: GeoCoords.hpp:374
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
Header for GeographicLib::Constants class.
Math::real AltScale() const
Definition: GeoCoords.hpp:379
void Reset(int zone, bool northp, real easting, real northing)
Definition: GeoCoords.hpp:264
char Hemisphere() const
Definition: GeoCoords.hpp:317
Math::real Easting() const
Definition: GeoCoords.hpp:292
void SetAltZone(int zone=UTMUPS::STANDARD) const
Definition: GeoCoords.hpp:341
Math::real Flattening() const
Definition: GeoCoords.hpp:542
static int StandardZone(real lat, real lon, int setzone=STANDARD)
Definition: UTMUPS.cpp:42
Math::real Longitude() const
Definition: GeoCoords.hpp:287