pktools  2.6.7
Processing Kernel for geospatial data
ImgRasterGdal.cc
1 /**********************************************************************
2 ImgRasterGdal.cc: 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 #include <iostream>
21 #include "ogr_spatialref.h"
22 #include "ImgRasterGdal.h"
23 
25  : m_gds(NULL), m_ncol(0), m_nrow(0), m_nband(0), m_dataType(GDT_Unknown)
26 {}
27 
29 {
30 }
31 
33 {
34  GDALClose(m_gds);
35 }
36 
40 std::string ImgRasterGdal::getProjection(void) const
41 {
42  // if(m_gds)
43  // return(m_gds->GetProjectionRef());
44  // else
45  return(m_projection);
46 }
47 
51 std::string ImgRasterGdal::getProjectionRef(void) const
52 {
53  // if(m_gds)
54  // return(m_gds->GetProjectionRef());
55  // else
56  return(m_projection);
57 }
58 
63 CPLErr ImgRasterGdal::setProjectionProj4(const std::string& projection)
64 {
65  OGRSpatialReference theRef;
66  theRef.SetFromUserInput(projection.c_str());
67  char *wktString;
68  theRef.exportToWkt(&wktString);
69  m_projection=wktString;
70  if(m_gds)
71  return(m_gds->SetProjection(wktString));
72  else
73  return(CE_Failure);
74 }
75 
79 CPLErr ImgRasterGdal::setProjection(const std::string& projection)
80 {
81  m_projection=projection;
82  if(m_gds)
83  return(m_gds->SetProjection(projection.c_str()));
84  else
85  return(CE_Failure);
86 }
87 
92 GDALDataType ImgRasterGdal::getDataType(int band) const
93 {
94  assert(band<m_nband+1);
95  if(getRasterBand(band))
96  return((getRasterBand(band)->GetRasterDataType()));
97  else
98  return(m_dataType);
99 }
100 
105 GDALRasterBand* ImgRasterGdal::getRasterBand(int band) const
106 {
107  assert(band<m_nband+1);
108  if(m_gds)
109  return((m_gds->GetRasterBand(band+1)));
110  else
111  return(0);
112 }
113 
118 GDALColorTable* ImgRasterGdal::getColorTable(int band) const
119 {
120  assert(band<m_nband+1);
121  GDALRasterBand* theRasterBand=getRasterBand(band);
122  if(theRasterBand)
123  return(theRasterBand->GetColorTable());
124  else
125  return(0);
126 }
127 
132 {
133  std::string driverDescription;
134  if(m_gds)
135  driverDescription=m_gds->GetDriver()->GetDescription();
136  return(driverDescription);
137 }
138 
149  // m_isGeoRef=true;
150  m_gt[0]=gt[0];
151  m_gt[1]=gt[1];
152  m_gt[2]=gt[2];
153  m_gt[3]=gt[3];
154  m_gt[4]=gt[4];
155  m_gt[5]=gt[5];
156  if(m_gds)
157  return(m_gds->SetGeoTransform(m_gt));
158  else
159  return(CE_Failure);
160 
161 }
162 
167 {
168  setProjection(imgSrc.getProjection());
169  double gt[6];
170  imgSrc.getGeoTransform(gt);
171  setGeoTransform(gt);
172 }
173 
183 void ImgRasterGdal::getGeoTransform(double* gt) const{
184  // if(m_gds){
185  // m_gds->GetGeoTransform(gt);
186  // }
187  // else{
188  gt[0]=m_gt[0];
189  gt[1]=m_gt[1];
190  gt[2]=m_gt[2];
191  gt[3]=m_gt[3];
192  gt[4]=m_gt[4];
193  gt[5]=m_gt[5];
194  // }
195 }
196 
201 {
202  std::string gtString;
203  double gt[6];// { 444720, 30, 0, 3751320, 0, -30 };
204  getGeoTransform(gt);
205  std::ostringstream s;
206  s << "[" << gt[0] << "," << gt[1] << "," << gt[2] << "," << gt[3] << "," << gt[4] << "," << gt[5] << "]";
207  gtString=s.str();
208  return(s.str());
209 }
210 
215 {
216  if(m_gds){
217  if(m_gds->GetMetadata()!=NULL)
218  return(m_gds->GetMetadata());
219  }
220  else
221  return (char**)"";
222 }
223 
228 {
229  if(m_gds){
230  if(m_gds->GetMetadata()!=NULL)
231  return(m_gds->GetMetadata());
232  }
233  else
234  return (char**)"";
235 }
236 
240 void ImgRasterGdal::getMetadata(std::list<std::string>& metadata) const
241 {
242  if(m_gds){
243  char** cmetadata=m_gds->GetMetadata();
244  while(*cmetadata!=NULL){
245  metadata.push_back(*(cmetadata));
246  ++cmetadata;
247  }
248  }
249 }
250 
254 std::string ImgRasterGdal::getDescription() const
255 {
256  if(m_gds){
257  if(m_gds->GetDriver()->GetDescription()!=NULL)
258  return m_gds->GetDriver()->GetDescription();
259  }
260  else
261  return("");
262 }
263 
267 std::string ImgRasterGdal::getMetadataItem() const
268 {
269  if(m_gds){
270  if(m_gds->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME )!=NULL)
271  return m_gds->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME );
272  }
273  else
274  return("");
275 }
276 
281 {
282  if(m_gds){
283  if(m_gds->GetDriver()->GetMetadataItem("TIFFTAG_IMAGEDESCRIPTION")!=NULL)
284  return m_gds->GetDriver()->GetMetadataItem("TIFFTAG_IMAGEDESCRIPTION");
285  }
286  else
287  return("");
288 }
289 
293 std::string ImgRasterGdal::getInterleave() const
294 {
295  if(m_gds){
296  if(m_gds->GetMetadataItem( "INTERLEAVE", "IMAGE_STRUCTURE"))
297  return m_gds->GetMetadataItem( "INTERLEAVE", "IMAGE_STRUCTURE");
298  else
299  return("BAND");
300  }
301  else
302  return("");
303 }
304 
308 std::string ImgRasterGdal::getCompression() const
309 {
310  if(m_gds){
311  if(m_gds->GetMetadataItem( "COMPRESSION", "IMAGE_STRUCTURE"))
312  return m_gds->GetMetadataItem( "COMPRESSION", "IMAGE_STRUCTURE");
313  }
314  else
315  return("NONE");
316 }
317 
333 bool ImgRasterGdal::getBoundingBox(double& ulx, double& uly, double& lrx, double& lry) const
334 {
335  double gt[6];// { 444720, 30, 0, 3751320, 0, -30 };
336  getGeoTransform(gt);
337 
338  ulx=gt[0];
339  uly=gt[3];
340  lrx=gt[0]+nrOfCol()*gt[1]+nrOfRow()*gt[2];
341  lry=gt[3]+nrOfCol()*gt[4]+nrOfRow()*gt[5];
342  if(isGeoRef())
343  return true;
344  else
345  return false;
346 }
347 
359 bool ImgRasterGdal::getCenterPos(double& x, double& y) const
360 {
361  double gt[6];// { 444720, 30, 0, 3751320, 0, -30 };
362  getGeoTransform(gt);
363 
364  x=gt[0]+(nrOfCol()/2.0)*gt[1]+(nrOfRow()/2.0)*gt[2];
365  y=gt[3]+(nrOfCol()/2.0)*gt[4]+(nrOfRow()/2.0)*gt[5];
366  if(isGeoRef()){
367  // x=m_ulx+(nrOfCol()/2.0)*m_delta_x;
368  // y=m_uly-(nrOfRow()/2.0)*m_delta_y;
369  return true;
370  }
371  else
372  return false;
373 }
374 
387 bool ImgRasterGdal::geo2image(double x, double y, double& i, double& j) const
388 {
389  double gt[6];// { 444720, 30, 0, 3751320, 0, -30 };
390  getGeoTransform(gt);
391 
392  double denom=(gt[1]-gt[2]*gt[4]/gt[5]);
393  double eps=0.00001;
394  if(fabs(denom)>eps){
395  i=(x-gt[0]-gt[2]/gt[5]*(y-gt[3]))/denom;
396  j=(y-gt[3]-gt[4]*(x-gt[0]-gt[2]/gt[5]*(y-gt[3]))/denom)/gt[5];
397  }
398  if(isGeoRef())
399  return true;
400  else
401  return false;
402 }
403 
416 bool ImgRasterGdal::image2geo(double i, double j, double& x, double& y) const
417 {
418  double gt[6];// { 444720, 30, 0, 3751320, 0, -30 };
419  getGeoTransform(gt);
420 
421  x=gt[0]+(0.5+i)*gt[1]+(0.5+j)*gt[2];
422  y=gt[3]+(0.5+i)*gt[4]+(0.5+j)*gt[5];
423  if(isGeoRef()){
424  // x=m_ulx+(0.5+i)*m_delta_x;
425  // y=m_uly-(0.5+j)*m_delta_y;
426  return true;
427  }
428  else
429  return false;
430 }
431 
443 bool ImgRasterGdal::covers(double x, double y) const
444 {
445  double theULX, theULY, theLRX, theLRY;
446  getBoundingBox(theULX,theULY,theLRX,theLRY);
447  return((x > theULX)&&
448  (x < theLRX)&&
449  (y < theULY)&&
450  (y >theLRY));
451 }
452 
467 bool ImgRasterGdal::covers(double ulx, double uly, double lrx, double lry) const
468 {
469  double theULX, theULY, theLRX, theLRY;
470  getBoundingBox(theULX,theULY,theLRX,theLRY);
471  return((ulx < theLRX)&&(lrx > theULX)&&(lry < theULY)&&(uly > theLRY));
472 }
473 
478 int ImgRasterGdal::getNoDataValues(std::vector<double>& noDataValues) const
479 {
480  if(m_noDataValues.size()){
481  noDataValues=m_noDataValues;
482  return m_noDataValues.size();
483  }
484  else
485  return 0;
486 }
487 
492 int ImgRasterGdal::pushNoDataValue(double noDataValue)
493 {
494  if(find(m_noDataValues.begin(),m_noDataValues.end(),noDataValue)==m_noDataValues.end())
495  m_noDataValues.push_back(noDataValue);
496  return(m_noDataValues.size());
497 }
ImgRasterGdal(void)
default constructor
std::string getDriverDescription() const
Get the GDAL driver description of this dataset.
virtual ~ImgRasterGdal(void)
destructor
GDALDataType m_dataType
GDAL data type for this dataset.
int m_nband
number of bands in this dataset
std::string getImageDescription() const
Get the image description from the metadata of this dataset.
std::string getProjectionRef(void) const
Get the projection reference.
bool geo2image(double x, double y, double &i, double &j) const
Convert georeferenced coordinates (x and y) to image coordinates (column and row) ...
GDALColorTable * getColorTable(int band=0) const
Get the GDAL color table for this dataset as an instance of the GDALColorTable class.
GDALDataType getDataType(int band=0) const
Get the GDAL datatype for this dataset.
bool isGeoRef() const
Is this dataset georeferenced (pixel size in y must be negative) ?
int getNoDataValues(std::vector< double > &noDataValues) const
Get the no data values of this dataset as a standard template library (stl) vector.
CPLErr setProjectionProj4(const std::string &projection)
Set the projection for this dataset from user input (supports epsg: format) ...
std::string getMetadataItem() const
Get metadata item of this dataset.
std::vector< double > m_noDataValues
no data values for this dataset
double m_gt[6]
geotransform information of this dataset
int nrOfRow(void) const
Get the number of rows of this dataset.
GDALDataset * m_gds
instance of the GDAL dataset of this dataset
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.
bool image2geo(double i, double j, double &x, double &y) const
Convert image coordinates (column and row) to georeferenced coordinates (x and y) ...
std::string getDescription() const
Get the image description from the driver of this dataset.
CPLErr setGeoTransform(double *gt)
Set the geotransform data for this dataset.
int pushNoDataValue(double noDataValue)
Push a no data value for this dataset.
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.
char ** getMetadata()
Get the metadata of this dataset.
void copyGeoTransform(const ImgRasterGdal &imgSrc)
Copy geotransform information from another georeferenced image.
std::string getCompression() const
Get the compression from the metadata of this dataset.
virtual void close(void)
Close the image.
CPLErr setProjection(const std::string &projection)
Set the projection for this dataset in well known text (wkt) format.
bool getCenterPos(double &x, double &y) const
Get the center position of this dataset in georeferenced coordinates.
std::string getInterleave() const
Get the band coding (interleave)
std::string getProjection(void) const
Get the projection string (deprecated, use getProjectionRef instead)
bool getBoundingBox(double &ulx, double &uly, double &lrx, double &lry) const
Get the bounding box of this dataset in georeferenced coordinates.
int nrOfCol(void) const
Get the number of columns of this dataset.
Definition: ImgRasterGdal.h:98