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