pktools  2.6.6
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 "ImgRasterGdal.h"
22 
23 ImgRasterGdal::ImgRasterGdal(void)
24  : m_gds(NULL), m_ncol(0), m_nrow(0), m_nband(0)
25 {}
26 
27 //--------------------------------------------------------------------------
28 void ImgRasterGdal::close(void)
29 {
30  GDALClose(m_gds);
31 }
32 
33 std::string ImgRasterGdal::getProjection(void) const
34 {
35  std::string theProjection=m_gds->GetProjectionRef();
36  // size_t startpos,endpos;
37  // while((startpos=theProjection.find(",AUTHORITY"))!=std::string::npos){
38  // endpos=theProjection.find("]",startpos+1,1)+1;
39  // theProjection.erase(startpos,endpos-startpos);
40  // }
41  return theProjection;
42 }
43 
44 std::string ImgRasterGdal::getProjectionRef(void) const
45 {
46  std::string theProjection;
47  if(m_gds->GetProjectionRef())
48  return(m_gds->GetProjectionRef());
49  else
50  return "";
51 }
52 
53 GDALDataType ImgRasterGdal::getDataType(int band) const
54 {
55  assert(band<m_nband+1);
56  return (m_gds->GetRasterBand(band+1))->GetRasterDataType();
57 }
58 
59 GDALRasterBand* ImgRasterGdal::getRasterBand(int band)
60 {
61  assert(band<m_nband+1);
62  return (m_gds->GetRasterBand(band+1));
63 }
64 
65 GDALColorTable* ImgRasterGdal::getColorTable(int band) const
66 {
67  assert(band<m_nband+1);
68  return (m_gds->GetRasterBand(band+1))->GetColorTable();
69 }
70 
71 std::string ImgRasterGdal::getDriverDescription() const
72 {
73  return m_gds->GetDriver()->GetDescription();
74 }
75 
76 void ImgRasterGdal::getGeoTransform(double* gt) const{
77  m_gds->GetGeoTransform(gt);
78 }
79 
80 // void ImgRasterGdal::getGeoTransform(double& ulx, double& uly, double& deltaX, double& deltaY, double& rot1, double& rot2) const
81 // {
82 // double adfGeoTransform[6];// { 444720, 30, 0, 3751320, 0, -30 };
83 // m_gds->GetGeoTransform(adfGeoTransform);
84 // ulx=adfGeoTransform[0];
85 // deltaX=adfGeoTransform[1];
86 // rot1=adfGeoTransform[2];
87 // uly=adfGeoTransform[3];
88 // rot2=adfGeoTransform[4];
89 // deltaY=-adfGeoTransform[5];//convention of GDAL!
90 // }
91 
92 std::string ImgRasterGdal::getGeoTransform() const
93 {
94  double gt[6];// { 444720, 30, 0, 3751320, 0, -30 };
95  m_gds->GetGeoTransform(gt);
96  std::ostringstream s;
97  s << "[" << gt[0] << "," << gt[1] << "," << gt[2] << "," << gt[3] << "," << gt[4] << "," << gt[5] << "]";
98  return(s.str());
99  // if(!isGeoRef())
100  // return("");
101  // else{
102  // double adfGeoTransform[6];// { 444720, 30, 0, 3751320, 0, -30 };
103  // m_gds->GetGeoTransform(adfGeoTransform);
104  // double ulx=adfGeoTransform[0];
105  // double deltaX=adfGeoTransform[1];
106  // double rot1=adfGeoTransform[2];
107  // double uly=adfGeoTransform[3];
108  // double rot2=adfGeoTransform[4];
109  // double deltaY=-adfGeoTransform[5];//convention of GDAL!
110  // std::ostringstream s;
111  // s << "[" << ulx << "," << deltaX << "," << rot1 << "," << uly << "," << rot2 << "," << -deltaY << "]";
112  // return(s.str());
113  // }
114 }
115 
116 char** ImgRasterGdal::getMetadata()
117 {
118  if(m_gds->GetMetadata()!=NULL)
119  return(m_gds->GetMetadata());
120  else
121  return (char**)"";
122 }
123 
124 char** ImgRasterGdal::getMetadata() const
125 {
126  if(m_gds->GetMetadata()!=NULL)
127  return(m_gds->GetMetadata());
128  else
129  return (char**)"";
130 }
131 
132 void ImgRasterGdal::getMetadata(std::list<std::string>& metadata) const
133 {
134  char** cmetadata=m_gds->GetMetadata();
135  while(*cmetadata!=NULL){
136  metadata.push_back(*(cmetadata));
137  ++cmetadata;
138  }
139 }
140 
141 std::string ImgRasterGdal::getDescription() const
142 {
143  if(m_gds->GetDriver()->GetDescription()!=NULL)
144  return m_gds->GetDriver()->GetDescription();
145  else
146  return "";
147 }
148 
149 std::string ImgRasterGdal::getMetadataItem() const
150 {
151  if(m_gds->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME )!=NULL)
152  return m_gds->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME );
153  else
154  return "";
155 }
156 std::string ImgRasterGdal::getImageDescription() const
157 {
158  if(m_gds->GetDriver()->GetMetadataItem("TIFFTAG_IMAGEDESCRIPTION")!=NULL)
159  return m_gds->GetDriver()->GetMetadataItem("TIFFTAG_IMAGEDESCRIPTION");
160  else
161  return "";
162 }
163 
164 std::string ImgRasterGdal::getInterleave() const
165 {
166  if(m_gds->GetMetadataItem( "INTERLEAVE", "IMAGE_STRUCTURE"))
167  return m_gds->GetMetadataItem( "INTERLEAVE", "IMAGE_STRUCTURE");
168  else
169  return("BAND");
170 }
171 
172 std::string ImgRasterGdal::getCompression() const
173 {
174  if(m_gds->GetMetadataItem( "COMPRESSION", "IMAGE_STRUCTURE"))
175  return m_gds->GetMetadataItem( "COMPRESSION", "IMAGE_STRUCTURE");
176  else
177  return("NONE");
178 }
179 
180 bool ImgRasterGdal::getBoundingBox(double& ulx, double& uly, double& lrx, double& lry) const
181 {
182  double gt[6];// { 444720, 30, 0, 3751320, 0, -30 };
183  m_gds->GetGeoTransform(gt);
184 
185  //assuming
186  //adfGeotransform[0]: ULX (upper left X coordinate)
187  //adfGeotransform[1]: $cos(\alpha)\cdot\textrm{Xres}$
188  //adfGeotransform[2]: $-sin(\alpha)\cdot\textrm{Xres}$
189  //adfGeotransform[3]: ULY (upper left Y coordinate)
190  //adfGeotransform[4]: $-sin(\alpha)\cdot\textrm{Yres}$
191  //adfGeotransform[5]: $-cos(\alpha)\cdot\textrm{Yres}$
192  ulx=gt[0];
193  uly=gt[3];
194  lrx=gt[0]+nrOfCol()*gt[1]+nrOfRow()*gt[2];
195  lry=gt[3]+nrOfCol()*gt[4]+nrOfRow()*gt[5];
196  if(isGeoRef()){
197  // ulx=m_ulx;
198  // uly=m_uly;
199  // lrx=ulx+nrOfCol()*m_delta_x;
200  // lry=uly-nrOfRow()*m_delta_y;
201  return true;
202  }
203  else{
204  // ulx=0;
205  // uly=nrOfRow()-1;
206  // lrx=nrOfCol()-1;
207  // lry=0;
208  return false;
209  }
210 }
211 
212 bool ImgRasterGdal::getCenterPos(double& x, double& y) const
213 {
214  double gt[6];// { 444720, 30, 0, 3751320, 0, -30 };
215  m_gds->GetGeoTransform(gt);
216 
217  //assuming
218  //adfGeotransform[0]: ULX (upper left X coordinate)
219  //adfGeotransform[1]: $cos(\alpha)\cdot\textrm{Xres}$
220  //adfGeotransform[2]: $-sin(\alpha)\cdot\textrm{Xres}$
221  //adfGeotransform[3]: ULY (upper left Y coordinate)
222  //adfGeotransform[4]: $-sin(\alpha)\cdot\textrm{Yres}$
223  //adfGeotransform[5]: $-cos(\alpha)\cdot\textrm{Yres}$
224  x=gt[0]+(nrOfCol()/2.0)*gt[1]+(nrOfRow()/2.0)*gt[2];
225  y=gt[3]+(nrOfCol()/2.0)*gt[4]+(nrOfRow()/2.0)*gt[5];
226  if(isGeoRef()){
227  // x=m_ulx+(nrOfCol()/2.0)*m_delta_x;
228  // y=m_uly-(nrOfRow()/2.0)*m_delta_y;
229  return true;
230  }
231  else{
232  // x=nrOfCol()/2.0;
233  // y=nrOfRow()/2.0;
234  return false;
235  }
236 }
237 
238 //i and j represent fraction of pixels, return true if image is georeferenced
239 bool ImgRasterGdal::geo2image(double x, double y, double& i, double& j) const
240 {
241  //double values are returned, caller is responsible for interpolation step
242  double gt[6];// { 444720, 30, 0, 3751320, 0, -30 };
243  m_gds->GetGeoTransform(gt);
244  //assuming
245  //adfGeotransform[0]: ULX (upper left X coordinate)
246  //adfGeotransform[1]: $cos(\alpha)\cdot\textrm{Xres}$
247  //adfGeotransform[2]: $-sin(\alpha)\cdot\textrm{Xres}$
248  //adfGeotransform[3]: ULY (upper left Y coordinate)
249  //adfGeotransform[4]: $-sin(\alpha)\cdot\textrm{Yres}$
250  //adfGeotransform[5]: $-cos(\alpha)\cdot\textrm{Yres}$
251 
252  double denom=(gt[1]-gt[2]*gt[4]/gt[5]);
253  double eps=0.00001;
254  if(fabs(denom)>eps){
255  i=(x-gt[0]-gt[2]/gt[5]*(y-gt[3]))/denom;
256  j=(y-gt[3]-gt[4]*(x-gt[0]-gt[2]/gt[5]*(y-gt[3]))/denom)/gt[5];
257  }
258  if(isGeoRef()){
259  // double ulx=m_ulx;
260  // double uly=m_uly;
261  // i=(x-ulx)/m_delta_x;
262  // j=(uly-y)/m_delta_y;
263  return true;
264  }
265  else{
266  // i=x;
267  // j=nrOfRow()-y;
268  return false;
269  }
270 }
271 
272 //x and y represent center of pixel, return true if image is georeferenced
273 bool ImgRasterGdal::image2geo(double i, double j, double& x, double& y) const
274 {
275  double gt[6];// { 444720, 30, 0, 3751320, 0, -30 };
276  m_gds->GetGeoTransform(gt);
277 
278  //assuming
279  //adfGeotransform[0]: ULX (upper left X coordinate)
280  //adfGeotransform[1]: $cos(\alpha)\cdot\textrm{Xres}$
281  //adfGeotransform[2]: $-sin(\alpha)\cdot\textrm{Xres}$
282  //adfGeotransform[3]: ULY (upper left Y coordinate)
283  //adfGeotransform[4]: $-sin(\alpha)\cdot\textrm{Yres}$
284  //adfGeotransform[5]: $-cos(\alpha)\cdot\textrm{Yres}$
285 
286  x=gt[0]+(0.5+i)*gt[1]+(0.5+j)*gt[2];
287  y=gt[3]+(0.5+i)*gt[4]+(0.5+j)*gt[5];
288  if(isGeoRef()){
289  // x=m_ulx+(0.5+i)*m_delta_x;
290  // y=m_uly-(0.5+j)*m_delta_y;
291  return true;
292  }
293  else{
294  // x=0.5+i;
295  // y=nrOfRow()-(0.5+j);
296  return false;
297  }
298 }
299 
300 bool ImgRasterGdal::covers(double x, double y) const
301 {
302  double theULX, theULY, theLRX, theLRY;
303  getBoundingBox(theULX,theULY,theLRX,theLRY);
304  return((x > theULX)&&
305  (x < theLRX)&&
306  (y < theULY)&&
307  (y >theLRY));
308 }
309 
310 bool ImgRasterGdal::covers(double ulx, double uly, double lrx, double lry) const
311 {
312  double theULX, theULY, theLRX, theLRY;
313  getBoundingBox(theULX,theULY,theLRX,theLRY);
314  return((ulx < theLRX)&&(lrx > theULX)&&(lry < theULY)&&(uly > theLRY));
315 }
316 
317 int ImgRasterGdal::getNoDataValues(std::vector<double>& noDataValues) const
318 {
319  if(m_noDataValues.size()){
320  noDataValues=m_noDataValues;
321  return m_noDataValues.size();
322  }
323  else
324  return 0;
325 }
326 
327 int ImgRasterGdal::pushNoDataValue(double noDataValue)
328 {
329  if(find(m_noDataValues.begin(),m_noDataValues.end(),noDataValue)==m_noDataValues.end())
330  m_noDataValues.push_back(noDataValue);
331  return(m_noDataValues.size());
332 }
333 
334 // bool ImgRasterGdal::setNoDataValue(double noDataValue,int band)
335 // {
336 // GDALRasterBand *poBand;
337 // poBand = m_gds->GetRasterBand(band+1);
338 // if(poBand->SetNoDataValue(noDataValue)!=CE_None)
339 // return false;
340 // else
341 // return true;
342 // }