pktools  2.6.7
Processing Kernel for geospatial data
ImgRasterGdal.h
1 /**********************************************************************
2 ImgRasterGdal.h: class to read raster files using GDAL API library
3 Copyright (C) 2008-2012 Pieter Kempeneers
4 
5 This file is part of pktools
6 
7 pktools is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 pktools is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with pktools. If not, see <http://www.gnu.org/licenses/>.
19 ***********************************************************************/
20 #ifndef _IMGRASTERGDAL_H_
21 #define _IMGRASTERGDAL_H_
22 
23 #include <typeinfo>
24 #include <fstream>
25 #include <sstream>
26 #include <string>
27 #include <vector>
28 #include <list>
29 #include <algorithm>
30 #include <assert.h>
31 #include "gdal_priv.h"
32 
33 enum RESAMPLE { NEAR = 0, BILINEAR = 1, BICUBIC = 2 };
34 
39 template<typename T1> GDALDataType getGDALDataType(){
40  if (typeid(T1) == typeid(char))
41  return GDT_Byte;
42  else if (typeid(T1) == typeid(unsigned char))
43  return GDT_Byte;
44  else if (typeid(T1) == typeid(unsigned short))
45  return GDT_UInt16;
46  else if (typeid(T1) == typeid(short))
47  return GDT_Int16;
48  else if (typeid(T1) == typeid(int))
49  return GDT_Int32;
50  else if (typeid(T1) == typeid(unsigned int))
51  return GDT_UInt32;
52  else if (typeid(T1) == typeid(long))
53  return GDT_Int32;
54  else if (typeid(T1) == typeid(unsigned long))
55  return GDT_UInt32;
56  else if (typeid(T1) == typeid(float))
57  return GDT_Float32;
58  else if (typeid(T1) == typeid(double))
59  return GDT_Float64;
60  else
61  return GDT_Byte;
62 };
63 
68 {
69 public:
71  ImgRasterGdal(void);
73  virtual ~ImgRasterGdal(void);
75  void setScale(double theScale, int band=0){
76  if(m_scale.size()!=nrOfBand()){//initialize
77  m_scale.resize(nrOfBand());
78  for(int iband=0;iband<nrOfBand();++iband)
79  m_scale[iband]=1.0;
80  }
81  m_scale[band]=theScale;
82  };
84  void setOffset(double theOffset, int band=0){
85  if(m_offset.size()!=nrOfBand()){
86  m_offset.resize(nrOfBand());
87  for(int iband=0;iband<nrOfBand();++iband)
88  m_offset[iband]=0.0;
89  }
90  m_offset[band]=theOffset;
91  };
92 
94  virtual void close(void);
96  std::string getFileName() const {return m_filename;};
98  int nrOfCol(void) const { return m_ncol;};
100  int nrOfRow(void) const { return m_nrow;};
102  int nrOfBand(void) const { return m_nband;};
104  bool isGeoRef() const {double gt[6];getGeoTransform(gt);if(gt[5]<0) return true;else return false;};
106  std::string getProjection(void) const;
108  std::string getProjectionRef(void) const;
110  std::string getGeoTransform() const;
112  void getGeoTransform(double* gt) const;
114  CPLErr setGeoTransform(double* gt);
116  void copyGeoTransform(const ImgRasterGdal& imgSrc);
118  CPLErr setProjection(const std::string& projection);
120  CPLErr setProjectionProj4(const std::string& projection);
122  bool getBoundingBox (double& ulx, double& uly, double& lrx, double& lry) const;
124  bool getCenterPos(double& x, double& y) const;
126  double getUlx() const {double ulx, uly, lrx,lry;getBoundingBox(ulx,uly,lrx,lry);return(ulx);};
128  double getUly() const {double ulx, uly, lrx,lry;getBoundingBox(ulx,uly,lrx,lry);return(uly);};
130  double getLrx() const {double ulx, uly, lrx,lry;getBoundingBox(ulx,uly,lrx,lry);return(lrx);};
132  double getLry() const {double ulx, uly, lrx,lry;getBoundingBox(ulx,uly,lrx,lry);return(lry);};
134  int getNoDataValues(std::vector<double>& noDataValues) const;
136  bool isNoData(double value) const{if(m_noDataValues.empty()) return false;else return find(m_noDataValues.begin(),m_noDataValues.end(),value)!=m_noDataValues.end();};
138  int pushNoDataValue(double noDataValue);
140  int setNoData(const std::vector<double> nodata){m_noDataValues=nodata; return(m_noDataValues.size());};
142  CPLErr GDALSetNoDataValue(double noDataValue, int band=0) {return getRasterBand(band)->SetNoDataValue(noDataValue);};
144  bool covers(double x, double y) const;
146  bool covers(double ulx, double uly, double lrx, double lry) const;
148  bool geo2image(double x, double y, double& i, double& j) const;
150  bool image2geo(double i, double j, double& x, double& y) const;
152  double getDeltaX(void) const {double gt[6];getGeoTransform(gt);return gt[1];};
154  double getDeltaY(void) const {double gt[6];getGeoTransform(gt);return -gt[5];};
156  GDALDataType getDataType(int band=0) const;
158  GDALRasterBand* getRasterBand(int band=0) const;
160  GDALColorTable* getColorTable(int band=0) const;
162  std::string getDriverDescription() const;
164  std::string getImageType() const{return getDriverDescription();};
166  std::string getInterleave() const;
168  std::string getCompression() const;
169  //Get a pointer to the GDAL dataset
170  GDALDataset* getDataset(){return m_gds;};
172  char** getMetadata();
174  char** getMetadata() const;
176  void getMetadata(std::list<std::string>& metadata) const;
178  std::string getDescription() const;
180  std::string getMetadataItem() const;
182  std::string getImageDescription() const;
183  int nrOfBlockX(int band=0)
184  {
185  int nXBlockSize, nYBlockSize;
186  getRasterBand(band)->GetBlockSize( &nXBlockSize, &nYBlockSize );
187  int nXBlocks = (nrOfCol() + nXBlockSize - 1) / nXBlockSize;
188  return nXBlocks;
189  }
190  int nrOfBlockY(int band=0)
191  {
192  int nXBlockSize, nYBlockSize;
193  getRasterBand(band)->GetBlockSize( &nXBlockSize, &nYBlockSize );
194  int nYBlocks = (nrOfRow() + nYBlockSize - 1) / nYBlockSize;
195  return nYBlocks;
196  }
197 
198  friend class ImgReaderGdal;
199  friend class ImgWriterGdal;
200 
201 protected:
203  std::string m_filename;
205  GDALDataset *m_gds;
207  int m_ncol;
209  int m_nrow;
211  int m_nband;
213  GDALDataType m_dataType;
215  double m_gt[6];
216  //projection string in wkt format
217  std::string m_projection;
219  std::vector<double> m_noDataValues;
221  std::vector<double> m_scale;
223  std::vector<double> m_offset;
224 
225 private:
226 };
227 
228 #endif // _IMGRASTERGDAL_H_
ImgRasterGdal(void)
default constructor
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.
double getLry() const
Get the lower right corner y (georeferenced) coordinate of this dataset.
virtual ~ImgRasterGdal(void)
destructor
GDALDataType m_dataType
GDAL data type for this dataset.
std::string getDescription() const
Get the image description from the driver of this dataset.
int m_nband
number of bands in this dataset
void setOffset(double theOffset, int band=0)
Set offset for a specific band when writing the raster data values. The scaling and offset are applie...
Definition: ImgRasterGdal.h:84
double getUly() const
Get the upper left corner y (georeferenced) coordinate of this dataset.
bool geo2image(double x, double y, double &i, double &j) const
Convert georeferenced coordinates (x and y) to image coordinates (column and row) ...
std::string getMetadataItem() const
Get metadata item of this dataset.
bool isNoData(double value) const
Check if value is nodata in this dataset.
CPLErr setProjectionProj4(const std::string &projection)
Set the projection for this dataset from user input (supports epsg:<number> format) ...
int nrOfCol(void) const
Get the number of columns of this dataset.
Definition: ImgRasterGdal.h:98
std::string m_filename
filename of this dataset
bool getBoundingBox(double &ulx, double &uly, double &lrx, double &lry) const
Get the bounding box of this dataset in georeferenced coordinates.
std::vector< double > m_noDataValues
no data values for this dataset
double m_gt[6]
geotransform information of this dataset
GDALDataset * m_gds
instance of the GDAL dataset of this dataset
std::string getDriverDescription() const
Get the GDAL driver description of this dataset.
double getUlx() const
Get the upper left corner x (georeferenced) coordinate of this dataset.
int nrOfRow(void) const
Get the number of rows of this dataset.
bool getCenterPos(double &x, double &y) const
Get the center position of this dataset in georeferenced coordinates.
bool image2geo(double i, double j, double &x, double &y) const
Convert image coordinates (column and row) to georeferenced coordinates (x and y) ...
GDALColorTable * getColorTable(int band=0) const
Get the GDAL color table for this dataset as an instance of the GDALColorTable class.
std::string getImageDescription() const
Get the image description from the metadata of this dataset.
CPLErr setGeoTransform(double *gt)
Set the geotransform data for this dataset.
std::string getImageType() const
Get the image type (implemented as the driver description)
double getLrx() const
Get the lower right corner x (georeferenced) coordinate of this dataset.
bool isGeoRef() const
Is this dataset georeferenced (pixel size in y must be negative) ?
int m_nrow
number of rows in this dataset
double getDeltaX(void) const
Get the pixel cell spacing in x.
int pushNoDataValue(double noDataValue)
Push a no data value for this dataset.
char ** getMetadata()
Get the metadata of this dataset.
void copyGeoTransform(const ImgRasterGdal &imgSrc)
Copy geotransform information from another georeferenced image.
int m_ncol
number of columns in this dataset
double getDeltaY(void) const
Get the pixel cell spacing in y.
std::vector< double > m_scale
Vector containing the scale factor to be applied (one scale value for each band)
int setNoData(const std::vector< double > nodata)
Set the no data values of this dataset using a standard template library (stl) vector as input...
std::string getCompression() const
Get the compression from the metadata of this dataset.
virtual void close(void)
Close the image.
std::string getFileName() const
Get the filename of this dataset.
Definition: ImgRasterGdal.h:96
std::string getProjectionRef(void) const
Get the projection reference.
std::string getProjection(void) const
Get the projection string (deprecated, use getProjectionRef instead)
GDALDataType getDataType(int band=0) const
Get the GDAL datatype for this dataset.
CPLErr setProjection(const std::string &projection)
Set the projection for this dataset in well known text (wkt) format.
GDALRasterBand * getRasterBand(int band=0) const
Get the GDAL rasterband for this dataset.
std::string getGeoTransform() const
Get the geotransform data for this dataset as a string.
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 supp...
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) ...
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...
Definition: ImgRasterGdal.h:75
int getNoDataValues(std::vector< double > &noDataValues) const
Get the no data values of this dataset as a standard template library (stl) vector.
std::string getInterleave() const
Get the band coding (interleave)