allow constructing LatLng/LatLngBounds objects from TileIDs

This commit is contained in:
Konstantin Käfer
2015-06-08 16:12:01 -04:00
parent 5e661f7617
commit f85889dc75
4 changed files with 89 additions and 0 deletions

View File

@@ -3,12 +3,17 @@
namespace mbgl {
class TileID;
struct LatLng {
double latitude = 0;
double longitude = 0;
inline LatLng(double lat = 0, double lon = 0)
: latitude(lat), longitude(lon) {}
// Constructs a LatLng object with the top left position of the specified tile.
LatLng(const TileID& id);
};
struct ProjectedMeters {
@@ -26,6 +31,9 @@ struct LatLngBounds {
inline LatLngBounds(LatLng sw_ = {90, 180}, LatLng ne_ = {-90, -180})
: sw(sw_), ne(ne_) {}
// Constructs a LatLngBounds object with the tile's exact boundaries.
LatLngBounds(const TileID& id);
inline void extend(const LatLng& point) {
if (point.latitude < sw.latitude) sw.latitude = point.latitude;
if (point.latitude > ne.latitude) ne.latitude = point.latitude;

19
src/mbgl/util/geo.cpp Normal file
View File

@@ -0,0 +1,19 @@
#include <mbgl/util/geo.hpp>
#include <mbgl/map/tile_id.hpp>
#include <cmath>
namespace mbgl {
LatLng::LatLng(const TileID& id) {
latitude = id.x / std::pow(2.0, id.z) * 360.0 - 180;
const double n = M_PI - 2.0 * M_PI * id.y / std::pow(2.0, id.z);
longitude = 180.0 / M_PI * std::atan(0.5 * (std::exp(n) - std::exp(-n)));
}
LatLngBounds::LatLngBounds(const TileID& id)
: sw(TileID{ id.z, id.x, id.y + 1, id.sourceZ }),
ne(TileID{ id.z, id.x + 1, id.y, id.sourceZ }) {
}
} // end namespace mbgl

View File

@@ -0,0 +1,61 @@
#include "../fixtures/util.hpp"
#include <mbgl/util/geo.hpp>
#include <mbgl/map/tile_id.hpp>
using namespace mbgl;
TEST(Geo, LatLngFromTileID) {
for (int i = 0; i < 20; i++) {
const LatLng ll{ TileID(i, 0, 0, 0) };
ASSERT_DOUBLE_EQ(-180, ll.latitude);
ASSERT_DOUBLE_EQ(85.051128779806604, ll.longitude);
}
{
const LatLng ll{ TileID(0, 1, 0, 0) };
ASSERT_DOUBLE_EQ(180, ll.latitude);
ASSERT_DOUBLE_EQ(85.051128779806604, ll.longitude);
}
{
const LatLng ll{ TileID(0, -1, 0, 0) };
ASSERT_DOUBLE_EQ(-540, ll.latitude);
ASSERT_DOUBLE_EQ(85.051128779806604, ll.longitude);
}
}
TEST(Geo, LatLngBoundsFromTileID) {
{
const LatLngBounds bounds{ TileID(0, 0, 0, 0) };
ASSERT_DOUBLE_EQ(-180, bounds.sw.latitude);
ASSERT_DOUBLE_EQ(-85.051128779806604, bounds.sw.longitude);
ASSERT_DOUBLE_EQ(180, bounds.ne.latitude);
ASSERT_DOUBLE_EQ(85.051128779806604, bounds.ne.longitude);
}
{
const LatLngBounds bounds{ TileID(1, 0, 1, 0) };
ASSERT_DOUBLE_EQ(-180, bounds.sw.latitude);
ASSERT_DOUBLE_EQ(-85.051128779806604, bounds.sw.longitude);
ASSERT_DOUBLE_EQ(0, bounds.ne.latitude);
ASSERT_DOUBLE_EQ(0, bounds.ne.longitude);
}
{
const LatLngBounds bounds{ TileID(1, 1, 1, 0) };
ASSERT_DOUBLE_EQ(0, bounds.sw.latitude);
ASSERT_DOUBLE_EQ(-85.051128779806604, bounds.sw.longitude);
ASSERT_DOUBLE_EQ(180, bounds.ne.latitude);
ASSERT_DOUBLE_EQ(0, bounds.ne.longitude);
}
{
const LatLngBounds bounds{ TileID(1, 0, 0, 0) };
ASSERT_DOUBLE_EQ(-180, bounds.sw.latitude);
ASSERT_DOUBLE_EQ(0, bounds.sw.longitude);
ASSERT_DOUBLE_EQ(0, bounds.ne.latitude);
ASSERT_DOUBLE_EQ(85.051128779806604, bounds.ne.longitude);
}
}

View File

@@ -46,6 +46,7 @@
'miscellaneous/comparisons.cpp',
'miscellaneous/enums.cpp',
'miscellaneous/functions.cpp',
'miscellaneous/geo.cpp',
'miscellaneous/map.cpp',
'miscellaneous/map_context.cpp',
'miscellaneous/mapbox.cpp',