pktools  2.6.3
Processing Kernel for geospatial data
pkascii2ogr.cc
1 /**********************************************************************
2 pkascii2ogr.cc: program to create vector points or polygons from text 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 <assert.h>
23 #include "base/Optionpk.h"
24 #include "imageclasses/ImgWriterOgr.h"
25 
26 using namespace std;
27 
28 int main(int argc, char *argv[])
29 {
30  Optionpk<string> input_opt("i","input","Input ASCII file");
31  Optionpk<string> output_opt("o", "output", "Output file");
32  Optionpk<string> ogrformat_opt("f", "f", "Output sample file format","ESRI Shapefile");
33  Optionpk<short> colX_opt("x", "x", "column number of x (0)", 0);
34  Optionpk<short> colY_opt("y", "y", "column number of y (1)", 1);
35  Optionpk<bool> polygon_opt("l", "line", "create OGRPolygon as geometry instead of points. Fields are taken from first point and polygon is automatically closed (no need to repeat first point at last line). (false: use OGRPoint)", false);
36  Optionpk<string> fname_opt("n", "name", "Field names for the columns in the input ascii file");
37  Optionpk<string> ftype_opt("ot", "ot", "Field type (Real, Integer, String) for each of the fields as defined by name","Real");
38  Optionpk<string> projection_opt("a_srs", "a_srs", "Override the projection for the output file, use epsg:<code> or Wkt string", "epsg:4326");
39  Optionpk<char> fs_opt("fs","fs","field separator.",' ');
40  Optionpk<int> verbose_opt("v", "verbose", "verbose (0)", 0,2);
41 
42  bool doProcess;//stop process when program was invoked with help option (-h --help)
43  try{
44  doProcess=input_opt.retrieveOption(argc,argv);
45  output_opt.retrieveOption(argc,argv);
46  ogrformat_opt.retrieveOption(argc,argv);
47  colX_opt.retrieveOption(argc,argv);
48  colY_opt.retrieveOption(argc,argv);
49  polygon_opt.retrieveOption(argc,argv);
50  fname_opt.retrieveOption(argc,argv);
51  ftype_opt.retrieveOption(argc,argv);
52  projection_opt.retrieveOption(argc,argv);
53  fs_opt.retrieveOption(argc,argv);
54  verbose_opt.retrieveOption(argc,argv);
55  }
56  catch(string predefinedString){
57  std::cout << predefinedString << std::endl;
58  exit(0);
59  }
60  if(!doProcess){
61  cout << endl;
62  cout << "Usage: pkascii2ogr -i input.txt -o output" << endl;
63  cout << endl;
64  std::cout << "short option -h shows basic options only, use long option --help to show all options" << std::endl;
65  exit(0);//help was invoked, stop processing
66  }
67  if(input_opt.empty()){
68  std::cerr << "No input file provided (use option -i). Use --help for help information";
69  exit(0);
70  }
71  if(output_opt.empty()){
72  std::cerr << "No output file provided (use option -o). Use --help for help information";
73  exit(0);
74  }
75 
76  string theProjection;
77  theProjection=projection_opt[0];
78  int ogr_typecount=11;//hard coded for now!
79  while(ftype_opt.size()<fname_opt.size())
80  ftype_opt.push_back(ftype_opt[0]);
81  // vector<string> fname(fname_opt.size());
82  vector<OGRFieldType> ftype(ftype_opt.size());
83  if(verbose_opt[0])
84  cout << "field types can be: ";
85  for(int ifield=0;ifield<fname_opt.size();++ifield){
86  // fname[ifield]=fname_opt[ifield];
87  for(int iType = 0; iType < ogr_typecount; ++iType){
88  if(!ifield&&verbose_opt[0])
89  cout << " " << OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType);
90  if( OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType) != NULL
91  && EQUAL(OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType),
92  ftype_opt[ifield].c_str()))
93  ftype[ifield]=(OGRFieldType) iType;
94  }
95  }
96  //todo: what if unknown?
97  if(verbose_opt[0]){
98  cout << endl << "field types are: ";
99  for(int ifield=0;ifield<ftype.size();++ifield)
100  cout << OGRFieldDefn::GetFieldTypeName(ftype[ifield]) << " ";
101  cout << endl;
102  }
103 
104  ImgWriterOgr imgWriter;
105  imgWriter.open(output_opt[0],ogrformat_opt[0]);
106  try{
107  if(polygon_opt[0])
108  imgWriter.ascii2ogr(input_opt[0], "New Layer", fname_opt, ftype, colX_opt[0], colY_opt[0], theProjection, wkbPolygon, fs_opt[0]);
109  else
110  imgWriter.ascii2ogr(input_opt[0], "New Layer", fname_opt, ftype, colX_opt[0], colY_opt[0], theProjection, wkbPoint, fs_opt[0]);
111  }
112  catch(string errorString){
113  cout << errorString << endl;
114  }
115  imgWriter.close();
116 }