#include <GeographicLib/Geohash.hpp>
#include <GeographicLib/Utility.hpp>
namespace GeographicLib {
using namespace std;
const char* const Geohash::lcdigits_ = "0123456789bcdefghjkmnpqrstuvwxyz";
const char* const Geohash::ucdigits_ = "0123456789BCDEFGHJKMNPQRSTUVWXYZ";
void Geohash::Forward(real lat, real lon, int len, string& geohash) {
using std::isnan; static const real shift = ldexp(real(1), 45);
static const real loneps = Math::hd / shift;
static const real lateps = Math::qd / shift;
if (fabs(lat) > Math::qd)
throw GeographicErr("Latitude " + Utility::str(lat)
+ "d not in [-" + to_string(Math::qd)
+ "d, " + to_string(Math::qd) + "d]");
if (isnan(lat) || isnan(lon)) {
geohash = "invalid";
return;
}
if (lat == Math::qd) lat -= lateps / 2;
lon = Math::AngNormalize(lon);
if (lon == Math::hd) lon = -Math::hd; len = max(0, min(int(maxlen_), len));
unsigned long long
ulon = (unsigned long long)(floor(lon/loneps) + shift),
ulat = (unsigned long long)(floor(lat/lateps) + shift);
char geohash1[maxlen_];
unsigned byte = 0;
for (unsigned i = 0; i < 5 * unsigned(len);) {
if ((i & 1) == 0) {
byte = (byte << 1) + unsigned((ulon & mask_) != 0);
ulon <<= 1;
} else {
byte = (byte << 1) + unsigned((ulat & mask_) != 0);
ulat <<= 1;
}
++i;
if (i % 5 == 0) {
geohash1[(i/5)-1] = lcdigits_[byte];
byte = 0;
}
}
geohash.resize(len);
copy(geohash1, geohash1 + len, geohash.begin());
}
void Geohash::Reverse(const string& geohash, real& lat, real& lon,
int& len, bool centerp) {
static const real shift = ldexp(real(1), 45);
static const real loneps = Math::hd / shift;
static const real lateps = Math::qd / shift;
int len1 = min(int(maxlen_), int(geohash.length()));
if (len1 >= 3 &&
((toupper(geohash[0]) == 'I' &&
toupper(geohash[1]) == 'N' &&
toupper(geohash[2]) == 'V') ||
(toupper(geohash[1]) == 'A' &&
toupper(geohash[0]) == 'N' &&
toupper(geohash[2]) == 'N'))) {
lat = lon = Math::NaN();
return;
}
unsigned long long ulon = 0, ulat = 0;
for (unsigned k = 0, j = 0; k < unsigned(len1); ++k) {
int byte = Utility::lookup(ucdigits_, geohash[k]);
if (byte < 0)
throw GeographicErr("Illegal character in geohash " + geohash);
for (unsigned m = 16; m; m >>= 1) {
if (j == 0)
ulon = (ulon << 1) + unsigned((byte & m) != 0);
else
ulat = (ulat << 1) + unsigned((byte & m) != 0);
j ^= 1;
}
}
ulon <<= 1; ulat <<= 1;
if (centerp) {
ulon += 1;
ulat += 1;
}
int s = 5 * (maxlen_ - len1);
ulon <<= (s / 2);
ulat <<= s - (s / 2);
lon = ulon * loneps - Math::hd;
lat = ulat * lateps - Math::qd;
len = len1;
}
}