| 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
67 class ImgRasterGdal
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
Definition: ImgRasterGdal.h:68
ImgRasterGdal::getDeltaY
double getDeltaY(void) const
Get the pixel cell spacing in y.
Definition: ImgRasterGdal.h:154
ImgRasterGdal::setNoData
int setNoData(const std::vector< double > nodata)
Set the no data values of this dataset using a standard template library (stl) vector as input.
Definition: ImgRasterGdal.h:140
ImgRasterGdal::close
virtual void close(void)
Close the image.
Definition: ImgRasterGdal.cc:32
ImgRasterGdal::nrOfRow
int nrOfRow(void) const
Get the number of rows of this dataset.
Definition: ImgRasterGdal.h:100
ImgRasterGdal::isGeoRef
bool isGeoRef() const
Is this dataset georeferenced (pixel size in y must be negative) ?
Definition: ImgRasterGdal.h:104
ImgRasterGdal::getDriverDescription
std::string getDriverDescription() const
Get the GDAL driver description of this dataset.
Definition: ImgRasterGdal.cc:131
ImgRasterGdal::geo2image
bool geo2image(double x, double y, double &i, double &j) const
Convert georeferenced coordinates (x and y) to image coordinates (column and row)
Definition: ImgRasterGdal.cc:387
ImgRasterGdal::getCenterPos
bool getCenterPos(double &x, double &y) const
Get the center position of this dataset in georeferenced coordinates.
Definition: ImgRasterGdal.cc:359
ImgRasterGdal::getProjection
std::string getProjection(void) const
Get the projection string (deprecated, use getProjectionRef instead)
Definition: ImgRasterGdal.cc:40
ImgRasterGdal::m_nband
int m_nband
number of bands in this dataset
Definition: ImgRasterGdal.h:211
ImgRasterGdal::m_ncol
int m_ncol
number of columns in this dataset
Definition: ImgRasterGdal.h:207
ImgRasterGdal::isNoData
bool isNoData(double value) const
Check if value is nodata in this dataset.
Definition: ImgRasterGdal.h:136
ImgRasterGdal::m_filename
std::string m_filename
filename of this dataset
Definition: ImgRasterGdal.h:203
ImgRasterGdal::getUly
double getUly() const
Get the upper left corner y (georeferenced) coordinate of this dataset.
Definition: ImgRasterGdal.h:128
ImgRasterGdal::getMetadata
char ** getMetadata()
Get the metadata of this dataset.
Definition: ImgRasterGdal.cc:214
ImgRasterGdal::getNoDataValues
int getNoDataValues(std::vector< double > &noDataValues) const
Get the no data values of this dataset as a standard template library (stl) vector.
Definition: ImgRasterGdal.cc:478
ImgRasterGdal::m_gds
GDALDataset * m_gds
instance of the GDAL dataset of this dataset
Definition: ImgRasterGdal.h:205
ImgRasterGdal::getGeoTransform
std::string getGeoTransform() const
Get the geotransform data for this dataset as a string.
Definition: ImgRasterGdal.cc:200
ImgRasterGdal::getColorTable
GDALColorTable * getColorTable(int band=0) const
Get the GDAL color table for this dataset as an instance of the GDALColorTable class.
Definition: ImgRasterGdal.cc:118
ImgRasterGdal::setGeoTransform
CPLErr setGeoTransform(double *gt)
Set the geotransform data for this dataset.
Definition: ImgRasterGdal.cc:148
ImgRasterGdal::getLrx
double getLrx() const
Get the lower right corner x (georeferenced) coordinate of this dataset.
Definition: ImgRasterGdal.h:130
ImgRasterGdal::nrOfBand
int nrOfBand(void) const
Get the number of bands of this dataset.
Definition: ImgRasterGdal.h:102
ImgRasterGdal::getCompression
std::string getCompression() const
Get the compression from the metadata of this dataset.
Definition: ImgRasterGdal.cc:308
ImgRasterGdal::setScale
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
ImgRasterGdal::copyGeoTransform
void copyGeoTransform(const ImgRasterGdal &imgSrc)
Copy geotransform information from another georeferenced image.
Definition: ImgRasterGdal.cc:166
ImgRasterGdal::m_nrow
int m_nrow
number of rows in this dataset
Definition: ImgRasterGdal.h:209
ImgRasterGdal::m_offset
std::vector< double > m_offset
Vector containing the offset factor to be applied (one offset value for each band)
Definition: ImgRasterGdal.h:223
ImgRasterGdal::getProjectionRef
std::string getProjectionRef(void) const
Get the projection reference.
Definition: ImgRasterGdal.cc:51
ImgRasterGdal::~ImgRasterGdal
virtual ~ImgRasterGdal(void)
destructor
Definition: ImgRasterGdal.cc:28
ImgRasterGdal::setProjectionProj4
CPLErr setProjectionProj4(const std::string &projection)
Set the projection for this dataset from user input (supports epsg:<number> format)
Definition: ImgRasterGdal.cc:63
ImgRasterGdal::getImageDescription
std::string getImageDescription() const
Get the image description from the metadata of this dataset.
Definition: ImgRasterGdal.cc:280
ImgRasterGdal::nrOfCol
int nrOfCol(void) const
Get the number of columns of this dataset.
Definition: ImgRasterGdal.h:98
ImgRasterGdal::getDeltaX
double getDeltaX(void) const
Get the pixel cell spacing in x.
Definition: ImgRasterGdal.h:152
ImgRasterGdal::setOffset
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
ImgRasterGdal::m_noDataValues
std::vector< double > m_noDataValues
no data values for this dataset
Definition: ImgRasterGdal.h:219
ImgRasterGdal::getInterleave
std::string getInterleave() const
Get the band coding (interleave)
Definition: ImgRasterGdal.cc:293
ImgRasterGdal::getRasterBand
GDALRasterBand * getRasterBand(int band=0) const
Get the GDAL rasterband for this dataset.
Definition: ImgRasterGdal.cc:105
ImgRasterGdal::ImgRasterGdal
ImgRasterGdal(void)
default constructor
Definition: ImgRasterGdal.cc:24
ImgRasterGdal::pushNoDataValue
int pushNoDataValue(double noDataValue)
Push a no data value for this dataset.
Definition: ImgRasterGdal.cc:492
ImgRasterGdal::getLry
double getLry() const
Get the lower right corner y (georeferenced) coordinate of this dataset.
Definition: ImgRasterGdal.h:132
ImgRasterGdal::m_scale
std::vector< double > m_scale
Vector containing the scale factor to be applied (one scale value for each band)
Definition: ImgRasterGdal.h:221
ImgRasterGdal::image2geo
bool image2geo(double i, double j, double &x, double &y) const
Convert image coordinates (column and row) to georeferenced coordinates (x and y)
Definition: ImgRasterGdal.cc:416
ImgRasterGdal::GDALSetNoDataValue
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...
Definition: ImgRasterGdal.h:142
ImgRasterGdal::getImageType
std::string getImageType() const
Get the image type (implemented as the driver description)
Definition: ImgRasterGdal.h:164
ImgRasterGdal::getMetadataItem
std::string getMetadataItem() const
Get metadata item of this dataset.
Definition: ImgRasterGdal.cc:267
ImgRasterGdal::m_gt
double m_gt[6]
geotransform information of this dataset
Definition: ImgRasterGdal.h:215
ImgRasterGdal::getDataType
GDALDataType getDataType(int band=0) const
Get the GDAL datatype for this dataset.
Definition: ImgRasterGdal.cc:92
ImgRasterGdal::setProjection
CPLErr setProjection(const std::string &projection)
Set the projection for this dataset in well known text (wkt) format.
Definition: ImgRasterGdal.cc:79
ImgRasterGdal::covers
bool covers(double x, double y) const
Check if a geolocation is covered by this dataset. Only the bounding box is checked,...
Definition: ImgRasterGdal.cc:443
ImgRasterGdal::m_dataType
GDALDataType m_dataType
GDAL data type for this dataset.
Definition: ImgRasterGdal.h:213
ImgRasterGdal::getFileName
std::string getFileName() const
Get the filename of this dataset.
Definition: ImgRasterGdal.h:96
ImgRasterGdal::getUlx
double getUlx() const
Get the upper left corner x (georeferenced) coordinate of this dataset.
Definition: ImgRasterGdal.h:126
ImgRasterGdal::getDescription
std::string getDescription() const
Get the image description from the driver of this dataset.
Definition: ImgRasterGdal.cc:254
ImgRasterGdal::getBoundingBox
bool getBoundingBox(double &ulx, double &uly, double &lrx, double &lry) const
Get the bounding box of this dataset in georeferenced coordinates.
Definition: ImgRasterGdal.cc:333
ImgReaderGdal
Definition: ImgReaderGdal.h:37
ImgWriterGdal
Definition: ImgWriterGdal.h:38