GeographicLib  1.44
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-2015) <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 string Geohash::lcdigits_ = "0123456789bcdefghjkmnpqrstuvwxyz";
18  const string Geohash::ucdigits_ = "0123456789BCDEFGHJKMNPQRSTUVWXYZ";
19 
20  void Geohash::Forward(real lat, real lon, int len, std::string& geohash) {
21  static const real shift = pow(real(2), 45);
22  static const real loneps = 180 / shift;
23  static const real lateps = 90 / shift;
24  if (abs(lat) > 90)
25  throw GeographicErr("Latitude " + Utility::str(lat)
26  + "d not in [-90d, 90d]");
27  if (Math::isnan(lat) || Math::isnan(lon)) {
28  geohash = "invalid";
29  return;
30  }
31  if (lat == 90) lat -= lateps / 2;
32  lon = Math::AngNormalize(lon); // lon in [-180,180)
33  // lon/loneps in [-2^45,2^45); lon/loneps + shift in [0,2^46)
34  // similarly for lat
35  len = max(0, min(int(maxlen_), len));
36  unsigned long long
37  ulon = (unsigned long long)(floor(lon/loneps) + shift),
38  ulat = (unsigned long long)(floor(lat/lateps) + shift);
39  char geohash1[maxlen_];
40  unsigned byte = 0;
41  for (unsigned i = 0; i < 5 * unsigned(len);) {
42  if ((i & 1) == 0) {
43  byte = (byte << 1) + unsigned((ulon & mask_) != 0);
44  ulon <<= 1;
45  } else {
46  byte = (byte << 1) + unsigned((ulat & mask_) != 0);
47  ulat <<= 1;
48  }
49  ++i;
50  if (i % 5 == 0) {
51  geohash1[(i/5)-1] = lcdigits_[byte];
52  byte = 0;
53  }
54  }
55  geohash.resize(len);
56  copy(geohash1, geohash1 + len, geohash.begin());
57  }
58 
59  void Geohash::Reverse(const std::string& geohash, real& lat, real& lon,
60  int& len, bool centerp) {
61  static const real shift = pow(real(2), 45);
62  static const real loneps = 180 / shift;
63  static const real lateps = 90 / shift;
64  int len1 = min(int(maxlen_), int(geohash.length()));
65  if (len1 >= 3 &&
66  ((toupper(geohash[0]) == 'I' &&
67  toupper(geohash[1]) == 'N' &&
68  toupper(geohash[2]) == 'V') ||
69  // Check A first because it is not in a standard geohash
70  (toupper(geohash[1]) == 'A' &&
71  toupper(geohash[0]) == 'N' &&
72  toupper(geohash[2]) == 'N'))) {
73  lat = lon = Math::NaN();
74  return;
75  }
76  unsigned long long ulon = 0, ulat = 0;
77  for (unsigned k = 0, j = 0; k < unsigned(len1); ++k) {
78  int byte = Utility::lookup(ucdigits_, geohash[k]);
79  if (byte < 0)
80  throw GeographicErr("Illegal character in geohash " + geohash);
81  for (unsigned m = 16; m; m >>= 1) {
82  if (j == 0)
83  ulon = (ulon << 1) + unsigned((byte & m) != 0);
84  else
85  ulat = (ulat << 1) + unsigned((byte & m) != 0);
86  j ^= 1;
87  }
88  }
89  ulon <<= 1; ulat <<= 1;
90  if (centerp) {
91  ulon += 1;
92  ulat += 1;
93  }
94  int s = 5 * (maxlen_ - len1);
95  ulon <<= (s / 2);
96  ulat <<= s - (s / 2);
97  lon = ulon * loneps - 180;
98  lat = ulat * lateps - 90;
99  len = len1;
100  }
101 
102 } // namespace GeographicLib
static T AngNormalize(T x)
Definition: Math.hpp:451
static T NaN()
Definition: Math.hpp:783
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
Header for GeographicLib::Utility class.
static bool isnan(T x)
Definition: Math.hpp:800
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:20
Exception handling for GeographicLib.
Definition: Constants.hpp:386
static int lookup(const std::string &s, char c)
Definition: Utility.hpp:407
static void Reverse(const std::string &geohash, real &lat, real &lon, int &len, bool centerp=true)
Definition: Geohash.cpp:59
Header for GeographicLib::Geohash class.