Use POSIX definition of pi constant (M_PI) + define tau in terms of pi + better namings + remove deprecated proj4 style definitions

This commit is contained in:
Artem Pavlenko 2021-03-15 18:06:26 +00:00
parent baad6b3819
commit 6099804559
8 changed files with 28 additions and 53 deletions

View file

@ -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

View file

@ -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.
*

View file

@ -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 <typename T>
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);

View file

@ -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<bool> is_known_geographic(std::string const& srs);
MAPNIK_DECL boost::optional<well_known_srs_e> is_well_known_srs(std::string const& srs);

View file

@ -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),

View file

@ -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;

View file

@ -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
}

View file

@ -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<well_known_srs_e> is_well_known_srs(std::string const& srs)
{
if (srs == "epsg:4326" || srs == MAPNIK_LONGLAT_PROJ)
if (srs == MAPNIK_GEOGRAPHIC_PROJ)
{
return boost::optional<well_known_srs_e>(mapnik::WGS_84);
}
else if (srs == "epsg:3857" || srs == MAPNIK_GMERC_PROJ)
else if (srs == MAPNIK_WEBMERCATOR_PROJ)
{
return boost::optional<well_known_srs_e>(mapnik::G_MERC);
return boost::optional<well_known_srs_e>(mapnik::WEB_MERC);
}
return boost::optional<well_known_srs_e>();
}
@ -65,29 +64,14 @@ boost::optional<well_known_srs_e> is_well_known_srs(std::string const& srs)
boost::optional<bool> is_known_geographic(std::string const& srs)
{
std::string trimmed = util::trim_copy(srs);
if (trimmed == "epsg:3857")
{
return boost::optional<bool>(false);
}
else if (trimmed == "epsg:4326")
if (trimmed == MAPNIK_GEOGRAPHIC_PROJ)
{
return boost::optional<bool>(true);
}
else if (srs.find("+proj=") != std::string::npos)
{
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)
)
else if (trimmed == MAPNIK_WEBMERCATOR_PROJ)
{
return boost::optional<bool>(true);
}
else
{
return boost::optional<bool>(false);
}
}
return boost::optional<bool>();
}
@ -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;
}