src/OSGB.cpp
Go to the documentation of this file.
1 
10 #include <GeographicLib/OSGB.hpp>
12 
13 namespace GeographicLib {
14 
15  using namespace std;
16 
17  const char* const OSGB::letters_ = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
18  const char* const OSGB::digits_ = "0123456789";
19 
21  static const TransverseMercator osgbtm(MajorRadius(), Flattening(),
22  CentralScale());
23  return osgbtm;
24  }
25 
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  // Need extra real because, since C++11, pow(float, int) returns double
59  real mult = real(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 = real(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  if (len >= 2 &&
94  toupper(gridref[0]) == 'I' &&
95  toupper(gridref[1]) == 'N') {
96  x = y = Math::NaN();
97  prec = -2; // For compatibility with MGRS::Reverse.
98  return;
99  }
100  char grid[2 + 2 * maxprec_];
101  for (int i = 0; i < len; ++i) {
102  if (!isspace(gridref[i])) {
103  if (p >= 2 + 2 * maxprec_)
104  throw GeographicErr("OSGB string " + gridref + " too long");
105  grid[p++] = gridref[i];
106  }
107  }
108  len = p;
109  p = 0;
110  if (len < 2)
111  throw GeographicErr("OSGB string " + gridref + " too short");
112  if (len % 2)
113  throw GeographicErr("OSGB string " + gridref +
114  " has odd number of characters");
115  int
116  xh = 0,
117  yh = 0;
118  while (p < 2) {
119  int i = Utility::lookup(letters_, grid[p++]);
120  if (i < 0)
121  throw GeographicErr("Illegal prefix character " + gridref);
122  yh = yh * tilegrid_ + tilegrid_ - (i / tilegrid_) - 1;
123  xh = xh * tilegrid_ + (i % tilegrid_);
124  }
125  xh -= tileoffx_;
126  yh -= tileoffy_;
127 
128  int prec1 = (len - p)/2;
129  real
130  unit = tile_,
131  x1 = unit * xh,
132  y1 = unit * yh;
133  for (int i = 0; i < prec1; ++i) {
134  unit /= base_;
135  int
136  ix = Utility::lookup(digits_, grid[p + i]),
137  iy = Utility::lookup(digits_, grid[p + i + prec1]);
138  if (ix < 0 || iy < 0)
139  throw GeographicErr("Encountered a non-digit in " + gridref);
140  x1 += unit * ix;
141  y1 += unit * iy;
142  }
143  if (centerp) {
144  x1 += unit/2;
145  y1 += unit/2;
146  }
147  x = x1;
148  y = y1;
149  prec = prec1;
150  }
151 
153  // Limits are all multiples of 100km and are all closed on the lower end
154  // and open on the upper end -- and this is reflected in the error
155  // messages. NaNs are let through.
156  if (x < minx_ || x >= maxx_)
157  throw GeographicErr("Easting " + Utility::str(int(floor(x/1000)))
158  + "km not in OSGB range ["
159  + Utility::str(minx_/1000) + "km, "
160  + Utility::str(maxx_/1000) + "km)");
161  if (y < miny_ || y >= maxy_)
162  throw GeographicErr("Northing " + Utility::str(int(floor(y/1000)))
163  + "km not in OSGB range ["
164  + Utility::str(miny_/1000) + "km, "
165  + Utility::str(maxy_/1000) + "km)");
166  }
167 
168 } // namespace GeographicLib
static const char *const letters_
Definition: OSGB.hpp:47
static T NaN()
Definition: Math.hpp:830
#define max(a, b)
Definition: datatypes.h:20
float real
Definition: datatypes.h:10
Scalar * y
return int(ret)+1
Header for GeographicLib::Utility class.
#define min(a, b)
Definition: datatypes.h:19
static bool isnan(T x)
Definition: Math.hpp:853
Math::real real
Definition: OSGB.hpp:46
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
Definition: Half.h:150
Transverse Mercator projection.
static const char *const digits_
Definition: OSGB.hpp:48
Header for GeographicLib::OSGB class.
static void CheckCoords(real x, real y)
Definition: src/OSGB.cpp:152
Namespace for GeographicLib.
static std::string str(T x, int p=-1)
Definition: Utility.hpp:276
static real computenorthoffset()
Definition: src/OSGB.cpp:26
static void GridReference(real x, real y, int prec, std::string &gridref)
Definition: src/OSGB.cpp:34
EIGEN_DEVICE_FUNC const FloorReturnType floor() const
Exception handling for GeographicLib.
Definition: Constants.hpp:389
Pose3 x1
Definition: testPose3.cpp:588
float * p
static int lookup(const std::string &s, char c)
Definition: Utility.hpp:459
int EIGEN_BLAS_FUNC() copy(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Definition: level1_impl.h:29
Jet< T, N > pow(const Jet< T, N > &f, double g)
Definition: jet.h:570
size_t len(handle h)
Definition: pytypes.h:1514
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
static const TransverseMercator & OSGBTM()
Definition: src/OSGB.cpp:20


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:43:06