17 const char*
const Georef::digits_ =
"0123456789";
18 const char*
const Georef::lontile_ =
"ABCDEFGHJKLMNPQRSTUVWXYZ";
19 const char*
const Georef::lattile_ =
"ABCDEFGHJKLM";
20 const char*
const Georef::degrees_ =
"ABCDEFGHJKLMNPQ";
26 +
"d not in [-90d, 90d]");
27 if (isnan(lat) || isnan(lon)) {
32 if (lat == 90) lat *= (1 - numeric_limits<real>::epsilon() / 2);
33 prec = max(-1, min(
int(maxprec_), prec));
34 if (prec == 1) ++prec;
37 static_assert(numeric_limits<long long>::digits >= 45,
38 "long long not wide enough to store 21600e9");
39 const long long m = 60000000000LL;
41 x = (
long long)(floor(lon *
real(m))) - lonorig_ * m,
42 y = (
long long)(floor(lat *
real(m))) - latorig_ * m;
43 int ilon = int(x / m);
int ilat = int(y / m);
44 char georef1[maxlen_];
45 georef1[0] = lontile_[ilon / tile_];
46 georef1[1] = lattile_[ilat / tile_];
48 georef1[2] = degrees_[ilon % tile_];
49 georef1[3] = degrees_[ilat % tile_];
51 x -= m * ilon; y -= m * ilat;
52 long long d = (
long long)pow(
real(base_), maxprec_ - prec);
54 for (
int c = prec; c--;) {
55 georef1[baselen_ + c ] = digits_[x % base_]; x /= base_;
56 georef1[baselen_ + c + prec] = digits_[y % base_]; y /= base_;
60 georef.resize(baselen_ + 2 * prec);
61 copy(georef1, georef1 + baselen_ + 2 * prec, georef.begin());
65 int& prec,
bool centerp) {
66 int len = int(georef.length());
68 toupper(georef[0]) ==
'I' &&
69 toupper(georef[1]) ==
'N' &&
70 toupper(georef[2]) ==
'V') {
74 if (len < baselen_ - 2)
75 throw GeographicErr(
"Georef must start with at least 2 letters "
77 int prec1 = (2 + len - baselen_) / 2 - 1;
81 throw GeographicErr(
"Bad longitude tile letter in georef " + georef);
82 real lon1 = k + lonorig_ / tile_;
85 throw GeographicErr(
"Bad latitude tile letter in georef " + georef);
86 real lat1 = k + latorig_ / tile_;
92 throw GeographicErr(
"Bad longitude degree letter in georef " + georef);
93 lon1 = lon1 * tile_ + k;
95 throw GeographicErr(
"Missing latitude degree letter in georef "
99 throw GeographicErr(
"Bad latitude degree letter in georef " + georef);
100 lat1 = lat1 * tile_ + k;
102 if (georef.find_first_not_of(digits_, baselen_) != string::npos)
103 throw GeographicErr(
"Non digits in trailing portion of georef "
104 + georef.substr(baselen_));
106 throw GeographicErr(
"Georef must end with an even number of digits "
107 + georef.substr(baselen_));
109 throw GeographicErr(
"Georef needs at least 4 digits for minutes "
110 + georef.substr(baselen_));
111 if (prec1 > maxprec_)
113 +
" digits in georef "
114 + georef.substr(baselen_));
115 for (
int i = 0; i < prec1; ++i) {
116 int m = i ? base_ : 6;
121 if (!(i || (x < m && y < m)))
122 throw GeographicErr(
"Minutes terms in georef must be less than 60 "
123 + georef.substr(baselen_));
130 unit *= 2; lat1 = 2 * lat1 + 1; lon1 = 2 * lon1 + 1;
132 lat = (tile_ * lat1) / unit;
133 lon = (tile_ * lon1) / unit;
GeographicLib::Math::real real
Header for GeographicLib::Georef class.
Header for GeographicLib::Utility class.
Exception handling for GeographicLib.
static void Forward(real lat, real lon, int prec, std::string &georef)
static void Reverse(const std::string &georef, real &lat, real &lon, int &prec, bool centerp=true)
static T AngNormalize(T x)
static int lookup(const std::string &s, char c)
static std::string str(T x, int p=-1)
Namespace for GeographicLib.