2006-10-17 19:26:35 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
//$Id$
|
|
|
|
|
2008-02-04 12:54:07 +01:00
|
|
|
// mapnik
|
2006-10-17 19:26:35 +02:00
|
|
|
#include <mapnik/proj_transform.hpp>
|
2008-02-04 12:54:07 +01:00
|
|
|
#include <mapnik/utils.hpp>
|
|
|
|
// proj4
|
2007-10-08 19:42:41 +02:00
|
|
|
#include <proj_api.h>
|
|
|
|
|
2006-10-17 19:26:35 +02:00
|
|
|
namespace mapnik {
|
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
proj_transform::proj_transform(projection const& source,
|
|
|
|
projection const& dest)
|
|
|
|
: source_(source),
|
|
|
|
dest_(dest)
|
|
|
|
{
|
2010-08-10 20:18:31 +02:00
|
|
|
is_source_longlat_ = source_.is_geographic();
|
|
|
|
is_dest_longlat_ = dest_.is_geographic();
|
2010-07-22 01:05:22 +02:00
|
|
|
is_source_equal_dest_ = (source_ == dest_);
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2010-07-22 01:05:22 +02:00
|
|
|
|
|
|
|
bool proj_transform::equal() const
|
|
|
|
{
|
|
|
|
return is_source_equal_dest_;
|
|
|
|
}
|
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
bool proj_transform::forward (double & x, double & y , double & z) const
|
|
|
|
{
|
2010-07-22 01:05:22 +02:00
|
|
|
if (is_source_equal_dest_)
|
2010-06-02 13:03:30 +02:00
|
|
|
return true;
|
2008-12-07 17:23:57 +01:00
|
|
|
|
2010-08-10 20:18:31 +02:00
|
|
|
if (is_source_longlat_)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
x *= DEG_TO_RAD;
|
|
|
|
y *= DEG_TO_RAD;
|
|
|
|
}
|
2010-07-22 01:05:22 +02:00
|
|
|
|
2010-09-24 17:26:50 +02:00
|
|
|
#if defined(MAPNIK_THREADSAFE) && PJ_VERSION < 480
|
2010-07-22 01:05:22 +02:00
|
|
|
mutex::scoped_lock lock(projection::mutex_);
|
|
|
|
#endif
|
2006-10-17 19:26:35 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
if (pj_transform( source_.proj_, dest_.proj_, 1,
|
|
|
|
0, &x,&y,&z) != 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2006-10-17 19:26:35 +02:00
|
|
|
|
2010-08-10 20:18:31 +02:00
|
|
|
if (is_dest_longlat_)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
x *= RAD_TO_DEG;
|
|
|
|
y *= RAD_TO_DEG;
|
|
|
|
}
|
2006-10-17 19:26:35 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
return true;
|
|
|
|
}
|
2006-10-17 19:26:35 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
bool proj_transform::backward (double & x, double & y , double & z) const
|
|
|
|
{
|
2010-07-22 01:05:22 +02:00
|
|
|
if (is_source_equal_dest_)
|
2006-10-17 19:26:35 +02:00
|
|
|
return true;
|
2010-06-02 13:03:30 +02:00
|
|
|
|
2010-08-10 20:18:31 +02:00
|
|
|
if (is_dest_longlat_)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
x *= DEG_TO_RAD;
|
|
|
|
y *= DEG_TO_RAD;
|
2009-05-24 08:31:32 +02:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
|
2010-09-24 17:26:50 +02:00
|
|
|
#if defined(MAPNIK_THREADSAFE) && PJ_VERSION < 480
|
2010-07-22 01:05:22 +02:00
|
|
|
mutex::scoped_lock lock(projection::mutex_);
|
|
|
|
#endif
|
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
if (pj_transform( dest_.proj_, source_.proj_, 1,
|
|
|
|
0, &x,&y,&z) != 0)
|
2009-05-24 08:31:32 +02:00
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
return false;
|
2009-05-24 08:31:32 +02:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
|
2010-08-10 20:18:31 +02:00
|
|
|
if (is_source_longlat_)
|
2009-05-24 08:31:32 +02:00
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
x *= RAD_TO_DEG;
|
|
|
|
y *= RAD_TO_DEG;
|
2009-05-24 08:31:32 +02:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-04-13 21:40:44 +02:00
|
|
|
bool proj_transform::forward (box2d<double> & box) const
|
|
|
|
{
|
|
|
|
if (is_source_equal_dest_)
|
|
|
|
return true;
|
|
|
|
double minx = box.minx();
|
|
|
|
double miny = box.miny();
|
|
|
|
double maxx = box.maxx();
|
|
|
|
double maxy = box.maxy();
|
|
|
|
double z = 0.0;
|
|
|
|
bool ok0 = forward(minx,miny,z);
|
|
|
|
bool ok1 = forward(maxx,maxy,z);
|
|
|
|
box.init(minx,miny,maxx,maxy);
|
|
|
|
return ok0 & ok1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool proj_transform::backward (box2d<double> & box) const
|
|
|
|
{
|
|
|
|
if (is_source_equal_dest_)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
double minx = box.minx();
|
|
|
|
double miny = box.miny();
|
|
|
|
double maxx = box.maxx();
|
|
|
|
double maxy = box.maxy();
|
|
|
|
double z = 0.0;
|
|
|
|
bool ok0 = backward(minx,miny,z);
|
|
|
|
bool ok1 = backward(maxx,maxy,z);
|
|
|
|
box.init(minx,miny,maxx,maxy);
|
|
|
|
return ok0 & ok1;
|
|
|
|
}
|
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
mapnik::projection const& proj_transform::source() const
|
|
|
|
{
|
|
|
|
return source_;
|
|
|
|
}
|
|
|
|
mapnik::projection const& proj_transform::dest() const
|
|
|
|
{
|
|
|
|
return dest_;
|
|
|
|
}
|
2009-05-24 08:31:32 +02:00
|
|
|
|
2006-10-17 19:26:35 +02:00
|
|
|
}
|