| pktools 2.6.7 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/******************************************************************************/
70 using namespace std;
71
72 int main(int argc, char *argv[])
73{
74 Optionpk<string> input_opt("i","input","Input ASCII file");
75 Optionpk<string> output_opt("o", "output", "Output file");
76 Optionpk<string> ogrformat_opt("f", "f", "Output sample file format","ESRI Shapefile");
77 Optionpk<short> colX_opt("x", "x", "column number of x (0)", 0);
78 Optionpk<short> colY_opt("y", "y", "column number of y (1)", 1);
79 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);
80 Optionpk<string> fname_opt("n", "name", "Field names for the columns in the input ascii file");
81 Optionpk<string> ftype_opt("ot", "ot", "Field type (Real, Integer, String) for each of the fields as defined by name","Real");
82 Optionpk<string> projection_opt("a_srs", "a_srs", "Override the projection for the output file, use epsg:<code> or Wkt string", "epsg:4326");
83 Optionpk<char> fs_opt("fs","fs","field separator.",' ');
84 Optionpk<int> verbose_opt("v", "verbose", "verbose (0)", 0,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 output_opt.retrieveOption(argc,argv);
90 ogrformat_opt.retrieveOption(argc,argv);
91 colX_opt.retrieveOption(argc,argv);
92 colY_opt.retrieveOption(argc,argv);
93 polygon_opt.retrieveOption(argc,argv);
94 fname_opt.retrieveOption(argc,argv);
95 ftype_opt.retrieveOption(argc,argv);
96 projection_opt.retrieveOption(argc,argv);
97 fs_opt.retrieveOption(argc,argv);
98 verbose_opt.retrieveOption(argc,argv);
99 }
100 catch(string predefinedString){
101 std::cout << predefinedString << std::endl;
102 exit(0);
103 }
104 if(!doProcess){
105 cout << endl;
106 cout << "Usage: pkascii2ogr -i input.txt -o output" << endl;
107 cout << endl;
108 std::cout << "short option -h shows basic options only, use long option --help to show all options" << std::endl;
109 exit(0);//help was invoked, stop processing
110 }
111 if(input_opt.empty()){
112 std::cerr << "No input file provided (use option -i). Use --help for help information";
113 exit(0);
114 }
115 if(output_opt.empty()){
116 std::cerr << "No output file provided (use option -o). Use --help for help information";
117 exit(0);
118 }
119
120 string theProjection;
121 theProjection=projection_opt[0];
122 int ogr_typecount=11;//hard coded for now!
123 while(ftype_opt.size()<fname_opt.size())
124 ftype_opt.push_back(ftype_opt[0]);
125 // vector<string> fname(fname_opt.size());
126 vector<OGRFieldType> ftype(ftype_opt.size());
127 if(verbose_opt[0])
128 cout << "field types can be: ";
129 for(int ifield=0;ifield<fname_opt.size();++ifield){
130 // fname[ifield]=fname_opt[ifield];
131 for(int iType = 0; iType < ogr_typecount; ++iType){
132 if(!ifield&&verbose_opt[0])
133 cout << " " << OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType);
134 if( OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType) != NULL
135 && EQUAL(OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType),
136 ftype_opt[ifield].c_str()))
137 ftype[ifield]=(OGRFieldType) iType;
138 }
139 }
140 //todo: what if unknown?
141 if(verbose_opt[0]){
142 cout << endl << "field types are: ";
143 for(int ifield=0;ifield<ftype.size();++ifield)
144 cout << OGRFieldDefn::GetFieldTypeName(ftype[ifield]) << " ";
145 cout << endl;
146 }
147
148 ImgWriterOgr imgWriter;
149 imgWriter.open(output_opt[0],ogrformat_opt[0]);
150 try{
151 if(polygon_opt[0])
152 imgWriter.ascii2ogr(input_opt[0], "New Layer", fname_opt, ftype, colX_opt[0], colY_opt[0], theProjection, wkbPolygon, fs_opt[0]);
153 else
154 imgWriter.ascii2ogr(input_opt[0], "New Layer", fname_opt, ftype, colX_opt[0], colY_opt[0], theProjection, wkbPoint, fs_opt[0]);
155 }
156 catch(string errorString){
157 cout << errorString << endl;
158 }
159 imgWriter.close();
160}
ImgWriterOgr
Definition: ImgWriterOgr.h:36
Optionpk
Definition: Optionpk.h:107