mapnik/src/projection.cpp

243 lines
5.9 KiB
C++
Raw Normal View History

/*****************************************************************************
2012-02-02 01:53:35 +00:00
*
* This file is part of Mapnik (c++ mapping toolkit)
*
2014-11-20 14:25:50 +00:00
* Copyright (C) 2014 Artem Pavlenko
*
* 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>
#include <mapnik/utils.hpp>
#include <mapnik/util/trim.hpp>
#include <mapnik/well_known_srs.hpp>
2013-06-03 03:19:33 +00:00
// stl
#include <stdexcept>
#ifdef MAPNIK_USE_PROJ4
2007-10-08 17:42:41 +00:00
// proj4
#include <proj_api.h>
2014-07-23 02:36:39 +00:00
#if defined(MAPNIK_THREADSAFE) && PJ_VERSION < 480
#include <mutex>
static std::mutex mutex_;
#ifdef _MSC_VER
#pragma NOTE(mapnik is building against < proj 4.8, reprojection will be faster if you use >= 4.8)
#else
#warning mapnik is building against < proj 4.8, reprojection will be faster if you use >= 4.8
#endif
#endif
#endif
namespace mapnik {
projection::projection(std::string const& params, bool defer_proj_init)
: params_(params),
defer_proj_init_(defer_proj_init),
2014-08-08 00:42:24 +00:00
is_geographic_(false),
2013-11-27 15:52:47 +00:00
proj_(nullptr),
proj_ctx_(nullptr)
2012-02-02 01:53:35 +00:00
{
boost::optional<bool> is_known = is_known_geographic(params_);
if (is_known){
is_geographic_ = *is_known;
}
else
{
#ifdef MAPNIK_USE_PROJ4
init_proj4();
#else
throw std::runtime_error(std::string("Cannot initialize projection '") + params_ + " ' without proj4 support (-DMAPNIK_USE_PROJ4)");
#endif
}
if (!defer_proj_init_) init_proj4();
2010-06-02 11:03:30 +00:00
}
2012-02-02 01:53:35 +00:00
2010-06-02 11:03:30 +00:00
projection::projection(projection const& rhs)
: params_(rhs.params_),
defer_proj_init_(rhs.defer_proj_init_),
is_geographic_(rhs.is_geographic_),
2013-11-27 15:52:47 +00:00
proj_(nullptr),
proj_ctx_(nullptr)
2010-06-02 11:03:30 +00:00
{
2013-03-27 10:39:02 +00:00
if (!defer_proj_init_) init_proj4();
2010-06-02 11:03:30 +00:00
}
2012-02-02 01:53:35 +00:00
projection& projection::operator=(projection const& rhs)
{
2010-06-02 11:03:30 +00:00
projection tmp(rhs);
swap(tmp);
2015-04-26 21:24:19 +00:00
proj_ctx_ = nullptr;
proj_ = nullptr;
2013-03-27 10:39:02 +00:00
if (!defer_proj_init_) init_proj4();
2010-06-02 11:03:30 +00:00
return *this;
}
2012-02-02 01:53:35 +00:00
bool projection::operator==(const projection& other) const
2010-06-02 11:03:30 +00:00
{
return (params_ == other.params_);
}
2012-02-02 01:53:35 +00:00
bool projection::operator!=(const projection& other) const
2010-06-02 11:03:30 +00:00
{
return !(*this == other);
}
2012-02-02 01:53:35 +00:00
void projection::init_proj4() const
{
#ifdef MAPNIK_USE_PROJ4
if (!proj_)
{
#if PJ_VERSION >= 480
proj_ctx_ = pj_ctx_alloc();
proj_ = pj_init_plus_ctx(proj_ctx_, params_.c_str());
2015-04-26 21:24:19 +00:00
if (!proj_ || !proj_ctx_)
{
if (proj_ctx_) {
pj_ctx_free(proj_ctx_);
2015-04-26 21:24:19 +00:00
proj_ctx_ = nullptr;
}
if (proj_) {
pj_free(proj_);
proj_ = nullptr;
}
throw proj_init_error(params_);
}
#else
#if defined(MAPNIK_THREADSAFE)
std::lock_guard<std::mutex> lock(mutex_);
#endif
proj_ = pj_init_plus(params_.c_str());
if (!proj_) throw proj_init_error(params_);
#endif
is_geographic_ = pj_is_latlong(proj_) ? true : false;
}
#endif
}
2010-06-02 11:03:30 +00:00
bool projection::is_initialized() const
{
return proj_ ? true : false;
}
2012-02-02 01:53:35 +00:00
2010-06-02 11:03:30 +00:00
bool projection::is_geographic() const
{
return is_geographic_;
2010-06-02 11:03:30 +00:00
}
2012-02-02 01:53:35 +00:00
boost::optional<well_known_srs_e> projection::well_known() const
{
return is_well_known_srs(params_);
}
2010-06-02 11:03:30 +00:00
std::string const& projection::params() const
{
return params_;
}
2012-02-02 01:53:35 +00:00
2010-06-02 11:03:30 +00:00
void projection::forward(double & x, double &y ) const
{
#ifdef MAPNIK_USE_PROJ4
if (!proj_)
{
throw std::runtime_error("projection::forward not supported unless proj4 is initialized");
}
#if defined(MAPNIK_THREADSAFE) && PJ_VERSION < 480
std::lock_guard<std::mutex> lock(mutex_);
#endif
2010-06-02 11:03:30 +00:00
projUV p;
p.u = x * DEG_TO_RAD;
p.v = y * DEG_TO_RAD;
p = pj_fwd(p,proj_);
x = p.u;
y = p.v;
if (is_geographic_)
{
2010-06-02 11:03:30 +00:00
x *=RAD_TO_DEG;
y *=RAD_TO_DEG;
2012-02-02 01:53:35 +00:00
}
#else
throw std::runtime_error("projection::forward not supported without proj4 support (-DMAPNIK_USE_PROJ4)");
#endif
2010-06-02 11:03:30 +00:00
}
2012-02-02 01:53:35 +00:00
2010-06-02 11:03:30 +00:00
void projection::inverse(double & x,double & y) const
{
#ifdef MAPNIK_USE_PROJ4
if (!proj_)
{
throw std::runtime_error("projection::inverse not supported unless proj4 is initialized");
}
#if defined(MAPNIK_THREADSAFE) && PJ_VERSION < 480
std::lock_guard<std::mutex> lock(mutex_);
#endif
if (is_geographic_)
{
2010-06-02 11:03:30 +00:00
x *=DEG_TO_RAD;
y *=DEG_TO_RAD;
2012-02-02 01:53:35 +00:00
}
2010-06-02 11:03:30 +00:00
projUV p;
p.u = x;
p.v = y;
p = pj_inv(p,proj_);
x = RAD_TO_DEG * p.u;
y = RAD_TO_DEG * p.v;
#else
throw std::runtime_error("projection::inverse not supported without proj4 support (-DMAPNIK_USE_PROJ4)");
#endif
2010-06-02 11:03:30 +00:00
}
2012-02-02 01:53:35 +00:00
projection::~projection()
2010-06-02 11:03:30 +00:00
{
#ifdef MAPNIK_USE_PROJ4
2015-04-26 21:24:19 +00:00
#if defined(MAPNIK_THREADSAFE) && PJ_VERSION < 480
std::lock_guard<std::mutex> lock(mutex_);
2015-04-26 21:24:19 +00:00
#endif
if (proj_)
{
pj_free(proj_);
proj_ = nullptr;
}
#if PJ_VERSION >= 480
if (proj_ctx_)
{
pj_ctx_free(proj_ctx_);
proj_ctx_ = nullptr;
}
#endif
#endif
2010-06-02 11:03:30 +00:00
}
2012-02-02 01:53:35 +00:00
std::string projection::expanded() const
{
#ifdef MAPNIK_USE_PROJ4
if (proj_) return mapnik::util::trim_copy(pj_get_def( proj_, 0 ));
#endif
return params_;
}
2012-02-02 01:53:35 +00:00
void projection::swap(projection& rhs)
2010-06-02 11:03:30 +00:00
{
std::swap(params_,rhs.params_);
2013-03-27 10:39:02 +00:00
std::swap(defer_proj_init_,rhs.defer_proj_init_);
std::swap(is_geographic_,rhs.is_geographic_);
2010-06-02 11:03:30 +00:00
}
}