well_known_srs: move implementation to .cpp
- add lonlat2merc, merc2lonlat overloads for single point - add stride argument to overloads for coordinate arrays - make all functions exported - change static string constants to exported - change static numeric constants to constexpr - remove superfluous numeric constants
This commit is contained in:
parent
e11f3a660a
commit
32e7202223
2 changed files with 84 additions and 73 deletions
|
@ -24,9 +24,9 @@
|
|||
#define MAPNIK_WELL_KNOWN_SRS_HPP
|
||||
|
||||
// mapnik
|
||||
#include <mapnik/global.hpp> // for M_PI on windows
|
||||
#include <mapnik/enumeration.hpp>
|
||||
#include <mapnik/geometry/point.hpp>
|
||||
#include <mapnik/util/math.hpp>
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#include <mapnik/warning_ignore.hpp>
|
||||
|
@ -34,7 +34,6 @@
|
|||
#pragma GCC diagnostic pop
|
||||
|
||||
// stl
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
|
||||
namespace mapnik {
|
||||
|
@ -47,81 +46,27 @@ enum well_known_srs_enum : std::uint8_t {
|
|||
|
||||
DEFINE_ENUM( well_known_srs_e, well_known_srs_enum );
|
||||
|
||||
static const double EARTH_RADIUS = 6378137.0;
|
||||
static const double EARTH_DIAMETER = EARTH_RADIUS * 2.0;
|
||||
static const double EARTH_CIRCUMFERENCE = EARTH_DIAMETER * M_PI;
|
||||
static const double MAXEXTENT = EARTH_CIRCUMFERENCE / 2.0;
|
||||
static const double M_PI_by2 = M_PI / 2;
|
||||
static const double D2R = M_PI / 180.0;
|
||||
static const double R2D = 180.0 / M_PI;
|
||||
static const double M_PIby360 = M_PI / 360.0;
|
||||
static const double MAXEXTENTby180 = MAXEXTENT / 180.0;
|
||||
static const double MAX_LATITUDE = R2D * (2 * std::atan(std::exp(180.0 * D2R)) - M_PI_by2);
|
||||
static const std::string MAPNIK_LONGLAT_PROJ = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs";
|
||||
static const std::string 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";
|
||||
constexpr double EARTH_RADIUS = 6378137.0;
|
||||
constexpr double EARTH_CIRCUMFERENCE = EARTH_RADIUS * util::tau;
|
||||
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)
|
||||
|
||||
boost::optional<well_known_srs_e> is_well_known_srs(std::string const& srs);
|
||||
extern MAPNIK_DECL std::string const MAPNIK_LONGLAT_PROJ;
|
||||
extern MAPNIK_DECL std::string const MAPNIK_GMERC_PROJ;
|
||||
|
||||
boost::optional<bool> is_known_geographic(std::string const& srs);
|
||||
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);
|
||||
|
||||
static inline bool lonlat2merc(double * x, double * y, std::size_t point_count)
|
||||
{
|
||||
for (std::size_t i = 0; i < point_count; ++i)
|
||||
{
|
||||
if (x[i] > 180) x[i] = 180;
|
||||
else if (x[i] < -180) x[i] = -180;
|
||||
if (y[i] > MAX_LATITUDE) y[i] = MAX_LATITUDE;
|
||||
else if (y[i] < -MAX_LATITUDE) y[i] = -MAX_LATITUDE;
|
||||
x[i] = x[i] * MAXEXTENTby180;
|
||||
y[i] = std::log(std::tan((90.0 + y[i]) * M_PIby360)) * R2D * MAXEXTENTby180;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
MAPNIK_DECL bool lonlat2merc(double & x, double & y);
|
||||
MAPNIK_DECL bool lonlat2merc(double * x, double * y, std::size_t point_count,
|
||||
std::size_t stride = 1);
|
||||
MAPNIK_DECL bool lonlat2merc(std::vector<geometry::point<double>> & ls);
|
||||
|
||||
static inline bool merc2lonlat(double * x, double * y, std::size_t point_count)
|
||||
{
|
||||
for(std::size_t i = 0; i < point_count; ++i)
|
||||
{
|
||||
if (x[i] > MAXEXTENT) x[i] = MAXEXTENT;
|
||||
else if (x[i] < -MAXEXTENT) x[i] = -MAXEXTENT;
|
||||
if (y[i] > MAXEXTENT) y[i] = MAXEXTENT;
|
||||
else if (y[i] < -MAXEXTENT) y[i] = -MAXEXTENT;
|
||||
x[i] = (x[i] / MAXEXTENT) * 180;
|
||||
y[i] = (y[i] / MAXEXTENT) * 180;
|
||||
y[i] = R2D * (2 * std::atan(std::exp(y[i] * D2R)) - M_PI_by2);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline bool lonlat2merc(std::vector<geometry::point<double>> & ls)
|
||||
{
|
||||
for (auto& p : ls)
|
||||
{
|
||||
if (p.x > 180) p.x = 180;
|
||||
else if (p.x < -180) p.x = -180;
|
||||
if (p.y > MAX_LATITUDE) p.y = MAX_LATITUDE;
|
||||
else if (p.y < -MAX_LATITUDE) p.y = -MAX_LATITUDE;
|
||||
p.x = p.x * MAXEXTENTby180;
|
||||
p.y = std::log(std::tan((90 + p.y) * M_PIby360)) * R2D;
|
||||
p.y = p.y * MAXEXTENTby180;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline bool merc2lonlat(std::vector<geometry::point<double>> & ls)
|
||||
{
|
||||
for (auto & p : ls)
|
||||
{
|
||||
if (p.x > MAXEXTENT) p.x = MAXEXTENT;
|
||||
else if (p.x < -MAXEXTENT) p.x = -MAXEXTENT;
|
||||
if (p.y > MAXEXTENT) p.y = MAXEXTENT;
|
||||
else if (p.y < -MAXEXTENT) p.y = -MAXEXTENT;
|
||||
p.x = (p.x / MAXEXTENT) * 180;
|
||||
p.y = (p.y / MAXEXTENT) * 180;
|
||||
p.y = R2D * (2 * std::atan(std::exp(p.y * D2R)) - M_PI_by2);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
MAPNIK_DECL bool merc2lonlat(double & x, double & y);
|
||||
MAPNIK_DECL bool merc2lonlat(double * x, double * y, std::size_t point_count,
|
||||
std::size_t stride = 1);
|
||||
MAPNIK_DECL bool merc2lonlat(std::vector<geometry::point<double>> & ls);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -30,8 +30,18 @@
|
|||
#include <boost/optional.hpp>
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
// stl
|
||||
#include <cmath>
|
||||
|
||||
namespace mapnik {
|
||||
|
||||
extern std::string const MAPNIK_LONGLAT_PROJ =
|
||||
"+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs";
|
||||
|
||||
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";
|
||||
|
||||
static const char * well_known_srs_strings[] = {
|
||||
"mapnik-longlat",
|
||||
"mapnik-gmerc",
|
||||
|
@ -82,4 +92,60 @@ boost::optional<bool> is_known_geographic(std::string const& srs)
|
|||
|
||||
IMPLEMENT_ENUM( well_known_srs_e, well_known_srs_strings )
|
||||
|
||||
bool lonlat2merc(double & x, double & y)
|
||||
{
|
||||
using namespace util;
|
||||
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));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool lonlat2merc(double * x, double * y, std::size_t point_count, std::size_t stride)
|
||||
{
|
||||
for (std::size_t i = 0; i < point_count; ++i)
|
||||
{
|
||||
lonlat2merc(x[i * stride], y[i * stride]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool lonlat2merc(std::vector<geometry::point<double>> & ls)
|
||||
{
|
||||
for (auto & p : ls)
|
||||
{
|
||||
lonlat2merc(p.x, p.y);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool merc2lonlat(double & x, double & y)
|
||||
{
|
||||
using namespace util;
|
||||
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);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool merc2lonlat(double * x, double * y, std::size_t point_count, std::size_t stride)
|
||||
{
|
||||
for(std::size_t i = 0; i < point_count; ++i)
|
||||
{
|
||||
merc2lonlat(x[i * stride], y[i * stride]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool merc2lonlat(std::vector<geometry::point<double>> & ls)
|
||||
{
|
||||
for (auto & p : ls)
|
||||
{
|
||||
merc2lonlat(p.x, p.y);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue