2006-10-17 19:26:35 +02:00
|
|
|
/*****************************************************************************
|
2012-02-02 02:53:35 +01:00
|
|
|
*
|
2006-10-17 19:26:35 +02:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2021-01-05 15:39:07 +01:00
|
|
|
* Copyright (C) 2021 Artem Pavlenko
|
2006-10-17 19:26:35 +02:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
// mapnik
|
|
|
|
#include <mapnik/projection.hpp>
|
2012-12-07 23:06:13 +01:00
|
|
|
#include <mapnik/util/trim.hpp>
|
2013-01-28 07:47:32 +01:00
|
|
|
#include <mapnik/well_known_srs.hpp>
|
2011-05-17 21:05:15 +02:00
|
|
|
|
2013-06-03 05:19:33 +02:00
|
|
|
// stl
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2021-01-15 16:21:09 +01:00
|
|
|
#ifdef MAPNIK_USE_PROJ
|
|
|
|
// proj
|
|
|
|
#include <proj.h>
|
2021-01-21 13:46:57 +01:00
|
|
|
#include <cmath> // HUGE_VAL
|
2013-01-28 19:45:41 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace mapnik {
|
|
|
|
|
2013-01-28 07:47:32 +01:00
|
|
|
projection::projection(std::string const& params, bool defer_proj_init)
|
|
|
|
: params_(params),
|
|
|
|
defer_proj_init_(defer_proj_init),
|
2014-08-08 02:42:24 +02:00
|
|
|
is_geographic_(false),
|
2013-11-27 16:52:47 +01:00
|
|
|
proj_(nullptr),
|
|
|
|
proj_ctx_(nullptr)
|
2012-02-02 02:53:35 +01:00
|
|
|
{
|
2013-01-28 07:47:32 +01:00
|
|
|
boost::optional<bool> is_known = is_known_geographic(params_);
|
|
|
|
if (is_known){
|
|
|
|
is_geographic_ = *is_known;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-15 16:21:09 +01:00
|
|
|
#ifdef MAPNIK_USE_PROJ
|
|
|
|
init_proj();
|
2013-01-28 19:45:41 +01:00
|
|
|
#else
|
2021-01-15 16:21:09 +01:00
|
|
|
throw std::runtime_error(std::string("Cannot initialize projection '") + params_ + " ' without proj support (-DMAPNIK_USE_PROJ)");
|
2013-01-28 19:45:41 +01:00
|
|
|
#endif
|
2013-01-28 07:47:32 +01:00
|
|
|
}
|
2021-01-15 16:21:09 +01:00
|
|
|
if (!defer_proj_init_) init_proj();
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
projection::projection(projection const& rhs)
|
2013-01-28 07:47:32 +01:00
|
|
|
: params_(rhs.params_),
|
|
|
|
defer_proj_init_(rhs.defer_proj_init_),
|
|
|
|
is_geographic_(rhs.is_geographic_),
|
2013-11-27 16:52:47 +01:00
|
|
|
proj_(nullptr),
|
|
|
|
proj_ctx_(nullptr)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2021-01-15 16:21:09 +01:00
|
|
|
if (!defer_proj_init_) init_proj();
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
|
|
|
projection& projection::operator=(projection const& rhs)
|
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
projection tmp(rhs);
|
|
|
|
swap(tmp);
|
2015-04-26 23:24:19 +02:00
|
|
|
proj_ctx_ = nullptr;
|
|
|
|
proj_ = nullptr;
|
2021-01-15 16:21:09 +01:00
|
|
|
if (!defer_proj_init_) init_proj();
|
2010-06-02 13:03:30 +02:00
|
|
|
return *this;
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
|
|
|
bool projection::operator==(const projection& other) const
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
return (params_ == other.params_);
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
|
|
|
bool projection::operator!=(const projection& other) const
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2021-01-15 16:21:09 +01:00
|
|
|
void projection::init_proj() const
|
2013-01-28 07:47:32 +01:00
|
|
|
{
|
2021-01-15 16:21:09 +01:00
|
|
|
#ifdef MAPNIK_USE_PROJ
|
2013-01-28 07:47:32 +01:00
|
|
|
if (!proj_)
|
|
|
|
{
|
2021-01-15 16:21:09 +01:00
|
|
|
proj_ctx_ = proj_context_create();
|
2021-01-27 10:40:28 +01:00
|
|
|
proj_ = proj_create(proj_ctx_, params_.c_str());
|
2015-04-26 23:24:19 +02:00
|
|
|
if (!proj_ || !proj_ctx_)
|
2013-01-28 07:47:32 +01:00
|
|
|
{
|
2014-03-06 10:25:57 +01:00
|
|
|
if (proj_ctx_) {
|
2021-01-15 16:21:09 +01:00
|
|
|
proj_context_destroy(proj_ctx_);
|
2015-04-26 23:24:19 +02:00
|
|
|
proj_ctx_ = nullptr;
|
|
|
|
}
|
|
|
|
if (proj_) {
|
2021-01-15 16:21:09 +01:00
|
|
|
proj_destroy(proj_);
|
2015-04-26 23:24:19 +02:00
|
|
|
proj_ = nullptr;
|
2014-03-06 10:25:57 +01:00
|
|
|
}
|
2013-01-28 07:47:32 +01:00
|
|
|
throw proj_init_error(params_);
|
|
|
|
}
|
2021-01-27 10:40:28 +01:00
|
|
|
PJ_TYPE type = proj_get_type(proj_);
|
|
|
|
is_geographic_ = (type == PJ_TYPE_GEOGRAPHIC_2D_CRS
|
|
|
|
||
|
|
|
|
type == PJ_TYPE_GEOGRAPHIC_3D_CRS) ? true : false;
|
2013-01-28 07:47:32 +01:00
|
|
|
}
|
2013-01-28 19:45:41 +01:00
|
|
|
#endif
|
2013-01-28 07:47:32 +01:00
|
|
|
}
|
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
bool projection::is_initialized() const
|
|
|
|
{
|
|
|
|
return proj_ ? true : false;
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
bool projection::is_geographic() const
|
|
|
|
{
|
2010-07-22 01:05:22 +02:00
|
|
|
return is_geographic_;
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2013-01-28 07:47:32 +01:00
|
|
|
boost::optional<well_known_srs_e> projection::well_known() const
|
|
|
|
{
|
|
|
|
return is_well_known_srs(params_);
|
|
|
|
}
|
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
std::string const& projection::params() const
|
|
|
|
{
|
|
|
|
return params_;
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
void projection::forward(double & x, double &y ) const
|
|
|
|
{
|
2021-01-15 16:21:09 +01:00
|
|
|
#ifdef MAPNIK_USE_PROJ
|
2013-01-28 19:45:41 +01:00
|
|
|
if (!proj_)
|
|
|
|
{
|
2021-01-15 16:21:09 +01:00
|
|
|
throw std::runtime_error("projection::forward not supported unless proj is initialized");
|
2012-02-02 02:53:35 +01:00
|
|
|
}
|
2021-01-15 16:21:09 +01:00
|
|
|
PJ_COORD coord;
|
|
|
|
coord.lpzt.z = 0.0;
|
|
|
|
coord.lpzt.t = HUGE_VAL;
|
|
|
|
coord.lpzt.lam = x;
|
|
|
|
coord.lpzt.phi = y;
|
|
|
|
PJ_COORD coord_out = proj_trans(proj_, PJ_FWD, coord);
|
|
|
|
x = coord_out.xy.x;
|
|
|
|
y = coord_out.xy.y;
|
2013-01-28 19:45:41 +01:00
|
|
|
#else
|
2021-01-15 16:21:09 +01:00
|
|
|
throw std::runtime_error("projection::forward not supported without proj support (-DMAPNIK_USE_PROJ)");
|
2013-01-28 19:45:41 +01:00
|
|
|
#endif
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
void projection::inverse(double & x,double & y) const
|
|
|
|
{
|
2021-01-15 16:21:09 +01:00
|
|
|
#ifdef MAPNIK_USE_PROJ
|
2013-01-28 19:45:41 +01:00
|
|
|
if (!proj_)
|
|
|
|
{
|
2021-01-15 16:21:09 +01:00
|
|
|
throw std::runtime_error("projection::forward not supported unless proj is initialized");
|
2013-01-28 19:45:41 +01:00
|
|
|
}
|
2021-01-15 16:21:09 +01:00
|
|
|
PJ_COORD coord;
|
|
|
|
coord.xyzt.z = 0.0;
|
|
|
|
coord.xyzt.t = HUGE_VAL;
|
|
|
|
coord.xyzt.x = x;
|
|
|
|
coord.xyzt.y = y;
|
|
|
|
PJ_COORD coord_out = proj_trans(proj_, PJ_INV, coord);
|
|
|
|
x = coord_out.xy.x;
|
|
|
|
y = coord_out.xy.y;
|
2013-01-28 19:45:41 +01:00
|
|
|
#else
|
2021-01-15 16:21:09 +01:00
|
|
|
throw std::runtime_error("projection::inverse not supported without proj support (-DMAPNIK_USE_PROJ)");
|
2013-01-28 19:45:41 +01:00
|
|
|
#endif
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
|
|
|
projection::~projection()
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2021-01-15 16:21:09 +01:00
|
|
|
#ifdef MAPNIK_USE_PROJ
|
2015-04-26 23:24:19 +02:00
|
|
|
if (proj_)
|
|
|
|
{
|
2021-01-15 16:21:09 +01:00
|
|
|
proj_destroy(proj_);
|
2015-04-26 23:24:19 +02:00
|
|
|
proj_ = nullptr;
|
|
|
|
}
|
|
|
|
if (proj_ctx_)
|
|
|
|
{
|
2021-01-15 16:21:09 +01:00
|
|
|
proj_context_destroy(proj_ctx_);
|
2015-04-26 23:24:19 +02:00
|
|
|
proj_ctx_ = nullptr;
|
|
|
|
}
|
2010-09-24 17:26:50 +02:00
|
|
|
#endif
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2011-05-17 21:05:15 +02:00
|
|
|
std::string projection::expanded() const
|
|
|
|
{
|
2021-01-15 16:21:09 +01:00
|
|
|
#ifdef MAPNIK_USE_PROJ
|
|
|
|
if (proj_)
|
|
|
|
{
|
|
|
|
PJ_PROJ_INFO info = proj_pj_info(proj_);
|
|
|
|
return mapnik::util::trim_copy(info.definition);
|
|
|
|
}
|
2013-01-28 19:45:41 +01:00
|
|
|
#endif
|
|
|
|
return params_;
|
2011-05-17 21:05:15 +02:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2012-04-11 17:23:41 +02:00
|
|
|
void projection::swap(projection& rhs)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
std::swap(params_,rhs.params_);
|
2013-03-27 11:39:02 +01:00
|
|
|
std::swap(defer_proj_init_,rhs.defer_proj_init_);
|
|
|
|
std::swap(is_geographic_,rhs.is_geographic_);
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2013-01-28 07:47:32 +01:00
|
|
|
|
2006-10-17 19:26:35 +02:00
|
|
|
}
|