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 <cstddef>
35#include <iostream>
36#include <set>
37#include <stdexcept>
38#include <string>
39
40#include <drain/Log.h>
41#include "Point.h"
42#include "Projector.h"
43
44
45namespace drain
46{
47
63class Proj6
64{
65public:
66
67 Proj6();
68
69 Proj6(const Proj6 &p);
70
71 virtual ~Proj6();
72
73
74 static inline
75 const std::string & getProjVersion(){
76 return Projector::proj4version;
77 }
78
79 Projector src;
80 Projector dst;
81
82
84
87 /*
88 inline
89 void setProjectionSrc(const std::string & projDef, Projector::CRS_mode crs=Projector::FORCE_CRS){
90 //src.clear();
91 src.setProjection(projDef, crs);
92 setMapping(true);
93 }
94 */
95
96 template <typename ...T>
97 inline
98 void setProjectionSrc(const T& ...args){
99 //src.clear();
100 src.setProjection(args...);
101 setDirectMapping(true);
102 }
103
105
108 inline
109 void setProjectionSrc(const Projector & projDef){
110 drain::Logger mout(__FILE__, __FUNCTION__);
111 mout.unimplemented<LOG_ERR>("Not setting ", projDef.getProjDef());
112 //src.setProjection(projDef, crs);
113 // setMapping(true);
114 }
115
117 /*
118 inline
119 void setProjectionDst(const std::string & projDef, Projector::CRS_mode crs=Projector::FORCE_CRS){
120 dst.setProjection(projDef, crs);
121 setDirectMapping(true);
122 }
123 */
124
125 template <typename ...T>
126 inline
127 void setProjectionDst(const T & ...args){
128 dst.setProjection(args...);
129 setDirectMapping(true);
130 }
131
132
134 inline
135 void setProjectionDst(const Projector & projDef){
136 drain::Logger mout(__FILE__, __FUNCTION__);
137 mout.unimplemented<LOG_ERR>("Not setting ", projDef.getProjDef());
138 // dst.setProjection(projDef, crs);
139 // setMapping(true);
140 }
141
143 inline
144 void setProjections(const std::string & projDefSrc, const std::string & projDefDst){
145 src.setProjection(projDefSrc, Projector::FORCE_CRS);
146 dst.setProjection(projDefDst, Projector::FORCE_CRS);
147 setDirectMapping(false);
148 }
149
150
151 //void setMapping(const std::string & proj1, const std::string & proj2);
152
153
155
160 inline
161 const std::string & getProjStrSrc() const {
162 return src.getProjDef();
163 //return getProjection(projSrc, projStrSrc);
164 };
165
166 inline
167 const std::string & getProjStrDst() const {
168 return dst.getProjDef();
169 //return getProjection(projDst, projStrDst);
170 };
171
172 inline
173 const Projector & getSrc() const {
174 return src;
175 };
176
177 inline
178 const Projector & getDst() const {
179 return dst;
180 };
181
182
183
185 inline
186 void projectFwd(double & x, double & y) const {
187 project<PJ_FWD>(x,y);
188 }
189
191 inline
192 void projectFwd(double x, double y, double & x2, double & y2) const {
193 x2 = x;
194 y2 = y;
195 project<PJ_FWD>(x2,y2);
196 }
197
199 inline
200 void projectFwd(drain::Point2D<double> & point) const {
201 project<PJ_FWD>(point);
202 }
203
205 inline
206 void projectFwd(const drain::Point2D<double> & point, drain::Point2D<double> & point2) const {
207 project<PJ_FWD>(point2 = point);
208 }
209
210
211 // Inverse projection (in-place)
212 inline
213 void projectInv(double & x, double & y) const {
214 project<PJ_INV>(x,y);
215 }
216
217 // Inverse projection
218 inline
219 void projectInv(double x, double y, double & x2, double & y2) const {
220 x2 = x;
221 y2 = y;
222 project<PJ_INV>(x2,y2);
223 }
224
225 // Inverse projection. Example implementation of project<>() .
226 inline
227 void projectInv(drain::Point2D<double> & point) const {
228 project<PJ_INV>(point);
229 }
230
232 inline
233 void projectInv(const drain::Point2D<double> & point, drain::Point2D<double> & point2) const {
234 project<PJ_INV>(point2 = point);
235 }
236
237
238
239 inline
240 void debug(std::ostream & ostr = std::cout, int wkt = -1){
241
242 ostr << "SRC:\n";
243 src.info(ostr, wkt);
244 ostr << std::endl;
245
246 ostr << "DST:\n";
247 dst.info(ostr, wkt);
248 ostr << std::endl;
249 }
250
251
252
254 inline
255 bool isLongLat() const {
256 return dst.isLongLat();
257 }
258
259 inline
260 bool isSet() const {
261 return (src.isSet() && dst.isSet());
262 }
263
264 inline
265 std::string getErrorString() const {
266 int err = proj_context_errno(pjContext);
267 return proj_errno_string(err);
268 //return "Not Impl."; //std::string(pj_strerrno(*pj_get_errno_ref()));
269 };
270
271
272
274
286 // static short int pickEpsgCodeOLD(const std::string & projDef, std::list<std::string> & projArgs);
287
288protected:
289
290 PJ_CONTEXT *pjContext = nullptr; // = proj_context_create();
291 PJ *proj = nullptr; // two-way
292
294 void setDirectMapping(bool lenient);
295
296
300 template
301 <PJ_DIRECTION D,class POINT_XY>
302 inline
303 void project(POINT_XY & point) const {
304 // void project(drain::Point2D<double> & point) const {
305 // Note: sizeof not needed, future option for arrays.
306 proj_trans_generic(proj, D, &point.x, sizeof(POINT_XY), 1, &point.y, sizeof(POINT_XY), 1, 0, 0, 0, 0, 0, 0);
307 }
308
309 template
310 <PJ_DIRECTION D>
311 inline
312 void project(double & x, double & y) const {
313 // Note: sizeof not needed, future option for arrays.
314 proj_trans_generic(proj, D, &x, sizeof(double), 1, &y, sizeof(double), 1, 0, 0, 0, 0, 0, 0);
315 }
316
317
318
319
320};
321
322DRAIN_TYPENAME(Proj6);
323
324std::ostream & operator<<(std::ostream & ostr, const Proj6 &p);
325
326} // drain
327
328
329#endif /*PROJ4_H_*/
330
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:64
void setDirectMapping(bool lenient)
Set crs_to_crs projection, if both src and dst projections are set.
Definition Proj6.cpp:83
void setProjectionDst(const Projector &projDef)
Sets destination projection, primarily using EPSG code.
Definition Proj6.h:135
PJ_CONTEXT * pjContext
Detect EPSG code from "+init=epsg:EPSG" argument.
Definition Proj6.h:290
bool isLongLat() const
Check if destination projection is longitude-latitude degrees.
Definition Proj6.h:255
void setProjectionSrc(const T &...args)
Sets source projection.
Definition Proj6.h:98
void setProjectionSrc(const Projector &projDef)
Sets source projection, primarily using EPSG code.
Definition Proj6.h:109
void setProjections(const std::string &projDefSrc, const std::string &projDefDst)
Sets source and destination projection. TOOD: EPSG code handling.
Definition Proj6.h:144
void projectInv(const drain::Point2D< double > &point, drain::Point2D< double > &point2) const
Forward projection. Example implementation of project<>() .
Definition Proj6.h:233
const std::string & getProjStrSrc() const
Returns the projection std::string applied by the last setProjection call.
Definition Proj6.h:161
void projectFwd(double x, double y, double &x2, double &y2) const
Forward projection.
Definition Proj6.h:192
void project(POINT_XY &point) const
Definition Proj6.h:303
void projectFwd(double &x, double &y) const
Forward projection (in-place)
Definition Proj6.h:186
void projectFwd(drain::Point2D< double > &point) const
Forward projection. Example implementation of project<>() .
Definition Proj6.h:200
void setProjectionDst(const T &...args)
Sets destination projection.
Definition Proj6.h:127
void projectFwd(const drain::Point2D< double > &point, drain::Point2D< double > &point2) const
Forward projection. Example implementation of project<>() .
Definition Proj6.h:206
Definition Projector.h:54
bool isSet() const
Returns true, if PJ object has been set.
Definition Projector.h:157
void info(std::ostream &ostr=std::cout, int wkt=-1) const
Prunes "+init=epsg:<...>" and optionally "+type=crs" codes.
Definition Projector.h:181
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
DRAIN_TYPENAME(void)
Add a specialization for each type of those you want to support.
Definition Point.h:48