Proj6.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood 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 
47 namespace drain
48 {
49 
65 class Proj6
66 {
67 public:
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 
168 public:
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 
190  // Inverse projection (in-place)
191  inline
192  void projectInv(double & x, double & y) const {
193  project<PJ_INV>(x,y);
194  }
195 
196  // Inverse projection
197  inline
198  void projectInv(double x, double y, double & x2, double & y2) const {
199  x2 = x;
200  y2 = y;
201  project<PJ_INV>(x2,y2);
202  }
203 
204  // Inverse projection. Example implementation of project<>() .
205  inline
206  void projectInv(drain::Point2D<double> & point) const {
207  project<PJ_INV>(point);
208  }
209 
210 
211 
212  inline
213  void debug(std::ostream & ostr = std::cout, int wkt = -1){
214 
215  ostr << "SRC:\n";
216  src.info(ostr, wkt);
217  ostr << std::endl;
218 
219  ostr << "DST:\n";
220  dst.info(ostr, wkt);
221  ostr << std::endl;
222  }
223 
224 
225 
227  inline
228  bool isLongLat() const {
229  return dst.isLongLat();
230  }
231 
232  inline
233  bool isSet() const {
234  return (src.isSet() && dst.isSet());
235  }
236 
237  inline
238  std::string getErrorString() const {
239  int err = proj_context_errno(pjContext);
240  return proj_errno_string(err);
241  //return "Not Impl."; //std::string(pj_strerrno(*pj_get_errno_ref()));
242  };
243 
244 
245 
247 
259  // static short int pickEpsgCodeOLD(const std::string & projDef, std::list<std::string> & projArgs);
260 
261 protected:
262 
263  PJ_CONTEXT *pjContext = nullptr; // = proj_context_create();
264  PJ *proj = nullptr; // two-way
265 
266  // typedef drain::Dictionary<int, std::string> epsg_dict_t;
267  void setMapping(bool lenient);
268 
269 protected:
270 
274  template
275  <PJ_DIRECTION D,class POINT_XY>
276  inline
277  void project(POINT_XY & point) const {
278  // void project(drain::Point2D<double> & point) const {
279  // Note: sizeof not needed, future option for arrays.
280  proj_trans_generic(proj, D, &point.x, sizeof(POINT_XY), 1, &point.y, sizeof(POINT_XY), 1, 0, 0, 0, 0, 0, 0);
281  }
282 
283  template
284  <PJ_DIRECTION D>
285  inline
286  void project(double & x, double & y) const {
287  // Note: sizeof not needed, future option for arrays.
288  proj_trans_generic(proj, D, &x, sizeof(double), 1, &y, sizeof(double), 1, 0, 0, 0, 0, 0, 0);
289  }
290 
291 
292 
293 
294 };
295 
296 DRAIN_TYPENAME(Proj6);
297 
298 std::ostream & operator<<(std::ostream & ostr, const Proj6 &p);
299 
300 } // drain
301 
302 
303 #endif /*PROJ4_H_*/
304 
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition: Log.h:310
Logger & unimplemented(const TT &... args)
Feature to be done. Special type of Logger::note().
Definition: Log.h:509
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:263
bool isLongLat() const
Check if destination projection is longitude-latitude degrees.
Definition: Proj6.h:228
void setProjectionSrc(const Projector &projDef)
Sets source projection, primarily using EPSG code.
Definition: Proj6.h:101
void setProjections(const std::string &projDefSrc, const std::string &projDefDst)
Sets source and destination projection. TOOD: EPSG code handling.
Definition: Proj6.h:127
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:277
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
const std::string & getProjectionSrc() const
Returns the projection std::string applied by the last setProjection call.
Definition: Proj6.h:144
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