Loading...
Searching...
No Matches
Projector.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_PROJECTOR_H_
32#define DRAIN_PROJECTOR_H_
33
34#include <drain/Enum.h>
35#include <iostream>
36#include <set>
37#include <string>
38
39#include <proj.h>
40
41// #include <drain/Dictionary.h>
42// #include "Point.h"
43
44
45namespace drain
46{
47
48class Proj6;
49
50
51// Helper class for containing a single (one-way) Proj object and its
52class Projector {
53
54 friend class Proj6;
55
56 static const SprinterLayout projDefLayout; // space-separated, =, no hypens (" ","","=", "","");
57
58
59public:
60
61 static const std::string proj4version;
62
64
65 //typedef enum {PROJ4, PROJ6, CRS} version;
66 typedef enum {
67 ACCEPT_CRS, // Do not touch
68 REMOVE_CRS, // Delete "+type=crs" from projDef, warn if EPSG:<code> syntax used
69 FORCE_CRS // Add "+type=crs"
70 } CRS_mode;
71
72
74 typedef enum {
75 ORIG, // Supplied by user
76 MODIFIED, // EPSG-converted and filtered
77 // FINAL, //
78 PROJ4, // Final, formulated by projlib
79 PROJ5, // Final, formulated by projlib
80 SIMPLE // Final, simplified for backward compatibility: "+type=crs" and "+init=epsg:..." pruned.
82
83
84 inline // NEW 2024: proj_context_create()
85 Projector(const std::string & projDef = "", CRS_mode crs=ACCEPT_CRS) : pjContext(proj_context_create()), pj(nullptr), epsg(0){
86 if (projDef.empty()){
87 projDefs = {{ORIG,""}, {MODIFIED,""}, {PROJ4,""}, {PROJ5,""}, {SIMPLE,""}};
88 }
89 else {
90 setProjection(projDef, crs);
91 }
92 }
93
94 inline
95 Projector(PJ_CONTEXT *pjContext, const std::string & projDef = "", CRS_mode crs=ACCEPT_CRS) : pjContext(pjContext), pj(nullptr), epsg(0){
96
97 if (projDef.empty()){
98 // CONSIDER projeDef.empty() check in setProjection(projDef, crs); ?
99 projDefs = {{ORIG,""}, {MODIFIED,""}, {PROJ4,""}, {PROJ5,""}, {SIMPLE,""}};
100 }
101 else {
102 setProjection(projDef, crs);
103 }
104
105 }
106
107 // Moved to .cpp for version 6/7/8 problems (context_clone)
108 Projector(const Projector & pr);
109
110
111 virtual inline
112 ~Projector(){
113 proj_destroy(pj);
114 proj_context_destroy(pjContext); // NEW 2024
115 }
116
117
118 static
119 int extractEPSG(const std::string & projDef);
120
121
122 const std::string & getProjDef(PROJDEF_variant v = SIMPLE) const { // For external objects, excluding +init=epsg and +type=crs
123 return projDefs[v];
124 }
125
126
127
129 void clear(){
130 projDefs = {{ORIG,""}, {MODIFIED,""}, {PROJ4,""}, {PROJ5,""}, {SIMPLE,""}};
131 pjContext = nullptr; // TODO: flag for own CTX => destroy at end
132 proj_destroy(pj);
133 pj = nullptr;
134 projDefDict.clear();
135 epsg = 0;
136 }
137
139
142 void setProjection(const std::string &str, CRS_mode crs=FORCE_CRS);
143
145
148 void setProjection(int epsg, CRS_mode crs=FORCE_CRS);
149
150 // protect
151 void createProjection(CRS_mode crs);
152
154 inline
155 bool isSet() const {
156 return (pj != nullptr);
157 }
158
159 // NOTE: compliancy problems Proj.4...6...
160 inline
161 bool isLongLat() const {
162 // TODO: what if not defined.
163 return isLongLat(pj);
164 }
165
166 inline
167 int getEPSG() const {
168 return epsg;
169 }
170
171
172
174 //static
175 // int filterProjStr(const std::string & src, std::ostream & ostr, CRS_mode crs=ACCEPT_CRS);
176
177
178 inline
179 void info(std::ostream & ostr = std::cout, int wkt = -1) const {
180 /*
181 for (const auto & entry: projDefs){
182 ostr << entry.first << ": '" << entry.second << "'\n";
183 }
184 */
185 info(pj, ostr, wkt);
186 }
187
188 inline
189 std::string getErrorString() const {
190 int err = proj_context_errno(pjContext);
191 return proj_errno_string(err);
192 //return "Not Impl."; //std::string(pj_strerrno(*pj_get_errno_ref()));
193 };
194
195// To be protected?
196
197
198protected:
199
201 void info(PJ *pj, std::ostream & ostr = std::cout, int wkt = -1) const ;
202
204 PJ_CONTEXT *pjContext;
205
207 PJ *pj;
208
211
213 int epsg = 0;
214
215 mutable
216 std::map<PROJDEF_variant,std::string> projDefs;
217
218 void storeProjDef(const std::string & str);
219
220 static
221 void getProjDefStr(const ProjDef & dict, std::stringstream & sstr, const std::set<std::string> & excludeKeys = {"+type"});
222
223 // static
224 // int extractEPSGold(const ProjDef & projDefDict);
225
227
231 // PJ * getProjection(const std::string & projStr, CRS_mode crs=ACCEPT_CRS) const;
232
233 static
234 bool isLongLat(const PJ *prj);
235
236
237};
238
239//template <>
240//const drain::Enum<Projector::PROJDEF_variant>::dict_t drain::Enum<Projector::PROJDEF_variant>::dict;
241DRAIN_ENUM_DICT(Projector::PROJDEF_variant);
242
243DRAIN_ENUM_OSTREAM(Projector::PROJDEF_variant);
244
245
246} // drain
247
248
249#endif /*PROJ4_H_*/
250
Two-way mapping between strings and objects of template class T.
Definition Dictionary.h:61
Definition Proj6.h:61
Definition Projector.h:52
bool isSet() const
Returns true, if PJ object has been set.
Definition Projector.h:155
PJ_CONTEXT * pjContext
Metadata for PROJ (introduced in latest versions, currently not used by Drain & Rack )
Definition Projector.h:204
PROJDEF_variant
Each projector has three (3) versions of project definition.
Definition Projector.h:74
ProjDef projDefDict
Secondary description of state.
Definition Projector.h:210
PJ * pj
Essential member: the pointer to a PJ object. This is the primary description of state.
Definition Projector.h:207
void info(std::ostream &ostr=std::cout, int wkt=-1) const
Prunes "+init=epsg:<...>" and optionally "+type=crs" codes.
Definition Projector.h:179
void setProjection(const std::string &str, CRS_mode crs=FORCE_CRS)
Sets projection defined as Proj string.
Definition Projector.cpp:98
void clear()
Deletes projection object and clears all the metadata.
Definition Projector.h:129
Definition DataSelector.cpp:1277
Definition Sprinter.h:136