20 #ifndef _IMGREADERGDAL_H_
21 #define _IMGREADERGDAL_H_
27 #include "gdal_priv.h"
28 #include "base/Vector2d.h"
30 enum RESAMPLE { NEAR = 0, BILINEAR = 1, BICUBIC = 2 };
39 void open(
const std::string& filename);
41 std::string getFileName()
const {
return m_filename;};
42 int nrOfCol(
void)
const {
return m_ncol;};
43 int nrOfRow(
void)
const {
return m_nrow;};
44 int nrOfBand(
void)
const {
return m_nband;};
45 bool isGeoRef()
const {
double gt[6];getGeoTransform(gt);
if(gt[5]<0)
return true;
else return false;};
46 std::string getProjection(
void)
const;
47 std::string getProjectionRef(
void)
const;
48 std::string getGeoTransform()
const;
49 void getGeoTransform(
double* gt)
const;
51 std::string getDescription()
const;
52 std::string getMetadataItem()
const;
53 std::string getImageDescription()
const;
54 bool getBoundingBox (
double& ulx,
double& uly,
double& lrx,
double& lry)
const;
55 bool getCenterPos(
double& x,
double& y)
const;
56 double getUlx()
const {
double ulx, uly, lrx,lry;getBoundingBox(ulx,uly,lrx,lry);
return(ulx);};
57 double getUly()
const {
double ulx, uly, lrx,lry;getBoundingBox(ulx,uly,lrx,lry);
return(uly);};
58 double getLrx()
const {
double ulx, uly, lrx,lry;getBoundingBox(ulx,uly,lrx,lry);
return(lrx);};
59 double getLry()
const {
double ulx, uly, lrx,lry;getBoundingBox(ulx,uly,lrx,lry);
return(lry);};
61 void setScale(
double theScale,
int band=0){
63 if(m_scale.size()!=nrOfBand()){
64 m_scale.resize(nrOfBand());
65 for(
int iband=0;iband<nrOfBand();++iband)
68 m_scale[band]=theScale;
71 void setOffset(
double theOffset,
int band=0){
73 if(m_offset.size()!=nrOfBand()){
74 m_offset.resize(nrOfBand());
75 for(
int iband=0;iband<nrOfBand();++iband)
78 m_offset[band]=theOffset;
81 int getNoDataValues(std::vector<double>& noDataValues)
const;
82 bool isNoData(
double value)
const{
if(m_noDataValues.empty())
return false;
else return find(m_noDataValues.begin(),m_noDataValues.end(),value)!=m_noDataValues.end();};
83 int pushNoDataValue(
double noDataValue);
84 int setNoData(
const std::vector<double> nodata){m_noDataValues=nodata;};
85 CPLErr GDALSetNoDataValue(
double noDataValue,
int band=0) {
return getRasterBand(band)->SetNoDataValue(noDataValue);};
86 bool covers(
double x,
double y)
const;
87 bool covers(
double ulx,
double uly,
double lrx,
double lry)
const;
88 bool geo2image(
double x,
double y,
double& i,
double& j)
const;
89 bool image2geo(
double i,
double j,
double& x,
double& y)
const;
90 double getDeltaX(
void)
const {
double gt[6];getGeoTransform(gt);
return gt[1];};
91 double getDeltaY(
void)
const {
double gt[6];getGeoTransform(gt);
return -gt[5];};
92 template<
typename T>
void readData(T& value,
const GDALDataType& dataType,
int col,
int row,
int band=0)
const;
93 template<
typename T>
void readData(std::vector<T>& buffer,
const GDALDataType& dataType ,
int minCol,
int maxCol,
int row,
int band=0)
const;
94 template<
typename T>
void readData(std::vector<T>& buffer,
const GDALDataType& dataType ,
int minCol,
int maxCol,
double row,
int band=0, RESAMPLE resample=NEAR)
const;
95 template<
typename T>
void readDataBlock(
Vector2d<T>& buffer,
const GDALDataType& dataType ,
int minCol,
int maxCol,
int minRow,
int maxRow,
int band=0)
const;
96 template<
typename T>
void readDataBlock(std::vector<T>& buffer,
const GDALDataType& dataType ,
int minCol,
int maxCol,
int minRow,
int maxRow,
int band=0)
const;
97 template<
typename T>
void readData(std::vector<T>& buffer,
const GDALDataType& dataType,
int row,
int band=0)
const;
98 template<
typename T>
void readData(std::vector<T>& buffer,
const GDALDataType& dataType,
double row,
int band=0, RESAMPLE resample=NEAR)
const;
99 void getMinMax(
int startCol,
int endCol,
int startRow,
int endRow,
int band,
double& minValue,
double& maxValue)
const;
100 void getMinMax(
double& minValue,
double& maxValue,
int band=0,
bool exhaustiveSearch=
false)
const;
101 double getMin(
int& col,
int& row,
int band=0)
const;
102 unsigned long int getHistogram(std::vector<unsigned long int>& histvector,
double& min,
double& max,
unsigned int& nbin,
int theBand=0)
const;
103 double getMax(
int& col,
int& row,
int band=0)
const;
104 void getRefPix(
double& refX,
double &refY,
int band=0)
const;
105 void getRange(std::vector<short>& range,
int Band=0)
const;
106 GDALDataType getDataType(
int band=0)
const;
107 GDALRasterBand* getRasterBand(
int band=0);
108 GDALColorTable* getColorTable(
int band=0)
const;
109 std::string getDriverDescription()
const;
110 std::string getImageType()
const{
return getDriverDescription();};
112 std::string getInterleave()
const;
113 std::string getCompression()
const;
114 GDALDataset* getDataset(){
return m_gds;};
115 char** getMetadata();
116 char** getMetadata()
const;
117 void getMetadata(std::list<std::string>& metadata)
const;
122 std::string m_filename;
133 std::vector<double> m_noDataValues;
134 std::vector<double> m_scale;
135 std::vector<double> m_offset;
145 template<
typename T>
void ImgReaderGdal::readData(T& value,
const GDALDataType& dataType,
int col,
int row,
int band)
const
148 GDALRasterBand *poBand;
149 assert(band<nrOfBand()+1);
150 poBand = m_gds->GetRasterBand(band+1);
151 assert(col<nrOfCol());
153 assert(row<nrOfRow());
155 poBand->RasterIO(GF_Read,col,row,1,1,&value,1,1,dataType,0,0);
156 if(m_scale.size()>band)
157 value=static_cast<double>(value)*m_scale[band];
158 if(m_offset.size()>band)
159 value=static_cast<double>(value)+m_offset[band];
162 template<
typename T>
void ImgReaderGdal::readData(std::vector<T>& buffer,
const GDALDataType& dataType,
int minCol,
int maxCol,
int row,
int band)
const
165 GDALRasterBand *poBand;
166 assert(band<nrOfBand()+1);
167 poBand = m_gds->GetRasterBand(band+1);
168 assert(minCol<nrOfCol());
170 assert(maxCol<nrOfCol());
171 assert(minCol<=maxCol);
172 assert(row<nrOfRow());
174 if(buffer.size()!=maxCol-minCol+1)
175 buffer.resize(maxCol-minCol+1);
176 poBand->RasterIO(GF_Read,minCol,row,buffer.size(),1,&(buffer[0]),buffer.size(),1,dataType,0,0);
177 if(m_scale.size()>band||m_offset.size()>band){
180 if(m_scale.size()>band)
181 theScale=m_scale[band];
182 if(m_offset.size()>band)
183 theOffset=m_offset[band];
184 for(
int index=0;index<buffer.size();++index)
185 buffer[index]=theScale*static_cast<double>(buffer[index])+theOffset;
189 template<
typename T>
void ImgReaderGdal::readData(std::vector<T>& buffer,
const GDALDataType& dataType ,
int minCol,
int maxCol,
double row,
int band, RESAMPLE resample)
const
192 std::vector<T> readBuffer_upper;
193 std::vector<T> readBuffer_lower;
194 if(buffer.size()!=maxCol-minCol+1)
195 buffer.resize(maxCol-minCol+1);
196 double upperRow=row-0.5;
197 upperRow=
static_cast<int>(upperRow);
198 double lowerRow=row+0.5;
199 lowerRow=
static_cast<int>(lowerRow);
202 if(lowerRow>=nrOfRow())
203 lowerRow=nrOfRow()-1;
206 readData(readBuffer_upper,GDT_Float64,minCol,maxCol,static_cast<int>(upperRow),band);
207 readData(readBuffer_lower,GDT_Float64,minCol,maxCol,static_cast<int>(lowerRow),band);
209 for(
int icol=0;icol<maxCol-minCol+1;++icol){
210 buffer[icol]=(lowerRow-row+0.5)*readBuffer_upper[icol]+(1-lowerRow+row-0.5)*readBuffer_lower[icol];
214 readData(buffer,GDT_Float64,minCol,maxCol,static_cast<int>(row),band);
219 template<
typename T>
void ImgReaderGdal::readDataBlock(
Vector2d<T>& buffer,
const GDALDataType& dataType ,
int minCol,
int maxCol,
int minRow,
int maxRow,
int band)
const
221 buffer.resize(maxRow-minRow+1);
222 for(
int irow=minRow;irow<=maxRow;++irow){
223 buffer[irow-minRow].resize(maxCol-minCol+1);
224 readData(buffer[irow-minRow],dataType,minCol,maxCol,irow,band);
228 template<
typename T>
void ImgReaderGdal::readDataBlock(std::vector<T>& buffer,
const GDALDataType& dataType ,
int minCol,
int maxCol,
int minRow,
int maxRow,
int band)
const
232 if(m_scale.size()>band)
233 theScale=m_scale[band];
234 if(m_offset.size()>band)
235 theOffset=m_offset[band];
237 GDALRasterBand *poBand;
238 assert(band<nrOfBand()+1);
239 poBand = m_gds->GetRasterBand(band+1);
240 if(minCol>=nrOfCol() ||
242 (maxCol>=nrOfCol()) ||
244 (minRow>=nrOfRow()) ||
246 (maxRow>=nrOfRow()) ||
248 std::string errorString=
"block not within image boundaries";
259 if(buffer.size()!=(maxRow-minRow+1)*(maxCol-minCol+1))
260 buffer.resize((maxRow-minRow+1)*(maxCol-minCol+1));
261 poBand->RasterIO(GF_Read,minCol,minRow,maxCol-minCol+1,maxRow-minRow+1,&(buffer[0]),(maxCol-minCol+1),(maxRow-minRow+1),dataType,0,0);
262 if(m_scale.size()>band||m_offset.size()>band){
263 for(
int index=0;index<buffer.size();++index)
264 buffer[index]=theScale*buffer[index]+theOffset;
289 template<
typename T>
void ImgReaderGdal::readData(std::vector<T>& buffer,
const GDALDataType& dataType,
int row,
int band)
const
291 readData(buffer,dataType,0,nrOfCol()-1,row,band);
294 template<
typename T>
void ImgReaderGdal::readData(std::vector<T>& buffer,
const GDALDataType& dataType,
double row,
int band, RESAMPLE resample)
const
296 readData(buffer,dataType,0,nrOfCol()-1,row,band,resample);
300 #endif // _IMGREADERGDAL_H_