pktools 2.6.7
Processing Kernel for geospatial data
Public Member Functions | Protected Member Functions | List of all members
ImgReaderGdal Class Reference

#include <ImgReaderGdal.h>

Inheritance diagram for ImgReaderGdal:
Inheritance graph
[legend]
Collaboration diagram for ImgReaderGdal:
Collaboration graph
[legend]

Public Member Functions

 ImgReaderGdal (void)
 default constructor. Image needs to be opened later with one of the open methods. More...
 
 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 More...
 
 ~ImgReaderGdal (void)
 destructor More...
 
void open (const std::string &filename, const GDALAccess &readMode=GA_ReadOnly)
 Open an image. More...
 
void close (void)
 Set the memory (in MB) to cache a number of rows in memory. More...
 
template<typename T >
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 counting from 0) More...
 
template<typename T >
void readData (std::vector< T > &buffer, int minCol, int maxCol, int row, int band=0)
 Read pixel cell values for a range of columns for a specific row and band (all indices start counting from 0) More...
 
template<typename T >
void readData (std::vector< T > &buffer, int minCol, int maxCol, double row, int band=0, RESAMPLE resample=NEAR)
 Read pixel cell values for a range of columns for a specific row and band (all indices start counting from 0). The row counter can be floating, in which case a resampling is applied at the row level. You still must apply the resampling at column level. This function will be deprecated, as the GDAL API now supports rasterIO resampling (see http://www.gdal.org/structGDALRasterIOExtraArg.html) More...
 
template<typename T >
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 counting from 0). The buffer is a two dimensional vector (stl vector of stl vector) representing [row][col]. More...
 
template<typename T >
void readDataBlock (std::vector< T > &buffer, 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 counting from 0). The buffer is a one dimensional stl vector representing all pixel values read starting from upper left to lower right. More...
 
template<typename T >
void readData (std::vector< T > &buffer, int row, int band=0)
 Read pixel cell values for an entire row for a specific band (all indices start counting from 0) More...
 
template<typename T >
void readData (std::vector< T > &buffer, double row, int band=0, RESAMPLE resample=NEAR)
 Read pixel cell values for an entire row for a specific band (all indices start counting from 0). The row counter can be floating, in which case a resampling is applied at the row level. You still must apply the resampling at column level. This function will be deprecated, as the GDAL API now supports rasterIO resampling (see http://www.gdal.org/structGDALRasterIOExtraArg.html) More...
 
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 startCol, endCol, startRow and endRow (all indices start counting from 0). More...
 
void getMinMax (double &minValue, double &maxValue, int band=0)
 Get the minimum and maximum cell values for a specific band (all indices start counting from 0). More...
 
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 value was found (all indices start counting from 0). More...
 
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 value was found (all indices start counting from 0). More...
 
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 minimum and maximum value. A kernel density function can also be applied (default is false). More...
 
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 taking into account no data values) for a specific band (start counting from 0). More...
 
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). More...
 
unsigned long int getNvalid (int band)
 Calculate the number of valid pixels (with a value not defined as no data). More...
 
- Public Member Functions inherited from ImgRasterGdal
 ImgRasterGdal (void)
 default constructor More...
 
virtual ~ImgRasterGdal (void)
 destructor More...
 
void setScale (double theScale, int band=0)
 Set scale for a specific band when writing the raster data values. The scaling and offset are applied on a per band basis. You need to set the scale for each band. More...
 
void setOffset (double theOffset, int band=0)
 Set offset for a specific band when writing the raster data values. The scaling and offset are applied on a per band basis. You need to set the offset for each band. More...
 
virtual void close (void)
 Close the image. More...
 
std::string getFileName () const
 Get the filename of this dataset. More...
 
int nrOfCol (void) const
 Get the number of columns of this dataset. More...
 
int nrOfRow (void) const
 Get the number of rows of this dataset. More...
 
int nrOfBand (void) const
 Get the number of bands of this dataset. More...
 
bool isGeoRef () const
 Is this dataset georeferenced (pixel size in y must be negative) ? More...
 
std::string getProjection (void) const
 Get the projection string (deprecated, use getProjectionRef instead) More...
 
std::string getProjectionRef (void) const
 Get the projection reference. More...
 
std::string getGeoTransform () const
 Get the geotransform data for this dataset as a string. More...
 
void getGeoTransform (double *gt) const
 Get the geotransform data for this dataset. More...
 
CPLErr setGeoTransform (double *gt)
 Set the geotransform data for this dataset. More...
 
void copyGeoTransform (const ImgRasterGdal &imgSrc)
 Copy geotransform information from another georeferenced image. More...
 
CPLErr setProjection (const std::string &projection)
 Set the projection for this dataset in well known text (wkt) format. More...
 
