GeographicLib: GeographicLib::Geohash Class Reference

GeographicLib 2.5.2

Loading...
Searching...
No Matches
GeographicLib::Geohash Class Reference

Conversions for geohashes. More...

`#include <GeographicLib/Geohash.hpp>`

## Static Public Member Functions
static void | Forward (real lat, real lon, int len, std::string &geohash)
static void | Reverse (const std::string &geohash, real &lat, real &lon, int &len, bool centerp=true)
static Math::real | LatitudeResolution (int len)
static Math::real | LongitudeResolution (int len)
static int | GeohashLength (real res)
static int | GeohashLength (real latres, real lonres)
static int | DecimalPrecision (int len)

## Detailed Description

Conversions for geohashes.

Geohashes are described in

- https://en.wikipedia.org/wiki/Geohash
- http://geohash.org/

They provide a compact string representation of a particular geographic location (expressed as latitude and longitude), with the property that if trailing characters are dropped from the string the geographic location remains nearby. The classes Georef and GARS implement similar compact representations.

Example of use:

// Example of using the GeographicLib::Geohash class
#include <iostream>
#include <iomanip>
#include <exception>
#include <string>
#include <GeographicLib/Geohash.hpp>
using namespace std;
using namespace GeographicLib;
int main() {
try {
{
// Sample forward calculation
double lat = 57.64911, lon = 10.40744; // Jutland (the wikipedia example)
string geohash;
int maxlen = Geohash::GeohashLength(1.0e-5);
for (int len = 0; len <= maxlen; ++len) {
Geohash::Forward(lat, lon, len, geohash);
cout << len << " " << geohash << "\n";
}
}
{
// Sample reverse calculation
string geohash = "u4pruydqqvj";
double lat, lon, latres, lonres;
cout << fixed;
for (unsigned i = 0; i <= geohash.length(); ++i) {
int len;
Geohash::Reverse(geohash.substr(0, i), lat, lon, len);
latres = Geohash::LatitudeResolution(len);
lonres = Geohash::LongitudeResolution(len);
cout << setprecision(max(0, Geohash::DecimalPrecision(len)))
<< len << " "
<< lat << "+/-" << latres/2 << " "
<< lon << "+/-" << lonres/2 << "\n";
}
}
}
catch (const exception& e) {
cerr << "Caught exception: " << e.what() << "\n";
return 1;
}
}
main
int main(int argc, const char *const argv[])
Definition CartConvert.cpp:28
Geohash.hpp
Header for GeographicLib::Geohash class.
GeographicLib::Geohash::LatitudeResolution
static Math::real LatitudeResolution(int len)
Definition Geohash.hpp:93
GeographicLib::Geohash::DecimalPrecision
static int DecimalPrecision(int len)
Definition Geohash.hpp:161
GeographicLib::Geohash::LongitudeResolution
static Math::real LongitudeResolution(int len)
Definition Geohash.hpp:107
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::Geohash::GeohashLength
static int GeohashLength(real res)
Definition Geohash.hpp:122
GeographicLib
Namespace for GeographicLib.
Definition Accumulator.cpp:12
std
Definition NearestNeighbor.hpp:806

Definition at line 34 of file Geohash.hpp.

## Member Function Documentation

## ◆ Forward()

void GeographicLib::Geohash::Forward | ( | real | lat,
|  | real | lon,
|  | int | len,
|  | std::string & | geohash )
| static

Convert from geographic coordinates to a geohash.

Parameters

  [in] | lat | latitude of point (degrees).
  [in] | lon | longitude of point (degrees).
  [in] | len | the length of the resulting geohash.
  [out] | geohash | the geohash.

Exceptions

  GeographicErr | if lat is not in [−90°, 90°].
  std::bad_alloc | if memory for geohash can't be allocated.

Internally, len is first put in the range [0, 18]. (len = 18 provides approximately 1μm precision.)

If lat or lon is NaN, the returned geohash is "invalid".

Definition at line 20 of file Geohash.cpp.

References GeographicLib::Math::AngNormalize(), GeographicLib::Math::hd, GeographicLib::Math::qd, and GeographicLib::Utility::str().

## ◆ Reverse()

void GeographicLib::Geohash::Reverse | ( | const std::string & | geohash,
|  | real & | lat,
|  | real & | lon,
|  | int & | len,
|  | bool | centerp = true )
| static

Convert from a geohash to geographic coordinates.

Parameters

  [in] | geohash | the geohash.
  [out] | lat | latitude of point (degrees).
  [out] | lon | longitude of point (degrees).
  [out] | len | the length of the geohash.
  [in] | centerp | if true (the default) return the center of the geohash location, otherwise return the south-west corner.

Exceptions

  GeographicErr | if geohash contains illegal characters.

Only the first 18 characters for geohash are considered. (18 characters provides approximately 1μm precision.) The case of the letters in geohash is ignored.

If the first 3 characters of geohash are "inv", then lat and lon are set to NaN and len is unchanged. ("nan" is treated similarly.)

Definition at line 62 of file Geohash.cpp.

References GeographicLib::Math::hd, GeographicLib::Utility::lookup(), GeographicLib::Math::NaN(), and GeographicLib::Math::qd.

## ◆ LatitudeResolution()

Math::real GeographicLib::Geohash::LatitudeResolution | ( | int | len | ) |
| inlinestatic

The latitude resolution of a geohash.

Parameters

  [in] | len | the length of the geohash.

Returns
  the latitude resolution (degrees).

Internally, len is first put in the range [0, 18].

Definition at line 93 of file Geohash.hpp.

References GeographicLib::Math::hd.

Referenced by DecimalPrecision(), and GeohashLength().

## ◆ LongitudeResolution()

Math::real GeographicLib::Geohash::LongitudeResolution | ( | int | len | ) |
| inlinestatic

The longitude resolution of a geohash.

Parameters

  [in] | len | the length of the geohash.

Returns
  the longitude resolution (degrees).

Internally, len is first put in the range [0, 18].

Definition at line 107 of file Geohash.hpp.

References GeographicLib::Math::td.

Referenced by GeohashLength(), and GeohashLength().

## ◆ GeohashLength() [1/2]

int GeographicLib::Geohash::GeohashLength | ( | real | res | ) |
| inlinestatic

The geohash length required to meet a given geographic resolution.

Parameters

  [in] | res | the minimum of resolution in latitude and longitude (degrees).

Returns
  geohash length.

The returned length is in the range [0, 18].

Definition at line 122 of file Geohash.hpp.

References LongitudeResolution().

## ◆ GeohashLength() [2/2]

int GeographicLib::Geohash::GeohashLength | ( | real | latres,
|  | real | lonres )
| inlinestatic

The geohash length required to meet a given geographic resolution.

Parameters

  [in] | latres | the resolution in latitude (degrees).
  [in] | lonres | the resolution in longitude (degrees).

Returns
  geohash length.

The returned length is in the range [0, 18].

Definition at line 139 of file Geohash.hpp.

References LatitudeResolution(), and LongitudeResolution().

## ◆ DecimalPrecision()

int GeographicLib::Geohash::DecimalPrecision | ( | int | len | ) |
| inlinestatic

The decimal geographic precision required to match a given geohash length. This is the number of digits needed after decimal point in a decimal degrees representation.

Parameters

  [in] | len | the length of the geohash.

Returns
  the decimal precision (may be negative).

Internally, len is first put in the range [0, 18]. The returned decimal precision is in the range [−2, 12].

Definition at line 161 of file Geohash.hpp.

References LatitudeResolution().

The documentation for this class was generated from the following files:

- Geohash.hpp
- Geohash.cpp