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