pktools  2.6.7
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 /******************************************************************************/
72 using namespace std;
73 
74 int main(int argc, char *argv[])
75 {
76  Optionpk<std::string> input_opt("i","input","input ASCII file");
77  Optionpk<string> output_opt("o", "output", "Output image file");
78  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");
79  Optionpk<string> imageType_opt("of", "oformat", "image type string (see also gdal_translate)", "GTiff");
80  Optionpk<string> option_opt("co", "co", "Creation option for output file. Multiple options can be specified.");
81  Optionpk<double> ulx_opt("ulx", "ulx", "Upper left x value bounding box (in geocoordinates if georef is true)", 0.0);
82  Optionpk<double> uly_opt("uly", "uly", "Upper left y value bounding box (in geocoordinates if georef is true)", 0.0);
83  Optionpk<double> dx_opt("dx", "dx", "Output resolution in x (in meter)");
84  Optionpk<double> dy_opt("dy", "dy", "Output resolution in y (in meter)");
85  Optionpk<string> projection_opt("a_srs", "a_srs", "Override the projection for the output file");
86  Optionpk<string> colorTable_opt("ct", "ct", "color table (file with 5 columns: id R G B ALFA (0: transparent, 255: solid)");
87  Optionpk<string> description_opt("d", "description", "Set image description");
88  Optionpk<bool> verbose_opt("v", "verbose", "verbose", false,2);
89 
90  bool doProcess;//stop process when program was invoked with help option (-h --help)
91  try{
92  doProcess=input_opt.retrieveOption(argc,argv);
93  output_opt.retrieveOption(argc,argv);
94  dataType_opt.retrieveOption(argc,argv);
95  imageType_opt.retrieveOption(argc,argv);
96  option_opt.retrieveOption(argc,argv);
97  ulx_opt.retrieveOption(argc,argv);
98  uly_opt.retrieveOption(argc,argv);
99  dx_opt.retrieveOption(argc,argv);
100  dy_opt.retrieveOption(argc,argv);
101  colorTable_opt.retrieveOption(argc,argv);
102  projection_opt.retrieveOption(argc,argv);
103  description_opt.retrieveOption(argc,argv);
104  verbose_opt.retrieveOption(argc,argv);
105  }
106  catch(string predefinedString){
107  std::cout << predefinedString << std::endl;
108  exit(0);
109  }
110  if(!doProcess){
111  cout << endl;
112  cout << "Usage: pkascii2img -i input.txt -o output" << endl;
113  cout << endl;
114  std::cout << "short option -h shows basic options only, use long option --help to show all options" << std::endl;
115  exit(0);//help was invoked, stop processing
116  }
117 
118  assert(input_opt.size());
119  assert(output_opt.size());
120  ImgWriterGdal imgWriter;
121  ifstream ifile(input_opt[0].c_str(),ios::in);
122  //get number of lines
123  string line;
124  int nrow=0;
125  int ncol=0;
126  int irow=0;
127  string interleave="BAND";
128  vector< vector<double> > data;
129  vector<double> row;
130  double value;
131  try{
132  while(getline(ifile,line)){
133  row.clear();
134  //read data from ascii file
135  istringstream ist(line);
136  while(ist>>value)
137  row.push_back(value);
138  if(!ncol){
139  ncol=row.size();
140  data.push_back(row);
141  }
142  else
143  data.push_back(row);
144  if(verbose_opt[0]){
145  for(int icol=0;icol<row.size();++icol)
146  cout << row[icol] << " ";
147  cout << endl;
148  }
149  ++irow;
150  }
151  nrow=irow;
152  assert(ncol);
153  assert(nrow);
154  if(verbose_opt[0]){
155  cout << "nrow: " << nrow << endl;
156  cout << "ncol: " << ncol << endl;
157  }
158  }
159  catch(string theError){
160  cout << theError << endl;
161  }
162 
163  GDALDataType dataType=GDT_Unknown;
164  if(verbose_opt[0])
165  cout << "possible output data types: ";
166  for(int iType = 0; iType < GDT_TypeCount; ++iType){
167  if(verbose_opt[0])
168  cout << " " << GDALGetDataTypeName((GDALDataType)iType);
169  if( GDALGetDataTypeName((GDALDataType)iType) != NULL
170  && EQUAL(GDALGetDataTypeName((GDALDataType)iType),
171  dataType_opt[0].c_str()))
172  dataType=(GDALDataType) iType;
173  }
174  if(verbose_opt[0])
175  cout << endl;
176  if(verbose_opt[0]){
177  if(dataType==GDT_Unknown)
178  cout << "Unknown output pixel type: " << dataType_opt[0] << endl;
179  else
180  cout << "Output pixel type: " << GDALGetDataTypeName(dataType) << endl;
181  }
182 
183  imgWriter.open(output_opt[0],ncol,nrow,1,dataType,imageType_opt[0],option_opt);
184  if(description_opt.size())
185  imgWriter.setImageDescription(description_opt[0]);
186  if(projection_opt.size()){
187  assert(dx_opt.size());
188  assert(dy_opt.size());
189  if(verbose_opt[0])
190  cout << output_opt[0] << " is georeferenced." << endl;
191  double gt[6];
192  gt[0]=ulx_opt[0];
193  gt[1]=dx_opt[0];
194  gt[2]=0;
195  gt[3]=uly_opt[0];
196  gt[4]=0;
197  gt[5]=-dy_opt[0];
198  imgWriter.setGeoTransform(gt);
199  imgWriter.setProjectionProj4(projection_opt[0]);
200  }
201  else{
202  if(verbose_opt[0])
203  cout << output_opt[0] << " is not georeferenced." << endl;
204  assert(!imgWriter.isGeoRef());
205  }
206  if(colorTable_opt.size()){
207  assert(imgWriter.getDataType()==GDT_Byte);
208  imgWriter.setColorTable(colorTable_opt[0]);
209  }
210  assert(data.size()==nrow);
211  for(irow=0;irow<nrow;++irow)
212  imgWriter.writeData(data[irow],irow);
213  imgWriter.close();
214 }
215 
STL namespace.