20 #ifndef _IMGWRITERGDAL_H_
21 #define _IMGWRITERGDAL_H_
27 #include "gdal_priv.h"
28 #include "ImgReaderGdal.h"
29 #include "ImgReaderOgr.h"
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>());
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>());
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;};
47 std::string setProjection(
void);
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);
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;};
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;
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>());
83 void setCodec(
const std::string& imageType);
86 std::string m_filename;
100 std::vector<std::string> m_options;
103 template<
typename T>
bool ImgWriterGdal::writeData(T& value,
const GDALDataType& dataType,
int col,
int row,
int band)
const
106 GDALRasterBand *poBand;
107 if(band>=nrOfBand()+1){
108 std::ostringstream s;
109 s <<
"band (" << band <<
") exceeds nrOfBand (" << nrOfBand() <<
")";
112 poBand = m_gds->GetRasterBand(band+1);
114 std::ostringstream s;
115 s <<
"col (" << col <<
") exceeds nrOfCol (" << nrOfCol() <<
")";
119 std::ostringstream s;
120 s <<
"col (" << col <<
") is negative";
124 std::ostringstream s;
125 s <<
"row (" << row <<
") exceeds nrOfRow (" << nrOfRow() <<
")";
129 std::ostringstream s;
130 s <<
"row (" << row <<
") is negative";
133 poBand->RasterIO(GF_Write,col,row,1,1,&value,1,1,dataType,0,0);
137 template<
typename T>
bool ImgWriterGdal::writeData(std::vector<T>& buffer,
const GDALDataType& dataType,
int minCol,
int maxCol,
int row,
int band)
const
140 GDALRasterBand *poBand;
141 if(band>=nrOfBand()+1){
142 std::ostringstream s;
143 s <<
"band (" << band <<
") exceeds nrOfBand (" << nrOfBand() <<
")";
146 poBand = m_gds->GetRasterBand(band+1);
147 if(buffer.size()!=maxCol-minCol+1){
148 std::string errorstring=
"invalid buffer size";
151 if(minCol>=nrOfCol()){
152 std::ostringstream s;
153 s <<
"minCol (" << minCol <<
") exceeds nrOfCol (" << nrOfCol() <<
")";
157 std::ostringstream s;
158 s <<
"mincol (" << minCol <<
") is negative";
161 if(maxCol>=nrOfCol()){
162 std::ostringstream s;
163 s <<
"maxCol (" << maxCol <<
") exceeds nrOfCol (" << nrOfCol() <<
")";
167 std::ostringstream s;
168 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";
182 poBand->RasterIO(GF_Write,minCol,row,buffer.size(),1,&(buffer[0]),buffer.size(),1,dataType,0,0);
186 template<
typename T>
bool ImgWriterGdal::writeData(std::vector<T>& buffer,
const GDALDataType& dataType,
int row,
int band)
const
189 GDALRasterBand *poBand;
190 if(band>=nrOfBand()+1){
191 std::ostringstream s;
192 s <<
"band (" << band <<
") exceeds nrOfBand (" << nrOfBand() <<
")";
195 poBand = m_gds->GetRasterBand(band+1);
196 if(buffer.size()!=nrOfCol()){
197 std::string errorstring=
"invalid buffer size";
201 std::ostringstream s;
202 s <<
"row (" << row <<
") exceeds nrOfRow (" << nrOfRow() <<
")";
205 poBand->RasterIO(GF_Write,0,row,buffer.size(),1,&(buffer[0]),buffer.size(),1,dataType,0,0);
209 template<
typename T>
bool ImgWriterGdal::writeDataBlock(
Vector2d<T>& buffer,
const GDALDataType& dataType ,
int minCol,
int maxCol,
int minRow,
int maxRow,
int band)
const
212 GDALRasterBand *poBand;
213 if(band>=nrOfBand()+1){
214 std::ostringstream s;
215 s <<
"band (" << band <<
") exceeds nrOfBand (" << nrOfBand() <<
")";
218 poBand = m_gds->GetRasterBand(band+1);
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);
225 #endif // _IMGWRITERGDAL_H_