CPLErr setProjectionProj4 (const std::string &projection)
 Set the projection for this dataset from user input (supports epsg:<number> format) More...
 
bool getBoundingBox (double &ulx, double &uly, double &lrx, double &lry) const
 Get the bounding box of this dataset in georeferenced coordinates. More...
 
bool getCenterPos (double &x, double &y) const
 Get the center position of this dataset in georeferenced coordinates. More...
 
double getUlx () const
 Get the upper left corner x (georeferenced) coordinate of this dataset. More...
 
double getUly () const
 Get the upper left corner y (georeferenced) coordinate of this dataset. More...
 
double getLrx () const
 Get the lower right corner x (georeferenced) coordinate of this dataset. More...
 
double getLry () const
 Get the lower right corner y (georeferenced) coordinate of this dataset. More...
 
int getNoDataValues (std::vector< double > &noDataValues) const
 Get the no data values of this dataset as a standard template library (stl) vector. More...
 
bool isNoData (double value) const
 Check if value is nodata in this dataset. More...
 
int pushNoDataValue (double noDataValue)
 Push a no data value for this dataset. More...
 
int setNoData (const std::vector< double > nodata)
 Set the no data values of this dataset using a standard template library (stl) vector as input. More...
 
CPLErr GDALSetNoDataValue (double noDataValue, int band=0)
 Set the GDAL (internal) no data value for this data set. Only a single no data value per band is supported. More...
 
bool covers (double x, double y) const
 Check if a geolocation is covered by this dataset. Only the bounding box is checked, irrespective of no data values. More...
 
bool covers (double ulx, double uly, double lrx, double lry) const
 Check if a region of interest is (partially) covered by this dataset. Only the bounding box is checked, irrespective of no data values. More...
 
bool geo2image (double x, double y, double &i, double &j) const
 Convert georeferenced coordinates (x and y) to image coordinates (column and row) More...
 
bool image2geo (double i, double j, double &x, double &y) const
 Convert image coordinates (column and row) to georeferenced coordinates (x and y) More...
 
double getDeltaX (void) const
 Get the pixel cell spacing in x. More...
 
double getDeltaY (void) const
 Get the pixel cell spacing in y. More...
 
GDALDataType getDataType (int band=0) const
 Get the GDAL datatype for this dataset. More...
 
GDALRasterBand * getRasterBand (int band=0) const
 Get the GDAL rasterband for this dataset. More...
 
GDALColorTable * getColorTable (int band=0) const
 Get the GDAL color table for this dataset as an instance of the GDALColorTable class. More...
 
std::string getDriverDescription () const
 Get the GDAL driver description of this dataset. More...
 
std::string getImageType () const
 Get the image type (implemented as the driver description) More...
 
std::string getInterleave () const
 Get the band coding (interleave) More...
 
std::string getCompression () const
 Get the compression from the metadata of this dataset. More...
 
GDALDataset * getDataset ()
 
char ** getMetadata ()
 Get the metadata of this dataset. More...
 
char ** getMetadata () const
 Get the metadata of this dataset (const version) More...
 
void getMetadata (std::list< std::string > &metadata) const
 Get the metadata of this dataset in the form of a list of strings (const version) More...
 
std::string getDescription () const
 Get the image description from the driver of this dataset. More...
 
std::string getMetadataItem () const
 Get metadata item of this dataset. More...
 
std::string getImageDescription () const
 Get the image description from the metadata of this dataset. More...
 
int nrOfBlockX (int band=0)
 
int nrOfBlockY (int band=0)
 

Protected Member Functions

void setCodec (const GDALAccess &readMode=GA_ReadOnly)
 Set GDAL dataset number of columns, rows, bands and geotransform. More...
 

Additional Inherited Members

- Protected Attributes inherited from ImgRasterGdal
std::string m_filename
 filename of this dataset More...
 
GDALDataset * m_gds
 instance of the GDAL dataset of this dataset More...
 
int m_ncol
 number of columns in this dataset More...
 
int m_nrow
 number of rows in this dataset More...
 
int m_nband
 number of bands in this dataset More...
 
GDALDataType m_dataType
 GDAL data type for this dataset. More...
 
double m_gt [6]
 geotransform information of this dataset More...
 
std::string m_projection
 
std::vector< double > m_noDataValues
 no data values for this dataset More...
 
std::vector< double > m_scale
 Vector containing the scale factor to be applied (one scale value for each band) More...
 
std::vector< double > m_offset
 Vector containing the offset factor to be applied (one offset value for each band) More...
 

Detailed Description

Class to read a raster dataset in a format supported by GDAL. Data are cached in memory for a number of rows (if memory>0) before read from file.

This class inherits from ImgRasterGdal, a general raster class to store e.g., filename, number of columns, rows and bands of the dataset.

Definition at line 36 of file ImgReaderGdal.h.

Constructor & Destructor Documentation

