GeographicLib  1.40
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OSGB.hpp
Go to the documentation of this file.
1 /**
2  * \file OSGB.hpp
3  * \brief Header for GeographicLib::OSGB class
4  *
5  * Copyright (c) Charles Karney (2010-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_OSGB_HPP)
11 #define GEOGRAPHICLIB_OSGB_HPP 1
12 
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 Ordnance Survey grid system for Great Britain
26  *
27  * The class implements the coordinate system used by the Ordnance Survey for
28  * maps of Great Britain and conversions to the grid reference system.
29  *
30  * See
31  * - <a href="http://www.ordnancesurvey.co.uk/docs/support/guide-coordinate-systems-great-britain.pdf">
32  * A guide to coordinate systems in Great Britain</a>
33  * - <a href="http://www.ordnancesurvey.co.uk/docs/support/national-grid.pdf">
34  * Guide to the National Grid</a>
35  *
36  * \b WARNING: the latitudes and longitudes for the Ordnance Survey grid
37  * system do not use the WGS84 datum. Do not use the values returned by this
38  * class in the UTMUPS, MGRS, or Geoid classes without first converting the
39  * datum (and vice versa).
40  *
41  * Example of use:
42  * \include example-OSGB.cpp
43  **********************************************************************/
45  private:
46  typedef Math::real real;
47  static const std::string letters_;
48  static const std::string digits_;
49  static const TransverseMercator& OSGBTM();
50  enum {
51  base_ = 10,
52  tile_ = 100000,
53  tilelevel_ = 5,
54  tilegrid_ = 5,
55  tileoffx_ = 2 * tilegrid_,
56  tileoffy_ = 1 * tilegrid_,
57  minx_ = - tileoffx_ * tile_,
58  miny_ = - tileoffy_ * tile_,
59  maxx_ = (tilegrid_*tilegrid_ - tileoffx_) * tile_,
60  maxy_ = (tilegrid_*tilegrid_ - tileoffy_) * tile_,
61  // Maximum precision is um
62  maxprec_ = 5 + 6,
63  };
64  static real computenorthoffset();
65  static void CheckCoords(real x, real y);
66  OSGB(); // Disable constructor
67  public:
68 
69  /**
70  * Forward projection, from geographic to OSGB coordinates.
71  *
72  * @param[in] lat latitude of point (degrees).
73  * @param[in] lon longitude of point (degrees).
74  * @param[out] x easting of point (meters).
75  * @param[out] y northing of point (meters).
76  * @param[out] gamma meridian convergence at point (degrees).
77  * @param[out] k scale of projection at point.
78  *
79  * \e lat should be in the range [&minus;90&deg;, 90&deg;]; \e lon
80  * should be in the range [&minus;540&deg;, 540&deg;).
81  **********************************************************************/
82  static void Forward(real lat, real lon,
83  real& x, real& y, real& gamma, real& k) {
84  OSGBTM().Forward(OriginLongitude(), lat, lon, x, y, gamma, k);
85  x += FalseEasting();
86  y += computenorthoffset();
87  }
88 
89  /**
90  * Reverse projection, from OSGB coordinates to geographic.
91  *
92  * @param[in] x easting of point (meters).
93  * @param[in] y northing of point (meters).
94  * @param[out] lat latitude of point (degrees).
95  * @param[out] lon longitude of point (degrees).
96  * @param[out] gamma meridian convergence at point (degrees).
97  * @param[out] k scale of projection at point.
98  *
99  * The value of \e lon returned is in the range [&minus;180&deg;,
100  * 180&deg;).
101  **********************************************************************/
102 
103  static void Reverse(real x, real y,
104  real& lat, real& lon, real& gamma, real& k) {
105  x -= FalseEasting();
106  y -= computenorthoffset();
107  OSGBTM().Reverse(OriginLongitude(), x, y, lat, lon, gamma, k);
108  }
109 
110  /**
111  * OSGB::Forward without returning the convergence and scale.
112  **********************************************************************/
113  static void Forward(real lat, real lon, real& x, real& y) {
114  real gamma, k;
115  Forward(lat, lon, x, y, gamma, k);
116  }
117 
118  /**
119  * OSGB::Reverse without returning the convergence and scale.
120  **********************************************************************/
121  static void Reverse(real x, real y, real& lat, real& lon) {
122  real gamma, k;
123  Reverse(x, y, lat, lon, gamma, k);
124  }
125 
126  /**
127  * Convert OSGB coordinates to a grid reference.
128  *
129  * @param[in] x easting of point (meters).
130  * @param[in] y northing of point (meters).
131  * @param[in] prec precision relative to 100 km.
132  * @param[out] gridref National Grid reference.
133  * @exception GeographicErr if \e prec, \e x, or \e y is outside its
134  * allowed range.
135  * @exception std::bad_alloc if the memory for \e gridref can't be
136  * allocatied.
137  *
138  * \e prec specifies the precision of the grid reference string as follows:
139  * - prec = 0 (min), 100km
140  * - prec = 1, 10km
141  * - prec = 2, 1km
142  * - prec = 3, 100m
143  * - prec = 4, 10m
144  * - prec = 5, 1m
145  * - prec = 6, 0.1m
146  * - prec = 11 (max), 1&mu;m
147  *
148  * The easting must be in the range [&minus;1000 km, 1500 km) and the
149  * northing must be in the range [&minus;500 km, 2000 km). These bounds
150  * are consistent with rules for the letter designations for the grid
151  * system.
152  *
153  * If \e x or \e y is NaN, the returned grid reference is "INVALID".
154  **********************************************************************/
155  static void GridReference(real x, real y, int prec, std::string& gridref);
156 
157  /**
158  * Convert OSGB coordinates to a grid reference.
159  *
160  * @param[in] gridref National Grid reference.
161  * @param[out] x easting of point (meters).
162  * @param[out] y northing of point (meters).
163  * @param[out] prec precision relative to 100 km.
164  * @param[in] centerp if true (default), return center of the grid square,
165  * else return SW (lower left) corner.
166  * @exception GeographicErr if \e gridref is illegal.
167  *
168  * The grid reference must be of the form: two letters (not including I)
169  * followed by an even number of digits (up to 22).
170  *
171  * If the first 2 characters of \e gridref are "IN", then \e x and \e y are
172  * set to NaN and \e prec is set to &minus;2.
173  **********************************************************************/
174  static void GridReference(const std::string& gridref,
175  real& x, real& y, int& prec,
176  bool centerp = true);
177 
178  /** \name Inspector functions
179  **********************************************************************/
180  ///@{
181  /**
182  * @return \e a the equatorial radius of the Airy 1830 ellipsoid (meters).
183  *
184  * This is 20923713 ft converted to meters using the rule 1 ft =
185  * 10<sup>9.48401603&minus;10</sup> m. (The Airy 1830 value is returned
186  * because the OSGB projection is based on this ellipsoid.)
187  **********************************************************************/
189  // result is about 6377563.3960320664406 m
190  using std::pow;
191  return pow(real(10), real(48401603 - 100000000) / 100000000)
192  * 20923713;
193  }
194 
195  /**
196  * @return \e f the inverse flattening of the Airy 1830 ellipsoid.
197  *
198  * For the Airy 1830 ellipsoid, \e a = 20923713 ft and \e b = 20853810 ft;
199  * thus the flattening = (20923713 &minus; 20853810)/20923713 =
200  * 7767/2324857 = 1/299.32496459... (The Airy 1830 value is returned
201  * because the OSGB projection is based on this ellipsoid.)
202  **********************************************************************/
204  { return real(20923713 - 20853810) / 20923713; }
205 
206  /// \cond SKIP
207  /**
208  * <b>DEPRECATED</b>
209  * @return \e r the inverse flattening of the Airy 1830 ellipsoid.
210  **********************************************************************/
211  static Math::real InverseFlattening() { return 1/Flattening(); }
212  /// \endcond
213 
214  /**
215  * @return \e k0 central scale for the OSGB projection (0.9996012717...).
216  *
217  * C. J. Mugnier, Grids &amp; Datums, PE&amp;RS, Oct. 2003, states that
218  * this is defined as 10<sup>9.9998268&minus;10</sup>.
219  **********************************************************************/
221  using std::pow;
222  return pow(real(10), real(9998268 - 10000000) / 10000000);
223  }
224 
225  /**
226  * @return latitude of the origin for the OSGB projection (49 degrees).
227  **********************************************************************/
228  static Math::real OriginLatitude() { return real(49); }
229 
230  /**
231  * @return longitude of the origin for the OSGB projection (&minus;2
232  * degrees).
233  **********************************************************************/
234  static Math::real OriginLongitude() { return real(-2); }
235 
236  /**
237  * @return false northing the OSGB projection (&minus;100000 meters).
238  **********************************************************************/
239  static Math::real FalseNorthing() { return real(-100000); }
240 
241  /**
242  * @return false easting the OSGB projection (400000 meters).
243  **********************************************************************/
244  static Math::real FalseEasting() { return real(400000); }
245  ///@}
246 
247  };
248 
249 } // namespace GeographicLib
250 
251 #if defined(_MSC_VER)
252 # pragma warning (pop)
253 #endif
254 
255 #endif // GEOGRAPHICLIB_OSGB_HPP
static void Forward(real lat, real lon, real &x, real &y, real &gamma, real &k)
Definition: OSGB.hpp:82
#define GEOGRAPHICLIB_EXPORT
Definition: Constants.hpp:69
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
static Math::real Flattening()
Definition: OSGB.hpp:203
static Math::real FalseEasting()
Definition: OSGB.hpp:244
static Math::real OriginLatitude()
Definition: OSGB.hpp:228
void Forward(real lon0, real lat, real lon, real &x, real &y, real &gamma, real &k) const
Transverse Mercator projection.
Header for GeographicLib::TransverseMercator class.
static Math::real CentralScale()
Definition: OSGB.hpp:220
static Math::real FalseNorthing()
Definition: OSGB.hpp:239
static Math::real OriginLongitude()
Definition: OSGB.hpp:234
static Math::real MajorRadius()
Definition: OSGB.hpp:188
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static void Reverse(real x, real y, real &lat, real &lon, real &gamma, real &k)
Definition: OSGB.hpp:103
void Reverse(real lon0, real x, real y, real &lat, real &lon, real &gamma, real &k) const
Ordnance Survey grid system for Great Britain.
Definition: OSGB.hpp:44
Header for GeographicLib::Constants class.
static void Forward(real lat, real lon, real &x, real &y)
Definition: OSGB.hpp:113
static void Reverse(real x, real y, real &lat, real &lon)
Definition: OSGB.hpp:121