pktools  2.6.3
Processing Kernel for geospatial data
pkinfo.cc
1 /**********************************************************************
2 pkinfo.cc: Report basic information from raster datasets (similar to gdalinfo)
3 Copyright (C) 2008-2014 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 <sstream>
21 #include <list>
22 #include "base/Optionpk.h"
23 #include "algorithms/Egcs.h"
24 #include "imageclasses/ImgReaderGdal.h"
25 #include "imageclasses/ImgReaderOgr.h"
26 
27 using namespace std;
28 
29 int main(int argc, char *argv[])
30 {
31  Optionpk<std::string> input_opt("i","input","Input image file");
32  Optionpk<bool> bbox_opt("bb", "bbox", "Shows bounding box ", false,0);
33  Optionpk<bool> bbox_te_opt("te", "te", "Shows bounding box in GDAL format: xmin ymin xmax ymax ", false,0);
34  Optionpk<bool> center_opt("c", "center", "Image center in projected X,Y coordinates ", false,0);
35  Optionpk<bool> colorTable_opt("ct", "colortable", "Shows colour table ", false,0);
36  Optionpk<bool> samples_opt("ns", "nsample", "Number of samples in image ", false,0);
37  Optionpk<bool> lines_opt("nl", "nline", "Number of lines in image ", false,0);
38  Optionpk<bool> nband_opt("nb", "nband", "Show number of bands in image", false,0);
39  Optionpk<short> band_opt("b", "band", "Band specific information", 0,0);
40  Optionpk<bool> dx_opt("dx", "dx", "Gets resolution in x (in m)", false,0);
41  Optionpk<bool> dy_opt("dy", "dy", "Gets resolution in y (in m)", false,0);
42  Optionpk<bool> minmax_opt("mm", "minmax", "Shows min and max value of the image ", false,0);
43  Optionpk<bool> min_opt("min", "minimum", "Shows min value of the image ", false,0);
44  Optionpk<bool> max_opt("max", "maximum", "Shows max value of the image ", false,0);
45  Optionpk<bool> stat_opt("stats", "statistics", "Shows statistics (min,max, mean and stdDev of the image)", false,0);
46  Optionpk<bool> projection_opt("a_srs", "a_srs", "Shows projection of the image ", false,0);
47  Optionpk<bool> geo_opt("geo", "geo", "Gets geotransform ", false,0);
48  Optionpk<bool> interleave_opt("il", "interleave", "Shows interleave ", false,0);
49  Optionpk<bool> filename_opt("f", "filename", "Shows image filename ", false,0);
50  Optionpk<bool> cover_opt("cover", "cover", "Print filename to stdout if current image covers the provided coordinates via bounding box, (x y) coordinates or extent of vector file", false,0);
51  Optionpk<double> x_opt("x", "xpos", "x pos");
52  Optionpk<double> y_opt("y", "ypos", "y pos");
53  Optionpk<bool> read_opt("r", "read", "Reads row y (in projected coordinates if geo option is set, otherwise in image coordinates, 0 based)",false,0);
54  Optionpk<bool> refpixel_opt("ref", "reference", "Gets reference pixel (lower left corner of center of gravity pixel)", false,0);
55  Optionpk<bool> driver_opt("of", "oformat", "Gets driver description ", false,0);
56  Optionpk<std::string> extent_opt("e", "extent", "Gets boundary from vector file");
57  Optionpk<double> ulx_opt("ulx", "ulx", "Upper left x value bounding box");
58  Optionpk<double> uly_opt("uly", "uly", "Upper left y value bounding box");
59  Optionpk<double> lrx_opt("lrx", "lrx", "Lower right x value bounding box");
60  Optionpk<double> lry_opt("lry", "lry", "Lower right y value bounding box");
61  Optionpk<bool> type_opt("ot", "otype", "Returns data type", false,0);
62  Optionpk<bool> description_opt("d", "description", "Returns image description", false,0);
63  Optionpk<bool> metadata_opt("meta", "meta", "Shows meta data ", false,0);
64  Optionpk<double> nodata_opt("nodata", "nodata", "Sets no data value(s) for calculations (nodata values in input image)");
65 
66  bool doProcess;//stop process when program was invoked with help option (-h --help)
67  try{
68  doProcess=input_opt.retrieveOption(argc,argv);
69  bbox_opt.retrieveOption(argc,argv);
70  bbox_te_opt.retrieveOption(argc,argv);
71  center_opt.retrieveOption(argc,argv);
72  colorTable_opt.retrieveOption(argc,argv);
73  samples_opt.retrieveOption(argc,argv);
74  lines_opt.retrieveOption(argc,argv);
75  nband_opt.retrieveOption(argc,argv);
76  band_opt.retrieveOption(argc,argv);
77  dx_opt.retrieveOption(argc,argv);
78  dy_opt.retrieveOption(argc,argv);
79  minmax_opt.retrieveOption(argc,argv);
80  min_opt.retrieveOption(argc,argv);
81  max_opt.retrieveOption(argc,argv);
82  stat_opt.retrieveOption(argc,argv);
83  projection_opt.retrieveOption(argc,argv);
84  geo_opt.retrieveOption(argc,argv);
85  interleave_opt.retrieveOption(argc,argv);
86  filename_opt.retrieveOption(argc,argv);
87  cover_opt.retrieveOption(argc,argv);
88  x_opt.retrieveOption(argc,argv);
89  y_opt.retrieveOption(argc,argv);
90  read_opt.retrieveOption(argc,argv);
91  refpixel_opt.retrieveOption(argc,argv);
92  driver_opt.retrieveOption(argc,argv);
93  extent_opt.retrieveOption(argc,argv);
94  ulx_opt.retrieveOption(argc,argv);
95  uly_opt.retrieveOption(argc,argv);
96  lrx_opt.retrieveOption(argc,argv);
97  lry_opt.retrieveOption(argc,argv);
98  type_opt.retrieveOption(argc,argv);
99  description_opt.retrieveOption(argc,argv);
100  metadata_opt.retrieveOption(argc,argv);
101  nodata_opt.retrieveOption(argc,argv);
102  }
103  catch(std::string predefinedString){
104  std::cout << predefinedString << std::endl;
105  exit(0);
106  }
107  if(!doProcess){
108  cout << endl;
109  cout << "Usage: pkinfo -i input [options]" << endl;
110  cout << endl;
111  std::cout << "short option -h shows basic options only, use long option --help to show all options" << std::endl;
112  exit(0);//help was invoked, stop processing
113  }
114  //for union
115  double maxLRX=0;
116  double maxULY=0;
117  double minULX=0;
118  double minLRY=0;
119 
120  //for intersect
121  double minLRX=0;
122  double minULY=0;
123  double maxULX=0;
124  double maxLRY=0;
125 
126  double theULX, theULY, theLRX, theLRY;
127  //get bounding box from extentReader if defined
128  ImgReaderOgr extentReader;
129  if(extent_opt.size()){
130  extentReader.open(extent_opt[0]);
131  if(!(extentReader.getExtent(theULX,theULY, theLRX, theLRY))){
132  std::cerr << "Error: could not get extent from " << extent_opt[0] << std::endl;
133  exit(1);
134  }
135  ulx_opt.push_back(theULX);
136  uly_opt.push_back(theULY);
137  lrx_opt.push_back(theLRX);
138  lry_opt.push_back(theLRY);
139  if(input_opt.empty()){//report bounding box from extent file instead
140  if(bbox_te_opt[0])
141  std::cout << std::setprecision(12) << "-te " << theULX << " " << theLRY << " " << theLRX << " " << theULY;
142  else
143  std::cout << std::setprecision(12) << "--ulx=" << theULX << " --uly=" << theULY << " --lrx=" << theLRX << " --lry=" << theLRY << " ";
144  }
145  }
146 
147  ImgReaderGdal imgReader;
148  for(int ifile=0;ifile<input_opt.size();++ifile){
149  imgReader.open(input_opt[ifile]);
150  for(int inodata=0;inodata<nodata_opt.size();++inodata){
151  if(!inodata)
152  imgReader.GDALSetNoDataValue(nodata_opt[0],band_opt[0]);//only single no data can be set in GDALRasterBand (used for ComputeStatistics)
153  imgReader.pushNoDataValue(nodata_opt[inodata]);
154  }
155  if(filename_opt[0])
156  std::cout << " --input " << input_opt[ifile] << " ";
157  if(center_opt[0]){
158  double theX, theY;
159  imgReader.getCenterPos(theX,theY);
160  std::cout << std::setprecision(12) << " -x " << theX << " -y " << theY << " ";
161  }
162  if(refpixel_opt[0]){
163  assert(band_opt[0]<imgReader.nrOfBand());
164  Egcs egcs;
165  double refX,refY;
166  //get center of reference (center of gravity) pixel in image
167  imgReader.getRefPix(refX,refY,band_opt[0]);
168  std::cout << std::setprecision(12) << "-x " << refX << " -y " << refY << std::endl;
169  egcs.setLevel(egcs.res2level(imgReader.getDeltaX()));
170  // unsigned short theLevel=egcs.getLevel(imgReader.getDeltaX());
171  // egcs.setLevel(theLevel);
172  //cout << "cell code at level " << egcs.getLevel() << " (resolution is " << egcs.getResolution() << "): " << egcs.geo2cell(refX,refY) << endl;
173  }
174  if(bbox_opt[0]||bbox_te_opt[0]){
175  imgReader.getBoundingBox(theULX,theULY,theLRX,theLRY);
176  if(bbox_te_opt[0])
177  std::cout << std::setprecision(12) << "-te " << theULX << " " << theLRY << " " << theLRX << " " << theULY;
178  else
179  std::cout << std::setprecision(12) << "--ulx=" << theULX << " --uly=" << theULY << " --lrx=" << theLRX << " --lry=" << theLRY << " ";
180  if(!ifile){
181  maxLRX=theLRX;
182  maxULY=theULY;
183  minULX=theULX;
184  minLRY=theLRY;
185 
186  minLRX=theLRX;
187  minULY=theULY;
188  maxULX=theULX;
189  maxLRY=theLRY;
190  }
191  else{
192  maxLRX=(theLRX>maxLRX)?theLRX:maxLRX;
193  maxULY=(theULY>maxULY)?theULY:maxULY;
194  minULX=(theULX<minULX)?theULX:minULX;
195  minLRY=(theLRY<minLRY)?theLRY:minLRY;
196 
197  minLRX=(theLRX<minLRX)?theLRX:minLRX;
198  minULY=(theULY<minULY)?theULY:minULY;
199  maxULX=(theULX>maxULX)?theULX:maxULX;
200  maxLRY=(theLRY>maxLRY)?theLRY:maxLRY;
201  }
202  }
203  if(dx_opt[0])
204  std::cout << "--dx " << imgReader.getDeltaX() << " ";
205  if(dy_opt[0])
206  std::cout << "--dy " << imgReader.getDeltaY() << " ";
207  if(cover_opt[0]){
208  if(ulx_opt.size()&&uly_opt.size()&&lrx_opt.size()&&lry_opt.size()){
209  if(imgReader.covers(ulx_opt[0],uly_opt[0],lrx_opt[0],lry_opt[0]))
210  std::cout << " -i " << input_opt[ifile] << " ";
211  }
212  else if(x_opt.size()&&y_opt.size()){
213  if(imgReader.covers(x_opt[0],y_opt[0]))
214  std::cout << " -i " << input_opt[ifile] << " ";
215  }
216  else{
217  std::cerr << "Error: failing extent (-e), bounding box or x and y position to define coverage" << std::endl;
218  exit(1);
219  }
220  }
221  else if(ulx_opt.size()||uly_opt.size()||lrx_opt.size()||lry_opt.size()){
222  double ulx,uly,lrx,lry;
223  imgReader.getBoundingBox(ulx,uly,lrx,lry);
224  if(ulx_opt.size())
225  std::cout << " --ulx=" << std::fixed << ulx << " ";
226  if(uly_opt.size())
227  std::cout << " --uly=" << std::fixed << uly << " ";
228  if(lrx_opt.size())
229  std::cout << " --lrx=" << std::fixed << lrx << " ";
230  if(lry_opt.size())
231  std::cout << " --lry=" << std::fixed << lry << " ";
232  }
233  if(colorTable_opt[0]){
234  GDALColorTable* colorTable=imgReader.getColorTable();
235  if(colorTable!=NULL){
236  for(int index=0;index<colorTable->GetColorEntryCount();++index){
237  GDALColorEntry sEntry=*(colorTable->GetColorEntry(index));
238  std::cout << index << " " << sEntry.c1 << " " << sEntry.c2 << " " << sEntry.c3 << " " << sEntry.c4 << std::endl;
239  }
240  }
241  else
242  std::cout << "-ct none ";
243  }
244  if(samples_opt[0])
245  std::cout << "--nsample " << imgReader.nrOfCol() << " ";
246  if(lines_opt[0])
247  std::cout << "--nline " << imgReader.nrOfRow() << " ";
248  if(nband_opt[0])
249  std::cout << "--nband " << imgReader.nrOfBand() << " ";
250  double minValue=0;
251  double maxValue=0;
252  double meanValue=0;
253  double stdDev=0;
254  int nband=band_opt.size();
255  if(band_opt[0]<0)
256  nband=imgReader.nrOfBand();
257  for(int iband=0;iband<nband;++iband){
258  unsigned short theBand=(band_opt[0]<0)? iband : band_opt[iband];
259  if(stat_opt[0]){
260  assert(theBand<imgReader.nrOfBand());
261  GDALProgressFunc pfnProgress;
262  void* pProgressData;
263  GDALRasterBand* rasterBand;
264  rasterBand=imgReader.getRasterBand(theBand);
265  rasterBand->ComputeStatistics(0,&minValue,&maxValue,&meanValue,&stdDev,pfnProgress,pProgressData);
266  std::cout << "-min " << minValue << " -max " << maxValue << " --mean " << meanValue << " --stdDev " << stdDev << " ";
267  }
268 
269  if(minmax_opt[0]||min_opt[0]||max_opt[0]){
270  assert(theBand<imgReader.nrOfBand());
271  if((ulx_opt.size()||uly_opt.size()||lrx_opt.size()||lry_opt.size())&&(imgReader.covers(ulx_opt[0],uly_opt[0],lrx_opt[0],lry_opt[0]))){
272  double uli,ulj,lri,lrj;
273  imgReader.geo2image(ulx_opt[0],uly_opt[0],uli,ulj);
274  imgReader.geo2image(lrx_opt[0],lry_opt[0],lri,lrj);
275  imgReader.getMinMax(static_cast<int>(uli),static_cast<int>(lri),static_cast<int>(ulj),static_cast<int>(lrj),theBand,minValue,maxValue);
276  }
277  else
278  imgReader.getMinMax(minValue,maxValue,theBand,true);
279  if(minmax_opt[0])
280  std::cout << "-min " << minValue << " -max " << maxValue << " ";
281  else{
282  if(min_opt[0])
283  std::cout << "-min " << minValue << " ";
284  if(max_opt[0])
285  std::cout << "-max " << maxValue << " ";
286  }
287  }
288  }
289  if(projection_opt[0]){
290  if(imgReader.isGeoRef())
291  std::cout << " -a_srs " << imgReader.getProjection() << " ";
292  else
293  std::cout << " -a_srs none" << " ";
294  }
295  if(geo_opt[0]&&!read_opt[0]){
296  std::cout << " -geo " << std::setprecision(12) << imgReader.getGeoTransform();
297  }
298  if(interleave_opt[0]){
299  std::cout << " --interleave " << imgReader.getInterleave() << " ";
300  }
301  if(type_opt[0]){
302  std::cout << "--otype " << GDALGetDataTypeName(imgReader.getDataType(band_opt[0])) << " ";
303  // std::cout << " -ot " << GDALGetDataTypeName(imgReader.getDataType(band_opt[0])) << " (" << static_cast<short>(imgReader.getDataType(band_opt[0])) << ")" << std::endl;
304  }
305  if(description_opt[0]){
306  // try{
307  // std::cout << "image description: " << imgReader.getImageDescription() << std::endl;
308  // }
309  // catch(...){
310  // std::cout << "catched" << std::endl;
311  // }
312  std::list<std::string> metaData;
313  imgReader.getMetadata(metaData);
314  std::list<std::string>::const_iterator lit=metaData.begin();
315  std::cout << " --description ";
316  while(lit!=metaData.end())
317  std::cout << *(lit++) << " ";
318  }
319  if(metadata_opt[0]){
320  std::cout << "Metadata: " << std::endl;
321  std::list<std::string> lmeta;
322  imgReader.getMetadata(lmeta);
323  std::list<std::string>::const_iterator lit=lmeta.begin();
324  while(lit!=lmeta.end()){
325  std::cout << *lit << std::endl;
326  ++lit;
327  }
328 // char** cmetadata=imgReader.getMetadata();
329 // while(*cmetadata!=NULL){
330 // std::cout << *(cmetadata) << std::endl;
331 // ++cmetadata;
332 // }
333  }
334  if(read_opt[0]){
335  // int nband=band_opt.size();
336  // if(band_opt[0]<0)
337  // nband=imgReader.nrOfBand();
338  std::cout.precision(12);
339  for(int iband=0;iband<nband;++iband){
340  unsigned short theBand=(band_opt[0]<0)? iband : band_opt[iband];
341  std::vector<float> rowBuffer;//buffer will be resized in readdata
342  for(int iy=0;iy<y_opt.size();++iy){
343  double theRow=y_opt[iy];
344  int ncol=(x_opt.size())? x_opt.size() : imgReader.nrOfCol();
345  for(int ix=0;ix<ncol;++ix){
346  double theCol=ix;
347  if(x_opt.size()){
348  if(geo_opt[0])
349  imgReader.geo2image(x_opt[ix],y_opt[iy],theCol,theRow);
350  else
351  theCol=x_opt[ix];
352  }
353  assert(theRow>=0);
354  assert(theRow<imgReader.nrOfRow());
355  imgReader.readData(rowBuffer,GDT_Float32, static_cast<int>(theRow), theBand);
356  assert(theCol<rowBuffer.size());
357  std::cout << rowBuffer[static_cast<int>(theCol)] << " ";
358  }
359  std::cout << std::endl;
360  }
361  }
362  }
363  if(driver_opt[0])
364  std::cout << " --oformat " << imgReader.getDriverDescription() << " ";
365  imgReader.close();
366  }
367  if((bbox_opt[0]||bbox_te_opt[0])&&input_opt.size()>1){
368  if(bbox_te_opt[0])
369  std::cout << std::setprecision(12) << "-te " << minULX << " " << minLRY << " " << maxLRX << " " << maxULY;
370  else
371  std::cout << "union bounding box: " << std::setprecision(12) << "--ulx=" << minULX << " --uly=" << maxULY << " --lrx=" << maxLRX << " --lry=" << minLRY << std::endl;
372  if(maxULX<minLRX&&minULY>maxLRY){
373  if(bbox_te_opt[0])
374  std::cout << "intersect bounding box: " << std::setprecision(12) << "-te " << maxULX << " " << maxLRY << " " << minLRX << " --lry=" << minULY << std::endl;
375  else
376  std::cout << "intersect bounding box: " << std::setprecision(12) << "--ulx=" << maxULX << " --uly=" << minULY << " --lrx=" << minLRX << " --lry=" << maxLRY << std::endl;
377  }
378  else
379  std::cout << "no intersect" << std::endl;
380  }
381  if(!read_opt[0])
382  std::cout << std::endl;
383 }
Definition: Egcs.h:26