pktools  2.6.6
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 "ImgRasterGdal.h"
29 #include "ImgReaderGdal.h"
30 #include "ImgReaderOgr.h"
31 
32 //--------------------------------------------------------------------------
33 class ImgWriterGdal : public virtual ImgRasterGdal
34 {
35 public:
36  ImgWriterGdal(void);
37  ~ImgWriterGdal(void);
38  void open(const std::string& filename);
39  void open(const std::string& filename, const ImgReaderGdal& imgSrc, const std::vector<std::string>& options=std::vector<std::string>());
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 
43  void copyGeoTransform(const ImgReaderGdal& imgSrc);
44  void setProjection(const std::string& projection);
45  std::string setProjectionProj4(const std::string& projection);
46 
47  void setImageDescription(const std::string& imageDescription){m_gds->SetMetadataItem( "TIFFTAG_IMAGEDESCRIPTION",imageDescription.c_str());};
48  void setGeoTransform(double* gt);
49 
50  template<typename T> bool writeData(T& value, const GDALDataType& dataType, int col, int row, int band=0) const;
51  template<typename T> bool writeData(std::vector<T>& buffer, const GDALDataType& dataType , int minCol, int maxCol, int row, int band=0) const;
52  template<typename T> bool writeData(std::vector<T>& buffer, const GDALDataType& dataType, int row, int band=0) const;
53  bool writeData(void* pdata, const GDALDataType& dataType, int band=0) const;
54  template<typename T> bool writeDataBlock(Vector2d<T>& buffer, const GDALDataType& dataType , int minCol, int maxCol, int minRow, int maxRow, int band=0) const;
55  // std::string getInterleave(){return m_interleave;};
56  // std::string getCompression(){return m_compression;};
57  void setColorTable(const std::string& filename, int band=0);
58  void setColorTable(GDALColorTable* colorTable, int band=0);
59  void setMetadata(char** metadata);
60  void rasterizeOgr(ImgReaderOgr& ogrReader, const std::vector<double>& burnValues=std::vector<double>(), const std::vector<std::string>& layernames=std::vector<std::string>());
61 
62 protected:
63  void setCodec(const GDALDataType& dataType, const std::string& imageType);
64  void setCodec(const ImgReaderGdal& ImgSrc);
65 
66  /* double m_ulx; */
67  /* double m_uly; */
68  /* double m_delta_x; */
69  /* double m_delta_y; */
70  /* bool m_isGeoRef; */
71  // std::string m_interleave;
72  // std::string m_compression;
73  std::vector<std::string> m_options;
74 };
75 
76 template<typename T> bool ImgWriterGdal::writeData(T& value, const GDALDataType& dataType, int col, int row, int band) const
77 {
78  //fetch raster band
79  GDALRasterBand *poBand;
80  if(band>=nrOfBand()+1){
81  std::ostringstream s;
82  s << "band (" << band << ") exceeds nrOfBand (" << nrOfBand() << ")";
83  throw(s.str());
84  }
85  poBand = m_gds->GetRasterBand(band+1);//GDAL uses 1 based index
86  if(col>=nrOfCol()){
87  std::ostringstream s;
88  s << "col (" << col << ") exceeds nrOfCol (" << nrOfCol() << ")";
89  throw(s.str());
90  }
91  if(col<0){
92  std::ostringstream s;
93  s << "col (" << col << ") is negative";
94  throw(s.str());
95  }
96  if(row>=nrOfRow()){
97  std::ostringstream s;
98  s << "row (" << row << ") exceeds nrOfRow (" << nrOfRow() << ")";
99  throw(s.str());
100  }
101  if(row<0){
102  std::ostringstream s;
103  s << "row (" << row << ") is negative";
104  throw(s.str());
105  }
106  poBand->RasterIO(GF_Write,col,row,1,1,&value,1,1,dataType,0,0);
107  return true;
108 }
109 
110 template<typename T> bool ImgWriterGdal::writeData(std::vector<T>& buffer, const GDALDataType& dataType, int minCol, int maxCol, int row, int band) const
111 {
112  //fetch raster band
113  GDALRasterBand *poBand;
114  if(band>=nrOfBand()+1){
115  std::ostringstream s;
116  s << "band (" << band << ") exceeds nrOfBand (" << nrOfBand() << ")";
117  throw(s.str());
118  }
119  poBand = m_gds->GetRasterBand(band+1);//GDAL uses 1 based index
120  if(buffer.size()!=maxCol-minCol+1){
121  std::string errorstring="invalid buffer size";
122  throw(errorstring);
123  }
124  if(minCol>=nrOfCol()){
125  std::ostringstream s;
126  s << "minCol (" << minCol << ") exceeds nrOfCol (" << nrOfCol() << ")";
127  throw(s.str());
128  }
129  if(minCol<0){
130  std::ostringstream s;
131  s << "mincol (" << minCol << ") is negative";
132  throw(s.str());
133  }
134  if(maxCol>=nrOfCol()){
135  std::ostringstream s;
136  s << "maxCol (" << maxCol << ") exceeds nrOfCol (" << nrOfCol() << ")";
137  throw(s.str());
138  }
139  if(maxCol<minCol){
140  std::ostringstream s;
141  s << "maxCol (" << maxCol << ") is less than minCol (" << minCol << ")";
142  throw(s.str());
143  }
144 
145  if(row>=nrOfRow()){
146  std::ostringstream s;
147  s << "row (" << row << ") exceeds nrOfRow (" << nrOfRow() << ")";
148  throw(s.str());
149  }
150  if(row<0){
151  std::ostringstream s;
152  s << "row (" << row << ") is negative";
153  throw(s.str());
154  }
155  poBand->RasterIO(GF_Write,minCol,row,buffer.size(),1,&(buffer[0]),buffer.size(),1,dataType,0,0);
156  return true;
157 }
158 
159 template<typename T> bool ImgWriterGdal::writeData(std::vector<T>& buffer, const GDALDataType& dataType, int row, int band) const
160 {
161  //fetch raster band
162  GDALRasterBand *poBand;
163  if(band>=nrOfBand()+1){
164  std::ostringstream s;
165  s << "band (" << band << ") exceeds nrOfBand (" << nrOfBand() << ")";
166  throw(s.str());
167  }
168  poBand = m_gds->GetRasterBand(band+1);//GDAL uses 1 based index
169  if(buffer.size()!=nrOfCol()){
170  std::string errorstring="invalid buffer size";
171  throw(errorstring);
172  }
173  if(row>=nrOfRow()){
174  std::ostringstream s;
175  s << "row (" << row << ") exceeds nrOfRow (" << nrOfRow() << ")";
176  throw(s.str());
177  }
178  poBand->RasterIO(GF_Write,0,row,buffer.size(),1,&(buffer[0]),buffer.size(),1,dataType,0,0);
179  return true;
180 }
181 
182 template<typename T> bool ImgWriterGdal::writeDataBlock(Vector2d<T>& buffer, const GDALDataType& dataType , int minCol, int maxCol, int minRow, int maxRow, int band) const
183 {
184  //fetch raster band
185  GDALRasterBand *poBand;
186  if(band>=nrOfBand()+1){
187  std::ostringstream s;
188  s << "band (" << band << ") exceeds nrOfBand (" << nrOfBand() << ")";
189  throw(s.str());
190  }
191  poBand = m_gds->GetRasterBand(band+1);//GDAL uses 1 based index
192  assert(buffer.size()==maxRow-minRow+1);
193  for(int irow=minRow;irow<=maxRow;++irow)
194  writeData(buffer[irow-minRow], dataType, minCol, maxCol, irow, band);
195  return true;
196 }
197 
198 #endif // _IMGWRITERGDAL_H_
199 
200 
201 // adfGeoTransform[0] /* top left x */
202 // adfGeoTransform[1] /* w-e pixel resolution */
203 // adfGeoTransform[2] /* rotation, 0 if image is "north up" */
204 // adfGeoTransform[3] /* top left y */
205 // adfGeoTransform[4] /* rotation, 0 if image is "north up" */
206 // adfGeoTransform[5] /* n-s pixel resolution */