◆ ImgReaderGdal() [1/2]

ImgReaderGdal::ImgReaderGdal ( void  )

default constructor. Image needs to be opened later with one of the open methods.

Definition at line 27 of file ImgReaderGdal.cc.

27{};

◆ ImgReaderGdal() [2/2]

ImgReaderGdal::ImgReaderGdal ( const std::string &  filename,
const GDALAccess &  readMode = GA_ReadOnly 
)
inline

constructor opening an image. Set memory (in MB) to cache a number of rows in memory

Definition at line 42 of file ImgReaderGdal.h.

42{open(filename, readMode);};
void open(const std::string &filename, const GDALAccess &readMode=GA_ReadOnly)
Open an image.

◆ ~ImgReaderGdal()

ImgReaderGdal::~ImgReaderGdal ( void  )

destructor

Definition at line 29 of file ImgReaderGdal.cc.

29{};

Member Function Documentation

◆ close()

void ImgReaderGdal::close ( void  )
virtual

Set the memory (in MB) to cache a number of rows in memory.

Close the image.

Reimplemented from ImgRasterGdal.

Reimplemented in ImgUpdaterGdal.

Definition at line 42 of file ImgReaderGdal.cc.

43{
45}
virtual void close(void)
Close the image.

◆ getHistogram()

double ImgReaderGdal::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 minimum and maximum value. A kernel density function can also be applied (default is false).

Parameters
histvectorThe reported histogram with counts per bin
min,maxOnly calculate histogram for values between min and max. If min>=max, calculate min and max from the image
nbinNumber of bins used for calculating the histogram. If nbin is 0, the number of bins is automatically calculated from min and max
theBandThe band for which to calculate the histogram (start counting from 0)
kdeApply kernel density function for a Gaussian basis function
Returns
number of valid pixels in this dataset for the the selected band

Definition at line 257 of file ImgReaderGdal.cc.

257 {
258 double minValue=0;
259 double maxValue=0;
260
261 if(min>=max)
262 getMinMax(minValue,maxValue,theBand);
263 else{
264 minValue=min;
265 maxValue=max;
266 }
267 if(min<max&&min>minValue)
268 minValue=min;
269 if(min<max&&max<maxValue)
270 maxValue=max;
271 min=minValue;
272 max=maxValue;
273
274 double sigma=0;
275 if(kde){
276 double meanValue=0;
277 double stdDev=0;
278 GDALProgressFunc pfnProgress;
279 void* pProgressData;
280 GDALRasterBand* rasterBand;
281 rasterBand=getRasterBand(theBand);
282 rasterBand->ComputeStatistics(0,&minValue,&maxValue,&meanValue,&stdDev,pfnProgress,pProgressData);
283 //rest minvalue and MaxValue as ComputeStatistics does not account for nodata, scale and offset
284 minValue=min;
285 maxValue=max;
286
287 if(m_scale.size()>theBand){
288 stdDev*=m_scale[theBand];
289 }
290 sigma=1.06*stdDev*pow(getNvalid(theBand),-0.2);
291 }
292
293 double scale=0;
294 if(maxValue>minValue){
295 if(nbin==0)
296 nbin=maxValue-minValue+1;
297 scale=static_cast<double>(nbin-1)/(maxValue-minValue);
298 }
299 else
300 nbin=1;
301 assert(nbin>0);
302 if(histvector.size()!=nbin){
303 histvector.resize(nbin);
304 for(int i=0;i<nbin;histvector[i++]=0);
305 }
306 double nvalid=0;
307 unsigned long int nsample=0;
308 unsigned long int ninvalid=0;
309 std::vector<double> lineBuffer(nrOfCol());
310 for(int irow=0;irow<nrOfRow();++irow){
311 readData(lineBuffer,irow,theBand);
312 for(int icol=0;icol<nrOfCol();++icol){
313 if(isNoData(lineBuffer[icol]))
314 ++ninvalid;
315 else if(lineBuffer[icol]>maxValue)
316 ++ninvalid;
317 else if(lineBuffer[icol]<minValue)
318 ++ninvalid;
319 else if(nbin==1)
320 ++histvector[0];
321 else{//scale to [0:nbin]
322 if(sigma>0){
323 //create kde for Gaussian basis function
324 //todo: speed up by calculating first and last bin with non-zero contriubtion...
325 //todo: calculate real surface below pdf by using gsl_cdf_gaussian_P(x-mean+binsize,sigma)-gsl_cdf_gaussian_P(x-mean,sigma)
326 for(int ibin=0;ibin<nbin;++ibin){
327 double icenter=minValue+static_cast<double>(maxValue-minValue)*(ibin+0.5)/nbin;
328 double thePdf=gsl_ran_gaussian_pdf(lineBuffer[icol]-icenter, sigma);
329 histvector[ibin]+=thePdf;
330 nvalid+=thePdf;
331 }
332 }
333 else{
334 int theBin=static_cast<unsigned long int>(scale*(lineBuffer[icol]-minValue));
335 assert(theBin>=0);
336 assert(theBin<nbin);
337 ++histvector[theBin];
338 ++nvalid;
339 }
340 // else if(lineBuffer[icol]==maxValue)
341 // ++histvector[nbin-1];
342 // else
343 // ++histvector[static_cast<int>(static_cast<double>(lineBuffer[icol]-minValue)/(maxValue-minValue)*(nbin-1))];
344 }
345 }
346 }
347 // unsigned long int nvalid=nrOfCol()*nrOfRow()-ninvalid;
348 return nvalid;
349}
int nrOfRow(void) const
Get the number of rows of this dataset.
bool isNoData(double value) const
Check if value is nodata in this dataset.
int nrOfCol(void) const
Get the number of columns of this dataset.
Definition: ImgRasterGdal.h:98
GDALRasterBand * getRasterBand(int band=0) const
Get the GDAL rasterband for this dataset.
std::vector< double > m_scale
Vector containing the scale factor to be applied (one scale value for each band)
unsigned long int getNvalid(int band)
Calculate the number of valid pixels (with a value not defined as no data).
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...
Definition: ImgReaderGdal.h:95
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...

