From 10a44050f485a18f8dd6523aca6a7a9f82f7afc7 Mon Sep 17 00:00:00 2001 From: Asheem Mamoowala Date: Fri, 5 Jan 2018 06:35:31 -0800 Subject: Support TileJSON bounds property (#10701) * [core] Parse TileJSON bounds property * [core] Add TileRange and LatLngBounds::contains(CanonicalTileID) Move LatLngBounds::contains impl to cpp file * [core] Skip tile creation outside of tileset bounds * [core] Fix TileRange for wrapped bounds and use for CustomTileLoader instead of LatLngBounds comparisons for tiles. --- include/mbgl/util/geo.hpp | 78 +++------------------------------------- include/mbgl/util/projection.hpp | 4 +++ include/mbgl/util/tileset.hpp | 13 ++++--- 3 files changed, 16 insertions(+), 79 deletions(-) (limited to 'include/mbgl/util') diff --git a/include/mbgl/util/geo.hpp b/include/mbgl/util/geo.hpp index 60043ee156..dacdb968f3 100644 --- a/include/mbgl/util/geo.hpp +++ b/include/mbgl/util/geo.hpp @@ -158,81 +158,11 @@ public: return (sw.wrapped().longitude() > ne.wrapped().longitude()); } - bool contains(const LatLng& point, LatLng::WrapMode wrap = LatLng::Unwrapped) const { - bool containsLatitude = point.latitude() >= sw.latitude() && - point.latitude() <= ne.latitude(); - if (!containsLatitude) { - return false; - } - - bool containsUnwrappedLongitude = point.longitude() >= sw.longitude() && - point.longitude() <= ne.longitude(); - if (containsUnwrappedLongitude) { - return true; - } else if (wrap == LatLng::Wrapped) { - LatLngBounds wrapped(sw.wrapped(), ne.wrapped()); - auto ptLon = point.wrapped().longitude(); - if (crossesAntimeridian()) { - return (ptLon >= wrapped.sw.longitude() && - ptLon <= util::LONGITUDE_MAX) || - (ptLon <= wrapped.ne.longitude() && - ptLon >= -util::LONGITUDE_MAX); - } else { - return (ptLon >= wrapped.sw.longitude() && - ptLon <= wrapped.ne.longitude()); - } - } - return false; - } + bool contains(const CanonicalTileID& tileID) const; + bool contains(const LatLng& point, LatLng::WrapMode wrap = LatLng::Unwrapped) const; + bool contains(const LatLngBounds& area, LatLng::WrapMode wrap = LatLng::Unwrapped) const; - bool contains(const LatLngBounds& area, LatLng::WrapMode wrap = LatLng::Unwrapped) const { - bool containsLatitude = area.north() <= north() && area.south() >= south(); - if (!containsLatitude) { - return false; - } - - bool containsUnwrapped = area.east() <= east() && area.west() >= west(); - if(containsUnwrapped) { - return true; - } else if (wrap == LatLng::Wrapped) { - LatLngBounds wrapped(sw.wrapped(), ne.wrapped()); - LatLngBounds other(area.sw.wrapped(), area.ne.wrapped()); - if (crossesAntimeridian() & !area.crossesAntimeridian()) { - return (other.east() <= util::LONGITUDE_MAX && other.west() >= wrapped.west()) || - (other.east() <= wrapped.east() && other.west() >= -util::LONGITUDE_MAX); - } else { - return other.east() <= wrapped.east() && other.west() >= wrapped.west(); - } - } - return false; - } - - bool intersects(const LatLngBounds area, LatLng::WrapMode wrap = LatLng::Unwrapped) const { - bool latitudeIntersects = area.north() > south() && area.south() < north(); - if (!latitudeIntersects) { - return false; - } - - bool longitudeIntersects = area.east() > west() && area.west() < east(); - if (longitudeIntersects) { - return true; - } else if (wrap == LatLng::Wrapped) { - LatLngBounds wrapped(sw.wrapped(), ne.wrapped()); - LatLngBounds other(area.sw.wrapped(), area.ne.wrapped()); - if (crossesAntimeridian()) { - return area.crossesAntimeridian() || - other.east() > wrapped.west() || - other.west() < wrapped.east(); - } else if (other.crossesAntimeridian()){ - return other.east() > wrapped.west() || - other.west() < wrapped.east(); - } else { - return other.east() > wrapped.west() && - other.west() < wrapped.east(); - } - } - return false; - } + bool intersects(const LatLngBounds area, LatLng::WrapMode wrap = LatLng::Unwrapped) const; private: LatLng sw; diff --git a/include/mbgl/util/projection.hpp b/include/mbgl/util/projection.hpp index 1613af3b36..b4a34521a4 100644 --- a/include/mbgl/util/projection.hpp +++ b/include/mbgl/util/projection.hpp @@ -78,6 +78,10 @@ public: return project_(latLng, worldSize(scale)); } + static Point project(const LatLng& latLng, uint8_t zoom) { + return project_(latLng, std::pow(2.0, zoom)); + } + static LatLng unproject(const Point& p, double scale, LatLng::WrapMode wrapMode = LatLng::Unwrapped) { auto p2 = p * util::DEGREES_MAX / worldSize(scale); return LatLng { diff --git a/include/mbgl/util/tileset.hpp b/include/mbgl/util/tileset.hpp index 5a03e1a9da..7bef0e89ed 100644 --- a/include/mbgl/util/tileset.hpp +++ b/include/mbgl/util/tileset.hpp @@ -2,7 +2,8 @@ #include #include - +#include +#include #include #include #include @@ -18,6 +19,7 @@ public: Range zoomRange; std::string attribution; Scheme scheme; + optional bounds; Tileset(std::vector tiles_ = std::vector(), Range zoomRange_ = { 0, util::DEFAULT_MAX_ZOOM }, @@ -26,13 +28,14 @@ public: : tiles(std::move(tiles_)), zoomRange(std::move(zoomRange_)), attribution(std::move(attribution_)), - scheme(scheme_) {} + scheme(scheme_), + bounds() {} - // TileJSON also includes center, zoom, and bounds, but they are not used by mbgl. + // TileJSON also includes center and zoom but they are not used by mbgl. friend bool operator==(const Tileset& lhs, const Tileset& rhs) { - return std::tie(lhs.tiles, lhs.zoomRange, lhs.attribution, lhs.scheme) - == std::tie(rhs.tiles, rhs.zoomRange, rhs.attribution, rhs.scheme); + return std::tie(lhs.tiles, lhs.zoomRange, lhs.attribution, lhs.scheme, lhs.bounds) + == std::tie(rhs.tiles, rhs.zoomRange, rhs.attribution, rhs.scheme, rhs.bounds); } }; -- cgit v1.2.1