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 /*
80 inline
81 void clear(){
82 src.clear();
83 dst.clear();
84 }
85 */
86
88
91 /*
92 inline
93 void setProjectionSrc(const std::string & projDef, Projector::CRS_mode crs=Projector::FORCE_CRS){
94 //src.clear();
95 src.setProjection(projDef, crs);
96 setMapping(true);
97 }
98 */
99
100 template <typename ...T>
101 inline
102 void setProjectionSrc(const T& ...args){
103 //src.clear();
104 src.setProjection(args...);
105 setDirectMapping(true);
106 }
107
109
112 inline
113 void setProjectionSrc(const Projector & projDef){
114 drain::Logger mout(__FILE__, __FUNCTION__);
115 mout.unimplemented<LOG_ERR>("Not setting ", projDef.getProjDef());
116 //src.setProjection(projDef, crs);
117 // setMapping(true);
118 }
119
121 /*
122 inline
123 void setProjectionDst(const std::string & projDef, Projector::CRS_mode crs=Projector::FORCE_CRS){
124 dst.setProjection(projDef, crs);
125 setDirectMapping(true);
126 }
127 */
128
129 template <typename ...T>
130 inline
131 void setProjectionDst(const T & ...args){
132 dst.setProjection(args...);
133 setDirectMapping(true);
134 }
135
136
138 inline
139 void setProjectionDst(const Projector & projDef){
140 drain::Logger mout(__FILE__, __FUNCTION__);
141 mout.unimplemented<LOG_ERR>("Not setting ", projDef.getProjDef());
142 // dst.setProjection(projDef, crs);
143 // setMapping(true);
144 }
145
147 inline
148 void setProjections(const std::string & projDefSrc, const std::string & projDefDst){
149 src.setProjection(projDefSrc, Projector::FORCE_CRS);
150 dst.setProjection(projDefDst, Projector::FORCE_CRS);
151 setDirectMapping(false);
152 }
153
154
155 //void setMapping(const std::string & proj1, const std::string & proj2);
156
157
159
164 inline
165 const std::string & getProjStrSrc() const {
166 return src.getProjDef();
167 //return getProjection(projSrc, projStrSrc);
168 };
169
170 inline
171 const std::string & getProjStrDst() const {
172 return dst.getProjDef();
173 //return getProjection(projDst, projStrDst);
174 };
175
176 inline
177 const Projector & getSrc() const {
178 return src;
179 };
180
181 inline
182 const Projector & getDst() const {
183 return dst;
184 };
185
186
187
189 inline
190 void projectFwd(double & x, double & y) const {
191 project<PJ_FWD>(x,y);
192 }
193
195 inline
196 void projectFwd(double x, double y, double & x2, double & y2) const {
197 x2 = x;
198 y2 = y;
199 project<PJ_FWD>(x2,y2);
200 }
201
203 inline
204 void projectFwd(drain::Point2D<double> & point) const {
205 project<PJ_FWD>(point);
206 }
207
209 inline
210 void projectFwd(const drain::Point2D<double> & point, drain::Point2D<double> & point2) const {
211 project<PJ_FWD>(point2 = point);
212 }
213
214
215 // Inverse projection (in-place)
216 inline
217 void projectInv(double & x, double & y) const {
218 project<PJ_INV>(x,y);
219 }
220
221 // Inverse projection
222 inline
223 void projectInv(double x, double y, double & x2, double & y2) const {
224 x2 = x;
225 y2 = y;
226 project<PJ_INV>(x2,y2);
227 }
228
229 // Inverse projection. Example implementation of project<>() .
230 inline
231 void projectInv(drain::Point2D<double> & point) const {
232 project<PJ_INV>(point);
233 }
234
236 inline
237 void projectInv(const drain::Point2D<double> & point, drain::Point2D<double> & point2) const {
238 project<PJ_INV>(point2 = point);
239 }
240
241
242
243 inline
244 void debug(std::ostream & ostr = std::cout, int wkt = -1){
245
246 ostr << "SRC:\n";
247 src.info(ostr, wkt);
248 ostr << std::endl;
249
250 ostr << "DST:\n";
251 dst.info(ostr, wkt);
252 ostr << std::endl;
253 }
254
255
256
258 inline
259 bool isLongLat() const {
260 return dst.isLongLat();
261 }
262
263 inline
264 bool isSet() const {
265 return (src.isSet() && dst.isSet());
266 }
267
268 inline
269 std::string getErrorString() const {
270 int err = proj_context_errno(pjContext);
271 return proj_errno_string(err);
272 //return "Not Impl."; //std::string(pj_strerrno(*pj_get_errno_ref()));
273 };
274
275
276
278
290 // static short int pickEpsgCodeOLD(const std::string & projDef, std::list<std::string> & projArgs);
291
292protected:
293
294 PJ_CONTEXT *pjContext = nullptr; // = proj_context_create();
295 PJ *proj = nullptr; // two-way
296
298 void setDirectMapping(bool lenient);
299
300
304 template
305 <PJ_DIRECTION D,class POINT_XY>
306 inline
307 void project(POINT_XY & point) const {
308 // void project(drain::Point2D<double> & point) const {
309 // Note: sizeof not needed, future option for arrays.
310 proj_trans_generic(proj, D, &point.x, sizeof(POINT_XY), 1, &point.y, sizeof(POINT_XY), 1, 0, 0, 0, 0, 0, 0);
311 }
312
313 template
314 <PJ_DIRECTION D>
315 inline
316 void project(double & x, double & y) const {
317 // Note: sizeof not needed, future option for arrays.
318 proj_trans_generic(proj, D, &x, sizeof(double), 1, &y, sizeof(double), 1, 0, 0, 0, 0, 0, 0);
319 }
320
321
322
323
324};
325
326DRAIN_TYPENAME(PJ_INFO);
327DRAIN_TYPENAME(Proj6);
328
329std::ostream & operator<<(std::ostream & ostr, const Proj6 &p);
330
331} // drain
332
333
334#endif /*PROJ4_H_*/
335
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:326
Logger & unimplemented(const TT &... args)
Feature to be done. Special type of Logger::note().
Definition Log.h:521
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:139
PJ_CONTEXT * pjContext
Detect EPSG code from "+init=epsg:EPSG" argument.
Definition Proj6.h:294
bool isLongLat() const
Check if destination projection is longitude-latitude degrees.
Definition Proj6.h:259
void setProjectionSrc(const T &...args)
Sets source projection.
Definition Proj6.h:102
void setProjectionSrc(const Projector &projDef)
Sets source projection, primarily using EPSG code.
Definition Proj6.h:113
void setProjections(const std::string &projDefSrc, const std::string &projDefDst)
Sets source and destination projection. TOOD: EPSG code handling.
Definition Proj6.h:148
void projectInv(const drain::Point2D< double > &point, drain::Point2D< double > &point2) const
Forward projection. Example implementation of project<>() .
Definition Proj6.h:237
const std::string & getProjStrSrc() const
Returns the projection std::string applied by the last setProjection call.
Definition Proj6.h:165
void projectFwd(double x, double y, double &x2, double &y2) const
Forward projection.
Definition Proj6.h:196
void project(POINT_XY &point) const
Definition Proj6.h:307
void projectFwd(double &x, double &y) const
Forward projection (in-place)
Definition Proj6.h:190
void projectFwd(drain::Point2D< double > &point) const
Forward projection. Example implementation of project<>() .
Definition Proj6.h:204
void setProjectionDst(const T &...args)
Sets destination projection.
Definition Proj6.h:131
void projectFwd(const drain::Point2D< double > &point, drain::Point2D< double > &point2) const
Forward projection. Example implementation of project<>() .
Definition Proj6.h:210
Definition Projector.h:52
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 Data.h:1473
std::ostream & operator<<(std::ostream &ostr, const PlainData< DT > &d)
Definition Data.h:631
Definition Point.h:48