Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Clamp TileJSON.bounds latitudes to [-90, 90] (#11964)
Browse files Browse the repository at this point in the history
Fixes #11963.
  • Loading branch information
Asheem Mamoowala authored May 21, 2018
1 parent bef2a7c commit 78fdf5c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
17 changes: 9 additions & 8 deletions src/mbgl/style/conversion/tileset.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
#include <mbgl/style/conversion/tileset.hpp>
#include <mbgl/util/geo.hpp>
#include <mbgl/math/clamp.hpp>

namespace mbgl {
namespace style {
namespace conversion {

bool validateLatitude(const double lat) {
return lat <= 90 && lat >= -90;
}

optional<Tileset> Converter<Tileset>::operator()(const Convertible& value, Error& error) const {
Tileset result;

Expand Down Expand Up @@ -95,16 +92,20 @@ optional<Tileset> Converter<Tileset>::operator()(const Convertible& value, Error
error = { "bounds array must contain numeric longitude and latitude values" };
return {};
}
if (!validateLatitude(*bottom) || !validateLatitude(*top) || top <= bottom){
error = { "bounds latitude values must be between -90 and 90 with bottom less than top" };

bottom = util::clamp(*bottom, -90.0, 90.0);
top = util::clamp(*top, -90.0, 90.0);
if (top <= bottom){
error = { "bounds bottom latitude must be between smaller than top latitude" };
return {};
}

if(*left >= *right) {
error = { "bounds left longitude should be less than right longitude" };
return {};
}
*left = util::max(-180.0, *left);
*right = util::min(180.0, *right);
left = util::max(-180.0, *left);
right = util::min(180.0, *right);
result.bounds = LatLngBounds::hull({ *bottom, *left }, { *top, *right });
}

Expand Down
2 changes: 1 addition & 1 deletion test/style/conversion/tileset.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ TEST(Tileset, BoundsAreClamped) {
Error error;
mbgl::optional<Tileset> converted = convertJSON<Tileset>(R"JSON({
"tiles": ["http://mytiles"],
"bounds": [-181.0000005,-90,180.00000000000006,90]
"bounds": [-181.0000005,-90.000000006,180.00000000000006,91]
})JSON", error);
EXPECT_TRUE((bool) converted);
EXPECT_EQ(converted->bounds, LatLngBounds::hull({90, -180}, {-90, 180}));
Expand Down

0 comments on commit 78fdf5c

Please sign in to comment.