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 inline
90 void setProjectionSrc(const std::string & projDef, Projector::CRS_mode crs=Projector::FORCE_CRS){
91 //src.clear();
92 src.setProjection(projDef, crs);
93 setMapping(true);
94 }
95
97
100 inline
101 void setProjectionSrc(const Projector & projDef){
102 drain::Logger mout(__FILE__, __FUNCTION__);
103 mout.unimplemented<LOG_ERR>("Not setting ", projDef.getProjDef());
104 //src.setProjection(projDef, crs);
105 // setMapping(true);
106 }
107
109 inline
110 void setProjectionDst(const std::string & projDef, Projector::CRS_mode crs=Projector::FORCE_CRS){
111 dst.setProjection(projDef, crs);
112 setMapping(true);
113 }
114
115
117 inline
118 void setProjectionDst(const Projector & projDef){
119 drain::Logger mout(__FILE__, __FUNCTION__);
120 mout.unimplemented<LOG_ERR>("Not setting ", projDef.getProjDef());
121 // dst.setProjection(projDef, crs);
122 // setMapping(true);
123 }
124
126 inline
127 void setProjections(const std::string & projDefSrc, const std::string & projDefDst){
128 src.setProjection(projDefSrc, Projector::FORCE_CRS);
129 dst.setProjection(projDefDst, Projector::FORCE_CRS);
130 setMapping(false);
131 }
132
133
134 //void setMapping(const std::string & proj1, const std::string & proj2);
135
136
138
143 inline
144 const std::string & getProjectionSrc() const {
145 return src.getProjDef();
146 //return getProjection(projSrc, projStrSrc);
147 };
148
149 inline
150 const std::string & getProjectionDst() const {
151 return dst.getProjDef();
152 //return getProjection(projDst, projStrDst);
153 };
154
155 inline
156 const Projector & getSrc() const {
157 return src;
158 };
159
160 inline
161 const Projector & getDst() const {
162 return dst;
163 };
164
165
166
167
168public:
169
171 inline
172 void projectFwd(double & x, double & y) const {
173 project<PJ_FWD>(x,y);
174 }
175
177 inline
178 void projectFwd(double x, double y, double & x2, double & y2) const {
179 x2 = x;
180 y2 = y;
181 project<PJ_FWD>(x2,y2);
182 }
183
185 inline
186 void projectFwd(drain::Point2D<double> & point) const {
187 project<PJ_FWD>(point);
188 }
189
191 inline
192 void projectFwd(const drain::Point2D<double> & point, drain::Point2D<double> & point2) const {
193 project<PJ_FWD>(point2 = point);
194 }
195
196
197 // Inverse projection (in-place)
198 inline
199 void projectInv(double & x, double & y) const {
200 project<PJ_INV>(x,y);
201 }
202
203 // Inverse projection
204 inline
205 void projectInv(double x, double y, double & x2, double & y2) const {
206 x2 = x;
207 y2 = y;
208 project<PJ_INV>(x2,y2);
209 }
210
211 // Inverse projection. Example implementation of project<>() .
212 inline
213 void projectInv(drain::Point2D<double> & point) const {
214 project<PJ_INV>(point);
215 }
216
218 inline
219 void projectInv(const drain::Point2D<double> & point, drain::Point2D<double> & point2) const {
220 project<PJ_INV>(point2 = point);
221 }
222
223
224
225 inline
226 void debug(std::ostream & ostr = std::cout, int wkt = -1){
227
228 ostr << "SRC:\n";
229 src.info(ostr, wkt);
230 ostr << std::endl;
231
232 ostr << "DST:\n";
233 dst.info(ostr, wkt);
234 ostr << std::endl;
235 }
236
237
238
240 inline
241 bool isLongLat() const {
242 return dst.isLongLat();
243 }
244
245 inline
246 bool isSet() const {
247 return (src.isSet() && dst.isSet());
248 }
249
250 inline
251 std::string getErrorString() const {
252 int err = proj_context_errno(pjContext);
253 return proj_errno_string(err);
254 //return "Not Impl."; //std::string(pj_strerrno(*pj_get_errno_ref()));
255 };
256
257
258
260
272 // static short int pickEpsgCodeOLD(const std::string & projDef, std::list<std::string> & projArgs);
273
274protected:
275
276 PJ_CONTEXT *pjContext = nullptr; // = proj_context_create();
277 PJ *proj = nullptr; // two-way
278
279 // typedef drain::Dictionary<int, std::string> epsg_dict_t;
280 void setMapping(bool lenient);
281
282protected:
283
287 template
288 <PJ_DIRECTION D,class POINT_XY>
289 inline
290 void project(POINT_XY & point) const {
291 // void project(drain::Point2D<double> & point) const {
292 // Note: sizeof not needed, future option for arrays.
293 proj_trans_generic(proj, D, &point.x, sizeof(POINT_XY), 1, &point.y, sizeof(POINT_XY), 1, 0, 0, 0, 0, 0, 0);
294 }
295
296 template
297 <PJ_DIRECTION D>
298 inline
299 void project(double & x, double & y) const {
300 // Note: sizeof not needed, future option for arrays.
301 proj_trans_generic(proj, D, &x, sizeof(double), 1, &y, sizeof(double), 1, 0, 0, 0, 0, 0, 0);
302 }
303
304
305
306
307};
308
309DRAIN_TYPENAME(Proj6);
310
311std::ostream & operator<<(std::ostream & ostr, const Proj6 &p);
312
313} // drain
314
315
316#endif /*PROJ4_H_*/
317
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:312
Logger & unimplemented(const TT &... args)
Feature to be done. Special type of Logger::note().
Definition Log.h:511
Definition Proj6.h:66
void setProjectionDst(const std::string &projDef, Projector::CRS_mode crs=Projector::FORCE_CRS)
Sets destination projection.
Definition Proj6.h:110
void setProjectionDst(const Projector &projDef)
Sets destination projection, primarily using EPSG code.
Definition Proj6.h:118
void setProjectionSrc(const std::string &projDef, Projector::CRS_mode crs=Projector::FORCE_CRS)
Sets source projection.
Definition Proj6.h:90
PJ_CONTEXT * pjContext
Detect EPSG code from "+init=epsg:EPSG" argument.
Definition Proj6.h:276
bool isLongLat() const
Check if destination projection is longitude-latitude degrees.
Definition Proj6.h:241
void setProjectionSrc(const Projector &projDef)
Sets source projection, primarily using EPSG code.
Definition Proj6.h:101
const std::string & getProjectionSrc() const
Returns the projection std::string applied by the last setProjection call.
Definition Proj6.h:144
void setProjections(const std::string &projDefSrc, const std::string &projDefDst)
Sets source and destination projection. TOOD: EPSG code handling.
Definition Proj6.h:127
void projectInv(const drain::Point2D< double > &point, drain::Point2D< double > &point2) const
Forward projection. Example implementation of project<>() .
Definition Proj6.h:219
void projectFwd(double x, double y, double &x2, double &y2) const
Forward projection.
Definition Proj6.h:178
void project(POINT_XY &point) const
Definition Proj6.h:290
void projectFwd(double &x, double &y) const
Forward projection (in-place)
Definition Proj6.h:172
void projectFwd(drain::Point2D< double > &point) const
Forward projection. Example implementation of project<>() .
Definition Proj6.h:186
void projectFwd(const drain::Point2D< double > &point, drain::Point2D< double > &point2) const
Forward projection. Example implementation of project<>() .
Definition Proj6.h:192
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