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