◆ getMax()

double ImgReaderGdal::getMax ( int &  x,
int &  y,
int  band = 0 
)

Get the maximum cell values for a specific band and report the column and row in which the maximum value was found (all indices start counting from 0).

Parameters
xReported column where maximum value in image was found (start counting from 0)
yReported row where maximum value in image was found (start counting from 0)
bandSearch mininum value in image for this band
Returns
maximum value in image for the selected band

Definition at line 125 of file ImgReaderGdal.cc.

125 {
126 double maxValue=0;
127 std::vector<double> lineBuffer(nrOfCol());
128 bool isValid=false;
129 for(int irow=0;irow<nrOfRow();++irow){
130 readData(lineBuffer,irow,band);
131 for(int icol=0;icol<nrOfCol();++icol){
132 if(isNoData(lineBuffer[icol]))
133 continue;
134 if(isValid){
135 if(lineBuffer[icol]>maxValue){
136 y=irow;
137 x=icol;
138 maxValue=lineBuffer[icol];
139 }
140 }
141 else{
142 y=irow;
143 x=icol;
144 maxValue=lineBuffer[icol];
145 isValid=true;
146 }
147 }
148 }
149 if(isValid)
150 return maxValue;
151 else
152 throw(static_cast<std::string>("Warning: not initialized"));
153}

◆ getMin()

double ImgReaderGdal::getMin ( int &  x,
int &  y,
int  band = 0 
)

Get the minimum cell values for a specific band and report the column and row in which the minimum value was found (all indices start counting from 0).

Parameters
xReported column where minimum value in image was found (start counting from 0)
yReported row where minimum value in image was found (start counting from 0)
bandSearch mininum value in image for this band
Returns
minimum value in image for the selected band

Definition at line 89 of file ImgReaderGdal.cc.

89 {
90 double minValue=0;
91 std::vector<double> lineBuffer(nrOfCol());
92 bool isValid=false;
93 for(int irow=0;irow<nrOfRow();++irow){
94 readData(lineBuffer,irow,band);
95 for(int icol=0;icol<nrOfCol();++icol){
96 if(isNoData(lineBuffer[icol]))
97 continue;
98 if(isValid){
99 if(lineBuffer[icol]<minValue){
100 y=irow;
101 x=icol;
102 minValue=lineBuffer[icol];
103 }
104 }
105 else{
106 y=irow;
107 x=icol;
108 minValue=lineBuffer[icol];
109 isValid=true;
110 }
111 }
112 }
113 if(isValid)
114 return minValue;
115 else
116 throw(static_cast<std::string>("Warning: not initialized"));
117}

◆ getMinMax() [1/2]

void ImgReaderGdal::getMinMax ( double &  minValue,
double &  maxValue,
int  band = 0 
)

Get the minimum and maximum cell values for a specific band (all indices start counting from 0).

Parameters
minValueReported minimum value in image
maxValueReported maximum value in image
bandSearch extreme value in image for this band

Definition at line 208 of file ImgReaderGdal.cc.

