pktools 2.6.7
Processing Kernel for geospatial data
pkdumpogr.cc
1/**********************************************************************
2pkdumpogr.cc: dump ogr file to text file or standard output
3Copyright (C) 2008-2014 Pieter Kempeneers
4
5This file is part of pktools
6
7pktools is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
11
12pktools is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along 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/******************************************************************************/
70using namespace std;
71
72int main(int argc, char *argv[])
73{
74 Optionpk<string> input_opt("i", "input", "Input shape file");
75 Optionpk<string> layer_opt("ln", "lname", "Layer name(s) in sample (leave empty to select all)");
76 Optionpk<string> output_opt("o", "output", "Output ASCII file");
77 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");
78 Optionpk<bool> pos_opt("pos","pos","include position (x and y)",false);
79 Optionpk<bool> transpose_opt("t","transpose","transpose output (does not work for -n ALL ",false);
80 Optionpk<char> fs_opt("fs","fs","field separator.",' ');
81 Optionpk<short> verbose_opt("v", "verbose", "verbose (Default: 0)", 0,2);
82
83 fs_opt.setHide(1);
84 verbose_opt.setHide(2);
85
86 bool doProcess;//stop process when program was invoked with help option (-h --help)
87 try{
88 doProcess=input_opt.retrieveOption(argc,argv);
89 layer_opt.retrieveOption(argc,argv);
90 output_opt.retrieveOption(argc,argv);
91 attribute_opt.retrieveOption(argc,argv);
92 pos_opt.retrieveOption(argc,argv);
93 transpose_opt.retrieveOption(argc,argv);
94 fs_opt.retrieveOption(argc,argv);
95 verbose_opt.retrieveOption(argc,argv);
96 }
97 catch(string predefinedString){
98 std::cout << predefinedString << std::endl;
99 exit(0);
100 }
101 if(!doProcess){
102 cout << endl;
103 cout << "Usage: pkdumpogr -i input [-o output]" << endl;
104 cout << endl;
105 std::cout << "short option -h shows basic options only, use long option --help to show all options" << std::endl;
106 exit(0);//help was invoked, stop processing
107 }
108
109 if(input_opt.empty()){
110 std::cerr << "No input file provided (use option -i). Use --help for help information" << std::endl;
111 exit(0);
112 }
113
114 ImgReaderOgr inputReader;
115 try{
116 inputReader.open(input_opt[0]);
117 }
118 catch(string errorstring){
119 cerr << errorstring << endl;
120 }
121 ofstream outputFile;
122 if(output_opt.size())
123 outputFile.open(output_opt[0].c_str(),ios::out);
124
125 inputReader.setFieldSeparator(fs_opt[0]);
126
127 //support multiple layers
128 int nlayerRead=inputReader.getDataSource()->GetLayerCount();
129 if(verbose_opt[0])
130 cout << "number of layers: " << nlayerRead << endl;
131
132 for(int ilayer=0;ilayer<nlayerRead;++ilayer){
133 OGRLayer *readLayer=inputReader.getLayer(ilayer);
134 string currentLayername=readLayer->GetName();
135 if(layer_opt.size())
136 if(find(layer_opt.begin(),layer_opt.end(),currentLayername)==layer_opt.end())
137 continue;
138 if(verbose_opt[0])
139 cout << "processing layer " << currentLayername << endl;
140 // if(layer_opt.size())
141 // cout << " --lname " << currentLayername << endl;
142
143 if(attribute_opt[0]=="ALL"){
144 attribute_opt.clear();
145 OGRFeatureDefn *poFDefn = readLayer->GetLayerDefn();
146 for(int iField=0;iField<poFDefn->GetFieldCount();++iField){
147 OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn(iField);
148 std::string fieldname=poFieldDefn->GetNameRef();
149 attribute_opt.push_back(fieldname);
150 }
151 }
152 // if(attribute_opt[0]!="ALL"){
153 vector<double> xvector;
154 vector<double> yvector;
155 if(inputReader.getGeometryType()==wkbPoint)
156 inputReader.readXY(xvector,yvector);
157 Vector2d<std::string> theData(attribute_opt.size());
158 for(int ifield=0;ifield<attribute_opt.size();++ifield){
159 if(verbose_opt[0])
160 cout << "field: " << ifield << endl;
161 theData[ifield].clear();
162 inputReader.readData(theData[ifield],OFTReal,attribute_opt[ifield],ilayer,verbose_opt[0]);
163 }
164 if(verbose_opt[0]){
165 std::cout << "number of fields: " << theData.size() << std::endl;
166 std::cout << "number of samples: " << theData[0].size() << std::endl;
167 }
168 if(transpose_opt[0]){
169 if(pos_opt[0]&&(inputReader.getGeometryType()==wkbPoint)){
170 if(output_opt.size()){
171 outputFile << "X" << fs_opt[0];
172 for(int isample=0;isample<xvector.size();++isample){
173 outputFile << xvector[isample];
174 if(isample<xvector.size()-1)
175 outputFile << fs_opt[0];
176 else
177 outputFile << std::endl;
178 }
179 outputFile << "Y" << fs_opt[0];
180 for(int isample=0;isample<yvector.size();++isample){
181 outputFile << yvector[isample];
182 if(isample<yvector.size()-1)
183 outputFile << fs_opt[0];
184 else
185 outputFile << std::endl;
186 }
187 }
188 else{
189 std::cout << "X" << fs_opt[0];
190 for(int isample=0;isample<xvector.size();++isample){
191 std::cout << xvector[isample];
192 if(isample<xvector.size()-1)
193 std::cout << fs_opt[0];
194 else
195 std::cout << std::endl;
196 }
197 std::cout << "Y" << fs_opt[0];
198 for(int isample=0;isample<yvector.size();++isample){
199 std::cout << yvector[isample];
200 if(isample<yvector.size()-1)
201 std::cout << fs_opt[0];
202 else
203 std::cout << std::endl;
204 }
205 }
206 }
207 for(int ifield=0;ifield<theData.size();++ifield){
208 if(output_opt.size()){
209 outputFile << ifield << fs_opt[0];
210 for(int isample=0;isample<theData[0].size();++isample){
211 outputFile << theData[ifield][isample];
212 if(isample<theData[0].size()-1)
213 outputFile << fs_opt[0];
214 else
215 outputFile << std::endl;
216 }
217 }
218 else{
219 std::cout << ifield << fs_opt[0];
220 for(int isample=0;isample<theData[0].size();++isample){
221 std::cout << theData[ifield][isample];
222 if(isample<theData[0].size()-1)
223 std::cout << fs_opt[0];
224 else
225 std::cout << std::endl;
226 }
227 }
228 }
229 }
230 else{
231 for(int isample=0;isample<theData[0].size();++isample){
232 if(output_opt.size()){
233 outputFile << isample << fs_opt[0];
234 if(pos_opt[0])
235 outputFile << xvector[isample] << fs_opt[0] << yvector[isample] << fs_opt[0];
236 for(int ifield=0;ifield<theData.size();++ifield){
237 outputFile << theData[ifield][isample];
238 if(ifield<theData.size()-1)
239 outputFile << fs_opt[0];
240 else
241 outputFile << std::endl;
242 }
243 }
244 else{
245 std::cout << isample << fs_opt[0];
246 if(pos_opt[0])
247 std::cout << xvector[isample] << fs_opt[0] << yvector[isample] << fs_opt[0];
248 for(int ifield=0;ifield<theData.size();++ifield){
249 std::cout << theData[ifield][isample];
250 if(ifield<theData.size()-1)
251 std::cout << fs_opt[0];
252 else
253 std::cout << std::endl;
254 }
255 }
256 }
257 }
258 if(output_opt.size())
259 outputFile.close();
260 // }
261 // else{
262 // if(output_opt.size()){
263 // ofstream outputFile(output_opt[0].c_str(),ios::out);
264 // outputFile << inputReader;
265 // outputFile.close();
266 // }
267 // else
268 // std::cout << inputReader;
269 // }
270 }
271 inputReader.close();
272}
273