22 #include "base/Optionpk.h" 23 #include "imageclasses/ImgReaderOgr.h" 24 #include "imageclasses/ImgWriterOgr.h" 25 #include "imageclasses/ImgReaderGdal.h" 26 #include "imageclasses/ImgWriterGdal.h" 61 int main(
int argc,
char *argv[])
65 Optionpk<int> nodata_opt(
"nodata",
"nodata",
"nodata value to put in vector dataset if not valid (0)", 0);
66 Optionpk<string> code_opt(
"code",
"code",
"Recode text file (2 columns: from to)");
67 Optionpk<string> class_opt(
"c",
"class",
"list of classes to reclass (in combination with reclass option)");
68 Optionpk<string> reclass_opt(
"r",
"reclass",
"list of recoded classes (in combination with class option)");
69 Optionpk<string> fieldname_opt(
"n",
"fname",
"field name of the shape file to be replaced",
"label");
74 doProcess=input_opt.retrieveOption(argc,argv);
75 nodata_opt.retrieveOption(argc,argv);
76 code_opt.retrieveOption(argc,argv);
77 class_opt.retrieveOption(argc,argv);
78 reclass_opt.retrieveOption(argc,argv);
79 output_opt.retrieveOption(argc,argv);
80 fieldname_opt.retrieveOption(argc,argv);
81 verbose_opt.retrieveOption(argc,argv);
83 catch(
string predefinedString){
84 std::cout << predefinedString << std::endl;
89 cout <<
"Usage: pkreclassogr -i input [-c from -r to]* -o output" << endl;
91 std::cout <<
"short option -h shows basic options only, use long option --help to show all options" << std::endl;
95 if(input_opt.empty()){
96 std::cerr <<
"No input file provided (use option -i). Use --help for help information" << std::endl;
99 if(output_opt.empty()){
100 std::cerr <<
"No output file provided (use option -o). Use --help for help information" << std::endl;
105 map<string,string> codemapString;
106 map<double,double> codemap;
109 cout <<
"opening code text file " << code_opt[0] << endl;
111 codefile.open(code_opt[0].c_str());
114 while(codefile>>theKey){
115 codefile >> theValue;
116 codemapString[theKey]=theValue;
117 codemap[string2type<double>(theKey)]=string2type<double>(theValue);
122 assert(class_opt.size()==reclass_opt.size());
123 for(
int iclass=0;iclass<class_opt.size();++iclass){
124 codemapString[class_opt[iclass]]=reclass_opt[iclass];
125 codemap[string2type<double>(class_opt[iclass])]=string2type<double>(reclass_opt[iclass]);
128 assert(codemapString.size());
129 assert(codemap.size());
132 map<string,string>::iterator mit;
133 cout << codemapString.size() <<
" codes used: " << endl;
134 for(mit=codemapString.begin();mit!=codemapString.end();++mit)
135 cout << (*mit).first <<
" " << (*mit).second << endl;
137 bool refIsRaster=
false;
140 ogrReader.open(input_opt[0]);
142 catch(
string errorString){
147 cout <<
"opening " << input_opt[0] <<
" for reading " << endl;
150 cout <<
"opening " << output_opt[0] <<
" for writing " << endl;
153 cout <<
"copied layer from " << input_opt[0] << endl << flush;
154 OGRFeatureDefn *poFDefn = ogrWriter.getLayer()->GetLayerDefn();
157 cout <<
"reset reading" << endl;
158 ogrReader.getLayer()->ResetReading();
159 unsigned long int ifeature=0;
161 cout <<
"going through features" << endl << flush;
164 OGRFeature *poFeature;
165 poFeature=ogrReader.getLayer()->GetNextFeature();
168 OGRFeatureDefn *poFDefn = ogrWriter.getLayer()->GetLayerDefn();
170 for(
int iField=0;iField<poFDefn->GetFieldCount();++iField){
171 OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn(iField);
172 string fieldname=poFieldDefn->GetNameRef();
173 if(fieldname==fieldname_opt[0]){
174 string fromClass=poFeature->GetFieldAsString(iField);
175 string toClass=fromClass;
176 if(codemapString.find(fromClass)!=codemapString.end())
177 toClass=codemapString[fromClass];
178 poFeature->SetField(iField,toClass.c_str());
180 cout <<
"feature " << ifeature <<
": " << fromClass <<
"->" << poFeature->GetFieldAsInteger(iField) << endl << flush;
185 ogrWriter.createFeature(poFeature);
186 OGRFeature::DestroyFeature( poFeature );
190 cout <<
"replaced " << ifeature <<
" features" << endl;