209{
210 bool isConstraint=(maxValue>minValue);
211 double minConstraint=minValue;
212 double maxConstraint=maxValue;
213 std::vector<double> lineBuffer(nrOfCol());
214 bool isValid=false;
215 for(int irow=0;irow<nrOfRow();++irow){
216 readData(lineBuffer,irow,band);
217 for(int icol=0;icol<nrOfCol();++icol){
218 if(isNoData(lineBuffer[icol]))
219 continue;
220 if(isValid){
221 if(isConstraint){
222 if(lineBuffer[icol]<minConstraint)
223 continue;
224 if(lineBuffer[icol]>maxConstraint)
225 continue;
226 }
227 if(lineBuffer[icol]<minValue)
228 minValue=lineBuffer[icol];
229 if(lineBuffer[icol]>maxValue)
230 maxValue=lineBuffer[icol];
231 }
232 else{
233 if(isConstraint){
234 if(lineBuffer[icol]<minConstraint)
235 continue;
236 if(lineBuffer[icol]>maxConstraint)
237 continue;
238 }
239 minValue=lineBuffer[icol];
240 maxValue=lineBuffer[icol];
241 isValid=true;
242 }
243 }
244 }
245 if(!isValid)
246 throw(static_cast<std::string>("Warning: not initialized"));
247}

◆ getMinMax() [2/2]

void ImgReaderGdal::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 startCol, endCol, startRow and endRow (all indices start counting from 0).

Parameters
startCol,endCol,startRow,endRowSearch extreme value in this region of interest (all indices start counting from 0)
bandSearch extreme value in image for this band
minValueReported minimum value within searched region
maxValueReported maximum value within searched region

Definition at line 161 of file ImgReaderGdal.cc.

162{
163 bool isConstraint=(maxValue>minValue);
164 double minConstraint=minValue;
165 double maxConstraint=maxValue;
166 std::vector<double> lineBuffer(endCol-startCol+1);
167 bool isValid=false;
168 assert(endRow<nrOfRow());
169 for(int irow=startCol;irow<endRow+1;++irow){
170 readData(lineBuffer,startCol,endCol,irow,band);
171 for(int icol=0;icol<lineBuffer.size();++icol){
172 if(isNoData(lineBuffer[icol]))
173 continue;
174 if(isValid){
175 if(isConstraint){
176 if(lineBuffer[icol]<minConstraint)
177 continue;
178 if(lineBuffer[icol]>maxConstraint)
179 continue;
180 }
181 if(lineBuffer[icol]<minValue)
182 minValue=lineBuffer[icol];
183 if(lineBuffer[icol]>maxValue)
184 maxValue=lineBuffer[icol];
185 }
186 else{
187 if(isConstraint){
188 if(lineBuffer[icol]<minConstraint)
189 continue;
190 if(lineBuffer[icol]>maxConstraint)
191 continue;
192 }
193 minValue=lineBuffer[icol];
194 maxValue=lineBuffer[icol];
195 isValid=true;
196 }
197 }
198 }
199 if(!isValid)
200 throw(static_cast<std::string>("Warning: not initialized"));
201}

◆ getNvalid()

unsigned long int ImgReaderGdal::getNvalid ( int  band)

Calculate the number of valid pixels (with a value not defined as no data).

Parameters
bandThe band for which to calculate the number of valid pixels
Returns
number of valid pixels in this dataset for the the selected band

Definition at line 373 of file ImgReaderGdal.cc.

374{
375 unsigned long int nvalid=0;
376 if(m_noDataValues.size()){
377 std::vector<double> lineBuffer(nrOfCol());
378 for(int irow=0;irow<nrOfRow();++irow){
379 readData(lineBuffer,irow,band);
380 for(int icol=0;icol<nrOfCol();++icol){
381 if(isNoData(lineBuffer[icol]))
382 continue;
383 else
384 ++nvalid;
385 }
386 }
387 return nvalid;
388 }
389 else
390 return(nrOfCol()*nrOfRow());
391}
std::vector< double > m_noDataValues
no data values for this dataset

◆ getRange()

void ImgReaderGdal::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).

Parameters
rangeSorted vector containing the range of image values
bandThe band for which to calculate the range

Definition at line 355 of file ImgReaderGdal.cc.

356{
357 std::vector<short> lineBuffer(nrOfCol());
358 range.clear();
359 for(int irow=0;irow<nrOfRow();++irow){
360 readData(lineBuffer,irow,band);
361 for(int icol=0;icol<nrOfCol();++icol){
362 if(find(range.begin(),range.end(),lineBuffer[icol])==range.end())
363 range.push_back(lineBuffer[icol]);
364 }
365 }
366 sort(range.begin(),range.end());
367}

◆ getRefPix()

void ImgReaderGdal::getRefPix ( double &  refX,
double &  refY,
int  band = 0 
)

Calculate the reference pixel as the centre of gravity pixel (weighted average of all values not taking into account no data values) for a specific band (start counting from 0).

Parameters
refX,refYCalculated reference pixel position in geo-refererenced coordinates
bandThe band for which to calculate the number of valid pixels

Definition at line 398 of file ImgReaderGdal.cc.

