pktools  2.6.3
Processing Kernel for geospatial data
pkascii2img.cc
1 /**********************************************************************
2 pkascii2img.cc: program to create raster image based on ascii file
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 <string>
21 #include <fstream>
22 #include "base/Optionpk.h"
23 #include <assert.h>
24 #include "imageclasses/ImgWriterGdal.h"
25 
26 using namespace std;
27 
28 int main(int argc, char *argv[])
29 {
30  Optionpk<std::string> input_opt("i","input","input ASCII file");
31  Optionpk<string> output_opt("o", "output", "Output image file");
32  Optionpk<string> dataType_opt("ot", "otype", "Data type for output image ({Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/CInt16/CInt32/CFloat32/CFloat64}). Empty string: inherit type from input image","Byte");
33  Optionpk<string> imageType_opt("of", "oformat", "image type string (see also gdal_translate)", "GTiff");
34  Optionpk<string> option_opt("co", "co", "Creation option for output file. Multiple options can be specified.");
35  Optionpk<double> ulx_opt("ulx", "ulx", "Upper left x value bounding box (in geocoordinates if georef is true)", 0.0);
36  Optionpk<double> uly_opt("uly", "uly", "Upper left y value bounding box (in geocoordinates if georef is true)", 0.0);
37  Optionpk<double> dx_opt("dx", "dx", "Output resolution in x (in meter)");
38  Optionpk<double> dy_opt("dy", "dy", "Output resolution in y (in meter)");
39  Optionpk<string> projection_opt("a_srs", "a_srs", "Override the projection for the output file");
40  Optionpk<string> colorTable_opt("ct", "ct", "color table (file with 5 columns: id R G B ALFA (0: transparent, 255: solid)");
41  Optionpk<string> description_opt("d", "description", "Set image description");
42  Optionpk<bool> verbose_opt("v", "verbose", "verbose", false,2);
43 
44  bool doProcess;//stop process when program was invoked with help option (-h --help)
45  try{
46  doProcess=input_opt.retrieveOption(argc,argv);
47  output_opt.retrieveOption(argc,argv);
48  dataType_opt.retrieveOption(argc,argv);
49  imageType_opt.retrieveOption(argc,argv);
50  option_opt.retrieveOption(argc,argv);
51  ulx_opt.retrieveOption(argc,argv);
52  uly_opt.retrieveOption(argc,argv);
53  dx_opt.retrieveOption(argc,argv);
54  dy_opt.retrieveOption(argc,argv);
55  colorTable_opt.retrieveOption(argc,argv);
56  projection_opt.retrieveOption(argc,argv);
57  description_opt.retrieveOption(argc,argv);
58  verbose_opt.retrieveOption(argc,argv);
59  }
60  catch(string predefinedString){
61  std::cout << predefinedString << std::endl;
62  exit(0);
63  }
64  if(!doProcess){
65  cout << endl;
66  cout << "Usage: pkascii2img -i input.txt -o output" << endl;
67  cout << endl;
68  std::cout << "short option -h shows basic options only, use long option --help to show all options" << std::endl;
69  exit(0);//help was invoked, stop processing
70  }
71 
72  assert(input_opt.size());
73  assert(output_opt.size());
74  ImgWriterGdal imgWriter;
75  ifstream ifile(input_opt[0].c_str(),ios::in);
76  //get number of lines
77  string line;
78  int nrow=0;
79  int ncol=0;
80  int irow=0;
81  string interleave="BAND";
82  vector< vector<double> > data;
83  vector<double> row;
84  double value;
85  try{
86  while(getline(ifile,line)){
87  row.clear();
88  //read data from ascii file
89  istringstream ist(line);
90  while(ist>>value)
91  row.push_back(value);
92  if(!ncol){
93  ncol=row.size();
94  data.push_back(row);
95  }
96  else
97  data.push_back(row);
98  if(verbose_opt[0]){
99  for(int icol=0;icol<row.size();++icol)
100  cout << row[icol] << " ";
101  cout << endl;
102  }
103  ++irow;
104  }
105  nrow=irow;
106  assert(ncol);
107  assert(nrow);
108  if(verbose_opt[0]){
109  cout << "nrow: " << nrow << endl;
110  cout << "ncol: " << ncol << endl;
111  }
112  }
113  catch(string theError){
114  cout << theError << endl;
115  }
116 
117  GDALDataType dataType=GDT_Unknown;
118  if(verbose_opt[0])
119  cout << "possible output data types: ";
120  for(int iType = 0; iType < GDT_TypeCount; ++iType){
121  if(verbose_opt[0])
122  cout << " " << GDALGetDataTypeName((GDALDataType)iType);
123  if( GDALGetDataTypeName((GDALDataType)iType) != NULL
124  && EQUAL(GDALGetDataTypeName((GDALDataType)iType),
125  dataType_opt[0].c_str()))
126  dataType=(GDALDataType) iType;
127  }
128  if(verbose_opt[0])
129  cout << endl;
130  if(verbose_opt[0]){
131  if(dataType==GDT_Unknown)
132  cout << "Unknown output pixel type: " << dataType_opt[0] << endl;
133  else
134  cout << "Output pixel type: " << GDALGetDataTypeName(dataType) << endl;
135  }
136 
137  imgWriter.open(output_opt[0],ncol,nrow,1,dataType,imageType_opt[0],option_opt);
138  if(description_opt.size())
139  imgWriter.setImageDescription(description_opt[0]);
140  if(projection_opt.size()){
141  assert(dx_opt.size());
142  assert(dy_opt.size());
143  if(verbose_opt[0])
144  cout << output_opt[0] << " is georeferenced." << endl;
145  double gt[6];
146  gt[0]=ulx_opt[0];
147  gt[1]=dx_opt[0];
148  gt[2]=0;
149  gt[3]=uly_opt[0];
150  gt[4]=0;
151  gt[5]=-dy_opt[0];
152  imgWriter.setGeoTransform(gt);
153  imgWriter.setProjectionProj4(projection_opt[0]);
154  }
155  else{
156  if(verbose_opt[0])
157  cout << output_opt[0] << " is not georeferenced." << endl;
158  assert(!imgWriter.isGeoRef());
159  }
160  if(colorTable_opt.size()){
161  assert(imgWriter.getDataType()==GDT_Byte);
162  imgWriter.setColorTable(colorTable_opt[0]);
163  }
164  assert(data.size()==nrow);
165  for(irow=0;irow<nrow;++irow)
166  imgWriter.writeData(data[irow],GDT_Float64,irow);
167  imgWriter.close();
168 }
169