20 #ifndef _IMGWRITERGDAL_H_ 21 #define _IMGWRITERGDAL_H_ 27 #include "gdal_priv.h" 28 #include "ImgRasterGdal.h" 29 #include "ImgReaderGdal.h" 30 #include "ImgReaderOgr.h" 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);};
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>());
55 void setImageDescription(
const std::string& imageDescription){
m_gds->SetMetadataItem(
"TIFFTAG_IMAGEDESCRIPTION",imageDescription.c_str());};
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);
66 template<
typename T>
bool writeDataBlock(
Vector2d<T>& buffer2d,
int minCol,
int maxCol,
int minRow,
int maxRow,
int band=0);
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>());
81 virtual void setCodec(
const std::string& imageType);
84 std::vector<std::string> m_options;
100 s <<
"Error: band (" << band <<
") exceeds nrOfBand (" <<
nrOfBand() <<
")";
104 std::ostringstream s;
105 s <<
"Error: col (" << col <<
") exceeds nrOfCol (" <<
nrOfCol() <<
")";
109 std::ostringstream s;
110 s <<
"Error: col (" << col <<
") is negative";
114 std::ostringstream s;
115 s <<
"Error: row (" << row <<
") exceeds nrOfRow (" <<
nrOfRow() <<
")";
119 std::ostringstream s;
120 s <<
"Error: row (" << row <<
") is negative";
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);
148 if(buffer.size()!=maxCol-minCol+1){
149 std::string errorstring=
"invalid size of buffer";
153 std::ostringstream s;
154 s <<
"minCol (" << minCol <<
") exceeds nrOfCol (" <<
nrOfCol() <<
")";
158 std::ostringstream s;
159 s <<
"mincol (" << minCol <<
") is negative";
163 std::ostringstream s;
164 s <<
"maxCol (" << maxCol <<
") exceeds nrOfCol (" <<
nrOfCol() <<
")";
168 std::ostringstream s;
169 s <<
"maxCol (" << maxCol <<
") is less than minCol (" << minCol <<
")";
173 std::ostringstream s;
174 s <<
"row (" << row <<
") exceeds nrOfRow (" <<
nrOfRow() <<
")";
178 std::ostringstream s;
179 s <<
"row (" << row <<
") is negative";
184 GDALRasterBand *poBand;
186 std::ostringstream s;
187 s <<
"band (" << band <<
") exceeds nrOfBand (" <<
nrOfBand() <<
")";
190 poBand =
m_gds->GetRasterBand(band+1);
191 poBand->RasterIO(GF_Write,minCol,row,buffer.size(),1,&(buffer[0]),buffer.size(),1,getGDALDataType<T>(),0,0);
222 if(buffer2d.size()!=maxRow-minRow+1){
223 std::string errorstring=
"invalid buffer size";
227 std::ostringstream s;
228 s <<
"band (" << band <<
") exceeds nrOfBand (" <<
nrOfBand() <<
")";
232 std::ostringstream s;
233 s <<
"minCol (" << minCol <<
") exceeds nrOfCol (" <<
nrOfCol() <<
")";
237 std::ostringstream s;
238 s <<
"mincol (" << minCol <<
") is negative";
242 std::ostringstream s;
243 s <<
"maxCol (" << maxCol <<
") exceeds nrOfCol (" <<
nrOfCol() <<
")";
247 std::ostringstream s;
248 s <<
"maxCol (" << maxCol <<
") is less than minCol (" << minCol <<
")";
252 typename std::vector<T> buffer((maxRow-minRow+1)*(maxCol-minCol+1));
254 GDALRasterBand *poBand;
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());
259 poBand =
m_gds->GetRasterBand(band+1);
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);
264 #endif // _IMGWRITERGDAL_H_
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.
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)
void setMetadata(char **metadata)
Set specific metadata (driver specific)
int nrOfCol(void) const
Get the number of columns of this dataset.
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...
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...
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