OSGB.hpp 9.7 KB

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