GeographicLib  1.43
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-2014) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * http://geographiclib.sourceforge.net/
8  **********************************************************************/
9 
12 
13 namespace GeographicLib {
14 
15  using namespace std;
16 
17  const int Geohash::decprec_[] = {-2, -1, 0, 0, 1, 2, 3, 3, 4, 5,
18  6, 6, 7, 8, 9, 9, 10, 11, 12};
19  const string Geohash::lcdigits_ = "0123456789bcdefghjkmnpqrstuvwxyz";
20  const string Geohash::ucdigits_ = "0123456789BCDEFGHJKMNPQRSTUVWXYZ";
21 
22  void Geohash::Forward(real lat, real lon, int len, std::string& geohash) {
23  if (abs(lat) > 90)
24  throw GeographicErr("Latitude " + Utility::str(lat)
25  + "d not in [-90d, 90d]");
26  if (lon < -540 || lon >= 540)
27  throw GeographicErr("Longitude " + Utility::str(lon)
28  + "d not in [-540d, 540d)");
29  if (Math::isnan(lat) || Math::isnan(lon)) {
30  geohash = "nan";
31  return;
32  }
33  if (lat == 90) lat -= lateps() / 2;
34  lon = Math::AngNormalize(lon); // lon in [-180,180)
35  // lon/loneps in [-2^45,2^45); lon/loneps + shift in [0,2^46)
36  // similarly for lat
37  len = max(0, min(int(maxlen_), len));
38  unsigned long long
39  ulon = (unsigned long long)(floor(lon/loneps()) + shift()),
40  ulat = (unsigned long long)(floor(lat/lateps()) + shift());
41  char geohash1[maxlen_];
42  unsigned byte = 0;
43  for (unsigned i = 0; i < 5 * unsigned(len);) {
44  if ((i & 1) == 0) {
45  byte = (byte << 1) + unsigned((ulon & mask_) != 0);
46  ulon <<= 1;
47  } else {
48  byte = (byte << 1) + unsigned((ulat & mask_) != 0);
49  ulat <<= 1;
50  }
51  ++i;
52  if (i % 5 == 0) {
53  geohash1[(i/5)-1] = lcdigits_[byte];
54  byte = 0;
55  }
56  }
57  geohash.resize(len);
58  copy(geohash1, geohash1 + len, geohash.begin());
59  }
60 
61  void Geohash::Reverse(const std::string& geohash, real& lat, real& lon,
62  int& len, bool centerp) {
63  len = min(int(maxlen_), int(geohash.length()));
64  if (len >= 3 &&
65  toupper(geohash[0]) == 'N' &&
66  toupper(geohash[1]) == 'A' &&
67  toupper(geohash[2]) == 'N') {
68  lat = lon = Math::NaN();
69  return;
70  }
71  unsigned long long ulon = 0, ulat = 0;
72  for (unsigned k = 0, j = 0; k < unsigned(len); ++k) {
73  int byte = Utility::lookup(ucdigits_, geohash[k]);
74  if (byte < 0)
75  throw GeographicErr("Illegal character in geohash " + geohash);
76  for (unsigned m = 16; m; m >>= 1) {
77  if (j == 0)
78  ulon = (ulon << 1) + unsigned((byte & m) != 0);
79  else
80  ulat = (ulat << 1) + unsigned((byte & m) != 0);
81  j ^= 1;
82  }
83  }
84  ulon <<= 1; ulat <<= 1;
85  if (centerp) {
86  ulon += 1;
87  ulat += 1;
88  }
89  int s = 5 * (maxlen_ - len);
90  ulon <<= (s / 2);
91  ulat <<= s - (s / 2);
92  lon = ulon * loneps() - 180;
93  lat = ulat * lateps() - 90;
94  }
95 
96 } // namespace GeographicLib
static T AngNormalize(T x)
Definition: Math.hpp:445
static T NaN()
Definition: Math.hpp:629
Header for GeographicLib::Utility class.
static bool isnan(T x)
Definition: Math.hpp:646
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static std::string str(T x, int p=-1)
Definition: Utility.hpp:276
static void Forward(real lat, real lon, int len, std::string &geohash)
Definition: Geohash.cpp:22
Exception handling for GeographicLib.
Definition: Constants.hpp:382
static int lookup(const std::string &s, char c)
Definition: Utility.hpp:405
static void Reverse(const std::string &geohash, real &lat, real &lon, int &len, bool centerp=true)
Definition: Geohash.cpp:61
Header for GeographicLib::Geohash class.