GeographicLib  1.38
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OSGB.cpp
Go to the documentation of this file.
1 /**
2  * \file OSGB.cpp
3  * \brief Implementation for GeographicLib::OSGB class
4  *
5  * Copyright (c) Charles Karney (2010-2012) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * http://geographiclib.sourceforge.net/
8  **********************************************************************/
9 
10 #include <GeographicLib/OSGB.hpp>
12 
13 namespace GeographicLib {
14 
15  using namespace std;
16 
17  const string OSGB::letters_ = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
18  const string OSGB::digits_ = "0123456789";
19 
20  const TransverseMercator& OSGB::OSGBTM() {
21  static const TransverseMercator osgbtm(MajorRadius(), Flattening(),
22  CentralScale());
23  return osgbtm;
24  }
25 
26  Math::real OSGB::northoffset_ = 0;
27  bool OSGB::init_ = false;
28 
29  Math::real OSGB::computenorthoffset() {
30  if (!init_) {
31  real x, y;
32  OSGBTM().Forward(real(0), OriginLatitude(), real(0), x, y);
33  northoffset_ = FalseNorthing() - y;
34  init_ = true;
35  }
36  return northoffset_;
37  }
38 
39  void OSGB::GridReference(real x, real y, int prec, std::string& gridref) {
40  CheckCoords(x, y);
41  if (!(prec >= 0 && prec <= maxprec_))
42  throw GeographicErr("OSGB precision " + Utility::str(prec)
43  + " not in [0, "
44  + Utility::str(int(maxprec_)) + "]");
45  char grid[2 + 2 * maxprec_];
46  int
47  xh = int(floor(x)) / tile_,
48  yh = int(floor(y)) / tile_;
49  real
50  xf = x - tile_ * xh,
51  yf = y - tile_ * yh;
52  xh += tileoffx_;
53  yh += tileoffy_;
54  int z = 0;
55  grid[z++] = letters_[(tilegrid_ - (yh / tilegrid_) - 1)
56  * tilegrid_ + (xh / tilegrid_)];
57  grid[z++] = letters_[(tilegrid_ - (yh % tilegrid_) - 1)
58  * tilegrid_ + (xh % tilegrid_)];
59  real mult = pow(real(base_), max(tilelevel_ - prec, 0));
60  int
61  ix = int(floor(xf / mult)),
62  iy = int(floor(yf / mult));
63  for (int c = min(prec, int(tilelevel_)); c--;) {
64  grid[z + c] = digits_[ ix % base_ ];
65  ix /= base_;
66  grid[z + c + prec] = digits_[ iy % base_ ];
67  iy /= base_;
68  }
69  if (prec > tilelevel_) {
70  xf -= floor(xf / mult);
71  yf -= floor(yf / mult);
72  mult = pow(real(base_), prec - tilelevel_);
73  ix = int(floor(xf * mult));
74  iy = int(floor(yf * mult));
75  for (int c = prec - tilelevel_; c--;) {
76  grid[z + c + tilelevel_] = digits_[ ix % base_ ];
77  ix /= base_;
78  grid[z + c + tilelevel_ + prec] = digits_[ iy % base_ ];
79  iy /= base_;
80  }
81  }
82  int mlen = z + 2 * prec;
83  gridref.resize(mlen);
84  copy(grid, grid + mlen, gridref.begin());
85  }
86 
87  void OSGB::GridReference(const std::string& gridref,
88  real& x, real& y, int& prec,
89  bool centerp) {
90  int
91  len = int(gridref.size()),
92  p = 0;
93  char grid[2 + 2 * maxprec_];
94  for (int i = 0; i < len; ++i) {
95  if (!isspace(gridref[i])) {
96  if (p >= 2 + 2 * maxprec_)
97  throw GeographicErr("OSGB string " + gridref + " too long");
98  grid[p++] = gridref[i];
99  }
100  }
101  len = p;
102  p = 0;
103  if (len < 2)
104  throw GeographicErr("OSGB string " + gridref + " too short");
105  if (len % 2)
106  throw GeographicErr("OSGB string " + gridref +
107  " has odd number of characters");
108  int
109  xh = 0,
110  yh = 0;
111  while (p < 2) {
112  int i = Utility::lookup(letters_, grid[p++]);
113  if (i < 0)
114  throw GeographicErr("Illegal prefix character " + gridref);
115  yh = yh * tilegrid_ + tilegrid_ - (i / tilegrid_) - 1;
116  xh = xh * tilegrid_ + (i % tilegrid_);
117  }
118  xh -= tileoffx_;
119  yh -= tileoffy_;
120 
121  int prec1 = (len - p)/2;
122  real
123  unit = tile_,
124  x1 = unit * xh,
125  y1 = unit * yh;
126  for (int i = 0; i < prec1; ++i) {
127  unit /= base_;
128  int
129  ix = Utility::lookup(digits_, grid[p + i]),
130  iy = Utility::lookup(digits_, grid[p + i + prec1]);
131  if (ix < 0 || iy < 0)
132  throw GeographicErr("Encountered a non-digit in " + gridref);
133  x1 += unit * ix;
134  y1 += unit * iy;
135  }
136  if (centerp) {
137  x1 += unit/2;
138  y1 += unit/2;
139  }
140  x = x1;
141  y = y1;
142  prec = prec1;
143  }
144 
145  void OSGB::CheckCoords(real x, real y) {
146  // Limits are all multiples of 100km and are all closed on the lower end
147  // and open on the upper end -- and this is reflected in the error
148  // messages.
149  if (! (x >= minx_ && x < maxx_) )
150  throw GeographicErr("Easting " + Utility::str(int(floor(x/1000)))
151  + "km not in OSGB range ["
152  + Utility::str(minx_/1000) + "km, "
153  + Utility::str(maxx_/1000) + "km)");
154  if (! (y >= miny_ && y < maxy_) )
155  throw GeographicErr("Northing " + Utility::str(int(floor(y/1000)))
156  + "km not in OSGB range ["
157  + Utility::str(miny_/1000) + "km, "
158  + Utility::str(maxy_/1000) + "km)");
159  }
160 
161 } // namespace GeographicLib
GeographicLib::Math::real real
Definition: GeodSolve.cpp:40
Header for GeographicLib::Utility class.
Header for GeographicLib::OSGB class.
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static std::string str(T x, int p=-1)
Definition: Utility.hpp:266
static void GridReference(real x, real y, int prec, std::string &gridref)
Definition: OSGB.cpp:39
Exception handling for GeographicLib.
Definition: Constants.hpp:362
static int lookup(const std::string &s, char c)
Definition: Utility.hpp:382