pktools  2.6.7
Processing Kernel for geospatial data
ImgWriterGdal.h
1 /**********************************************************************
2 ImgWriterGdal.h: class to write raster files using GDAL API library
3 Copyright (C) 2008-2016 Pieter Kempeneers
4 
5 This file is part of pktools
6 
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 "ImgRasterGdal.h"
29 #include "ImgReaderGdal.h"
30 #include "ImgReaderOgr.h"
31 
37 class ImgWriterGdal : public virtual ImgRasterGdal
38 {
39 public:
41  ImgWriterGdal(void);
43  ImgWriterGdal(const std::string& filename, const ImgReaderGdal& imgSrc, const std::vector<std::string>& options=std::vector<std::string>()){open(filename, imgSrc, options);};
45  ImgWriterGdal(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>()){open(filename, ncol, nrow, nband, dataType, imageType, options);};
47  ~ImgWriterGdal(void);
48 
50  void open(const std::string& filename, const ImgReaderGdal& imgSrc, const std::vector<std::string>& options=std::vector<std::string>());
52  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>());
53  void close(void);//definition in ImgWritergdal.cc
55  void setImageDescription(const std::string& imageDescription){m_gds->SetMetadataItem( "TIFFTAG_IMAGEDESCRIPTION",imageDescription.c_str());};
56 
58  template<typename T> bool writeData(T& value, int col, int row, int band=0);
60  template<typename T> bool writeData(std::vector<T>& buffer, int minCol, int maxCol, int row, int band=0);
62  template<typename T> bool writeData(std::vector<T>& buffer, int row, int band=0);
63  // deprecated? Write an entire image from memory to file
64  // bool writeData(void* pdata, const GDALDataType& dataType, int band=0);
66  template<typename T> bool writeDataBlock(Vector2d<T>& buffer2d, int minCol, int maxCol, int minRow, int maxRow, int band=0);
67 
69  void setColorTable(const std::string& filename, int band=0);
71  void setColorTable(GDALColorTable* colorTable, int band=0);
73  void setMetadata(char** metadata);
75  void rasterizeOgr(ImgReaderOgr& ogrReader, const std::vector<double>& burnValues, const std::vector<std::string>& controlOptions=std::vector<std::string>(), const std::vector<std::string>& layernames=std::vector<std::string>());
77  void rasterizeBuf(ImgReaderOgr& ogrReader, const std::vector<double>& burnValues, const std::vector<std::string>& controlOptions=std::vector<std::string>(), const std::vector<std::string>& layernames=std::vector<std::string>());
78 
79 protected:
81  virtual void setCodec(const std::string& imageType);
83  virtual void setCodec(const ImgReaderGdal& ImgSrc);
84  std::vector<std::string> m_options;
85 
86 private:
87 };
88 
96 template<typename T> bool ImgWriterGdal::writeData(T& value, int col, int row, int band)
97 {
98  if(band>=nrOfBand()+1){
99  std::ostringstream s;
100  s << "Error: band (" << band << ") exceeds nrOfBand (" << nrOfBand() << ")";
101  throw(s.str());
102  }
103  if(col>=nrOfCol()){
104  std::ostringstream s;
105  s << "Error: col (" << col << ") exceeds nrOfCol (" << nrOfCol() << ")";
106  throw(s.str());
107  }
108  if(col<0){
109  std::ostringstream s;
110  s << "Error: col (" << col << ") is negative";
111  throw(s.str());
112  }
113  if(row>=nrOfRow()){
114  std::ostringstream s;
115  s << "Error: row (" << row << ") exceeds nrOfRow (" << nrOfRow() << ")";
116  throw(s.str());
117  }
118  if(row<0){
119  std::ostringstream s;
120  s << "Error: row (" << row << ") is negative";
121  throw(s.str());
122  }
123  double theScale=1;
124  double theOffset=0;
125  if(m_scale.size()>band||m_offset.size()>band){
126  if(m_scale.size()>band)
127  theScale=m_scale[band];
128  if(m_offset.size()>band)
129  theOffset=m_offset[band];
130  }
131  //fetch raster band
132  GDALRasterBand *poBand;
133  T dvalue=theScale*value+theOffset;
134  poBand->RasterIO(GF_Write,col,row,1,1,&dvalue,1,1,getGDALDataType<T>(),0,0);
135  return true;
136 }
137 
146 template<typename T> bool ImgWriterGdal::writeData(std::vector<T>& buffer, int minCol, int maxCol, int row, int band)
147 {
148  if(buffer.size()!=maxCol-minCol+1){
149  std::string errorstring="invalid size of buffer";
150  throw(errorstring);
151  }
152  if(minCol>=nrOfCol()){
153  std::ostringstream s;
154  s << "minCol (" << minCol << ") exceeds nrOfCol (" << nrOfCol() << ")";
155  throw(s.str());
156  }
157  if(minCol<0){
158  std::ostringstream s;
159  s << "mincol (" << minCol << ") is negative";
160  throw(s.str());
161  }
162  if(maxCol>=nrOfCol()){
163  std::ostringstream s;
164  s << "maxCol (" << maxCol << ") exceeds nrOfCol (" << nrOfCol() << ")";
165  throw(s.str());
166  }
167  if(maxCol<minCol){
168  std::ostringstream s;
169  s << "maxCol (" << maxCol << ") is less than minCol (" << minCol << ")";
170  throw(s.str());
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  //todo: scaling and offset!
183  //fetch raster band
184  GDALRasterBand *poBand;
185  if(band>=nrOfBand()+1){
186  std::ostringstream s;
187  s << "band (" << band << ") exceeds nrOfBand (" << nrOfBand() << ")";
188  throw(s.str());
189  }
190  poBand = m_gds->GetRasterBand(band+1);//GDAL uses 1 based index
191  poBand->RasterIO(GF_Write,minCol,row,buffer.size(),1,&(buffer[0]),buffer.size(),1,getGDALDataType<T>(),0,0);
192  return true;
193 }
194 
201 template<typename T> bool ImgWriterGdal::writeData(std::vector<T>& buffer, int row, int band)
202 {
203  return writeData(buffer,0,nrOfCol()-1,row,band);
204 }
205 
214 template<typename T> bool ImgWriterGdal::writeDataBlock(Vector2d<T>& buffer2d, int minCol, int maxCol, int minRow, int maxRow, int band)
215 {
216  double theScale=1;
217  double theOffset=0;
218  if(m_scale.size()>band)
219  theScale=m_scale[band];
220  if(m_offset.size()>band)
221  theOffset=m_offset[band];
222  if(buffer2d.size()!=maxRow-minRow+1){
223  std::string errorstring="invalid buffer size";
224  throw(errorstring);
225  }
226  if(band>=nrOfBand()+1){
227  std::ostringstream s;
228  s << "band (" << band << ") exceeds nrOfBand (" << nrOfBand() << ")";
229  throw(s.str());
230  }
231  if(minCol>=nrOfCol()){
232  std::ostringstream s;
233  s << "minCol (" << minCol << ") exceeds nrOfCol (" << nrOfCol() << ")";
234  throw(s.str());
235  }
236  if(minCol<0){
237  std::ostringstream s;
238  s << "mincol (" << minCol << ") is negative";
239  throw(s.str());
240  }
241  if(maxCol>=nrOfCol()){
242  std::ostringstream s;
243  s << "maxCol (" << maxCol << ") exceeds nrOfCol (" << nrOfCol() << ")";
244  throw(s.str());
245  }
246  if(maxCol<minCol){
247  std::ostringstream s;
248  s << "maxCol (" << maxCol << ") is less than minCol (" << minCol << ")";
249  throw(s.str());
250  }
251  //todo: apply scaling and offset!
252  typename std::vector<T> buffer((maxRow-minRow+1)*(maxCol-minCol+1));
253  //fetch raster band
254  GDALRasterBand *poBand;
255  // typename std::vector<T>::iterator startit=buffer.begin();
256  for(int irow=minRow;irow<=maxRow;++irow){
257  buffer.insert(buffer.begin()+(maxCol-minCol+1)*(irow-minRow),buffer2d[irow-minRow].begin(),buffer2d[irow-minRow].end());
258  }
259  poBand = m_gds->GetRasterBand(band+1);//GDAL uses 1 based index
260  poBand->RasterIO(GF_Write,minCol,minRow,maxCol-minCol+1,maxRow-minRow+1,&(buffer[0]),(maxCol-minCol+1),(maxRow-minRow+1),getGDALDataType<T>(),0,0);
261  return true;
262 }
263 
264 #endif // _IMGWRITERGDAL_H_
265 
266 
267 // adfGeoTransform[0] /* top left x */
268 // adfGeoTransform[1] /* w-e pixel resolution */
269 // adfGeoTransform[2] /* rotation, 0 if image is "north up" */
270 // adfGeoTransform[3] /* top left y */
271 // adfGeoTransform[4] /* rotation, 0 if image is "north up" */
272 // adfGeoTransform[5] /* n-s pixel resolution */
ImgWriterGdal(const std::string &filename, const ImgReaderGdal &imgSrc, const std::vector< std::string > &options=std::vector< std::string >())
constructor opening an image for writing, copying image attributes from a source image. Image is directly writen to file.
Definition: ImgWriterGdal.h:43
void rasterizeOgr(ImgReaderOgr &ogrReader, const std::vector< double > &burnValues, const std::vector< std::string > &controlOptions=std::vector< std::string >(), const std::vector< std::string > &layernames=std::vector< std::string >())
Rasterize an OGR vector dataset using the gdal algorithm "GDALRasterizeLayers".
void setImageDescription(const std::string &imageDescription)
Set the image description (only for GeoTiff format: TIFFTAG_IMAGEDESCRIPTION)
Definition: ImgWriterGdal.h:55
void setMetadata(char **metadata)
Set specific metadata (driver specific)
int nrOfCol(void) const
Get the number of columns of this dataset.
Definition: ImgRasterGdal.h:98
virtual void setCodec(const std::string &imageType)
Register GDAL driver, setting the datatype, imagetype and some metadata.
ImgWriterGdal(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 >())
constructor opening an image for writing, defining all image attributes. Image is directly written to...
Definition: ImgWriterGdal.h:45
void setColorTable(const std::string &filename, int band=0)
Set the color table using an (ASCII) file with 5 columns (value R G B alpha)
GDALDataset * m_gds
instance of the GDAL dataset of this dataset
int nrOfRow(void) const
Get the number of rows of this dataset.
bool writeData(T &value, int col, int row, int band=0)
Write a single pixel cell value at a specific column and row for a specific band (all indices start c...
Definition: ImgWriterGdal.h:96
std::vector< double > m_scale
Vector containing the scale factor to be applied (one scale value for each band)
bool writeDataBlock(Vector2d< T > &buffer2d, int minCol, int maxCol, int minRow, int maxRow, int band=0)
Write pixel cell values for a range of columns and rows for a specific band (all indices start counti...
ImgWriterGdal(void)
default constructor. Image needs to be opened later with one of the open methods. ...
void open(const std::string &filename, const ImgReaderGdal &imgSrc, const std::vector< std::string > &options=std::vector< std::string >())
Open an image for writing, copying image attributes from a source image. Image is directly written to...
void rasterizeBuf(ImgReaderOgr &ogrReader, const std::vector< double > &burnValues, const std::vector< std::string > &controlOptions=std::vector< std::string >(), const std::vector< std::string > &layernames=std::vector< std::string >())
Rasterize an OGR vector dataset in memory using the gdal algorithm "GDALRasterizeLayersBuf".
void close(void)
Close the image.
int nrOfBand(void) const
Get the number of bands of this dataset.
std::vector< double > m_offset
Vector containing the offset factor to be applied (one offset value for each band) ...
~ImgWriterGdal(void)
destructor