Fix geohash decoding

This commit is contained in:
Carles Fernandez 2023-03-18 08:47:35 +01:00
parent 18e5345669
commit da1a75ec39
No known key found for this signature in database
GPG Key ID: 4C583C52B0C3877D
2 changed files with 19 additions and 8 deletions

View File

@ -122,8 +122,12 @@ std::array<double, 2> Geohash::decode(std::string geohash) const
// round to close to centre without excessive precision: ⌊2-log10(Δ°)⌋ decimal places
std::array<double, 2> latlon{};
latlon[0] = std::floor(lat * std::pow(10, std::floor(2.0 - std::log10(latMax - latMin))));
latlon[1] = std::floor(lon * std::pow(10, std::floor(2.0 - std::log10(lonMax - lonMin))));
int decimalPlaces = std::floor(2.0 - std::log10(latMax - latMin));
double factor = std::pow(10, decimalPlaces);
latlon[0] = std::round(lat * factor) / factor;
int decimalPlaces2 = std::floor(2.0 - std::log10(lonMax - lonMin));
double factor2 = std::pow(10, decimalPlaces2);
latlon[1] = std::round(lon * factor2) / factor2;
return latlon;
}

View File

@ -19,15 +19,22 @@
TEST(Geohash_Test, Encode)
{
Geohash gh = Geohash();
std::string hash;
EXPECT_NO_THROW(hash = gh.encode(52.205, 0.119, 7));
EXPECT_EQ(0, hash.compare("u120fxw"));
std::string geohash;
EXPECT_NO_THROW(geohash = gh.encode(52.205, 0.119, 7));
EXPECT_EQ(0, geohash.compare("u120fxw"));
EXPECT_THROW(gh.encode(52.205, 0.119, 0), std::invalid_argument);
}
TEST(Geohash_Test, precision)
TEST(Geohash_Test, Decode)
{
Geohash gh = Geohash();
auto latlon = gh.decode("sp36v1zk0e2g");
EXPECT_NEAR(41.274966141209006, latlon[0], 1e-8);
EXPECT_NEAR(1.9875180535018444, latlon[1], 1e-8);
EXPECT_THROW(gh.decode(""), std::runtime_error);
}
TEST(Geohash_Test, Precision)
{
Geohash gh = Geohash();
std::string hash;