pktools  2.6.5
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 #include "ImgReaderOgr.h"
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  void rasterizeOgr(ImgReaderOgr& ogrReader, const std::vector<double>& burnValues=std::vector<double>(), const std::vector<std::string>& layernames=std::vector<std::string>());
81 
82 protected:
83  void setCodec(const std::string& imageType);
84  void setCodec(const ImgReaderGdal& ImgSrc);
85 
86  std::string m_filename;
87  GDALDataset *m_gds;
88  int m_ncol;
89  int m_nrow;
90  int m_nband;
91  GDALDataType m_type;
92  double m_gt[6];
93  /* double m_ulx; */
94  /* double m_uly; */
95  /* double m_delta_x; */
96  /* double m_delta_y; */
97  /* bool m_isGeoRef; */
98  // std::string m_interleave;
99  // std::string m_compression;
100  std::vector<std::string> m_options;
101 };
102 
103 template<typename T> bool ImgWriterGdal::writeData(T& value, const GDALDataType& dataType, int col, int row, int band) const
104 {
105  //fetch raster band
106  GDALRasterBand *poBand;
107  if(band>=nrOfBand()+1){
108  std::ostringstream s;
109  s << "band (" << band << ") exceeds nrOfBand (" << nrOfBand() << ")";
110  throw(s.str());
111  }
112  poBand = m_gds->GetRasterBand(band+1);//GDAL uses 1 based index
113  if(col>=nrOfCol()){
114  std::ostringstream s;
115  s << "col (" << col << ") exceeds nrOfCol (" << nrOfCol() << ")";
116  throw(s.str());
117  }
118  if(col<0){
119  std::ostringstream s;
120  s << "col (" << col << ") is negative";
121  throw(s.str());
122  }
123  if(row>=nrOfRow()){
124  std::ostringstream s;
125  s << "row (" << row << ") exceeds nrOfRow (" << nrOfRow() << ")";
126  throw(s.str());
127  }
128  if(row<0){
129  std::ostringstream s;
130  s << "row (" << row << ") is negative";
131  throw(s.str());
132  }
133  poBand->RasterIO(GF_Write,col,row,1,1,&value,1,1,dataType,0,0);
134  return true;
135 }
136 
137 template<typename T> bool ImgWriterGdal::writeData(std::vector<T>& buffer, const GDALDataType& dataType, int minCol, int maxCol, int row, int band) const
138 {
139  //fetch raster band
140  GDALRasterBand *poBand;
141  if(band>=nrOfBand()+1){
142  std::ostringstream s;
143  s << "band (" << band << ") exceeds nrOfBand (" << nrOfBand() << ")";
144  throw(s.str());
145  }
146  poBand = m_gds->GetRasterBand(band+1);//GDAL uses 1 based index
147  if(buffer.size()!=maxCol-minCol+1){
148  std::string errorstring="invalid buffer size";
149  throw(errorstring);
150  }
151  if(minCol>=nrOfCol()){
152  std::ostringstream s;
153  s << "minCol (" << minCol << ") exceeds nrOfCol (" << nrOfCol() << ")";
154  throw(s.str());
155  }
156  if(minCol<0){
157  std::ostringstream s;
158  s << "mincol (" << minCol << ") is negative";
159  throw(s.str());
160  }
161  if(maxCol>=nrOfCol()){
162  std::ostringstream s;
163  s << "maxCol (" << maxCol << ") exceeds nrOfCol (" << nrOfCol() << ")";
164  throw(s.str());
165  }
166  if(maxCol<minCol){
167  std::ostringstream s;
168  s << "maxCol (" << maxCol << ") is less than minCol (" << minCol << ")";
169  throw(s.str());
170  }
171 
172  if(row>=nrOfRow()){
173  std::ostringstream s;
174  s << "row (" << row << ") exceeds nrOfRow (" << nrOfRow() << ")";
175  throw(s.str());
176  }
177  if(row<0){
178  std::ostringstream s;
179  s << "row (" << row << ") is negative";
180  throw(s.str());
181  }
182  poBand->RasterIO(GF_Write,minCol,row,buffer.size(),1,&(buffer[0]),buffer.size(),1,dataType,0,0);
183  return true;
184 }
185 
186 template<typename T> bool ImgWriterGdal::writeData(std::vector<T>& buffer, const GDALDataType& dataType, int row, int band) const
187 {
188  //fetch raster band
189  GDALRasterBand *poBand;
190  if(band>=nrOfBand()+1){
191  std::ostringstream s;
192  s << "band (" << band << ") exceeds nrOfBand (" << nrOfBand() << ")";
193  throw(s.str());
194  }
195  poBand = m_gds->GetRasterBand(band+1);//GDAL uses 1 based index
196  if(buffer.size()!=nrOfCol()){
197  std::string errorstring="invalid buffer size";
198  throw(errorstring);
199  }
200  if(row>=nrOfRow()){
201  std::ostringstream s;
202  s << "row (" << row << ") exceeds nrOfRow (" << nrOfRow() << ")";
203  throw(s.str());
204  }
205  poBand->RasterIO(GF_Write,0,row,buffer.size(),1,&(buffer[0]),buffer.size(),1,dataType,0,0);
206  return true;
207 }
208 
209 template<typename T> bool ImgWriterGdal::writeDataBlock(Vector2d<T>& buffer, const GDALDataType& dataType , int minCol, int maxCol, int minRow, int maxRow, int band) const
210 {
211  //fetch raster band
212  GDALRasterBand *poBand;
213  if(band>=nrOfBand()+1){
214  std::ostringstream s;
215  s << "band (" << band << ") exceeds nrOfBand (" << nrOfBand() << ")";
216  throw(s.str());
217  }
218  poBand = m_gds->GetRasterBand(band+1);//GDAL uses 1 based index
219  assert(buffer.size()==maxRow-minRow+1);
220  for(int irow=minRow;irow<=maxRow;++irow)
221  writeData(buffer[irow-minRow], dataType, minCol, maxCol, irow, band);
222  return true;
223 }
224 
225 #endif // _IMGWRITERGDAL_H_
226 
227 
228 // adfGeoTransform[0] /* top left x */
229 // adfGeoTransform[1] /* w-e pixel resolution */
230 // adfGeoTransform[2] /* rotation, 0 if image is "north up" */
231 // adfGeoTransform[3] /* top left y */
232 // adfGeoTransform[4] /* rotation, 0 if image is "north up" */
233 // adfGeoTransform[5] /* n-s pixel resolution */