GeographicLib 2.1.2
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-2020) <charles@karney.com> and licensed
6 * under the MIT/X11 License. For more information, see
7 * https://geographiclib.sourceforge.io/
8 **********************************************************************/
9
12
13#if defined(_MSC_VER)
14// Squelch warnings about enum-float expressions
15# pragma warning (disable: 5055)
16#endif
17
18namespace GeographicLib {
19
20 using namespace std;
21
22 const char* const OSGB::letters_ = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
23 const char* const OSGB::digits_ = "0123456789";
24
25 const TransverseMercator& OSGB::OSGBTM() {
26 static const TransverseMercator osgbtm(EquatorialRadius(), Flattening(),
27 CentralScale());
28 return osgbtm;
29 }
30
31 Math::real OSGB::computenorthoffset() {
32 real x, y;
33 static const real northoffset =
34 ( OSGBTM().Forward(real(0), OriginLatitude(), real(0), x, y),
35 FalseNorthing() - y );
36 return northoffset;
37 }
38
39 void OSGB::GridReference(real x, real y, int prec, std::string& gridref) {
40 using std::isnan; // Needed for Centos 7, ubuntu 14
41 CheckCoords(x, y);
42 if (!(prec >= 0 && prec <= maxprec_))
43 throw GeographicErr("OSGB precision " + Utility::str(prec)
44 + " not in [0, "
45 + Utility::str(int(maxprec_)) + "]");
46 if (isnan(x) || isnan(y)) {
47 gridref = "INVALID";
48 return;
49 }
50 char grid[2 + 2 * maxprec_];
51 int
52 xh = int(floor(x / tile_)),
53 yh = int(floor(y / tile_));
54 real
55 xf = x - tile_ * xh,
56 yf = y - tile_ * yh;
57 xh += tileoffx_;
58 yh += tileoffy_;
59 int z = 0;
60 grid[z++] = letters_[(tilegrid_ - (yh / tilegrid_) - 1)
61 * tilegrid_ + (xh / tilegrid_)];
62 grid[z++] = letters_[(tilegrid_ - (yh % tilegrid_) - 1)
63 * tilegrid_ + (xh % tilegrid_)];
64 // Need extra real because, since C++11, pow(float, int) returns double
65 real mult = real(pow(real(base_), max(tilelevel_ - prec, 0)));
66 int
67 ix = int(floor(xf / mult)),
68 iy = int(floor(yf / mult));
69 for (int c = min(prec, int(tilelevel_)); c--;) {
70 grid[z + c] = digits_[ ix % base_ ];
71 ix /= base_;
72 grid[z + c + prec] = digits_[ iy % base_ ];
73 iy /= base_;
74 }
75 if (prec > tilelevel_) {
76 xf -= floor(xf / mult);
77 yf -= floor(yf / mult);
78 mult = real(pow(real(base_), prec - tilelevel_));
79 ix = int(floor(xf * mult));
80 iy = int(floor(yf * mult));
81 for (int c = prec - tilelevel_; c--;) {
82 grid[z + c + tilelevel_] = digits_[ ix % base_ ];
83 ix /= base_;
84 grid[z + c + tilelevel_ + prec] = digits_[ iy % base_ ];
85 iy /= base_;
86 }
87 }
88 int mlen = z + 2 * prec;
89 gridref.resize(mlen);
90 copy(grid, grid + mlen, gridref.begin());
91 }
92
93 void OSGB::GridReference(const std::string& gridref,
94 real& x, real& y, int& prec,
95 bool centerp) {
96 int
97 len = int(gridref.size()),
98 p = 0;
99 if (len >= 2 &&
100 toupper(gridref[0]) == 'I' &&
101 toupper(gridref[1]) == 'N') {
102 x = y = Math::NaN();
103 prec = -2; // For compatibility with MGRS::Reverse.
104 return;
105 }
106 char grid[2 + 2 * maxprec_];
107 for (int i = 0; i < len; ++i) {
108 if (!isspace(gridref[i])) {
109 if (p >= 2 + 2 * maxprec_)
110 throw GeographicErr("OSGB string " + gridref + " too long");
111 grid[p++] = gridref[i];
112 }
113 }
114 len = p;
115 p = 0;
116 if (len < 2)
117 throw GeographicErr("OSGB string " + gridref + " too short");
118 if (len % 2)
119 throw GeographicErr("OSGB string " + gridref +
120 " has odd number of characters");
121 int
122 xh = 0,
123 yh = 0;
124 while (p < 2) {
125 int i = Utility::lookup(letters_, grid[p++]);
126 if (i < 0)
127 throw GeographicErr("Illegal prefix character " + gridref);
128 yh = yh * tilegrid_ + tilegrid_ - (i / tilegrid_) - 1;
129 xh = xh * tilegrid_ + (i % tilegrid_);
130 }
131 xh -= tileoffx_;
132 yh -= tileoffy_;
133
134 int prec1 = (len - p)/2;
135 real
136 unit = tile_,
137 x1 = unit * xh,
138 y1 = unit * yh;
139 for (int i = 0; i < prec1; ++i) {
140 unit /= base_;
141 int
142 ix = Utility::lookup(digits_, grid[p + i]),
143 iy = Utility::lookup(digits_, grid[p + i + prec1]);
144 if (ix < 0 || iy < 0)
145 throw GeographicErr("Encountered a non-digit in " + gridref);
146 x1 += unit * ix;
147 y1 += unit * iy;
148 }
149 if (centerp) {
150 x1 += unit/2;
151 y1 += unit/2;
152 }
153 x = x1;
154 y = y1;
155 prec = prec1;
156 }
157
158 void OSGB::CheckCoords(real x, real y) {
159 // Limits are all multiples of 100km and are all closed on the lower end
160 // and open on the upper end -- and this is reflected in the error
161 // messages. NaNs are let through.
162 if (x < minx_ || x >= maxx_)
163 throw GeographicErr("Easting " + Utility::str(int(floor(x/1000)))
164 + "km not in OSGB range ["
165 + Utility::str(minx_/1000) + "km, "
166 + Utility::str(maxx_/1000) + "km)");
167 if (y < miny_ || y >= maxy_)
168 throw GeographicErr("Northing " + Utility::str(int(floor(y/1000)))
169 + "km not in OSGB range ["
170 + Utility::str(miny_/1000) + "km, "
171 + Utility::str(maxy_/1000) + "km)");
172 }
173
174} // namespace GeographicLib
GeographicLib::Math::real real
Definition: GeodSolve.cpp:31
Header for GeographicLib::OSGB class.
Header for GeographicLib::Utility class.
Exception handling for GeographicLib.
Definition: Constants.hpp:316
static T NaN()
Definition: Math.cpp:250
static void GridReference(real x, real y, int prec, std::string &gridref)
Definition: OSGB.cpp:39
static Math::real CentralScale()
Definition: OSGB.hpp:226
static Math::real EquatorialRadius()
Definition: OSGB.hpp:202
static Math::real FalseNorthing()
Definition: OSGB.hpp:245
static Math::real Flattening()
Definition: OSGB.hpp:217
static Math::real OriginLatitude()
Definition: OSGB.hpp:234
void Forward(real lon0, real lat, real lon, real &x, real &y, real &gamma, real &k) const
static int lookup(const std::string &s, char c)
Definition: Utility.cpp:160
static std::string str(T x, int p=-1)
Definition: Utility.hpp:161
Namespace for GeographicLib.
Definition: Accumulator.cpp:12