@@ -20,7 +20,7 @@ const constexpr double EARTH_RADIUS_WGS84 = 6378137.0;
2020// earth circumference devided by 2
2121const constexpr double MAXEXTENT = EARTH_RADIUS_WGS84 * boost::math::constants::pi<double >();
2222// ^ math functions are not constexpr since they have side-effects (setting errno) :(
23- const constexpr double MAX_LATITUDE = 85 .;
23+ const constexpr double EPSG3857_MAX_LATITUDE = 85.051128779806592378 ; // 90(4*atan(exp(pi))/pi-1)
2424const constexpr double MAX_LONGITUDE = 180.0 ;
2525}
2626
@@ -29,6 +29,18 @@ const constexpr double DEGREE_TO_PX = detail::MAXEXTENT / 180.0;
2929// This is the global default tile size for all Mapbox Vector Tiles
3030const constexpr double TILE_SIZE = 256.0 ;
3131
32+ inline FloatLatitude clamp (const FloatLatitude lat)
33+ {
34+ return std::max (std::min (lat, FloatLatitude{detail::EPSG3857_MAX_LATITUDE}),
35+ FloatLatitude{-detail::EPSG3857_MAX_LATITUDE});
36+ }
37+
38+ inline FloatLongitude clamp (const FloatLongitude lon)
39+ {
40+ return std::max (std::min (lon, FloatLongitude{detail::MAX_LONGITUDE}),
41+ FloatLongitude{-detail::MAX_LONGITUDE});
42+ }
43+
3244inline FloatLatitude yToLat (const double y)
3345{
3446 const auto clamped_y = std::max (-180 ., std::min (180 ., y));
@@ -41,10 +53,9 @@ inline FloatLatitude yToLat(const double y)
4153inline double latToY (const FloatLatitude latitude)
4254{
4355 // apparently this is the (faster) version of the canonical log(tan()) version
44- const double f = std::sin (detail::DEGREE_TO_RAD * static_cast <double >(latitude));
45- const double y = detail::RAD_TO_DEGREE * 0.5 * std::log ((1 + f) / (1 - f));
46- const auto clamped_y = std::max (-180 ., std::min (180 ., y));
47- return clamped_y;
56+ const auto clamped_latitude = clamp (latitude);
57+ const double f = std::sin (detail::DEGREE_TO_RAD * static_cast <double >(clamped_latitude));
58+ return detail::RAD_TO_DEGREE * 0.5 * std::log ((1 + f) / (1 - f));
4859}
4960
5061template <typename T> constexpr double horner (double , T an) { return an; }
@@ -91,18 +102,6 @@ inline double latToYapprox(const FloatLatitude latitude)
91102 -3.23083224835967391884404730e-28 );
92103}
93104
94- inline FloatLatitude clamp (const FloatLatitude lat)
95- {
96- return std::max (std::min (lat, FloatLatitude{detail::MAX_LATITUDE}),
97- FloatLatitude{-detail::MAX_LATITUDE});
98- }
99-
100- inline FloatLongitude clamp (const FloatLongitude lon)
101- {
102- return std::max (std::min (lon, FloatLongitude{detail::MAX_LONGITUDE}),
103- FloatLongitude{-detail::MAX_LONGITUDE});
104- }
105-
106105inline void pixelToDegree (const double shift, double &x, double &y)
107106{
108107 const double b = shift / 2.0 ;
@@ -166,9 +165,9 @@ inline void xyzToMercator(
166165 xyzToWGS84 (x, y, z, minx, miny, maxx, maxy);
167166
168167 minx = static_cast <double >(clamp (util::FloatLongitude{minx})) * DEGREE_TO_PX;
169- miny = latToY (clamp ( util::FloatLatitude{miny}) ) * DEGREE_TO_PX;
168+ miny = latToY (util::FloatLatitude{miny}) * DEGREE_TO_PX;
170169 maxx = static_cast <double >(clamp (util::FloatLongitude{maxx})) * DEGREE_TO_PX;
171- maxy = latToY (clamp ( util::FloatLatitude{maxy}) ) * DEGREE_TO_PX;
170+ maxy = latToY (util::FloatLatitude{maxy}) * DEGREE_TO_PX;
172171}
173172}
174173}
0 commit comments