Static Public Member Functions | Private Types | Private Member Functions | Static Private Attributes | List of all members
GeographicLib::Geohash Class Reference

Conversions for geohashes. More...

#include <Geohash.hpp>

Static Public Member Functions

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

Private Types

typedef Math::real real
 

Private Member Functions

 Geohash ()
 

Static Private Attributes

static const char *const lcdigits_ = "0123456789bcdefghjkmnpqrstuvwxyz"
 
static const unsigned long long mask_ = 1ULL << 45
 
static const int maxlen_ = 18
 
static const char *const ucdigits_ = "0123456789BCDEFGHJKMNPQRSTUVWXYZ"
 

Detailed Description

Conversions for geohashes.

Geohashes are described in

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>
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;
cout << fixed;
for (unsigned i = 0; i <= geohash.length(); ++i) {
int len;
Geohash::Reverse(geohash.substr(0, i), lat, lon, len);
cout << setprecision(max(0, Geohash::DecimalPrecision(len)))
<< len << " " << lat << " " << lon << "\n";
}
}
}
catch (const exception& e) {
cerr << "Caught exception: " << e.what() << "\n";
return 1;
}
}

Definition at line 40 of file Geohash.hpp.

Member Typedef Documentation

◆ real

Definition at line 42 of file Geohash.hpp.

Constructor & Destructor Documentation

◆ Geohash()

GeographicLib::Geohash::Geohash ( )
private

Member Function Documentation

◆ DecimalPrecision()

static 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]lenthe 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 167 of file Geohash.hpp.

◆ Forward()

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

Convert from geographic coordinates to a geohash.

Parameters
[in]latlatitude of point (degrees).
[in]lonlongitude of point (degrees).
[in]lenthe length of the resulting geohash.
[out]geohashthe geohash.
Exceptions
GeographicErrif lat is not in [−90°, 90°].
std::bad_allocif 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 src/Geohash.cpp.

◆ GeohashLength() [1/2]

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

The geohash length required to meet a given geographic resolution.

Parameters
[in]latresthe resolution in latitude (degrees).
[in]lonresthe resolution in longitude (degrees).
Returns
geohash length.

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

Definition at line 145 of file Geohash.hpp.

◆ GeohashLength() [2/2]

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

The geohash length required to meet a given geographic resolution.

Parameters
[in]resthe minimum of resolution in latitude and longitude (degrees).
Returns
geohash length.

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

Definition at line 128 of file Geohash.hpp.

◆ LatitudeResolution()

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

The latitude resolution of a geohash.

Parameters
[in]lenthe length of the geohash.
Returns
the latitude resolution (degrees).

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

Definition at line 99 of file Geohash.hpp.

◆ LongitudeResolution()

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

The longitude resolution of a geohash.

Parameters
[in]lenthe length of the geohash.
Returns
the longitude resolution (degrees).

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

Definition at line 113 of file Geohash.hpp.

◆ 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]geohashthe geohash.
[out]latlatitude of point (degrees).
[out]lonlongitude of point (degrees).
[out]lenthe length of the geohash.
[in]centerpif true (the default) return the center of the geohash location, otherwise return the south-west corner.
Exceptions
GeographicErrif 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 59 of file src/Geohash.cpp.

Member Data Documentation

◆ lcdigits_

const char *const GeographicLib::Geohash::lcdigits_ = "0123456789bcdefghjkmnpqrstuvwxyz"
staticprivate

Definition at line 45 of file Geohash.hpp.

◆ mask_

const unsigned long long GeographicLib::Geohash::mask_ = 1ULL << 45
staticprivate

Definition at line 44 of file Geohash.hpp.

◆ maxlen_

const int GeographicLib::Geohash::maxlen_ = 18
staticprivate

Definition at line 43 of file Geohash.hpp.

◆ ucdigits_

const char *const GeographicLib::Geohash::ucdigits_ = "0123456789BCDEFGHJKMNPQRSTUVWXYZ"
staticprivate

Definition at line 46 of file Geohash.hpp.


The documentation for this class was generated from the following files:
GeographicLib::Geohash::Forward
static void Forward(real lat, real lon, int len, std::string &geohash)
Definition: src/Geohash.cpp:20
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
GeographicLib
Namespace for GeographicLib.
Definition: JacobiConformal.hpp:15
main
int main(int argc, char **argv)
Definition: cmake/example_cmake_find_gtsam/main.cpp:63
GeographicLib::Geohash::DecimalPrecision
static int DecimalPrecision(int len)
Definition: Geohash.hpp:167
GeographicLib::Geohash::GeohashLength
static int GeohashLength(real res)
Definition: Geohash.hpp:128
Geohash.hpp
Header for GeographicLib::Geohash class.
GeographicLib::Geohash::Reverse
static void Reverse(const std::string &geohash, real &lat, real &lon, int &len, bool centerp=true)
Definition: src/Geohash.cpp:59
std
Definition: BFloat16.h:88
len
size_t len(handle h)
Get the length of a Python object.
Definition: pytypes.h:2446
lon
static const double lon
Definition: testGeographicLib.cpp:34
max
#define max(a, b)
Definition: datatypes.h:20
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
lat
static const double lat
Definition: testGeographicLib.cpp:34


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:15:07