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
gtsam.examples.DogLegOptimizerExample.int
int
Definition: DogLegOptimizerExample.py:111
GeographicLib::Math::NaN
static T NaN()
Definition: Math.hpp:830
GeographicLib::Utility::str
static std::string str(T x, int p=-1)
Definition: Utility.hpp:276
GeographicLib
Namespace for GeographicLib.
Definition: JacobiConformal.hpp:15
GeographicLib::OSGB::CheckCoords
static void CheckCoords(real x, real y)
Definition: src/OSGB.cpp:152
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
x
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
Definition: gnuplot_common_settings.hh:12
real
float real
Definition: datatypes.h:10
copy
int EIGEN_BLAS_FUNC() copy(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Definition: level1_impl.h:29
GeographicLib::GeographicErr
Exception handling for GeographicLib.
Definition: Constants.hpp:389
GeographicLib::Utility::lookup
static int lookup(const std::string &s, char c)
Definition: Utility.hpp:459
GeographicLib::OSGB::GridReference
static void GridReference(real x, real y, int prec, std::string &gridref)
Definition: src/OSGB.cpp:34
GeographicLib::Math::real
double real
Definition: Math.hpp:129
GeographicLib::OSGB::OSGBTM
static const TransverseMercator & OSGBTM()
Definition: src/OSGB.cpp:20
y1
double y1(double x)
Definition: j1.c:199
x1
Pose3 x1
Definition: testPose3.cpp:663
OSGB.hpp
Header for GeographicLib::OSGB class.
Utility.hpp
Header for GeographicLib::Utility class.
pybind_wrapper_test_script.z
z
Definition: pybind_wrapper_test_script.py:61
ceres::pow
Jet< T, N > pow(const Jet< T, N > &f, double g)
Definition: jet.h:570
GeographicLib::OSGB::digits_
static const char *const digits_
Definition: OSGB.hpp:48
GeographicLib::Math::isnan
static bool isnan(T x)
Definition: Math.hpp:853
y
Scalar * y
Definition: level1_cplx_impl.h:124
std
Definition: BFloat16.h:88
p
float * p
Definition: Tutorial_Map_using.cpp:9
min
#define min(a, b)
Definition: datatypes.h:19
len
size_t len(handle h)
Get the length of a Python object.
Definition: pytypes.h:2446
max
#define max(a, b)
Definition: datatypes.h:20
GeographicLib::OSGB::letters_
static const char *const letters_
Definition: OSGB.hpp:47
real
Definition: main.h:100
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
GeographicLib::OSGB::computenorthoffset
static real computenorthoffset()
Definition: src/OSGB.cpp:26
GeographicLib::TransverseMercator
Transverse Mercator projection.
Definition: TransverseMercator.hpp:93
floor
const EIGEN_DEVICE_FUNC FloorReturnType floor() const
Definition: ArrayCwiseUnaryOps.h:481


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:03:14