/
projection.cpp
162 lines (142 loc) · 3.65 KB
/
projection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2011 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$
// mapnik
#include <mapnik/projection.hpp>
#include <mapnik/utils.hpp>
// boost
#include <boost/algorithm/string.hpp>
// proj4
#include <proj_api.h>
namespace mapnik {
#if defined(MAPNIK_THREADSAFE) && PJ_VERSION < 480
boost::mutex projection::mutex_;
#endif
projection::projection(std::string const& params)
: params_(params)
{
init(); //
}
projection::projection(projection const& rhs)
: params_(rhs.params_)
{
init(); //
}
projection& projection::operator=(projection const& rhs)
{
projection tmp(rhs);
swap(tmp);
return *this;
}
bool projection::operator==(const projection& other) const
{
return (params_ == other.params_);
}
bool projection::operator!=(const projection& other) const
{
return !(*this == other);
}
bool projection::is_initialized() const
{
return proj_ ? true : false;
}
bool projection::is_geographic() const
{
return is_geographic_;
}
std::string const& projection::params() const
{
return params_;
}
void projection::forward(double & x, double &y ) const
{
#if defined(MAPNIK_THREADSAFE) && PJ_VERSION < 480
mutex::scoped_lock lock(mutex_);
#endif
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_)
{
x *=RAD_TO_DEG;
y *=RAD_TO_DEG;
}
}
void projection::inverse(double & x,double & y) const
{
#if defined(MAPNIK_THREADSAFE) && PJ_VERSION < 480
mutex::scoped_lock lock(mutex_);
#endif
if (is_geographic_)
{
x *=DEG_TO_RAD;
y *=DEG_TO_RAD;
}
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;
}
projection::~projection()
{
#if defined(MAPNIK_THREADSAFE) && PJ_VERSION < 480
mutex::scoped_lock lock(mutex_);
#endif
if (proj_) pj_free(proj_);
#if PJ_VERSION >= 480
if (proj_ctx_) pj_ctx_free(proj_ctx_);
#endif
}
void projection::init()
{
#if defined(MAPNIK_THREADSAFE) && PJ_VERSION < 480
mutex::scoped_lock lock(mutex_);
#endif
#if PJ_VERSION >= 480
proj_ctx_=pj_ctx_alloc();
proj_=pj_init_plus_ctx(proj_ctx_, params_.c_str());
#else
proj_=pj_init_plus(params_.c_str());
#endif
if (!proj_) throw proj_init_error(params_);
is_geographic_ = pj_is_latlong(proj_) ? true : false;
}
std::string projection::expanded() const
{
if (proj_) {
std::string def(pj_get_def( proj_, 0 ));
//boost::algorithm::ireplace_first(def,params_,"");
return boost::trim_copy(def);
}
return std::string("");
}
void projection::swap (projection& rhs)
{
std::swap(params_,rhs.params_);
init ();
}
}