20 #ifndef _IMGWRITERGDAL_H_
21 #define _IMGWRITERGDAL_H_
27 #include "gdal_priv.h"
28 #include "ImgReaderGdal.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);
82 void setCodec(
const std::string& imageType);
85 std::string m_filename;
99 std::vector<std::string> m_options;
102 template<
typename T>
bool ImgWriterGdal::writeData(T& value,
const GDALDataType& dataType,
int col,
int row,
int band)
const
105 GDALRasterBand *poBand;
106 if(band>=nrOfBand()+1){
107 std::ostringstream s;
108 s <<
"band (" << band <<
") exceeds nrOfBand (" << nrOfBand() <<
")";
111 poBand = m_gds->GetRasterBand(band+1);
113 std::ostringstream s;
114 s <<
"col (" << col <<
") exceeds nrOfCol (" << nrOfCol() <<
")";
118 std::ostringstream s;
119 s <<
"col (" << col <<
") is negative";
123 std::ostringstream s;
124 s <<
"row (" << row <<
") exceeds nrOfRow (" << nrOfRow() <<
")";
128 std::ostringstream s;
129 s <<
"row (" << row <<
") is negative";
132 poBand->RasterIO(GF_Write,col,row,1,1,&value,1,1,dataType,0,0);
136 template<
typename T>
bool ImgWriterGdal::writeData(std::vector<T>& buffer,
const GDALDataType& dataType,
int minCol,
int maxCol,
int row,
int band)
const
139 GDALRasterBand *poBand;
140 if(band>=nrOfBand()+1){
141 std::ostringstream s;
142 s <<
"band (" << band <<
") exceeds nrOfBand (" << nrOfBand() <<
")";
145 poBand = m_gds->GetRasterBand(band+1);
146 if(buffer.size()!=maxCol-minCol+1){
147 std::string errorstring=
"invalid buffer size";
150 if(minCol>=nrOfCol()){
151 std::ostringstream s;
152 s <<
"minCol (" << minCol <<
") exceeds nrOfCol (" << nrOfCol() <<
")";
156 std::ostringstream s;
157 s <<
"mincol (" << minCol <<
") is negative";
160 if(maxCol>=nrOfCol()){
161 std::ostringstream s;
162 s <<
"maxCol (" << maxCol <<
") exceeds nrOfCol (" << nrOfCol() <<
")";
166 std::ostringstream s;
167 s <<
"maxCol (" << maxCol <<
") is less than minCol (" << minCol <<
")";
172 std::ostringstream s;
173 s <<
"row (" << row <<
") exceeds nrOfRow (" << nrOfRow() <<
")";
177 std::ostringstream s;
178 s <<
"row (" << row <<
") is negative";
181 poBand->RasterIO(GF_Write,minCol,row,buffer.size(),1,&(buffer[0]),buffer.size(),1,dataType,0,0);
185 template<
typename T>
bool ImgWriterGdal::writeData(std::vector<T>& buffer,
const GDALDataType& dataType,
int row,
int band)
const
188 GDALRasterBand *poBand;
189 if(band>=nrOfBand()+1){
190 std::ostringstream s;
191 s <<
"band (" << band <<
") exceeds nrOfBand (" << nrOfBand() <<
")";
194 poBand = m_gds->GetRasterBand(band+1);
195 if(buffer.size()!=nrOfCol()){
196 std::string errorstring=
"invalid buffer size";
200 std::ostringstream s;
201 s <<
"row (" << row <<
") exceeds nrOfRow (" << nrOfRow() <<
")";
204 poBand->RasterIO(GF_Write,0,row,buffer.size(),1,&(buffer[0]),buffer.size(),1,dataType,0,0);
208 template<
typename T>
bool ImgWriterGdal::writeDataBlock(
Vector2d<T>& buffer,
const GDALDataType& dataType ,
int minCol,
int maxCol,
int minRow,
int maxRow,
int band)
const
211 GDALRasterBand *poBand;
212 if(band>=nrOfBand()+1){
213 std::ostringstream s;
214 s <<
"band (" << band <<
") exceeds nrOfBand (" << nrOfBand() <<
")";
217 poBand = m_gds->GetRasterBand(band+1);
218 assert(buffer.size()==maxRow-minRow+1);
219 for(
int irow=minRow;irow<=maxRow;++irow)
220 writeData(buffer[irow-minRow], dataType, minCol, maxCol, irow, band);
224 #endif // _IMGWRITERGDAL_H_