pktools  2.6.3
Processing Kernel for geospatial data
ImgWriterGdal.h
1 /**********************************************************************
2 ImgWriterGdal.h: class to write raster files using GDAL API library
3 Copyright (C) 2008-2012 Pieter Kempeneers
4 
5 This file is part of pktools
6 n
7 pktools is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 pktools is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with pktools. If not, see <http://www.gnu.org/licenses/>.
19 ***********************************************************************/
20 #ifndef _IMGWRITERGDAL_H_
21 #define _IMGWRITERGDAL_H_
22 
23 #include <assert.h>
24 #include <fstream>
25 #include <string>
26 #include <sstream>
27 #include "gdal_priv.h"
28 #include "ImgReaderGdal.h"
29 
30 
31 //--------------------------------------------------------------------------
33 {
34 public:
35  ImgWriterGdal(void);
36  ~ImgWriterGdal(void);
37  void open(const std::string& filename);
38  void open(const std::string& filename, const ImgReaderGdal& imgSrc, const std::vector<std::string>& options=std::vector<std::string>());
39  // void open(const std::string& filename, int ncol, int nrow, int nband, const GDALDataType& dataType, const std::string& imageType="GTiff", const std::string& interleave="BAND", const std::string& compression="LZW", int magicX=1, int magicY=1);
40  void open(const std::string& filename, int ncol, int nrow, int nband, const GDALDataType& dataType, const std::string& imageType, const std::vector<std::string>& options=std::vector<std::string>());
41  void close(void);
42  std::string getFileName() const {return m_filename;};
43  int nrOfCol(void) const { return m_ncol;};
44  int nrOfRow(void) const { return m_nrow;};
45  int nrOfBand(void) const { return m_nband;};
46  void copyGeoTransform(const ImgReaderGdal& imgSrc);
47  std::string setProjection(void);//set (and return) default projection ETSR-LAEA
48  void setProjection(const std::string& projection);
49  std::string setProjectionProj4(const std::string& projection);
50  void setImageDescription(const std::string& imageDescription){m_gds->SetMetadataItem( "TIFFTAG_IMAGEDESCRIPTION",imageDescription.c_str());};
51  CPLErr GDALSetNoDataValue(double noDataValue, int band=0) {return getRasterBand(band)->SetNoDataValue(noDataValue);};
52  std::string getProjection(void) const;
53  std::string getGeoTransform() const;
54  void getGeoTransform(double* gt) const;
55  void setGeoTransform(double* gt);
56  /* void setGeoTransform(double ulx, double uly, double deltaX, double deltaY, double rot1=0, double rot2=0); */
57  /* void getGeoTransform(double& ulx, double& uly, double& deltaX, double& deltaY, double& rot1, double& rot2) const; */
58  bool getBoundingBox(double& ulx, double& uly, double& lrx, double& lry) const;
59  bool getCentrePos(double& x, double& y) const;
60  bool covers(double x, double y) const;
61  bool covers(double ulx, double uly, double lrx, double lry) const;
62  bool geo2image(double x, double y, double& i, double& j) const;
63  bool image2geo(double i, double j, double& x, double& y) const;
64  bool isGeoRef() const {double gt[6];getGeoTransform(gt);if(gt[5]<0) return true;else return false;};
65  // void getMagicPixel(double& x, double& y) const {x=m_magic_x;y=m_magic_y;};
66  double getDeltaX(void) const {double gt[6];getGeoTransform(gt);return gt[1];};
67  double getDeltaY(void) const {double gt[6];getGeoTransform(gt);return -gt[5];};
68  template<typename T> bool writeData(T& value, const GDALDataType& dataType, int col, int row, int band=0) const;
69  template<typename T> bool writeData(std::vector<T>& buffer, const GDALDataType& dataType , int minCol, int maxCol, int row, int band=0) const;
70  template<typename T> bool writeData(std::vector<T>& buffer, const GDALDataType& dataType, int row, int band=0) const;
71  bool writeData(void* pdata, const GDALDataType& dataType, int band=0) const;
72  template<typename T> bool writeDataBlock(Vector2d<T>& buffer, const GDALDataType& dataType , int minCol, int maxCol, int minRow, int maxRow, int band=0) const;
73  // std::string getInterleave(){return m_interleave;};
74  // std::string getCompression(){return m_compression;};
75  GDALDataType getDataType(int band=0) const;
76  GDALRasterBand* getRasterBand(int band);
77  void setColorTable(const std::string& filename, int band=0);
78  void setColorTable(GDALColorTable* colorTable, int band=0);
79  void setMetadata(char** metadata);
80 
81 protected:
82  void setCodec(const std::string& imageType);
83  void setCodec(const ImgReaderGdal& ImgSrc);
84 
85  std::string m_filename;
86  GDALDataset *m_gds;
87  int m_ncol;
88  int m_nrow;
89  int m_nband;
90  GDALDataType m_type;
91  double m_gt[6];
92  /* double m_ulx; */
93  /* double m_uly; */
94  /* double m_delta_x; */
95  /* double m_delta_y; */
96  /* bool m_isGeoRef; */
97  // std::string m_interleave;
98  // std::string m_compression;
99  std::vector<std::string> m_options;
100 };
101 
102 template<typename T> bool ImgWriterGdal::writeData(T& value, const GDALDataType& dataType, int col, int row, int band) const
103 {
104  //fetch raster band
105  GDALRasterBand *poBand;
106  if(band>=nrOfBand()+1){
107  std::ostringstream s;
108  s << "band (" << band << ") exceeds nrOfBand (" << nrOfBand() << ")";
109  throw(s.str());
110  }
111  poBand = m_gds->GetRasterBand(band+1);//GDAL uses 1 based index
112  if(col>=nrOfCol()){
113  std::ostringstream s;
114  s << "col (" << col << ") exceeds nrOfCol (" << nrOfCol() << ")";
115  throw(s.str());
116  }
117  if(col<0){
118  std::ostringstream s;
119  s << "col (" << col << ") is negative";
120  throw(s.str());
121  }
122  if(row>=nrOfRow()){
123  std::ostringstream s;
124  s << "row (" << row << ") exceeds nrOfRow (" << nrOfRow() << ")";
125  throw(s.str());
126  }
127  if(row<0){
128  std::ostringstream s;
129  s << "row (" << row << ") is negative";
130  throw(s.str());
131  }
132  poBand->RasterIO(GF_Write,col,row,1,1,&value,1,1,dataType,0,0);
133  return true;
134 }
135 
136 template<typename T> bool ImgWriterGdal::writeData(std::vector<T>& buffer, const GDALDataType& dataType, int minCol, int maxCol, int row, int band) const
137 {
138  //fetch raster band
139  GDALRasterBand *poBand;
140  if(band>=nrOfBand()+1){
141  std::ostringstream s;
142  s << "band (" << band << ") exceeds nrOfBand (" << nrOfBand() << ")";
143  throw(s.str());
144  }
145  poBand = m_gds->GetRasterBand(band+1);//GDAL uses 1 based index
146  if(buffer.size()!=maxCol-minCol+1){
147  std::string errorstring="invalid buffer size";
148  throw(errorstring);
149  }
150  if(minCol>=nrOfCol()){
151  std::ostringstream s;
152  s << "minCol (" << minCol << ") exceeds nrOfCol (" << nrOfCol() << ")";
153  throw(s.str());
154  }
155  if(minCol<0){
156  std::ostringstream s;
157  s << "mincol (" << minCol << ") is negative";
158  throw(s.str());
159  }
160  if(maxCol>=nrOfCol()){
161  std::ostringstream s;
162  s << "maxCol (" << maxCol << ") exceeds nrOfCol (" << nrOfCol() << ")";
163  throw(s.str());
164  }
165  if(maxCol<minCol){
166  std::ostringstream s;
167  s << "maxCol (" << maxCol << ") is less than minCol (" << minCol << ")";
168  throw(s.str());
169  }
170 
171  if(row>=nrOfRow()){
172  std::ostringstream s;
173  s << "row (" << row << ") exceeds nrOfRow (" << nrOfRow() << ")";
174  throw(s.str());
175  }
176  if(row<0){
177  std::ostringstream s;
178  s << "row (" << row << ") is negative";
179  throw(s.str());
180  }
181  poBand->RasterIO(GF_Write,minCol,row,buffer.size(),1,&(buffer[0]),buffer.size(),1,dataType,0,0);
182  return true;
183 }
184 
185 template<typename T> bool ImgWriterGdal::writeData(std::vector<T>& buffer, const GDALDataType& dataType, int row, int band) const
186 {
187  //fetch raster band
188  GDALRasterBand *poBand;
189  if(band>=nrOfBand()+1){
190  std::ostringstream s;
191  s << "band (" << band << ") exceeds nrOfBand (" << nrOfBand() << ")";
192  throw(s.str());
193  }
194  poBand = m_gds->GetRasterBand(band+1);//GDAL uses 1 based index
195  if(buffer.size()!=nrOfCol()){
196  std::string errorstring="invalid buffer size";
197  throw(errorstring);
198  }
199  if(row>=nrOfRow()){
200  std::ostringstream s;
201  s << "row (" << row << ") exceeds nrOfRow (" << nrOfRow() << ")";
202  throw(s.str());
203  }
204  poBand->RasterIO(GF_Write,0,row,buffer.size(),1,&(buffer[0]),buffer.size(),1,dataType,0,0);
205  return true;
206 }
207 
208 template<typename T> bool ImgWriterGdal::writeDataBlock(Vector2d<T>& buffer, const GDALDataType& dataType , int minCol, int maxCol, int minRow, int maxRow, int band) const
209 {
210  //fetch raster band
211  GDALRasterBand *poBand;
212  if(band>=nrOfBand()+1){
213  std::ostringstream s;
214  s << "band (" << band << ") exceeds nrOfBand (" << nrOfBand() << ")";
215  throw(s.str());
216  }
217  poBand = m_gds->GetRasterBand(band+1);//GDAL uses 1 based index
218  assert(buffer.size()==maxRow-minRow+1);
219  for(int irow=minRow;irow<=maxRow;++irow)
220  writeData(buffer[irow-minRow], dataType, minCol, maxCol, irow, band);
221  return true;
222 }
223 
224 #endif // _IMGWRITERGDAL_H_
225 
226 
227 // adfGeoTransform[0] /* top left x */
228 // adfGeoTransform[1] /* w-e pixel resolution */
229 // adfGeoTransform[2] /* rotation, 0 if image is "north up" */
230 // adfGeoTransform[3] /* top left y */
231 // adfGeoTransform[4] /* rotation, 0 if image is "north up" */
232 // adfGeoTransform[5] /* n-s pixel resolution */