20 #ifndef _IMGREADERGDAL_H_
21 #define _IMGREADERGDAL_H_
23 #include "ImgRasterGdal.h"
28 #include "gdal_priv.h"
29 #include "base/Vector2d.h"
42 ImgReaderGdal(
const std::string& filename,
const GDALAccess& readMode=GA_ReadOnly){
open(filename, readMode);};
46 void open(
const std::string& filename,
const GDALAccess& readMode=GA_ReadOnly);
52 template<
typename T>
void readData(T& value,
int col,
int row,
int band=0);
54 template<
typename T>
void readData(std::vector<T>& buffer,
int minCol,
int maxCol,
int row,
int band=0);
56 template<
typename T>
void readData(std::vector<T>& buffer,
int minCol,
int maxCol,
double row,
int band=0, RESAMPLE resample=NEAR);
58 template<
typename T>
void readDataBlock(
Vector2d<T>& buffer2d,
int minCol,
int maxCol,
int minRow,
int maxRow,
int band=0);
60 template<
typename T>
void readDataBlock(std::vector<T>& buffer ,
int minCol,
int maxCol,
int minRow,
int maxRow,
int band=0);
62 template<
typename T>
void readData(std::vector<T>& buffer,
int row,
int band=0);
64 template<
typename T>
void readData(std::vector<T>& buffer,
double row,
int band=0, RESAMPLE resample=NEAR);
66 void getMinMax(
int startCol,
int endCol,
int startRow,
int endRow,
int band,
double& minValue,
double& maxValue);
68 void getMinMax(
double& minValue,
double& maxValue,
int band=0);
70 double getMin(
int& col,
int& row,
int band=0);
72 double getMax(
int& col,
int& row,
int band=0);
74 double getHistogram(std::vector<double>& histvector,
double& min,
double& max,
unsigned int& nbin,
int theBand=0,
bool kde=
false);
76 void getRefPix(
double& refX,
double &refY,
int band=0);
78 void getRange(std::vector<short>& range,
int Band=0);
84 void setCodec(
const GDALAccess& readMode=GA_ReadOnly);
112 GDALRasterBand *poBand;
113 poBand =
m_gds->GetRasterBand(band+1);
114 poBand->RasterIO(GF_Read,col,row,1,1,&value,1,1,getGDALDataType<T>(),0,0);
115 dvalue=theScale*value+theOffset;
116 value=
static_cast<T
>(dvalue);
132 assert(minCol<=maxCol);
144 GDALRasterBand *poBand;
145 poBand =
m_gds->GetRasterBand(band+1);
146 if(buffer.size()!=maxCol-minCol+1)
147 buffer.resize(maxCol-minCol+1);
148 poBand->RasterIO(GF_Read,minCol,row,buffer.size(),1,&(buffer[0]),buffer.size(),1,getGDALDataType<T>(),0,0);
150 for(
int index=0;index<buffer.size();++index)
151 buffer[index]=theScale*static_cast<double>(buffer[index])+theOffset;
163 template<
typename T>
void ImgReaderGdal::readData(std::vector<T>& buffer,
int minCol,
int maxCol,
double row,
int band, RESAMPLE resample)
166 std::vector<T> readBuffer_upper;
167 std::vector<T> readBuffer_lower;
168 double upperRow, lowerRow;
169 if(buffer.size()!=maxCol-minCol+1)
170 buffer.resize(maxCol-minCol+1);
179 upperRow=
static_cast<int>(row-0.5+eps);
184 lowerRow=upperRow+1.0;
188 readData(readBuffer_upper,minCol,maxCol,static_cast<int>(upperRow),band);
189 readData(readBuffer_lower,minCol,maxCol,static_cast<int>(lowerRow),band);
191 for(
int icol=0;icol<maxCol-minCol+1;++icol){
193 if(!
isNoData(readBuffer_upper[icol])){
194 if(!
isNoData(readBuffer_lower[icol])){
195 buffer[icol]=(lowerRow-row+0.5)*readBuffer_upper[icol]+(1-lowerRow+row-0.5)*readBuffer_lower[icol];
198 buffer[icol]=readBuffer_upper[icol];
202 buffer[icol]=readBuffer_lower[icol];
207 readData(buffer,minCol,maxCol,static_cast<int>(row),band);
222 buffer2d.resize(maxRow-minRow+1);
223 typename std::vector<T> buffer;
225 typename std::vector<T>::const_iterator startit=buffer.begin();
226 typename std::vector<T>::const_iterator endit=startit;
227 for(
int irow=minRow;irow<=maxRow;++irow){
228 buffer2d[irow-minRow].resize(maxCol-minCol+1);
229 endit+=maxCol-minCol+1;
230 buffer2d[irow-minRow].assign(startit,endit);
231 startit+=maxCol-minCol+1;
259 std::string errorString=
"block not within image boundaries";
262 if(buffer.size()!=(maxRow-minRow+1)*(maxCol-minCol+1))
263 buffer.resize((maxRow-minRow+1)*(maxCol-minCol+1));
265 GDALRasterBand *poBand;
267 poBand =
m_gds->GetRasterBand(band+1);
268 poBand->RasterIO(GF_Read,minCol,minRow,maxCol-minCol+1,maxRow-minRow+1,&(buffer[0]),(maxCol-minCol+1),(maxRow-minRow+1),getGDALDataType<T>(),0,0);
270 for(
int index=0;index<buffer.size();++index)
271 buffer[index]=theScale*buffer[index]+theOffset;
296 #endif // _IMGREADERGDAL_H_
~ImgReaderGdal(void)
destructor
double getMin(int &col, int &row, int band=0)
Get the minimum cell values for a specific band and report the column and row in which the minimum va...
unsigned long int getNvalid(int band)
Calculate the number of valid pixels (with a value not defined as no data).
bool isNoData(double value) const
Check if value is nodata in this dataset.
int nrOfBand(void) const
Get the number of bands of this dataset.
ImgReaderGdal(const std::string &filename, const GDALAccess &readMode=GA_ReadOnly)
constructor opening an image. Set memory (in MB) to cache a number of rows in memory ...
double getHistogram(std::vector< double > &histvector, double &min, double &max, unsigned int &nbin, int theBand=0, bool kde=false)
Calculate the image histogram for a specific band using a defined number of bins and constrained by a...
void close(void)
Set the memory (in MB) to cache a number of rows in memory.
int nrOfRow(void) const
Get the number of rows of this dataset.
GDALDataset * m_gds
instance of the GDAL dataset of this dataset
void readData(T &value, int col, int row, int band=0)
Read a single pixel cell value at a specific column and row for a specific band (all indices start co...
void getRange(std::vector< short > &range, int Band=0)
Calculate the range of cell values in the image for a specific band (start counting from 0)...
void setCodec(const GDALAccess &readMode=GA_ReadOnly)
Set GDAL dataset number of columns, rows, bands and geotransform.
std::vector< double > m_scale
Vector containing the scale factor to be applied (one scale value for each band)
void getMinMax(int startCol, int endCol, int startRow, int endRow, int band, double &minValue, double &maxValue)
Get the minimum and maximum cell values for a specific band in a region of interest defined by startC...
ImgReaderGdal(void)
default constructor. Image needs to be opened later with one of the open methods. ...
double getMax(int &col, int &row, int band=0)
Get the maximum cell values for a specific band and report the column and row in which the maximum va...
void getRefPix(double &refX, double &refY, int band=0)
Calculate the reference pixel as the centre of gravity pixel (weighted average of all values not taki...
void readDataBlock(Vector2d< T > &buffer2d, int minCol, int maxCol, int minRow, int maxRow, int band=0)
Read pixel cell values for a range of columns and rows for a specific band (all indices start countin...
void open(const std::string &filename, const GDALAccess &readMode=GA_ReadOnly)
Open an image.
std::vector< double > m_offset
Vector containing the offset factor to be applied (one offset value for each band) ...
int nrOfCol(void) const
Get the number of columns of this dataset.