GeographicLib 2.5.2

Loading...
Searching...
No Matches
Geohash.cpp
Go to the documentation of this file.
1/**
2 * \file Geohash.cpp
3 * \brief Implementation for GeographicLib::Geohash class
4 *
5 * Copyright (c) Charles Karney (2012-2022) <karney@alum.mit.edu> and licensed
6 * under the MIT/X11 License. For more information, see
7 * https://geographiclib.sourceforge.io/
8 **********************************************************************/
9
10#include <GeographicLib/Geohash.hpp>
11#include <GeographicLib/Utility.hpp>
12
13 namespace GeographicLib {
14
15 using namespace std;
16
17 const char* const Geohash::lcdigits_ = "0123456789bcdefghjkmnpqrstuvwxyz";
18 const char* const Geohash::ucdigits_ = "0123456789BCDEFGHJKMNPQRSTUVWXYZ";
19
20 void Geohash::Forward(real lat, real lon, int len, string& geohash) {
21 using std::isnan; // Needed for Centos 7, ubuntu 14
22 static const real shift = ldexp(real(1), 45);
23 static const real loneps = Math::hd / shift;
24 static const real lateps = Math::qd / shift;
25 if (fabs(lat) > Math::qd)
26 throw GeographicErr("Latitude " + Utility::str(lat)
27 + "d not in [-" + to_string(Math::qd)
28 + "d, " + to_string(Math::qd) + "d]");
29 if (isnan(lat) || isnan(lon)) {
30 geohash = "invalid";
31 return;
32 }
33 if (lat == Math::qd) lat -= lateps / 2;
34 lon = Math::AngNormalize(lon);
35 if (lon == Math::hd) lon = -Math::hd; // lon now in [-180,180)
36 // lon/loneps in [-2^45,2^45); lon/loneps + shift in [0,2^46)
37 // similarly for lat
38 len = max(0, min(int(maxlen_), len));
39 unsigned long long
40 ulon = (unsigned long long)(floor(lon/loneps) + shift),
41 ulat = (unsigned long long)(floor(lat/lateps) + shift);
42 char geohash1[maxlen_];
43 unsigned byte = 0;
44 for (unsigned i = 0; i < 5 * unsigned(len);) {
45 if ((i & 1) == 0) {
46 byte = (byte << 1) + unsigned((ulon & mask_) != 0);
47 ulon <<= 1;
48 } else {
49 byte = (byte << 1) + unsigned((ulat & mask_) != 0);
50 ulat <<= 1;
51 }
52 ++i;
53 if (i % 5 == 0) {
54 geohash1[(i/5)-1] = lcdigits_[byte];
55 byte = 0;
56 }
57 }
58 geohash.resize(len);
59 copy(geohash1, geohash1 + len, geohash.begin());
60 }
61
62 void Geohash::Reverse(const string& geohash, real& lat, real& lon,
63 int& len, bool centerp) {
64 static const real shift = ldexp(real(1), 45);
65 static const real loneps = Math::hd / shift;
66 static const real lateps = Math::qd / shift;
67 int len1 = min(int(maxlen_), int(geohash.length()));
68 if (len1 >= 3 &&
69 ((toupper(geohash[0]) == 'I' &&
70 toupper(geohash[1]) == 'N' &&
71 toupper(geohash[2]) == 'V') ||
72 // Check A first because it is not in a standard geohash
73 (toupper(geohash[1]) == 'A' &&
74 toupper(geohash[0]) == 'N' &&
75 toupper(geohash[2]) == 'N'))) {
76 lat = lon = Math::NaN();
77 return;
78 }
79 unsigned long long ulon = 0, ulat = 0;
80 for (unsigned k = 0, j = 0; k < unsigned(len1); ++k) {
81 int byte = Utility::lookup(ucdigits_, geohash[k]);
82 if (byte < 0)
83 throw GeographicErr("Illegal character in geohash " + geohash);
84 for (unsigned m = 16; m; m >>= 1) {
85 if (j == 0)
86 ulon = (ulon << 1) + unsigned((byte & m) != 0);
87 else
88 ulat = (ulat << 1) + unsigned((byte & m) != 0);
89 j ^= 1;
90 }
91 }
92 ulon <<= 1; ulat <<= 1;
93 if (centerp) {
94 ulon += 1;
95 ulat += 1;
96 }
97 int s = 5 * (maxlen_ - len1);
98 ulon <<= (s / 2);
99 ulat <<= s - (s / 2);
100 lon = ulon * loneps - Math::hd;
101 lat = ulat * lateps - Math::qd;
102 len = len1;
103 }
104
105} // namespace GeographicLib
Geohash.hpp
Header for GeographicLib::Geohash class.
Utility.hpp
Header for GeographicLib::Utility class.
GeographicLib::GeographicErr
Exception handling for GeographicLib.
Definition Constants.hpp:316
GeographicLib::Geohash::Forward
static void Forward(real lat, real lon, int len, std::string &geohash)
Definition Geohash.cpp:20
GeographicLib::Geohash::Reverse
static void Reverse(const std::string &geohash, real &lat, real &lon, int &len, bool centerp=true)
Definition Geohash.cpp:62
GeographicLib::Math::qd
static constexpr int qd
degrees per quarter turn
Definition Math.hpp:150
GeographicLib::Math::AngNormalize
static T AngNormalize(T x)
Definition Math.cpp:69
GeographicLib::Math::NaN
static T NaN()
Definition Math.cpp:283
GeographicLib::Math::hd
static constexpr int hd
degrees per half turn
Definition Math.hpp:153
GeographicLib::Utility::lookup
static int lookup(const std::string &s, char c)
Definition Utility.cpp:160
GeographicLib::Utility::str
static std::string str(T x, int p=-1)
Definition Utility.hpp:161
GeographicLib
Namespace for GeographicLib.
Definition Accumulator.cpp:12
std
Definition NearestNeighbor.hpp:806