Loading...
Searching...
No Matches
FileGeoTIFF.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_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 "GeoFrame.h"
43#include "TreeXML-GDAL.h"
44
45
46
47namespace drain
48{
49
50namespace image
51{
52
53
54
60class FileGeoTIFF : public FileTIFF {
61public:
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
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;
128
129 static
130 complianceFlagger compliancyFlagger;
131
132 static
133 std::string compliancy;
134 //TiffCompliance compliance;
136 // static bool plainEPSG;
137
138 TreeGDAL gdalMetadata;
143 void setGdalScale(double scale=1.0, double offset=0.0);
144
146
149 void setGdalNoData(const std::string & nodata);
150
151
152 template <class T>
153 void setGdal(const std::string & key, const T & value, int sample=-1, const std::string & role = ""){
154
155 TreeGDAL & elem = gdalMetadata[key]; // defaulted (GDAL::ITEM);
156 NodeGDAL & item = elem.data;
157 item.name = key;
158 item.setText(value);
159 /*
160 this->name = name;
161 this->ctext = ctext.toStr();
162 this->sample = sample;
163 */
164
165 if (sample >= 0){
166 //item.sample = sample;
167 item.set("sample", sample); // create Variable
168 if (role.empty()){
169 item.role = key;
171 }
172 else {
173 item.role = role;
174 }
175 }
176
177 //item.set("name", "gdalItem.name"); // KLUDGE
178 //iItem.set("role", "gdalItem.role"); // KLUDGE
179 }
180
182
185 void setGeoMetaData(const drain::image::GeoFrame & frame);
186
188
193 bool setProjectionEPSG(short epsg);
194
196
199 void setProjection(const std::string & proj);
200
202 void setProjection(const drain::Proj6 & proj);
203
206
207
208
209 /*
210 template <typename T>
211 inline
212 tagtype_t getTagType(){
213 const typename tagtype_map_t::const_iterator it = tagtype_map.find(&typeid(T));
214 if (it != tagtype_map.end()){
215 return it->second;
216 }
217 else {
218 drain::Logger mout(__FILE__, __FUNCTION__);
219 mout.warn("unknown/unsupported GTIFF tag basetype: ", typeid(T).name());
220 return TYPE_UNKNOWN;
221 }
222 }
223 */
224
225protected:
226
227
228 GTIF *gtif;
229
230 /*
231 typedef std::map<const std::type_info *,tagtype_t> tagtype_map_t;
232
233 static
234 const tagtype_map_t tagtype_map;
235 */
236
237};
238
239template <>
240inline
241void FileGeoTIFF::setGeoTiffField(geokey_t tag, const char *value){
242 GTIFKeySet(gtif, tag, TYPE_ASCII, strlen(value)+1, value);
243}
244
245template <>
246inline
247void FileGeoTIFF::setGeoTiffField(geokey_t tag, const std::string & value){
248 GTIFKeySet(gtif, tag, TYPE_ASCII, value.size()+1, value.c_str());
249}
250
251
252} // image::
253
254//template <>
255// const drain::FlagResolver::dict_t drain::Enum<image::FileGeoTIFF::TiffCompliance>::dict;
256
257/*
258template <>
259template <>
260image::TreeGDAL & image::TreeGDAL::operator()(const image::NodeGDAL::tag_t & type);
261*/
262
263} // drain::
264#endif
265
266#endif
267
268
269/*
270
271template <>
272inline
273tagtype_t FileGeoTIFF::getTagType<unsigned char>(){ return TYPE_BYTE;};
274
275template <>
276inline
277tagtype_t FileGeoTIFF::getTagType<unsigned short>(){ return TYPE_SHORT;}
278
279template <>
280inline
281tagtype_t FileGeoTIFF::getTagType<unsigned long>(){ return TYPE_LONG;};
282
283template <>
284inline
285tagtype_t FileGeoTIFF::getTagType<std::string>(){ return TYPE_ASCII;};
286
287template <>
288inline
289tagtype_t FileGeoTIFF::getTagType<float>(){ return TYPE_FLOAT;};
290
291template <>
292inline
293tagtype_t FileGeoTIFF::getTagType<double>(){ return TYPE_DOUBLE;};
294
295template <>
296inline
297tagtype_t FileGeoTIFF::getTagType<signed char>(){ return TYPE_SBYTE;};
298
299template <>
300inline
301tagtype_t FileGeoTIFF::getTagType<signed short>(){ return TYPE_SSHORT;};
302
303template <>
304inline
305tagtype_t FileGeoTIFF::getTagType<signed long>(){ return TYPE_SLONG;};
306//inline getTagType<>(){ return TYPE_UNKNOWN);
307//inline getTagType<>(){ return TYPE_RATIONAL);
308*/
Flagger accepting values of enum type E.
Definition EnumFlagger.h:56
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:326
Logger & warn(const TT &... args)
Possible error, but execution can continue.
Definition Log.h:440
Logger & error(const TT &... args)
Echoes.
Definition Log.h:426
Definition Proj6.h:61
static std::string & lowerCase(std::string &s, size_t n=std::numeric_limits< size_t >::max())
Turns n first characters lowercase. ASCII only.
Definition StringTools.cpp:439
virtual void setText(const std::string &s)
Assign the text content of this node. If the node type is undefined, set it to CTEXT.
Definition XML.cpp:204
Definition FileGeoTIFF.h:60
TiffCompliance
Require strict GeoTIFF compliance.
Definition FileGeoTIFF.h:124
bool setProjectionEPSG(short epsg)
Set projection using EPSG code. This is the recommended way.
Definition FileGeoTIFF.cpp:429
void setGdalScale(double scale=1.0, double offset=0.0)
Definition FileGeoTIFF.cpp:248
void setGdalNoData(const std::string &nodata)
This is between Tiff and GeoTiff?
Definition FileGeoTIFF.cpp:258
void setGeoMetaData(const drain::image::GeoFrame &frame)
Sets projection and bounding box. Adjusts spatial resolution accordingly.
Definition FileGeoTIFF.cpp:291
virtual void open(const std::string &path, const std::string &mode="w")
Opens a GeoTIFF file.
Definition FileGeoTIFF.cpp:162
void setProjectionLongLat()
Sets projection to plain longitude-latitude mapping.
Definition FileGeoTIFF.cpp:508
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:196
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:360
For writing images in basic TIFF format. Reading not supported currently.
Definition FileTIFF.h:56
Array with georeferencing support.
Definition GeoFrame.h:56
Definition TreeXML-GDAL.h:61
std::string role
Standard GDAL attribute.
Definition TreeXML-GDAL.h:90
Definition Data.h:1473