pktools  2.6.3
Processing Kernel for geospatial data
pkdumpogr.cc
1 /**********************************************************************
2 pkdumpogr.cc: dump ogr file to text file or standard output
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 <math.h>
21 #include <string>
22 #include <fstream>
23 #include <assert.h>
24 #include "base/Optionpk.h"
25 #include "imageclasses/ImgReaderOgr.h"
26 #include "pkdumpogr.h"
27 
28 int main(int argc, char *argv[])
29 {
30  Optionpk<string> input_opt("i", "input", "Input shape file");
31  Optionpk<string> layer_opt("ln", "lname", "Layer name(s) in sample (leave empty to select all)");
32  Optionpk<string> output_opt("o", "output", "Output ASCII file");
33  Optionpk<string> attribute_opt("n", "name", "names of the attributes to select. Each attribute is stored in a separate band. Default is ALL: write all attributes", "ALL");
34  Optionpk<bool> pos_opt("pos","pos","include position (x and y)",false);
35  Optionpk<bool> transpose_opt("t","transpose","transpose output (does not work for -n ALL ",false);
36  Optionpk<short> verbose_opt("v", "verbose", "verbose (Default: 0)", 0,2);
37 
38  bool doProcess;//stop process when program was invoked with help option (-h --help)
39  try{
40  doProcess=input_opt.retrieveOption(argc,argv);
41  layer_opt.retrieveOption(argc,argv);
42  output_opt.retrieveOption(argc,argv);
43  attribute_opt.retrieveOption(argc,argv);
44  pos_opt.retrieveOption(argc,argv);
45  transpose_opt.retrieveOption(argc,argv);
46  verbose_opt.retrieveOption(argc,argv);
47  }
48  catch(string predefinedString){
49  std::cout << predefinedString << std::endl;
50  exit(0);
51  }
52  if(!doProcess){
53  cout << endl;
54  cout << "Usage: pkdumpogr -i input.txt [-o output]" << endl;
55  cout << endl;
56  std::cout << "short option -h shows basic options only, use long option --help to show all options" << std::endl;
57  exit(0);//help was invoked, stop processing
58  }
59 
60  assert(input_opt.size());
61  ImgReaderOgr imgReader;
62  try{
63  imgReader.open(input_opt[0]);
64  }
65  catch(string errorstring){
66  cerr << errorstring << endl;
67  }
68  ofstream outputFile;
69  if(output_opt.size())
70  outputFile.open(output_opt[0].c_str(),ios::out);
71 
72  ImgReaderOgr inputReader(input_opt[0]);
73 
74  //support multiple layers
75  int nlayerRead=inputReader.getDataSource()->GetLayerCount();
76  if(verbose_opt[0])
77  cout << "number of layers: " << nlayerRead << endl;
78 
79  for(int ilayer=0;ilayer<nlayerRead;++ilayer){
80  OGRLayer *readLayer=inputReader.getLayer(ilayer);
81  string currentLayername=readLayer->GetName();
82  if(layer_opt.size())
83  if(find(layer_opt.begin(),layer_opt.end(),currentLayername)==layer_opt.end())
84  continue;
85  if(verbose_opt[0])
86  cout << "processing layer " << currentLayername << endl;
87  if(layer_opt.size())
88  cout << " --lname " << currentLayername;
89 
90  if(attribute_opt[0]!="ALL"){
91  vector<double> xvector;
92  vector<double> yvector;
93  if(inputReader.getGeometryType()==wkbPoint)
94  inputReader.readXY(xvector,yvector);
95  Vector2d<std::string> theData(attribute_opt.size());
96  for(int ifield=0;ifield<attribute_opt.size();++ifield){
97  if(verbose_opt[0])
98  cout << "field: " << ifield << endl;
99  theData[ifield].clear();
100  inputReader.readData(theData[ifield],OFTReal,attribute_opt[ifield],ilayer,verbose_opt[0]);
101  }
102  if(verbose_opt[0]){
103  std::cout << "number of fields: " << theData.size() << std::endl;
104  std::cout << "number of samples: " << theData[0].size() << std::endl;
105  }
106  if(transpose_opt[0]){
107  if(pos_opt[0]&&(inputReader.getGeometryType()==wkbPoint)){
108  if(output_opt.size()){
109  outputFile << "X" << " ";
110  for(int isample=0;isample<xvector.size();++isample){
111  outputFile << xvector[isample];
112  if(isample<xvector.size()-1)
113  outputFile << " ";
114  else
115  outputFile << std::endl;
116  }
117  outputFile << "Y" << " ";
118  for(int isample=0;isample<yvector.size();++isample){
119  outputFile << yvector[isample];
120  if(isample<yvector.size()-1)
121  outputFile << " ";
122  else
123  outputFile << std::endl;
124  }
125  }
126  else{
127  std::cout << "X" << " ";
128  for(int isample=0;isample<xvector.size();++isample){
129  std::cout << xvector[isample];
130  if(isample<xvector.size()-1)
131  std::cout << " ";
132  else
133  std::cout << std::endl;
134  }
135  std::cout << "Y" << " ";
136  for(int isample=0;isample<yvector.size();++isample){
137  std::cout << yvector[isample];
138  if(isample<yvector.size()-1)
139  std::cout << " ";
140  else
141  std::cout << std::endl;
142  }
143  }
144  }
145  for(int ifield=0;ifield<theData.size();++ifield){
146  if(output_opt.size()){
147  outputFile << ifield << " ";
148  for(int isample=0;isample<theData[0].size();++isample){
149  outputFile << theData[ifield][isample];
150  if(isample<theData[0].size()-1)
151  outputFile << " ";
152  else
153  outputFile << std::endl;
154  }
155  }
156  else{
157  std::cout << ifield << " ";
158  for(int isample=0;isample<theData[0].size();++isample){
159  std::cout << theData[ifield][isample];
160  if(isample<theData[0].size()-1)
161  std::cout << " ";
162  else
163  std::cout << std::endl;
164  }
165  }
166  }
167  }
168  else{
169  for(int isample=0;isample<theData[0].size();++isample){
170  if(output_opt.size()){
171  outputFile << isample << " ";
172  if(pos_opt[0])
173  outputFile << xvector[isample] << " " << yvector[isample] << " ";
174  for(int ifield=0;ifield<theData.size();++ifield){
175  outputFile << theData[ifield][isample];
176  if(ifield<theData.size()-1)
177  outputFile << " ";
178  else
179  outputFile << std::endl;
180  }
181  }
182  else{
183  std::cout << isample << " ";
184  if(pos_opt[0])
185  std::cout << xvector[isample] << " " << yvector[isample] << " ";
186  for(int ifield=0;ifield<theData.size();++ifield){
187  std::cout << theData[ifield][isample];
188  if(ifield<theData.size()-1)
189  std::cout << " ";
190  else
191  std::cout << std::endl;
192  }
193  }
194  }
195  }
196  if(output_opt.size())
197  outputFile.close();
198  }
199  else{
200  if(output_opt.size()){
201  ofstream outputFile(output_opt[0].c_str(),ios::out);
202  outputFile << imgReader;
203  outputFile.close();
204  }
205  else
206  std::cout << imgReader;
207  }
208  }
209  inputReader.close();
210  imgReader.close();
211 }
212