Loading...
Searching...
No Matches
Proj6.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31#ifndef DRAIN_PROJ6_H_
32#define DRAIN_PROJ6_H_
33
34#include <iostream>
35#include <string>
36
37#include <drain/Log.h>
38#include "Point.h"
39#include "Projector.h"
40
41
42namespace drain
43{
44
60class Proj6
61{
62public:
63
64 Proj6();
65
66 Proj6(const Proj6 &p);
67
68 virtual ~Proj6();
69
70
71 static inline
72 const std::string & getProjVersion(){
73 return Projector::proj4version;
74 }
75
76 Projector src;
77 Projector dst;
78
79
81
84 /*
85 inline
86 void setProjectionSrc(const std::string & projDef, Projector::CRS_mode crs=Projector::FORCE_CRS){
87 //src.clear();
88 src.setProjection(projDef, crs);
89 setMapping(true);
90 }
91 */
92
93 template <typename ...T>
94 inline
95 void setProjectionSrc(const T& ...args){
96 //src.clear();
97 src.setProjection(args...);
98 setDirectMapping(true);
99 }
100
102
105 inline
106 void setProjectionSrc(const Projector & projDef){
107 drain::Logger mout(__FILE__, __FUNCTION__);
108 mout.unimplemented<LOG_ERR>("Not setting ", projDef.getProjDef());
109 //src.setProjection(projDef, crs);
110 // setMapping(true);
111 }
112
114 /*
115 inline
116 void setProjectionDst(const std::string & projDef, Projector::CRS_mode crs=Projector::FORCE_CRS){
117 dst.setProjection(projDef, crs);
118 setDirectMapping(true);
119 }
120 */
121
122 template <typename ...T>
123 inline
124 void setProjectionDst(const T & ...args){
125 dst.setProjection(args...);
126 setDirectMapping(true);
127 }
128
129
131 inline
132 void setProjectionDst(const Projector & projDef){
133 drain::Logger mout(__FILE__, __FUNCTION__);
134 mout.unimplemented<LOG_ERR>("Not setting ", projDef.getProjDef());
135 // dst.setProjection(projDef, crs);
136 // setMapping(true);
137 }
138
140 inline
141 void setProjections(const std::string & projDefSrc, const std::string & projDefDst){
142 src.setProjection(projDefSrc, Projector::FORCE_CRS);
143 dst.setProjection(projDefDst, Projector::FORCE_CRS);
144 setDirectMapping(false);
145 }
146
147
148 //void setMapping(const std::string & proj1, const std::string & proj2);
149
150
152
157 inline
158 const std::string & getProjStrSrc() const {
159 return src.getProjDef();
160 //return getProjection(projSrc, projStrSrc);
161 };
162
163 inline
164 const std::string & getProjStrDst() const {
165 return dst.getProjDef();
166 //return getProjection(projDst, projStrDst);
167 };
168
169 inline
170 const Projector & getSrc() const {
171 return src;
172 };
173
174 inline
175 const Projector & getDst() const {
176 return dst;
177 };
178
179
180
182 inline
183 void projectFwd(double & x, double & y) const {
184 project<PJ_FWD>(x,y);
185 }
186
188 inline
189 void projectFwd(double x, double y, double & x2, double & y2) const {
190 x2 = x;
191 y2 = y;
192 project<PJ_FWD>(x2,y2);
193 }
194
196 inline
197 void projectFwd(drain::Point2D<double> & point) const {
198 project<PJ_FWD>(point);
199 }
200
202 inline
203 void projectFwd(const drain::Point2D<double> & point, drain::Point2D<double> & point2) const {
204 project<PJ_FWD>(point2 = point);
205 }
206
207
208 // Inverse projection (in-place)
209 inline
210 void projectInv(double & x, double & y) const {
211 project<PJ_INV>(x,y);
212 }
213
214 // Inverse projection
215 inline
216 void projectInv(double x, double y, double & x2, double & y2) const {
217 x2 = x;
218 y2 = y;
219 project<PJ_INV>(x2,y2);
220 }
221
222 // Inverse projection. Example implementation of project<>() .
223 inline
224 void projectInv(drain::Point2D<double> & point) const {
225 project<PJ_INV>(point);
226 }
227
229 inline
230 void projectInv(const drain::Point2D<double> & point, drain::Point2D<double> & point2) const {
231 project<PJ_INV>(point2 = point);
232 }
233
234
235
236 inline
237 void debug(std::ostream & ostr = std::cout, int wkt = -1){
238
239 ostr << "SRC:\n";
240 src.info(ostr, wkt);
241 ostr << std::endl;
242
243 ostr << "DST:\n";
244 dst.info(ostr, wkt);
245 ostr << std::endl;
246 }
247
248
249
251 inline
252 bool isLongLat() const {
253 return dst.isLongLat();
254 }
255
256 inline
257 bool isSet() const {
258 return (src.isSet() && dst.isSet());
259 }
260
261 inline
262 std::string getErrorString() const {
263 int err = proj_context_errno(pjContext);
264 return proj_errno_string(err);
265 //return "Not Impl."; //std::string(pj_strerrno(*pj_get_errno_ref()));
266 };
267
268
269
271
283 // static short int pickEpsgCodeOLD(const std::string & projDef, std::list<std::string> & projArgs);
284
285protected:
286
287 PJ_CONTEXT *pjContext = nullptr; // = proj_context_create();
288 PJ *proj = nullptr; // two-way
289
291 void setDirectMapping(bool lenient);
292
293
297 template
298 <PJ_DIRECTION D,class POINT_XY>
299 inline
300 void project(POINT_XY & point) const {
301 // void project(drain::Point2D<double> & point) const {
302 // Note: sizeof not needed, future option for arrays.
303 proj_trans_generic(proj, D, &point.x, sizeof(POINT_XY), 1, &point.y, sizeof(POINT_XY), 1, 0, 0, 0, 0, 0, 0);
304 }
305
306 template
307 <PJ_DIRECTION D>
308 inline
309 void project(double & x, double & y) const {
310 // Note: sizeof not needed, future option for arrays.
311 proj_trans_generic(proj, D, &x, sizeof(double), 1, &y, sizeof(double), 1, 0, 0, 0, 0, 0, 0);
312 }
313
314
315
316
317};
318
319DRAIN_TYPENAME(PJ_INFO);
320DRAIN_TYPENAME(Proj6);
321
322std::ostream & operator<<(std::ostream & ostr, const Proj6 &p);
323
324} // drain
325
326
327#endif /*PROJ4_H_*/
328
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:313
Logger & unimplemented(const TT &... args)
Feature to be done. Special type of Logger::note().
Definition Log.h:512
Definition Proj6.h:61
void setDirectMapping(bool lenient)
Set crs_to_crs projection, if both src and dst projections are set.
Definition Proj6.cpp:81
void setProjectionDst(const Projector &projDef)
Sets destination projection, primarily using EPSG code.
Definition Proj6.h:132
PJ_CONTEXT * pjContext
Detect EPSG code from "+init=epsg:EPSG" argument.
Definition Proj6.h:287
bool isLongLat() const
Check if destination projection is longitude-latitude degrees.
Definition Proj6.h:252
void setProjectionSrc(const T &...args)
Sets source projection.
Definition Proj6.h:95
void setProjectionSrc(const Projector &projDef)
Sets source projection, primarily using EPSG code.
Definition Proj6.h:106
void setProjections(const std::string &projDefSrc, const std::string &projDefDst)
Sets source and destination projection. TOOD: EPSG code handling.
Definition Proj6.h:141
void projectInv(const drain::Point2D< double > &point, drain::Point2D< double > &point2) const
Forward projection. Example implementation of project<>() .
Definition Proj6.h:230
const std::string & getProjStrSrc() const
Returns the projection std::string applied by the last setProjection call.
Definition Proj6.h:158
void projectFwd(double x, double y, double &x2, double &y2) const
Forward projection.
Definition Proj6.h:189
void project(POINT_XY &point) const
Definition Proj6.h:300
void projectFwd(double &x, double &y) const
Forward projection (in-place)
Definition Proj6.h:183
void projectFwd(drain::Point2D< double > &point) const
Forward projection. Example implementation of project<>() .
Definition Proj6.h:197
void setProjectionDst(const T &...args)
Sets destination projection.
Definition Proj6.h:124
void projectFwd(const drain::Point2D< double > &point, drain::Point2D< double > &point2) const
Forward projection. Example implementation of project<>() .
Definition Proj6.h:203
Definition Projector.h:52
bool isSet() const
Returns true, if PJ object has been set.
Definition Projector.h:155
void info(std::ostream &ostr=std::cout, int wkt=-1) const
Prunes "+init=epsg:<...>" and optionally "+type=crs" codes.
Definition Projector.h:179
void setProjection(const std::string &str, CRS_mode crs=FORCE_CRS)
Sets projection defined as Proj string.
Definition Projector.cpp:98
Definition DataSelector.cpp:1277
Definition Point.h:48