FileGeoTIFF.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_FILE_GEO_TIFF
32 #define DRAIN_FILE_GEO_TIFF
33 
34 #include "FileTIFF.h"
35 
36 #ifndef USE_GEOTIFF_NO
37 
38 #include <geotiff.h>
39 #include <geotiffio.h>
40 #include <geo_normalize.h>
41 
42 #include "drain/util/EnumFlags.h"
43 #include "TreeXML-GDAL.h"
44 
45 
46 
47 namespace drain
48 {
49 
50 namespace image
51 {
52 
53 
54 
60 class FileGeoTIFF : public FileTIFF {
61 public:
62 
63  // typedef std::map<short, std::list<std::pair<geokey_t, drain::Variable> > > epsg_map_t;
64  // static epsg_map_t epsgConf;
65 
66  FileGeoTIFF() : FileTIFF(), gtif(nullptr){
67  //gdalInfo.data.setType(GDAL::tag_t::ROOT);
68  gdalMetadata(GDAL::tag_t::ROOT);
69  }
70 
71  FileGeoTIFF(const std::string & path, const std::string & mode = "w") : FileTIFF(), gtif(nullptr){
72  //gdalInfo.data.setType(GDAL::tag_t::ROOT);
73  gdalMetadata(GDAL::tag_t::ROOT);
74  open(path, mode);
75  }
76 
77  virtual inline
78  ~FileGeoTIFF(){
79  //gt_close();
80  close();
81  }
82 
84  virtual
85  void open(const std::string & path, const std::string & mode = "w");
86 
87 
88  template <typename T>
89  void setGeoTiffField(geokey_t tag, T value){
90  if ((std::is_enum<T>::value) || (std::is_integral<T>::value)){
91  GTIFKeySet(gtif, tag, TYPE_SHORT, 1, static_cast<short int>(value));
92  }
93  else if (std::is_floating_point<T>::value){
94  GTIFKeySet(gtif, tag, TYPE_FLOAT, 1, value);
95  }
96  else {
97  drain::Logger mout(__FILE__, __FUNCTION__);
98  mout.warn("TAG: ", tag, ", value:", value, ", type=", typeid(T).name()); // , drain::Type::call<drain::nameGetter>(typeid(T)));
99  mout.error("Not implemented");
100  }
101  }
102 
103 
105  void writeMetadata();
106 
107 
108  virtual
109  void close();
110 
112  inline virtual
113  bool isOpen() const {
114  return FileTIFF::isOpen() && (gtif != nullptr);
115  }
116 
118 
121  // static
122  // bool strictCompliance;
123 
124  typedef enum {UNDEFINED=0, TIFF=1, GEOTIFF=2, EPSG=6, STRICT=10} TiffCompliance;
125 
126  //typedef drain::EnumFlagger<drain::SingleFlagger<TiffCompliance> > tiffComplianceFlagger;
127  typedef drain::EnumFlagger<drain::MultiFlagger<TiffCompliance> > complianceFlagger;
128 
129  static
130  complianceFlagger compliancyFlagger;
131 
132  static
133  std::string compliancy;
134  //TiffCompliance compliance;
136  // static bool plainEPSG;
137 
138  TreeGDAL gdalMetadata;
142  void setGdalScale(double scale=1.0, double offset=0.0);
143 
145 
148  void setGdalNoData(const std::string & nodata);
149 
150 
151  template <class T>
152  void setGdal(const std::string & key, const T & value, int sample=-1, const std::string & role = ""){
153 
154  TreeGDAL & elem = gdalMetadata[key](GDAL::ITEM);
155  //elem->setType(GDAL::UNDEFINED);
156  //elem->setType(GDAL::ITEM);
157 
158  //NodeGDAL gdalItem = gdalMetadata[key](NodeGDAL::ITEM).data;
159 
160  NodeGDAL & item = elem.data;
161  item.name = key;
162  item.setText(value);
163  /*
164  this->name = name;
165  this->ctext = ctext.toStr();
166  this->sample = sample;
167  */
168 
169  if (sample >= 0){
170  //item.sample = sample;
171  item.set("sample", sample); // create Variable
172  if (role.empty()){
173  item.role = key;
175  }
176  else {
177  item.role = role;
178  }
179  }
180 
181  //item.set("name", "gdalItem.name"); // KLUDGE
182  //iItem.set("role", "gdalItem.role"); // KLUDGE
183  }
184 
186 
189  void setGeoMetaData(const drain::image::GeoFrame & frame);
190 
192 
195  void setProjectionEPSG(short epsg);
196 
198 
201  void setProjection(const std::string & proj);
202 
204  void setProjection(const drain::Proj6 & proj);
205 
207  void setProjectionLongLat();
208 
209 
210 
211  /*
212  template <typename T>
213  inline
214  tagtype_t getTagType(){
215  const typename tagtype_map_t::const_iterator it = tagtype_map.find(&typeid(T));
216  if (it != tagtype_map.end()){
217  return it->second;
218  }
219  else {
220  drain::Logger mout(__FILE__, __FUNCTION__);
221  mout.warn("unknown/unsupported GTIFF tag basetype: ", typeid(T).name());
222  return TYPE_UNKNOWN;
223  }
224  }
225  */
226 
227 protected:
228 
229 
230  GTIF *gtif;
231 
232  /*
233  typedef std::map<const std::type_info *,tagtype_t> tagtype_map_t;
234 
235  static
236  const tagtype_map_t tagtype_map;
237  */
238 
239 };
240 
241 template <>
242 inline
243 void FileGeoTIFF::setGeoTiffField(geokey_t tag, const char *value){
244  GTIFKeySet(gtif, tag, TYPE_ASCII, strlen(value)+1, value);
245 }
246 
247 template <>
248 inline
249 void FileGeoTIFF::setGeoTiffField(geokey_t tag, const std::string & value){
250  GTIFKeySet(gtif, tag, TYPE_ASCII, value.size()+1, value.c_str());
251 }
252 
253 
254 } // image::
255 
256 //template <>
257 // const drain::FlagResolver::dict_t drain::EnumDict<image::FileGeoTIFF::TiffCompliance>::dict;
258 
259 /*
260 template <>
261 template <>
262 image::TreeGDAL & image::TreeGDAL::operator()(const image::NodeGDAL::tag_t & type);
263 */
264 
265 } // drain::
266 #endif
267 
268 #endif
269 
270 
271 /*
272 
273 template <>
274 inline
275 tagtype_t FileGeoTIFF::getTagType<unsigned char>(){ return TYPE_BYTE;};
276 
277 template <>
278 inline
279 tagtype_t FileGeoTIFF::getTagType<unsigned short>(){ return TYPE_SHORT;}
280 
281 template <>
282 inline
283 tagtype_t FileGeoTIFF::getTagType<unsigned long>(){ return TYPE_LONG;};
284 
285 template <>
286 inline
287 tagtype_t FileGeoTIFF::getTagType<std::string>(){ return TYPE_ASCII;};
288 
289 template <>
290 inline
291 tagtype_t FileGeoTIFF::getTagType<float>(){ return TYPE_FLOAT;};
292 
293 template <>
294 inline
295 tagtype_t FileGeoTIFF::getTagType<double>(){ return TYPE_DOUBLE;};
296 
297 template <>
298 inline
299 tagtype_t FileGeoTIFF::getTagType<signed char>(){ return TYPE_SBYTE;};
300 
301 template <>
302 inline
303 tagtype_t FileGeoTIFF::getTagType<signed short>(){ return TYPE_SSHORT;};
304 
305 template <>
306 inline
307 tagtype_t FileGeoTIFF::getTagType<signed long>(){ return TYPE_SLONG;};
308 //inline getTagType<>(){ return TYPE_UNKNOWN);
309 //inline getTagType<>(){ return TYPE_RATIONAL);
310 */
Flagger accepting values of enum type E.
Definition: EnumFlags.h:190
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition: Log.h:310
Logger & error(const TT &... args)
Echoes.
Definition: Log.h:414
Logger & warn(const TT &... args)
Possible error, but execution can continue.
Definition: Log.h:428
Definition: Proj6.h:66
static std::string & lowerCase(std::string &s, size_t n=std::numeric_limits< size_t >::max())
Turns n first characters lowercase. ASCII only.
Definition: String.cpp:282
Definition: FileGeoTIFF.h:60
TiffCompliance
Require strict GeoTIFF compliance.
Definition: FileGeoTIFF.h:124
void setGdalScale(double scale=1.0, double offset=0.0)
Definition: FileGeoTIFF.cpp:247
void setGdalNoData(const std::string &nodata)
This is between Tiff and GeoTiff?
Definition: FileGeoTIFF.cpp:257
void setGeoMetaData(const drain::image::GeoFrame &frame)
Sets projection and bounding box. Adjusts spatial resolution accordingly.
Definition: FileGeoTIFF.cpp:290
virtual void open(const std::string &path, const std::string &mode="w")
Opens a GeoTIFF file.
Definition: FileGeoTIFF.cpp:163
void setProjectionLongLat()
Sets projection to plain longitude-latitude mapping.
Definition: FileGeoTIFF.cpp:482
void setProjectionEPSG(short epsg)
Set projection using EPSG code. This is the recommended way.
Definition: FileGeoTIFF.cpp:422
TreeGDAL gdalMetadata
Use EPSG specific support only, if found. Else use also fromProj4Str().
Definition: FileGeoTIFF.h:138
void writeMetadata()
Completes GeoTIFF structure.
Definition: FileGeoTIFF.cpp:197
virtual bool isOpen() const
Todo: subclass.
Definition: FileGeoTIFF.h:113
void setProjection(const std::string &proj)
Sets projection given in Proj.4 string format.
Definition: FileGeoTIFF.cpp:359
For writing images in basic TIFF format. Reading not supported currently.
Definition: FileTIFF.h:58
Array with georeferencing support.
Definition: GeoFrame.h:58
Definition: TreeXML-GDAL.h:60
Definition: DataSelector.cpp:1277