summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/mbgl/util/geo.hpp8
-rw-r--r--src/mbgl/util/geo.cpp19
-rw-r--r--test/miscellaneous/geo.cpp61
-rw-r--r--test/test.gypi1
4 files changed, 89 insertions, 0 deletions
diff --git a/include/mbgl/util/geo.hpp b/include/mbgl/util/geo.hpp
index 6ece6d4de9..ff13726803 100644
--- a/include/mbgl/util/geo.hpp
+++ b/include/mbgl/util/geo.hpp
@@ -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;
diff --git a/src/mbgl/util/geo.cpp b/src/mbgl/util/geo.cpp
new file mode 100644
index 0000000000..b08b10d1e6
--- /dev/null
+++ b/src/mbgl/util/geo.cpp
@@ -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
diff --git a/test/miscellaneous/geo.cpp b/test/miscellaneous/geo.cpp
new file mode 100644
index 0000000000..093ea7040c
--- /dev/null
+++ b/test/miscellaneous/geo.cpp
@@ -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);
+ }
+}
diff --git a/test/test.gypi b/test/test.gypi
index f2407f5058..42dc04448a 100644
--- a/test/test.gypi
+++ b/test/test.gypi
@@ -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',