From 609980455948ba42300c23a15d1841248d981703 Mon Sep 17 00:00:00 2001 From: Artem Pavlenko Date: Mon, 15 Mar 2021 18:06:26 +0000 Subject: [PATCH] Use POSIX definition of `pi` constant (M_PI) + define `tau` in terms of `pi` + better namings + remove deprecated proj4 style definitions --- include/mapnik/layer.hpp | 2 +- include/mapnik/map.hpp | 6 ++--- include/mapnik/util/math.hpp | 8 +++--- include/mapnik/well_known_srs.hpp | 6 ++--- src/map.cpp | 2 +- src/proj_transform.cpp | 4 +-- src/projection.cpp | 9 ------- src/well_known_srs.cpp | 44 ++++++++++--------------------- 8 files changed, 28 insertions(+), 53 deletions(-) diff --git a/include/mapnik/layer.hpp b/include/mapnik/layer.hpp index 96a161816..dd28057d5 100644 --- a/include/mapnik/layer.hpp +++ b/include/mapnik/layer.hpp @@ -50,7 +50,7 @@ class MAPNIK_DECL layer { public: layer(std::string const& name, - std::string const& srs=MAPNIK_LONGLAT_PROJ); + std::string const& srs = MAPNIK_GEOGRAPHIC_PROJ); // copy layer(layer const& l); // move diff --git a/include/mapnik/map.hpp b/include/mapnik/map.hpp index face79aca..ba1b08b7b 100644 --- a/include/mapnik/map.hpp +++ b/include/mapnik/map.hpp @@ -110,8 +110,8 @@ public: }; private: - static const unsigned MIN_MAPSIZE=16; - static const unsigned MAX_MAPSIZE=MIN_MAPSIZE<<10; + static const unsigned MIN_MAPSIZE = 16; + static const unsigned MAX_MAPSIZE = MIN_MAPSIZE << 10; unsigned width_; unsigned height_; std::string srs_; @@ -153,7 +153,7 @@ public: * @param height Initial map height. * @param srs Initial map projection. */ - Map(int width, int height, std::string const& srs = MAPNIK_LONGLAT_PROJ); + Map(int width, int height, std::string const& srs = MAPNIK_GEOGRAPHIC_PROJ); /*! \brief Copy Constructor. * diff --git a/include/mapnik/util/math.hpp b/include/mapnik/util/math.hpp index d2f1f6ef7..7e163ba57 100644 --- a/include/mapnik/util/math.hpp +++ b/include/mapnik/util/math.hpp @@ -27,8 +27,8 @@ namespace mapnik { namespace util { -constexpr double pi = 3.1415926535897932384626433832795; -constexpr double tau = 6.283185307179586476925286766559; +constexpr double pi = 3.14159265358979323846; +constexpr double tau = 2.0 * pi; template constexpr T const& clamp(T const& v, T const& lo, T const& hi) @@ -38,12 +38,12 @@ constexpr T const& clamp(T const& v, T const& lo, T const& hi) constexpr double degrees(double rad) { - return rad * (360 / tau); + return rad * (180.0 / pi); } constexpr double radians(double deg) { - return deg * (tau / 360); + return deg * (pi / 180); } MAPNIK_DECL double normalize_angle(double angle); diff --git a/include/mapnik/well_known_srs.hpp b/include/mapnik/well_known_srs.hpp index a2154a8e5..29ad18c05 100644 --- a/include/mapnik/well_known_srs.hpp +++ b/include/mapnik/well_known_srs.hpp @@ -41,7 +41,7 @@ namespace mapnik { enum well_known_srs_enum : std::uint8_t { WGS_84, - G_MERC, + WEB_MERC, well_known_srs_enum_MAX }; @@ -53,8 +53,8 @@ constexpr double MERC_MAX_EXTENT = EARTH_RADIUS * util::pi; constexpr double MERC_MAX_LATITUDE = 85.0511287798065923778; // MERC_MAX_LATITUDE = degrees(2 * atan(exp(pi)) - pi / 2) -extern MAPNIK_DECL std::string const MAPNIK_LONGLAT_PROJ; -extern MAPNIK_DECL std::string const MAPNIK_GMERC_PROJ; +extern MAPNIK_DECL std::string const MAPNIK_GEOGRAPHIC_PROJ; +extern MAPNIK_DECL std::string const MAPNIK_WEBMERCATOR_PROJ; MAPNIK_DECL boost::optional is_known_geographic(std::string const& srs); MAPNIK_DECL boost::optional is_well_known_srs(std::string const& srs); diff --git a/src/map.cpp b/src/map.cpp index 4cad760fe..10e281863 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -66,7 +66,7 @@ IMPLEMENT_ENUM( aspect_fix_mode_e, aspect_fix_mode_strings ) Map::Map() : width_(400), height_(400), - srs_(MAPNIK_LONGLAT_PROJ), + srs_(MAPNIK_GEOGRAPHIC_PROJ), buffer_size_(0), background_image_comp_op_(src_over), background_image_opacity_(1.0), diff --git a/src/proj_transform.cpp b/src/proj_transform.cpp index 5f391bb0d..4571cdad9 100644 --- a/src/proj_transform.cpp +++ b/src/proj_transform.cpp @@ -111,12 +111,12 @@ proj_transform::proj_transform(projection const& source, bool known_trans = false; if (src_k && dest_k) { - if (*src_k == WGS_84 && *dest_k == G_MERC) + if (*src_k == WGS_84 && *dest_k == WEB_MERC) { wgs84_to_merc_ = true; known_trans = true; } - else if (*src_k == G_MERC && *dest_k == WGS_84) + else if (*src_k == WEB_MERC && *dest_k == WGS_84) { merc_to_wgs84_ = true; known_trans = true; diff --git a/src/projection.cpp b/src/projection.cpp index 2da83a9e4..302ff418e 100644 --- a/src/projection.cpp +++ b/src/projection.cpp @@ -94,7 +94,6 @@ void projection::init_proj() const if (!proj_) { proj_ctx_ = proj_context_create(); - //proj_ = proj_create_crs_to_crs(proj_ctx_, "epsg:4326", params_.c_str(), nullptr); proj_ = proj_create(proj_ctx_, params_.c_str()); if (!proj_ || !proj_ctx_) { @@ -108,18 +107,10 @@ void projection::init_proj() const } throw proj_init_error(params_); } - // determine the type of CRS - //PJ* crs = proj_create(proj_ctx_, params_.c_str()); - //if (crs) - //if (proj_) - //{ - //PJ_TYPE type = proj_get_type(crs); PJ_TYPE type = proj_get_type(proj_); is_geographic_ = (type == PJ_TYPE_GEOGRAPHIC_2D_CRS || type == PJ_TYPE_GEOGRAPHIC_3D_CRS) ? true : false; - //} - //proj_destroy(crs); } #endif } diff --git a/src/well_known_srs.cpp b/src/well_known_srs.cpp index 8a09bbd78..25462bd67 100644 --- a/src/well_known_srs.cpp +++ b/src/well_known_srs.cpp @@ -36,28 +36,27 @@ MAPNIK_DISABLE_WARNING_POP namespace mapnik { -extern std::string const MAPNIK_LONGLAT_PROJ = - "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"; +extern std::string const MAPNIK_GEOGRAPHIC_PROJ = + "epsg:4326"; //wgs84 -extern std::string const MAPNIK_GMERC_PROJ = - "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0" - " +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over"; +extern std::string const MAPNIK_WEBMERCATOR_PROJ = + "epsg:3857"; // webmercator static const char * well_known_srs_strings[] = { - "mapnik-longlat", - "mapnik-gmerc", + MAPNIK_GEOGRAPHIC_PROJ.c_str(), + MAPNIK_WEBMERCATOR_PROJ.c_str(), "" }; boost::optional is_well_known_srs(std::string const& srs) { - if (srs == "epsg:4326" || srs == MAPNIK_LONGLAT_PROJ) + if (srs == MAPNIK_GEOGRAPHIC_PROJ) { return boost::optional(mapnik::WGS_84); } - else if (srs == "epsg:3857" || srs == MAPNIK_GMERC_PROJ) + else if (srs == MAPNIK_WEBMERCATOR_PROJ) { - return boost::optional(mapnik::G_MERC); + return boost::optional(mapnik::WEB_MERC); } return boost::optional(); } @@ -65,28 +64,13 @@ boost::optional is_well_known_srs(std::string const& srs) boost::optional is_known_geographic(std::string const& srs) { std::string trimmed = util::trim_copy(srs); - if (trimmed == "epsg:3857") - { - return boost::optional(false); - } - else if (trimmed == "epsg:4326") + if (trimmed == MAPNIK_GEOGRAPHIC_PROJ) { return boost::optional(true); } - else if (srs.find("+proj=") != std::string::npos) + else if (trimmed == MAPNIK_WEBMERCATOR_PROJ) { - if ((srs.find("+proj=longlat") != std::string::npos) || - (srs.find("+proj=latlong") != std::string::npos) || - (srs.find("+proj=lonlat") != std::string::npos) || - (srs.find("+proj=latlon") != std::string::npos) - ) - { - return boost::optional(true); - } - else - { - return boost::optional(false); - } + return boost::optional(true); } return boost::optional(); } @@ -99,7 +83,7 @@ bool lonlat2merc(double & x, double & y) auto dx = clamp(x, -180.0, 180.0); auto dy = clamp(y, -MERC_MAX_LATITUDE, MERC_MAX_LATITUDE); x = EARTH_RADIUS * radians(dx); - y = EARTH_RADIUS * std::log(std::tan(radians(90 + dy) / 2)); + y = EARTH_RADIUS * std::log(std::tan(radians(90.0 + dy) / 2.0)); return true; } @@ -127,7 +111,7 @@ bool merc2lonlat(double & x, double & y) auto rx = clamp(x / EARTH_RADIUS, -pi, pi); auto ry = clamp(y / EARTH_RADIUS, -pi, pi); x = degrees(rx); - y = degrees(2 * std::atan(std::exp(ry)) - pi / 2); + y = degrees(2.0 * std::atan(std::exp(ry)) - pi / 2.0); return true; }