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;
return(m_noDataValues.size());};
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)
const;
101 double getMin(
int& col,
int& row,
int band=0)
const;
102 double getHistogram(std::vector<double>& histvector,
double& min,
double& max,
unsigned int& nbin,
int theBand=0,
bool kde=
false);
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 unsigned long int getNvalid(
int band)
const;
107 GDALDataType getDataType(
int band=0)
const;
108 GDALRasterBand* getRasterBand(
int band=0);
109 GDALColorTable* getColorTable(
int band=0)
const;
110 std::string getDriverDescription()
const;
111 std::string getImageType()
const{
return getDriverDescription();};
113 std::string getInterleave()
const;
114 std::string getCompression()
const;
115 GDALDataset* getDataset(){
return m_gds;};
116 char** getMetadata();
117 char** getMetadata()
const;
118 void getMetadata(std::list<std::string>& metadata)
const;
123 std::string m_filename;
134 std::vector<double> m_noDataValues;
135 std::vector<double> m_scale;
136 std::vector<double> m_offset;
146 template<
typename T>
void ImgReaderGdal::readData(T& value,
const GDALDataType& dataType,
int col,
int row,
int band)
const
149 GDALRasterBand *poBand;
150 assert(band<nrOfBand()+1);
151 poBand = m_gds->GetRasterBand(band+1);
152 assert(col<nrOfCol());
154 assert(row<nrOfRow());
156 poBand->RasterIO(GF_Read,col,row,1,1,&value,1,1,dataType,0,0);
157 if(m_scale.size()>band)
158 value=static_cast<double>(value)*m_scale[band];
159 if(m_offset.size()>band)
160 value=static_cast<double>(value)+m_offset[band];
163 template<
typename T>
void ImgReaderGdal::readData(std::vector<T>& buffer,
const GDALDataType& dataType,
int minCol,
int maxCol,
int row,
int band)
const
166 GDALRasterBand *poBand;
167 assert(band<nrOfBand()+1);
168 poBand = m_gds->GetRasterBand(band+1);
169 assert(minCol<nrOfCol());
171 assert(maxCol<nrOfCol());
172 assert(minCol<=maxCol);
173 assert(row<nrOfRow());
175 if(buffer.size()!=maxCol-minCol+1)
176 buffer.resize(maxCol-minCol+1);
177 poBand->RasterIO(GF_Read,minCol,row,buffer.size(),1,&(buffer[0]),buffer.size(),1,dataType,0,0);
178 if(m_scale.size()>band||m_offset.size()>band){
181 if(m_scale.size()>band)
182 theScale=m_scale[band];
183 if(m_offset.size()>band)
184 theOffset=m_offset[band];
185 for(
int index=0;index<buffer.size();++index)
186 buffer[index]=theScale*static_cast<double>(buffer[index])+theOffset;
190 template<
typename T>
void ImgReaderGdal::readData(std::vector<T>& buffer,
const GDALDataType& dataType ,
int minCol,
int maxCol,
double row,
int band, RESAMPLE resample)
const
193 std::vector<T> readBuffer_upper;
194 std::vector<T> readBuffer_lower;
195 if(buffer.size()!=maxCol-minCol+1)
196 buffer.resize(maxCol-minCol+1);
197 double upperRow=row-0.5;
198 upperRow=
static_cast<int>(upperRow);
199 double lowerRow=row+0.5;
200 lowerRow=
static_cast<int>(lowerRow);
203 if(lowerRow>=nrOfRow())
204 lowerRow=nrOfRow()-1;
207 readData(readBuffer_upper,GDT_Float64,minCol,maxCol,static_cast<int>(upperRow),band);
208 readData(readBuffer_lower,GDT_Float64,minCol,maxCol,static_cast<int>(lowerRow),band);
210 for(
int icol=0;icol<maxCol-minCol+1;++icol){
211 buffer[icol]=(lowerRow-row+0.5)*readBuffer_upper[icol]+(1-lowerRow+row-0.5)*readBuffer_lower[icol];
215 readData(buffer,GDT_Float64,minCol,maxCol,static_cast<int>(row),band);
220 template<
typename T>
void ImgReaderGdal::readDataBlock(
Vector2d<T>& buffer,
const GDALDataType& dataType ,
int minCol,
int maxCol,
int minRow,
int maxRow,
int band)
const
222 buffer.resize(maxRow-minRow+1);
223 for(
int irow=minRow;irow<=maxRow;++irow){
224 buffer[irow-minRow].resize(maxCol-minCol+1);
225 readData(buffer[irow-minRow],dataType,minCol,maxCol,irow,band);
229 template<
typename T>
void ImgReaderGdal::readDataBlock(std::vector<T>& buffer,
const GDALDataType& dataType ,
int minCol,
int maxCol,
int minRow,
int maxRow,
int band)
const
233 if(m_scale.size()>band)
234 theScale=m_scale[band];
235 if(m_offset.size()>band)
236 theOffset=m_offset[band];
238 GDALRasterBand *poBand;
239 assert(band<nrOfBand()+1);
240 poBand = m_gds->GetRasterBand(band+1);
241 if(minCol>=nrOfCol() ||
243 (maxCol>=nrOfCol()) ||
245 (minRow>=nrOfRow()) ||
247 (maxRow>=nrOfRow()) ||
249 std::string errorString=
"block not within image boundaries";
260 if(buffer.size()!=(maxRow-minRow+1)*(maxCol-minCol+1))
261 buffer.resize((maxRow-minRow+1)*(maxCol-minCol+1));
262 poBand->RasterIO(GF_Read,minCol,minRow,maxCol-minCol+1,maxRow-minRow+1,&(buffer[0]),(maxCol-minCol+1),(maxRow-minRow+1),dataType,0,0);
263 if(m_scale.size()>band||m_offset.size()>band){
264 for(
int index=0;index<buffer.size();++index)
265 buffer[index]=theScale*buffer[index]+theOffset;
290 template<
typename T>
void ImgReaderGdal::readData(std::vector<T>& buffer,
const GDALDataType& dataType,
int row,
int band)
const
292 readData(buffer,dataType,0,nrOfCol()-1,row,band);
295 template<
typename T>
void ImgReaderGdal::readData(std::vector<T>& buffer,
const GDALDataType& dataType,
double row,
int band, RESAMPLE resample)
const
297 readData(buffer,dataType,0,nrOfCol()-1,row,band,resample);
301 #endif // _IMGREADERGDAL_H_