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 <drain/Enum.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;
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]; // defaulted (GDAL::ITEM);
155 NodeGDAL & item = elem.data;
156 item.name = key;
157 item.setText(value);
158 /*
159 this->name = name;
160 this->ctext = ctext.toStr();
161 this->sample = sample;
162 */
163
164 if (sample >= 0){
165 //item.sample = sample;
166 item.set("sample", sample); // create Variable
167 if (role.empty()){
168 item.role = key;
170 }
171 else {
172 item.role = role;
173 }
174 }
175
176 //item.set("name", "gdalItem.name"); // KLUDGE
177 //iItem.set("role", "gdalItem.role"); // KLUDGE
178 }
179
181
184 void setGeoMetaData(const drain::image::GeoFrame & frame);
185
187
190 void setProjectionEPSG(short epsg);
191
193
196 void setProjection(const std::string & proj);
197
199 void setProjection(const drain::Proj6 & proj);
200
203
204
205
206 /*
207 template <typename T>
208 inline
209 tagtype_t getTagType(){
210 const typename tagtype_map_t::const_iterator it = tagtype_map.find(&typeid(T));
211 if (it != tagtype_map.end()){
212 return it->second;
213 }
214 else {
215 drain::Logger mout(__FILE__, __FUNCTION__);
216 mout.warn("unknown/unsupported GTIFF tag basetype: ", typeid(T).name());
217 return TYPE_UNKNOWN;
218 }
219 }
220 */
221
222protected:
223
224
225 GTIF *gtif;
226
227 /*
228 typedef std::map<const std::type_info *,tagtype_t> tagtype_map_t;
229
230 static
231 const tagtype_map_t tagtype_map;
232 */
233
234};
235
236template <>
237inline
238void FileGeoTIFF::setGeoTiffField(geokey_t tag, const char *value){
239 GTIFKeySet(gtif, tag, TYPE_ASCII, strlen(value)+1, value);
240}
241
242template <>
243inline
244void FileGeoTIFF::setGeoTiffField(geokey_t tag, const std::string & value){
245 GTIFKeySet(gtif, tag, TYPE_ASCII, value.size()+1, value.c_str());
246}
247
248
249} // image::
250
251//template <>
252// const drain::FlagResolver::dict_t drain::Enum<image::FileGeoTIFF::TiffCompliance>::dict;
253
254/*
255template <>
256template <>
257image::TreeGDAL & image::TreeGDAL::operator()(const image::NodeGDAL::tag_t & type);
258*/
259
260} // drain::
261#endif
262
263#endif
264
265
266/*
267
268template <>
269inline
270tagtype_t FileGeoTIFF::getTagType<unsigned char>(){ return TYPE_BYTE;};
271
272template <>
273inline
274tagtype_t FileGeoTIFF::getTagType<unsigned short>(){ return TYPE_SHORT;}
275
276template <>
277inline
278tagtype_t FileGeoTIFF::getTagType<unsigned long>(){ return TYPE_LONG;};
279
280template <>
281inline
282tagtype_t FileGeoTIFF::getTagType<std::string>(){ return TYPE_ASCII;};
283
284template <>
285inline
286tagtype_t FileGeoTIFF::getTagType<float>(){ return TYPE_FLOAT;};
287
288template <>
289inline
290tagtype_t FileGeoTIFF::getTagType<double>(){ return TYPE_DOUBLE;};
291
292template <>
293inline
294tagtype_t FileGeoTIFF::getTagType<signed char>(){ return TYPE_SBYTE;};
295
296template <>
297inline
298tagtype_t FileGeoTIFF::getTagType<signed short>(){ return TYPE_SSHORT;};
299
300template <>
301inline
302tagtype_t FileGeoTIFF::getTagType<signed long>(){ return TYPE_SLONG;};
303//inline getTagType<>(){ return TYPE_UNKNOWN);
304//inline getTagType<>(){ return TYPE_RATIONAL);
305*/
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:313
Logger & warn(const TT &... args)
Possible error, but execution can continue.
Definition Log.h:431
Logger & error(const TT &... args)
Echoes.
Definition Log.h:417
NodeXML & setText(const S &value)
Assign the text content of this node. If the node type is undefined, set it to CTEXT.
Definition TreeXML.h:619
Definition Proj6.h:64
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:412
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:496
void setProjectionEPSG(short epsg)
Set projection using EPSG code. This is the recommended way.
Definition FileGeoTIFF.cpp:427
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:57
Array with georeferencing support.
Definition GeoFrame.h:58
Definition TreeXML-GDAL.h:61
std::string role
Standard GDAL attribute.
Definition TreeXML-GDAL.h:90
Definition DataSelector.cpp:1277