399{
400 std::vector<double> lineBuffer(nrOfCol());
401 double validCol=0;
402 double validRow=0;
403 int nvalidCol=0;
404 int nvalidRow=0;
405 for(int irow=0;irow<nrOfRow();++irow){
406 readData(lineBuffer,irow,band);
407 for(int icol=0;icol<nrOfCol();++icol){
408 // bool valid=(find(m_noDataValues.begin(),m_noDataValues.end(),lineBuffer[icol])==m_noDataValues.end());
409 // if(valid){
410 if(!isNoData(lineBuffer[icol])){
411 validCol+=icol+1;
412 ++nvalidCol;
413 validRow+=irow+1;
414 ++nvalidRow;
415 }
416 }
417 }
418 if(isGeoRef()){
419 //reference coordinate is lower left corner of pixel in center of gravity
420 //we need geo coordinates for exactly this location: validCol(Row)/nvalidCol(Row)-0.5
421 double cgravi=validCol/nvalidCol-0.5;
422 double cgravj=validRow/nvalidRow-0.5;
423 double refpixeli=floor(cgravi);
424 double refpixelj=ceil(cgravj-1);
425 //but image2geo provides location at center of pixel (shifted half pixel right down)
426 image2geo(refpixeli,refpixelj,refX,refY);
427 //refX and refY now refer to center of gravity pixel
428 refX-=0.5*getDeltaX();//shift to left corner
429 refY-=0.5*getDeltaY();//shift to lower left corner
430 }
431 else{
432 refX=floor(validCol/nvalidCol-0.5);//left corner
433 refY=floor(validRow/nvalidRow-0.5);//upper corner
434 //shift to lower left corner of pixel
435 refY+=1;
436 }
437}
double getDeltaY(void) const
Get the pixel cell spacing in y.
bool isGeoRef() const
Is this dataset georeferenced (pixel size in y must be negative) ?
double getDeltaX(void) const
Get the pixel cell spacing in x.
bool image2geo(double i, double j, double &x, double &y) const
Convert image coordinates (column and row) to georeferenced coordinates (x and y)

◆ open()

void ImgReaderGdal::open ( const std::string &  filename,
const GDALAccess &  readMode = GA_ReadOnly 
)

Open an image.

Parameters
filenameOpen a raster dataset with this filename
readModeOpen dataset in ReadOnly or Update mode
memoryAvailable memory to cache image raster data (in MB)

Definition at line 36 of file ImgReaderGdal.cc.

37{
38 m_filename = filename;
39 setCodec(readMode);
40}
std::string m_filename
filename of this dataset
void setCodec(const GDALAccess &readMode=GA_ReadOnly)
Set GDAL dataset number of columns, rows, bands and geotransform.

◆ readData() [1/5]

template<typename T >
void ImgReaderGdal::readData ( std::vector< T > &  buffer,
double  row,
int  band = 0,
RESAMPLE  resample = NEAR 
)

