GeographicLib  1.35
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
21  OSGB::OSGBTM_(MajorRadius(), Flattening(), CentralScale());
22 
23  Math::real OSGB::northoffset_ = 0;
24  bool OSGB::init_ = false;
25 
26  Math::real OSGB::computenorthoffset() throw() {
27  if (!init_) {
28  real x, y;
29  OSGBTM_.Forward(real(0), OriginLatitude(), real(0), x, y);
30  northoffset_ = FalseNorthing() - y;
31  init_ = true;
32  }
33  return northoffset_;
34  }
35 
36  void OSGB::GridReference(real x, real y, int prec, std::string& gridref) {
37  CheckCoords(x, y);
38  if (!(prec >= 0 && prec <= maxprec_))
39  throw GeographicErr("OSGB precision " + Utility::str(prec)
40  + " not in [0, "
41  + Utility::str(int(maxprec_)) + "]");
42  char grid[2 + 2 * maxprec_];
43  int
44  xh = int(floor(x)) / tile_,
45  yh = int(floor(y)) / tile_;
46  real
47  xf = x - tile_ * xh,
48  yf = y - tile_ * yh;
49  xh += tileoffx_;
50  yh += tileoffy_;
51  int z = 0;
52  grid[z++] = letters_[(tilegrid_ - (yh / tilegrid_) - 1)
53  * tilegrid_ + (xh / tilegrid_)];
54  grid[z++] = letters_[(tilegrid_ - (yh % tilegrid_) - 1)
55  * tilegrid_ + (xh % tilegrid_)];
56  real mult = pow(real(base_), max(tilelevel_ - prec, 0));
57  int
58  ix = int(floor(xf / mult)),
59  iy = int(floor(yf / mult));
60  for (int c = min(prec, int(tilelevel_)); c--;) {
61  grid[z + c] = digits_[ ix % base_ ];
62  ix /= base_;
63  grid[z + c + prec] = digits_[ iy % base_ ];
64  iy /= base_;
65  }
66  if (prec > tilelevel_) {
67  xf -= floor(xf / mult);
68  yf -= floor(yf / mult);
69  mult = pow(real(base_), prec - tilelevel_);
70  ix = int(floor(xf * mult));
71  iy = int(floor(yf * mult));
72  for (int c = prec - tilelevel_; c--;) {
73  grid[z + c + tilelevel_] = digits_[ ix % base_ ];
74  ix /= base_;
75  grid[z + c + tilelevel_ + prec] = digits_[ iy % base_ ];
76  iy /= base_;
77  }
78  }
79  int mlen = z + 2 * prec;
80  gridref.resize(mlen);
81  copy(grid, grid + mlen, gridref.begin());
82  }
83 
84  void OSGB::GridReference(const std::string& gridref,
85  real& x, real& y, int& prec,
86  bool centerp) {
87  int
88  len = int(gridref.size()),
89  p = 0;
90  char grid[2 + 2 * maxprec_];
91  for (int i = 0; i < len; ++i) {
92  if (!isspace(gridref[i])) {
93  if (p >= 2 + 2 * maxprec_)
94  throw GeographicErr("OSGB string " + gridref + " too long");
95  grid[p++] = gridref[i];
96  }
97  }
98  len = p;
99  p = 0;
100  if (len < 2)
101  throw GeographicErr("OSGB string " + gridref + " too short");
102  if (len % 2)
103  throw GeographicErr("OSGB string " + gridref +
104  " has odd number of characters");
105  int
106  xh = 0,
107  yh = 0;
108  while (p < 2) {
109  int i = Utility::lookup(letters_, grid[p++]);
110  if (i < 0)
111  throw GeographicErr("Illegal prefix character " + gridref);
112  yh = yh * tilegrid_ + tilegrid_ - (i / tilegrid_) - 1;
113  xh = xh * tilegrid_ + (i % tilegrid_);
114  }
115  xh -= tileoffx_;
116  yh -= tileoffy_;
117 
118  int prec1 = (len - p)/2;
119  real
120  unit = tile_,
121  x1 = unit * xh,
122  y1 = unit * yh;
123  for (int i = 0; i < prec1; ++i) {
124  unit /= base_;
125  int
126  ix = Utility::lookup(digits_, grid[p + i]),
127  iy = Utility::lookup(digits_, grid[p + i + prec1]);
128  if (ix < 0 || iy < 0)
129  throw GeographicErr("Encountered a non-digit in " + gridref);
130  x1 += unit * ix;
131  y1 += unit * iy;
132  }
133  if (centerp) {
134  x1 += unit/2;
135  y1 += unit/2;
136  }
137  x = x1;
138  y = y1;
139  prec = prec1;
140  }
141 
142  void OSGB::CheckCoords(real x, real y) {
143  // Limits are all multiples of 100km and are all closed on the lower end
144  // and open on the upper end -- and this is reflected in the error
145  // messages.
146  if (! (x >= minx_ && x < maxx_) )
147  throw GeographicErr("Easting " + Utility::str(int(floor(x/1000)))
148  + "km not in OSGB range ["
149  + Utility::str(minx_/1000) + "km, "
150  + Utility::str(maxx_/1000) + "km)");
151  if (! (y >= miny_ && y < maxy_) )
152  throw GeographicErr("Northing " + Utility::str(int(floor(y/1000)))
153  + "km not in OSGB range ["
154  + Utility::str(miny_/1000) + "km, "
155  + Utility::str(maxy_/1000) + "km)");
156  }
157 
158 } // namespace GeographicLib
GeographicLib::Math::real real
Definition: GeodSolve.cpp:40
Header for GeographicLib::Utility class.
Header for GeographicLib::OSGB class.
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:36
Exception handling for GeographicLib.
Definition: Constants.hpp:320
static int lookup(const std::string &s, char c)
Definition: Utility.hpp:364