| 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
24 ImgRasterGdal::ImgRasterGdal(void)
25 : m_gds(NULL), m_ncol(0), m_nrow(0), m_nband(0), m_dataType(GDT_Unknown)
26{}
27
28 ImgRasterGdal::~ImgRasterGdal(void)
29{
30}
31
32 void ImgRasterGdal::close(void)
33{
34 GDALClose(m_gds);
35}
36
40std::string ImgRasterGdal::getProjection(void) const
41{
42 // if(m_gds)
43 // return(m_gds->GetProjectionRef());
44 // else
45 return(m_projection);
46}
47
51std::string ImgRasterGdal::getProjectionRef(void) const
52{
53 // if(m_gds)
54 // return(m_gds->GetProjectionRef());
55 // else
56 return(m_projection);
57}
58
63CPLErr 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
79CPLErr 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
92GDALDataType 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
105GDALRasterBand* 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
118GDALColorTable* 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
131std::string ImgRasterGdal::getDriverDescription() const
132{
133 std::string driverDescription;
134 if(m_gds)
135 driverDescription=m_gds->GetDriver()->GetDescription();
136 return(driverDescription);
137}
138
148CPLErr ImgRasterGdal::setGeoTransform(double* gt){
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
166 void ImgRasterGdal::copyGeoTransform(const ImgRasterGdal& imgSrc)
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
200std::string ImgRasterGdal::getGeoTransform() const
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
214 char** ImgRasterGdal::getMetadata()
215{
216 if(m_gds){
217 if(m_gds->GetMetadata()!=NULL)
218 return(m_gds->GetMetadata());
219 }
220 else
221 return (char**)"";
222}
223
227 char** ImgRasterGdal::getMetadata() const
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
254std::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
267std::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
280std::string ImgRasterGdal::getImageDescription() const
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
293std::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
308std::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
Definition: ImgRasterGdal.h:68
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::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::getCompression
std::string getCompression() const
Get the compression from the metadata of this dataset.
Definition: ImgRasterGdal.cc:308
ImgRasterGdal::copyGeoTransform
void copyGeoTransform(const ImgRasterGdal &imgSrc)
Copy geotransform information from another georeferenced image.
Definition: ImgRasterGdal.cc:166
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::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::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::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::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