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)
|
|
|
|
*
|
2011-10-23 15:04:25 +02:00
|
|
|
* Copyright (C) 2011 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>
|
2008-02-04 12:54:07 +01:00
|
|
|
#include <mapnik/utils.hpp>
|
2011-05-17 21:05:15 +02:00
|
|
|
|
|
|
|
// boost
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
2007-10-08 19:42:41 +02:00
|
|
|
// proj4
|
|
|
|
#include <proj_api.h>
|
2006-10-17 19:26:35 +02:00
|
|
|
|
|
|
|
namespace mapnik {
|
2008-02-04 12:54:07 +01:00
|
|
|
|
2012-03-24 16:03:15 +01:00
|
|
|
#if defined(MAPNIK_THREADSAFE) && PJ_VERSION < 480
|
2012-04-11 17:23:41 +02:00
|
|
|
#warning mapnik is building against < proj 4.8, reprojection will be faster if you use >= 4.8
|
2010-06-02 13:03:30 +02:00
|
|
|
boost::mutex projection::mutex_;
|
2008-02-04 17:12:13 +01:00
|
|
|
#endif
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-08-05 13:56:11 +02:00
|
|
|
projection::projection(std::string const& params)
|
2010-06-02 13:03:30 +02:00
|
|
|
: params_(params)
|
2012-02-02 02:53:35 +01:00
|
|
|
{
|
2012-04-11 17:23:41 +02:00
|
|
|
init();
|
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)
|
2012-02-02 02:53:35 +01:00
|
|
|
: params_(rhs.params_)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2012-04-11 17:23:41 +02:00
|
|
|
init();
|
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);
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
{
|
2012-03-24 16:03:15 +01:00
|
|
|
#if defined(MAPNIK_THREADSAFE) && PJ_VERSION < 480
|
2010-06-02 13:03:30 +02:00
|
|
|
mutex::scoped_lock lock(mutex_);
|
2008-02-04 17:12:13 +01:00
|
|
|
#endif
|
2010-06-02 13:03:30 +02: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;
|
2010-07-22 01:05:22 +02:00
|
|
|
if (is_geographic_)
|
2006-10-17 19:26:35 +02:00
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
x *=RAD_TO_DEG;
|
|
|
|
y *=RAD_TO_DEG;
|
2012-02-02 02:53:35 +01:00
|
|
|
}
|
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
|
|
|
|
{
|
2012-03-24 16:03:15 +01:00
|
|
|
#if defined(MAPNIK_THREADSAFE) && PJ_VERSION < 480
|
2010-06-02 13:03:30 +02:00
|
|
|
mutex::scoped_lock lock(mutex_);
|
2008-02-04 17:12:13 +01:00
|
|
|
#endif
|
2010-07-22 01:05:22 +02:00
|
|
|
if (is_geographic_)
|
2006-10-17 19:26:35 +02:00
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
x *=DEG_TO_RAD;
|
|
|
|
y *=DEG_TO_RAD;
|
2012-02-02 02:53:35 +01:00
|
|
|
}
|
2010-06-02 13:03:30 +02: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;
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
|
|
|
projection::~projection()
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2012-03-24 16:03:15 +01:00
|
|
|
#if defined(MAPNIK_THREADSAFE) && PJ_VERSION < 480
|
2010-06-02 13:03:30 +02:00
|
|
|
mutex::scoped_lock lock(mutex_);
|
2008-02-04 17:12:13 +01:00
|
|
|
#endif
|
2010-06-02 13:03:30 +02:00
|
|
|
if (proj_) pj_free(proj_);
|
2010-09-24 17:26:50 +02:00
|
|
|
#if PJ_VERSION >= 480
|
|
|
|
if (proj_ctx_) pj_ctx_free(proj_ctx_);
|
|
|
|
#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::init()
|
|
|
|
{
|
2012-03-24 16:03:15 +01:00
|
|
|
#if defined(MAPNIK_THREADSAFE) && PJ_VERSION < 480
|
2010-06-02 13:03:30 +02:00
|
|
|
mutex::scoped_lock lock(mutex_);
|
2010-09-24 17:26:50 +02:00
|
|
|
#endif
|
|
|
|
#if PJ_VERSION >= 480
|
2012-04-11 17:06:29 +02:00
|
|
|
proj_ctx_ = pj_ctx_alloc();
|
|
|
|
proj_ = pj_init_plus_ctx(proj_ctx_, params_.c_str());
|
|
|
|
if (!proj_)
|
|
|
|
{
|
|
|
|
if (proj_ctx_) pj_ctx_free(proj_ctx_);
|
|
|
|
throw proj_init_error(params_);
|
|
|
|
}
|
2010-09-24 17:26:50 +02:00
|
|
|
#else
|
2012-04-11 17:06:29 +02:00
|
|
|
proj_ = pj_init_plus(params_.c_str());
|
2010-06-02 13:03:30 +02:00
|
|
|
if (!proj_) throw proj_init_error(params_);
|
2012-04-11 17:06:29 +02:00
|
|
|
#endif
|
2010-07-22 01:05:22 +02:00
|
|
|
is_geographic_ = pj_is_latlong(proj_) ? true : false;
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2011-05-17 21:05:15 +02:00
|
|
|
|
|
|
|
std::string projection::expanded() const
|
|
|
|
{
|
|
|
|
if (proj_) {
|
|
|
|
std::string def(pj_get_def( proj_, 0 ));
|
|
|
|
//boost::algorithm::ireplace_first(def,params_,"");
|
2012-04-06 22:39:13 +02:00
|
|
|
boost::trim(def);
|
|
|
|
return def;
|
2011-05-17 21:05:15 +02:00
|
|
|
}
|
|
|
|
return std::string("");
|
|
|
|
}
|
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_);
|
2012-04-11 17:23:41 +02:00
|
|
|
init();
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2006-10-17 19:26:35 +02:00
|
|
|
}
|