Read pixel cell values for an entire row for a specific band (all indices start counting from 0). The row counter can be floating, in which case a resampling is applied at the row level. You still must apply the resampling at column level. This function will be deprecated, as the GDAL API now supports rasterIO resampling (see http://www.gdal.org/structGDALRasterIOExtraArg.html)

Parameters
[out]bufferThe vector with all cell values that were read
[in]rowThe row number to read (counting starts from 0)
[in]bandThe band number to read (counting starts from 0)
[in]resampleThe resampling method (currently only BILINEAR and NEAR are supported).

Definition at line 291 of file ImgReaderGdal.h.

292{
293 readData(buffer,0,nrOfCol()-1,row,band,resample);
294}

◆ readData() [2/5]

template<typename T >
void ImgReaderGdal::readData ( std::vector< T > &  buffer,
int  minCol,
int  maxCol,
double  row,
int  band = 0,
RESAMPLE  resample = NEAR 
)

Read pixel cell values for a range of columns for a specific row and band (all indices start counting from 0). The row counter can be floating, in which case a resampling is applied at the row level. You still must apply the resampling at column level. This function will be deprecated, as the GDAL API now supports rasterIO resampling (see http://www.gdal.org/structGDALRasterIOExtraArg.html)

Parameters
[out]bufferThe vector with all cell values that were read
[in]minColFirst column from where to start reading (counting starts from 0)
[in]maxColLast column that must be read (counting starts from 0)
[in]rowThe row number to read (counting starts from 0)
[in]bandThe band number to read (counting starts from 0)
[in]resampleThe resampling method (currently only BILINEAR and NEAR are supported)

Definition at line 163 of file ImgReaderGdal.h.

164{
165 double eps=0.00001;
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);
171 /* double upperRow=row-0.5; */
172 /* upperRow=static_cast<int>(upperRow); */
173 /* double lowerRow=row+0.5; */
174 /* lowerRow=static_cast<int>(lowerRow); */
175 switch(resample){
176 case(BILINEAR):
177 /* if(lowerRow>=nrOfRow()) */
178 /* lowerRow=nrOfRow()-1; */
179 upperRow=static_cast<int>(row-0.5+eps);
180 if(upperRow<0)
181 upperRow=0;
182 if(upperRow>=nrOfRow())
183 upperRow=nrOfRow()-1;
184 lowerRow=upperRow+1.0;
185 if(lowerRow>=nrOfRow())
186 lowerRow=nrOfRow()-1;
187
188 readData(readBuffer_upper,minCol,maxCol,static_cast<int>(upperRow),band);
189 readData(readBuffer_lower,minCol,maxCol,static_cast<int>(lowerRow),band);
190 //do interpolation in y
191 for(int icol=0;icol<maxCol-minCol+1;++icol){
192 /* buffer[icol]=(lowerRow-row+0.5)*readBuffer_upper[icol]+(1-lowerRow+row-0.5)*readBuffer_lower[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];
196 }
197 else{
198 buffer[icol]=readBuffer_upper[icol];
199 }
200 }
201 else{
202 buffer[icol]=readBuffer_lower[icol];
203 }
204 }
205 break;
206 default:
207 readData(buffer,minCol,maxCol,static_cast<int>(row),band);
208 break;
209 }
210}

◆ readData() [3/5]

template<typename T >
void ImgReaderGdal::readData ( std::vector< T > &  buffer,
int  minCol,
int  maxCol,
int  row,
int  band = 0 
)

Read pixel cell values for a range of columns for a specific row and band (all indices start counting from 0)

Parameters
[out]bufferThe vector with all cell values that were read
[in]minColFirst column from where to start reading (counting starts from 0)
[in]maxColLast column that must be read (counting starts from 0)
[in]rowThe row number to read (counting starts from 0)
[in]bandThe band number to read (counting starts from 0)

Definition at line 126 of file ImgReaderGdal.h.

127{
128 assert(band<nrOfBand()+1);
129 assert(minCol<nrOfCol());
130 assert(minCol>=0);
131 assert(maxCol<nrOfCol());
132 assert(minCol<=maxCol);
133 assert(row<nrOfRow());
134 assert(row>=0);
135 double theScale=1;
136 double theOffset=0;
137 if(m_scale.size()>band||m_offset.size()>band){
138 if(m_scale.size()>band)
139 theScale=m_scale[band];
140 if(m_offset.size()>band)
141 theOffset=m_offset[band];
142 }
143 //fetch raster band
144 GDALRasterBand *poBand;
145 poBand = m_gds->GetRasterBand(band+1);//GDAL uses 1 based index
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);
149 if(m_scale.size()>band||m_offset.size()>band){
150 for(int index=0;index<buffer.size();++index)
151 buffer[index]=theScale*static_cast<double>(buffer[index])+theOffset;
152 }
153}
GDALDataset * m_gds
instance of the GDAL dataset of this dataset
int nrOfBand(void) const
Get the number of bands of this dataset.
std::vector< double > m_offset
Vector containing the offset factor to be applied (one offset value for each band)

◆ readData() [4/5]

template<typename T >
void ImgReaderGdal::readData ( std::vector< T > &  buffer,
int  row,
int  band = 0 
)

Read pixel cell values for an entire row for a specific band (all indices start counting from 0)

Parameters
[out]bufferThe vector with all cell values that were read
[in]rowThe row number to read (counting starts from 0)
[in]bandThe band number to read (counting starts from 0)

Definition at line 280 of file ImgReaderGdal.h.

281{
282 readData(buffer,0,nrOfCol()-1,row,band);
283}

◆ readData() [5/5]

template<typename T >
void ImgReaderGdal::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 counting from 0)

Parameters
[out]valueThe cell value that was read
[in]colThe column number to read (counting starts from 0)
[in]rowThe row number to read (counting starts from 0)
[in]bandThe band number to read (counting starts from 0)

Definition at line 95 of file ImgReaderGdal.h.

96{
97 assert(band<nrOfBand()+1);
98 assert(col<nrOfCol());
99 assert(col>=0);
100 assert(row<nrOfRow());
101 assert(row>=0);
102 double dvalue=0;
103 double theScale=1;
104 double theOffset=0;
105 if(m_scale.size()>band||m_offset.size()>band){
106 if(m_scale.size()>band)
107 theScale=m_scale[band];
108 if(m_offset.size()>band)
109 theOffset=m_offset[band];
110 }
111 //fetch raster band
112 GDALRasterBand *poBand;
113 poBand = m_gds->GetRasterBand(band+1);//GDAL uses 1 based index
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);
117}

◆ readDataBlock() [1/2]

template<typename T >
void ImgReaderGdal::readDataBlock ( std::vector< T > &  buffer,
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 counting from 0). The buffer is a one dimensional stl vector representing all pixel values read starting from upper left to lower right.

Parameters
[out]bufferOne dimensional vector representing all pixel values read starting from upper left to lower right.
[in]minColFirst column from where to start reading (counting starts from 0)
[in]maxColLast column that must be read (counting starts from 0)
[in]minRowFirst row from where to start reading (counting starts from 0)
[in]maxRowLast row that must be read (counting starts from 0)
[in]bandThe band number to read (counting starts from 0)

Definition at line 243 of file ImgReaderGdal.h.

244{
245 double theScale=1;
246 double theOffset=0;
247 if(m_scale.size()>band)
248 theScale=m_scale[band];
249 if(m_offset.size()>band)
250 theOffset=m_offset[band];
251 if(minCol>=nrOfCol() ||
252 (minCol<0) ||
253 (maxCol>=nrOfCol()) ||
254 (minCol>maxCol) ||
255 (minRow>=nrOfRow()) ||
256 (minRow<0) ||
257 (maxRow>=nrOfRow()) ||
258 (minRow>maxRow)){
259 std::string errorString="block not within image boundaries";
260 throw(errorString);
261 }
262 if(buffer.size()!=(maxRow-minRow+1)*(maxCol-minCol+1))
263 buffer.resize((maxRow-minRow+1)*(maxCol-minCol+1));
264 //fetch raster band
265 GDALRasterBand *poBand;
266 assert(band<nrOfBand()+1);
267 poBand = m_gds->GetRasterBand(band+1);//GDAL uses 1 based index
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);
269 if(m_scale.size()>band||m_offset.size()>band){
270 for(int index=0;index<buffer.size();++index)
271 buffer[index]=theScale*buffer[index]+theOffset;
272 }
273}

◆ readDataBlock() [2/2]

template<typename T >
void ImgReaderGdal::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 counting from 0). The buffer is a two dimensional vector (stl vector of stl vector) representing [row][col].

Parameters
[out]buffer2dTwo dimensional vector of type Vector2d (stl vector of stl vector) representing [row][col]. This vector contains all cell values that were read
[in]minColFirst column from where to start reading (counting starts from 0)
[in]maxColLast column that must be read (counting starts from 0)
[in]minRowFirst row from where to start reading (counting starts from 0)
[in]maxRowLast row that must be read (counting starts from 0)
[in]bandThe band number to read (counting starts from 0)

Definition at line 220 of file ImgReaderGdal.h.

221{
222 buffer2d.resize(maxRow-minRow+1);
223 typename std::vector<T> buffer;
224 readDataBlock(buffer,minCol,maxCol,minRow,maxRow,band);
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;
232 }
233}
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...

◆ setCodec()

void ImgReaderGdal::setCodec ( const GDALAccess &  readMode = GA_ReadOnly)
protected

Set GDAL dataset number of columns, rows, bands and geotransform.

Parameters
readModeOpen dataset in ReadOnly or Update mode

Definition at line 50 of file ImgReaderGdal.cc.

51{
52 GDALAllRegister();
53 // m_gds = (GDALDataset *) GDALOpen(m_filename.c_str(), readMode );
54#if GDAL_VERSION_MAJOR < 2
55 GDALAllRegister();
56 m_gds = (GDALDataset *) GDALOpen(m_filename.c_str(), readMode );
57#else
58 GDALAllRegister();
59 if(readMode==GA_ReadOnly)
60 m_gds = (GDALDataset*) GDALOpenEx(m_filename.c_str(), GDAL_OF_READONLY|GDAL_OF_RASTER, NULL, NULL, NULL);
61 else if(readMode==GA_Update)
62 m_gds = (GDALDataset*) GDALOpenEx(m_filename.c_str(), GDAL_OF_UPDATE|GDAL_OF_RASTER, NULL, NULL, NULL);
63#endif
64
65 if(m_gds == NULL){
66 std::string errorString="FileOpenError";
67 throw(errorString);
68 }
69 m_ncol= m_gds->GetRasterXSize();
70 m_nrow= m_gds->GetRasterYSize();
71 m_nband= m_gds->GetRasterCount();
72 double adfGeoTransform[6];
73 m_gds->GetGeoTransform( adfGeoTransform );
74 m_gt[0]=adfGeoTransform[0];
75 m_gt[1]=adfGeoTransform[1];
76 m_gt[2]=adfGeoTransform[2];
77 m_gt[3]=adfGeoTransform[3];
78 m_gt[4]=adfGeoTransform[4];
79 m_gt[5]=adfGeoTransform[5];
80 m_projection=m_gds->GetProjectionRef();
81}
int m_nband
number of bands in this dataset
int m_ncol
number of columns in this dataset
int m_nrow
number of rows in this dataset
double m_gt[6]
geotransform information of this dataset

The documentation for this class was generated from the following files: