[pktools] 175/375: redundant ilayer loop in readDataImageShape in ImgReaderOgr.cc

Bas Couwenberg sebastic at xs4all.nl
Wed Dec 3 21:54:11 UTC 2014


This is an automated email from the git hooks/post-receive script.

sebastic-guest pushed a commit to branch upstream-master
in repository pktools.

commit 8f2c83b00c79924faa51b65434cc134e42faace4
Author: Pieter Kempeneers <kempenep at gmail.com>
Date:   Sat Jan 4 21:36:07 2014 +0100

    redundant ilayer loop in readDataImageShape in ImgReaderOgr.cc
---
 src/apps/pkclassify_nn.cc        | 230 +++++++++++++++++----------------
 src/apps/pkclassify_svm.cc       | 272 ++++++++++++++++++++-------------------
 src/apps/pkextract.cc            |   2 +-
 src/imageclasses/ImgReaderOgr.cc | 104 +++++++--------
 src/imageclasses/ImgReaderOgr.h  |   1 +
 5 files changed, 315 insertions(+), 294 deletions(-)

diff --git a/src/apps/pkclassify_nn.cc b/src/apps/pkclassify_nn.cc
index e369096..3fadf15 100644
--- a/src/apps/pkclassify_nn.cc
+++ b/src/apps/pkclassify_nn.cc
@@ -981,121 +981,131 @@ int main(int argc, char *argv[])
 	else
 	  imgWriterOgr.createField("class",OFTString);
       }
-      OGRFeature *poFeature;
-      unsigned int ifeature=0;
-      unsigned int nFeatures=imgReaderOgr.getFeatureCount();
-      while( (poFeature = imgReaderOgr.getLayer()->GetNextFeature()) != NULL ){
-        if(verbose_opt[0]>1)
-          cout << "feature " << ifeature << endl;
-        if( poFeature == NULL )
-          break;
-        OGRFeature *poDstFeature = NULL;
-	if(output_opt.size()){
-          poDstFeature=imgWriterOgr.createFeature();
-          if( poDstFeature->SetFrom( poFeature, TRUE ) != OGRERR_NONE ){
-            CPLError( CE_Failure, CPLE_AppDefined,
-                      "Unable to translate feature %d from layer %s.\n",
-                      poFeature->GetFID(), imgWriterOgr.getLayerName().c_str() );
-            OGRFeature::DestroyFeature( poFeature );
-            OGRFeature::DestroyFeature( poDstFeature );
-          }
-        }
-        vector<float> validationPixel;
-        vector<float> validationFeature;
+      if(verbose_opt[0])
+	cout << "number of layers in input ogr file: " << imgReaderOgr.getLayerCount() << endl;
+      for(int ilayer=0;ilayer<imgReaderOgr.getLayerCount();++ilayer){
+	if(verbose_opt[0])
+	  cout << "processing input layer " << ilayer << endl;
+	unsigned int nFeatures=imgReaderOgr.getFeatureCount(ilayer);
+	unsigned int ifeature=0;
+	progress=0;
+	pfnProgress(progress,pszMessage,pProgressArg);
+	OGRFeature *poFeature;
+	while( (poFeature = imgReaderOgr.getLayer(ilayer)->GetNextFeature()) != NULL ){
+	  if(verbose_opt[0]>1)
+	    cout << "feature " << ifeature << endl;
+	  if( poFeature == NULL ){
+	    cout << "Warning: could not read feature " << ifeature << " in layer " << imgReaderOgr.getLayerName(ilayer) << endl;
+	    continue;
+	  }
+	  OGRFeature *poDstFeature = NULL;
+	  if(output_opt.size()){
+	    poDstFeature=imgWriterOgr.createFeature();
+	    if( poDstFeature->SetFrom( poFeature, TRUE ) != OGRERR_NONE ){
+	      CPLError( CE_Failure, CPLE_AppDefined,
+			"Unable to translate feature %d from layer %s.\n",
+			poFeature->GetFID(), imgWriterOgr.getLayerName().c_str() );
+	      OGRFeature::DestroyFeature( poFeature );
+	      OGRFeature::DestroyFeature( poDstFeature );
+	    }
+	  }
+	  vector<float> validationPixel;
+	  vector<float> validationFeature;
         
-        imgReaderOgr.readData(validationPixel,OFTReal,fields,poFeature);
-        assert(validationPixel.size()==nband);
-        vector<float> probOut(nclass);//posterior prob for each class
-        for(int iclass=0;iclass<nclass;++iclass)
-          probOut[iclass]=0;
-        for(int ibag=0;ibag<nbag;++ibag){
-          for(int iband=0;iband<nband;++iband){
-            validationFeature.push_back((validationPixel[iband]-offset[ibag][iband])/scale[ibag][iband]);
-            if(verbose_opt[0]==2)
-              std:: cout << " " << validationFeature.back();
-          }
-          if(verbose_opt[0]==2)
-            std::cout << std:: endl;
-          vector<float> result(nclass);
-          result=net[ibag].run(validationFeature);
+	  imgReaderOgr.readData(validationPixel,OFTReal,fields,poFeature,ilayer);
+	  assert(validationPixel.size()==nband);
+	  vector<float> probOut(nclass);//posterior prob for each class
+	  for(int iclass=0;iclass<nclass;++iclass)
+	    probOut[iclass]=0;
+	  for(int ibag=0;ibag<nbag;++ibag){
+	    for(int iband=0;iband<nband;++iband){
+	      validationFeature.push_back((validationPixel[iband]-offset[ibag][iband])/scale[ibag][iband]);
+	      if(verbose_opt[0]==2)
+		std:: cout << " " << validationFeature.back();
+	    }
+	    if(verbose_opt[0]==2)
+	      std::cout << std:: endl;
+	    vector<float> result(nclass);
+	    result=net[ibag].run(validationFeature);
 
-          if(verbose_opt[0]>1){
-            for(int iclass=0;iclass<result.size();++iclass)
-              std::cout << result[iclass] << " ";
-            std::cout << std::endl;
-          }
-          //calculate posterior prob of bag 
-          for(int iclass=0;iclass<nclass;++iclass){
-	    result[iclass]=(result[iclass]+1.0)/2.0;//bring back to scale [0,1]
-            switch(comb_opt[0]){
-            default:
-            case(0)://sum rule
-              probOut[iclass]+=result[iclass]*priors[iclass];//add probabilities for each bag
-              break;
-            case(1)://product rule
-              probOut[iclass]*=pow(priors[iclass],static_cast<float>(1.0-nbag)/nbag)*result[iclass];//multiply probabilities for each bag
+	    if(verbose_opt[0]>1){
+	      for(int iclass=0;iclass<result.size();++iclass)
+		std::cout << result[iclass] << " ";
+	      std::cout << std::endl;
+	    }
+	    //calculate posterior prob of bag 
+	    for(int iclass=0;iclass<nclass;++iclass){
+	      result[iclass]=(result[iclass]+1.0)/2.0;//bring back to scale [0,1]
+	      switch(comb_opt[0]){
+	      default:
+	      case(0)://sum rule
+		probOut[iclass]+=result[iclass]*priors[iclass];//add probabilities for each bag
               break;
-            case(2)://max rule
-              if(priors[iclass]*result[iclass]>probOut[iclass])
-                probOut[iclass]=priors[iclass]*result[iclass];
-              break;
-            }
-          }
-        }//for ibag
-        //search for max class prob
-        float maxBag=0;
-        float normBag=0;
-        string classOut="Unclassified";
-        for(int iclass=0;iclass<nclass;++iclass){
-          if(verbose_opt[0]>1)
-            std::cout << probOut[iclass] << " ";
-          if(probOut[iclass]>maxBag){
-            maxBag=probOut[iclass];
-	    classOut=nameVector[iclass];
-          }
-        }
-        //look for class name
-        if(verbose_opt[0]>1){
-	  if(classValueMap.size())
-	    std::cout << "->" << classValueMap[classOut] << std::endl;
-	  else	    
-	    std::cout << "->" << classOut << std::endl;
-	}
-	if(output_opt.size()){
-	  if(classValueMap.size())
-	    poDstFeature->SetField("class",classValueMap[classOut]);
-	  else	    
-	    poDstFeature->SetField("class",classOut.c_str());
-	  poDstFeature->SetFID( poFeature->GetFID() );
-	}
-	int labelIndex=poFeature->GetFieldIndex(label_opt[0].c_str());
-	if(labelIndex>=0){
-	  string classRef=poFeature->GetFieldAsString(labelIndex);
-	  if(classRef!="0"){
+	      case(1)://product rule
+		probOut[iclass]*=pow(priors[iclass],static_cast<float>(1.0-nbag)/nbag)*result[iclass];//multiply probabilities for each bag
+		break;
+	      case(2)://max rule
+		if(priors[iclass]*result[iclass]>probOut[iclass])
+		  probOut[iclass]=priors[iclass]*result[iclass];
+		break;
+	      }
+	    }
+	  }//for ibag
+	  //search for max class prob
+	  float maxBag=0;
+	  float normBag=0;
+	  string classOut="Unclassified";
+	  for(int iclass=0;iclass<nclass;++iclass){
+	    if(verbose_opt[0]>1)
+	      std::cout << probOut[iclass] << " ";
+	    if(probOut[iclass]>maxBag){
+	      maxBag=probOut[iclass];
+	      classOut=nameVector[iclass];
+	    }
+	  }
+	  //look for class name
+	  if(verbose_opt[0]>1){
 	    if(classValueMap.size())
-	      cm.incrementResult(type2string<short>(classValueMap[classRef]),type2string<short>(classValueMap[classOut]),1);
-	    else
-	      cm.incrementResult(classRef,classOut,1);
+	      std::cout << "->" << classValueMap[classOut] << std::endl;
+	    else	    
+	      std::cout << "->" << classOut << std::endl;
 	  }
-	}
-        CPLErrorReset();
-	if(output_opt.size()){
-          if(imgWriterOgr.createFeature( poDstFeature ) != OGRERR_NONE){
-            CPLError( CE_Failure, CPLE_AppDefined,
-                      "Unable to translate feature %d from layer %s.\n",
-                      poFeature->GetFID(), imgWriterOgr.getLayerName().c_str() );
-            OGRFeature::DestroyFeature( poDstFeature );
-            OGRFeature::DestroyFeature( poDstFeature );
-          }
-        }
-        ++ifeature;
-        if(!verbose_opt[0]){
-          progress=static_cast<float>(ifeature+1.0)/nFeatures;
-          pfnProgress(progress,pszMessage,pProgressArg);
-        }
-        OGRFeature::DestroyFeature( poFeature );
-        OGRFeature::DestroyFeature( poDstFeature );
-      }
+	  if(output_opt.size()){
+	    if(classValueMap.size())
+	      poDstFeature->SetField("class",classValueMap[classOut]);
+	    else	    
+	      poDstFeature->SetField("class",classOut.c_str());
+	    poDstFeature->SetFID( poFeature->GetFID() );
+	  }
+	  int labelIndex=poFeature->GetFieldIndex(label_opt[0].c_str());
+	  if(labelIndex>=0){
+	    string classRef=poFeature->GetFieldAsString(labelIndex);
+	    if(classRef!="0"){
+	      if(classValueMap.size())
+		cm.incrementResult(type2string<short>(classValueMap[classRef]),type2string<short>(classValueMap[classOut]),1);
+	      else
+		cm.incrementResult(classRef,classOut,1);
+	    }
+	  }
+	  CPLErrorReset();
+	  if(output_opt.size()){
+	    if(imgWriterOgr.createFeature( poDstFeature ) != OGRERR_NONE){
+	      CPLError( CE_Failure, CPLE_AppDefined,
+			"Unable to translate feature %d from layer %s.\n",
+			poFeature->GetFID(), imgWriterOgr.getLayerName().c_str() );
+	      OGRFeature::DestroyFeature( poDstFeature );
+	      OGRFeature::DestroyFeature( poDstFeature );
+	    }
+	  }
+	  ++ifeature;
+	  if(!verbose_opt[0]){
+	    progress=static_cast<float>(ifeature+1.0)/nFeatures;
+	    pfnProgress(progress,pszMessage,pProgressArg);
+	  }
+	  OGRFeature::DestroyFeature( poFeature );
+	  OGRFeature::DestroyFeature( poDstFeature );
+	}//get next feature
+      }//next layer
       imgReaderOgr.close();
       if(output_opt.size())
       imgWriterOgr.close();
diff --git a/src/apps/pkclassify_svm.cc b/src/apps/pkclassify_svm.cc
index 8a1512c..1ed2ffa 100644
--- a/src/apps/pkclassify_svm.cc
+++ b/src/apps/pkclassify_svm.cc
@@ -57,7 +57,7 @@ int main(int argc, char *argv[])
   Optionpk<short> band_opt("b", "band", "band index (starting from 0, either use band option or use start to end)");
   Optionpk<double> offset_opt("\0", "offset", "offset value for each spectral band input features: refl[band]=(DN[band]-offset[band])/scale[band]", 0.0);
   Optionpk<double> scale_opt("\0", "scale", "scale value for each spectral band input features: refl=(DN[band]-offset[band])/scale[band] (use 0 if scale min and max in each band to -1.0 and 1.0)", 0.0);
-  Optionpk<double> priors_opt("p", "prior", "prior probabilities for each class (e.g., -p 0.3 -p 0.3 -p 0.2 )", 0.0); 
+  Optionpk<double> priors_opt("p", "prior", "prior probabilities for each class (e.g., -p 0.3 -p 0.3 -p 0.2 ). Used for input only (ignored for cross validation)", 0.0); 
   Optionpk<string> priorimg_opt("pim", "priorimg", "prior probability image (multi-band img with band for each class"); 
   Optionpk<unsigned short> cv_opt("cv", "cv", "n-fold cross validation mode",0);
   Optionpk<std::string> svm_type_opt("svmt", "svmtype", "type of SVM (C_SVC, nu_SVC,one_class, epsilon_SVR, nu_SVR)","C_SVC");
@@ -979,144 +979,154 @@ int main(int argc, char *argv[])
 	else
 	  imgWriterOgr.createField("class",OFTString);
       }
-      OGRFeature *poFeature;
-      unsigned int ifeature=0;
-      unsigned int nFeatures=imgReaderOgr.getFeatureCount();
-      while( (poFeature = imgReaderOgr.getLayer()->GetNextFeature()) != NULL ){
-        if(verbose_opt[0]>1)
-          std::cout << "feature " << ifeature << std::endl;
-        if( poFeature == NULL )
-          break;
-        OGRFeature *poDstFeature = NULL;
-	if(output_opt.size()){
-	  poDstFeature=imgWriterOgr.createFeature();
-	  if( poDstFeature->SetFrom( poFeature, TRUE ) != OGRERR_NONE ){
-	    CPLError( CE_Failure, CPLE_AppDefined,
-		      "Unable to translate feature %d from layer %s.\n",
-		      poFeature->GetFID(), imgWriterOgr.getLayerName().c_str() );
-	    OGRFeature::DestroyFeature( poFeature );
-	    OGRFeature::DestroyFeature( poDstFeature );
+      if(verbose_opt[0])
+	cout << "number of layers in input ogr file: " << imgReaderOgr.getLayerCount() << endl;
+      for(int ilayer=0;ilayer<imgReaderOgr.getLayerCount();++ilayer){
+	if(verbose_opt[0])
+	  cout << "processing input layer " << ilayer << endl;
+	unsigned int nFeatures=imgReaderOgr.getFeatureCount(ilayer);
+	unsigned int ifeature=0;
+	progress=0;
+	pfnProgress(progress,pszMessage,pProgressArg);
+	OGRFeature *poFeature;
+	while( (poFeature = imgReaderOgr.getLayer(ilayer)->GetNextFeature()) != NULL ){
+	  if(verbose_opt[0]>1)
+	    std::cout << "feature " << ifeature << std::endl;
+	  if( poFeature == NULL ){
+	      cout << "Warning: could not read feature " << ifeature << " in layer " << imgReaderOgr.getLayerName(ilayer) << endl;
+	      continue;
 	  }
-	}
-        vector<float> validationPixel;
-        vector<float> validationFeature;
+	  OGRFeature *poDstFeature = NULL;
+	  if(output_opt.size()){
+	    poDstFeature=imgWriterOgr.createFeature();
+	    if( poDstFeature->SetFrom( poFeature, TRUE ) != OGRERR_NONE ){
+	      CPLError( CE_Failure, CPLE_AppDefined,
+			"Unable to translate feature %d from layer %s.\n",
+			poFeature->GetFID(), imgWriterOgr.getLayerName().c_str() );
+	      OGRFeature::DestroyFeature( poFeature );
+	      OGRFeature::DestroyFeature( poDstFeature );
+	    }
+	  }
+	  vector<float> validationPixel;
+	  vector<float> validationFeature;
         
-        imgReaderOgr.readData(validationPixel,OFTReal,fields,poFeature);
-        assert(validationPixel.size()==nband);
-        vector<float> probOut(nclass);//posterior prob for each class
-        for(short iclass=0;iclass<nclass;++iclass)
-          probOut[iclass]=0;
-        for(int ibag=0;ibag<nbag;++ibag){
-          for(int iband=0;iband<nband;++iband){
-            validationFeature.push_back((validationPixel[iband]-offset[ibag][iband])/scale[ibag][iband]);
-            if(verbose_opt[0]==2)
-              std::cout << " " << validationFeature.back();
-          }
-          if(verbose_opt[0]==2)
-            std::cout << std::endl;
-          vector<double> result(nclass);
-          struct svm_node *x;
-          x = (struct svm_node *) malloc((validationFeature.size()+1)*sizeof(struct svm_node));
-          for(int i=0;i<validationFeature.size();++i){
-            x[i].index=i+1;
-            x[i].value=validationFeature[i];
-          }
+	  imgReaderOgr.readData(validationPixel,OFTReal,fields,poFeature,ilayer);
+	  assert(validationPixel.size()==nband);
+	  vector<float> probOut(nclass);//posterior prob for each class
+	  for(short iclass=0;iclass<nclass;++iclass)
+	    probOut[iclass]=0;
+	  for(int ibag=0;ibag<nbag;++ibag){
+	    for(int iband=0;iband<nband;++iband){
+	      validationFeature.push_back((validationPixel[iband]-offset[ibag][iband])/scale[ibag][iband]);
+	      if(verbose_opt[0]==2)
+		std::cout << " " << validationFeature.back();
+	    }
+	    if(verbose_opt[0]==2)
+	      std::cout << std::endl;
+	    vector<double> result(nclass);
+	    struct svm_node *x;
+	    x = (struct svm_node *) malloc((validationFeature.size()+1)*sizeof(struct svm_node));
+	    for(int i=0;i<validationFeature.size();++i){
+	      x[i].index=i+1;
+	      x[i].value=validationFeature[i];
+	    }
 
-          x[validationFeature.size()].index=-1;//to end svm feature vector
-          double predict_label=0;
-          if(!prob_est_opt[0]){
-            predict_label = svm_predict(svm[ibag],x);
-            for(short iclass=0;iclass<nclass;++iclass){
-              if(iclass==static_cast<short>(predict_label))
-                result[iclass]=1;
-              else
-                result[iclass]=0;
-            }
-          }
-          else{
-            assert(svm_check_probability_model(svm[ibag]));
-            predict_label = svm_predict_probability(svm[ibag],x,&(result[0]));
-          }
-          if(verbose_opt[0]>1){
-            std::cout << "predict_label: " << predict_label << std::endl;
-            for(int iclass=0;iclass<result.size();++iclass)
-              std::cout << result[iclass] << " ";
-            std::cout << std::endl;
-          }
+	    x[validationFeature.size()].index=-1;//to end svm feature vector
+	    double predict_label=0;
+	    if(!prob_est_opt[0]){
+	      predict_label = svm_predict(svm[ibag],x);
+	      for(short iclass=0;iclass<nclass;++iclass){
+		if(iclass==static_cast<short>(predict_label))
+		  result[iclass]=1;
+		else
+		  result[iclass]=0;
+	      }
+	    }
+	    else{
+	      assert(svm_check_probability_model(svm[ibag]));
+	      predict_label = svm_predict_probability(svm[ibag],x,&(result[0]));
+	    }
+	    if(verbose_opt[0]>1){
+	      std::cout << "predict_label: " << predict_label << std::endl;
+	      for(int iclass=0;iclass<result.size();++iclass)
+		std::cout << result[iclass] << " ";
+	      std::cout << std::endl;
+	    }
 
-          //calculate posterior prob of bag 
-          for(short iclass=0;iclass<nclass;++iclass){
-            switch(comb_opt[0]){
-            default:
-            case(0)://sum rule
-              probOut[iclass]+=result[iclass]*priors[iclass];//add probabilities for each bag
-               break;
-            case(1)://product rule
-              probOut[iclass]*=pow(priors[iclass],static_cast<float>(1.0-nbag)/nbag)*result[iclass];//multiply probabilities for each bag
-              break;
-            case(2)://max rule
-              if(priors[iclass]*result[iclass]>probOut[iclass])
-                probOut[iclass]=priors[iclass]*result[iclass];
-              break;
-            }
-          }
-          free(x);
-        }//for ibag
+	    //calculate posterior prob of bag 
+	    for(short iclass=0;iclass<nclass;++iclass){
+	      switch(comb_opt[0]){
+	      default:
+	      case(0)://sum rule
+		probOut[iclass]+=result[iclass]*priors[iclass];//add probabilities for each bag
+	      break;
+	      case(1)://product rule
+		probOut[iclass]*=pow(priors[iclass],static_cast<float>(1.0-nbag)/nbag)*result[iclass];//multiply probabilities for each bag
+		break;
+	      case(2)://max rule
+		if(priors[iclass]*result[iclass]>probOut[iclass])
+		  probOut[iclass]=priors[iclass]*result[iclass];
+		break;
+	      }
+	    }
+	    free(x);
+	  }//for ibag
 
-        //search for max class prob
-        float maxBag=0;
-        float normBag=0;
-        string classOut="Unclassified";
-        for(short iclass=0;iclass<nclass;++iclass){
-          if(verbose_opt[0]>1)
-            std::cout << probOut[iclass] << " ";
-          if(probOut[iclass]>maxBag){
-            maxBag=probOut[iclass];
-	    classOut=nameVector[iclass];
-          }
-        }
-        //look for class name
-        if(verbose_opt[0]>1){
-	  if(classValueMap.size())
-	    std::cout << "->" << classValueMap[classOut] << std::endl;
-	  else	    
-	    std::cout << "->" << classOut << std::endl;
-	}
-	if(output_opt.size()){
-	  if(classValueMap.size())
-	    poDstFeature->SetField("class",classValueMap[classOut]);
-	  else	    
-	    poDstFeature->SetField("class",classOut.c_str());
-	  poDstFeature->SetFID( poFeature->GetFID() );
-	}
-	int labelIndex=poFeature->GetFieldIndex(label_opt[0].c_str());
-	if(labelIndex>=0){
-	  string classRef=poFeature->GetFieldAsString(labelIndex);
-	  if(classRef!="0"){
+	  //search for max class prob
+	  float maxBag=0;
+	  float normBag=0;
+	  string classOut="Unclassified";
+	  for(short iclass=0;iclass<nclass;++iclass){
+	    if(verbose_opt[0]>1)
+	      std::cout << probOut[iclass] << " ";
+	    if(probOut[iclass]>maxBag){
+	      maxBag=probOut[iclass];
+	      classOut=nameVector[iclass];
+	    }
+	  }
+	  //look for class name
+	  if(verbose_opt[0]>1){
 	    if(classValueMap.size())
-	      cm.incrementResult(type2string<short>(classValueMap[classRef]),type2string<short>(classValueMap[classOut]),1);
-	    else
-	      cm.incrementResult(classRef,classOut,1);
+	      std::cout << "->" << classValueMap[classOut] << std::endl;
+	    else	    
+	      std::cout << "->" << classOut << std::endl;
 	  }
-	}
-        CPLErrorReset();
-	if(output_opt.size()){
-	  if(imgWriterOgr.createFeature( poDstFeature ) != OGRERR_NONE){
-	    CPLError( CE_Failure, CPLE_AppDefined,
-		      "Unable to translate feature %d from layer %s.\n",
-		      poFeature->GetFID(), imgWriterOgr.getLayerName().c_str() );
-	    OGRFeature::DestroyFeature( poDstFeature );
-	    OGRFeature::DestroyFeature( poDstFeature );
+	  if(output_opt.size()){
+	    if(classValueMap.size())
+	      poDstFeature->SetField("class",classValueMap[classOut]);
+	    else	    
+	      poDstFeature->SetField("class",classOut.c_str());
+	    poDstFeature->SetFID( poFeature->GetFID() );
 	  }
-	}
-        ++ifeature;
-        if(!verbose_opt[0]){
-          progress=static_cast<float>(ifeature+1.0)/nFeatures;
-          pfnProgress(progress,pszMessage,pProgressArg);
-        }
-        OGRFeature::DestroyFeature( poFeature );
-        OGRFeature::DestroyFeature( poDstFeature );
-      }
+	  int labelIndex=poFeature->GetFieldIndex(label_opt[0].c_str());
+	  if(labelIndex>=0){
+	    string classRef=poFeature->GetFieldAsString(labelIndex);
+	    if(classRef!="0"){
+	      if(classValueMap.size())
+		cm.incrementResult(type2string<short>(classValueMap[classRef]),type2string<short>(classValueMap[classOut]),1);
+	      else
+		cm.incrementResult(classRef,classOut,1);
+	    }
+	  }
+	  CPLErrorReset();
+	  if(output_opt.size()){
+	    if(imgWriterOgr.createFeature( poDstFeature ) != OGRERR_NONE){
+	      CPLError( CE_Failure, CPLE_AppDefined,
+			"Unable to translate feature %d from layer %s.\n",
+			poFeature->GetFID(), imgWriterOgr.getLayerName().c_str() );
+	      OGRFeature::DestroyFeature( poDstFeature );
+	      OGRFeature::DestroyFeature( poDstFeature );
+	    }
+	  }
+	  ++ifeature;
+	  if(!verbose_opt[0]){
+	    progress=static_cast<float>(ifeature+1.0)/nFeatures;
+	    pfnProgress(progress,pszMessage,pProgressArg);
+	  }
+	  OGRFeature::DestroyFeature( poFeature );
+	  OGRFeature::DestroyFeature( poDstFeature );
+	}//get next feature
+      }//next layer
       imgReaderOgr.close();
       if(output_opt.size())
 	imgWriterOgr.close();
diff --git a/src/apps/pkextract.cc b/src/apps/pkextract.cc
index a831110..5d6820a 100644
--- a/src/apps/pkextract.cc
+++ b/src/apps/pkextract.cc
@@ -52,7 +52,7 @@ int main(int argc, char *argv[])
   Optionpk<string> bufferOutput_opt("bu", "bu", "Buffer output shape file");
   Optionpk<short> geo_opt("g", "geo", "geo coordinates", 1);
   Optionpk<short> down_opt("down", "down", "down sampling factor. Can be used to create grid points", 1);
-  Optionpk<float> threshold_opt("t", "threshold", "threshold for selecting samples (randomly). Provide probability in percentage (>0) or absolute (<0). Use multiple threshold values (e.g. -t 80 -t 60) if more classes are to be extracted with random selection. Use value 100 to select all pixels for selected class(es)", 100);
+  Optionpk<float> threshold_opt("t", "threshold", "threshold for selecting samples (randomly). Provide probability in percentage (>0) or absolute (<0). Use a single threshold for vector sample files. If using raster land cover maps as a sample file, you can provide a threshold value for each class (e.g. -t 80 -t 60). Use value 100 to select all pixels for selected class(es)", 100);
   Optionpk<double> min_opt("min", "min", "minimum number of samples to select (0)", 0);
   Optionpk<short> boundary_opt("bo", "boundary", "boundary for selecting the sample", 1);
   // Optionpk<short> rbox_opt("rb", "rbox", "rectangular boundary box (total width in m) to draw around the selected pixel. Can not combined with class option. Use multiple rbox options for multiple boundary boxes. Use value 0 for no box)", 0);
diff --git a/src/imageclasses/ImgReaderOgr.cc b/src/imageclasses/ImgReaderOgr.cc
index 09e54c5..7adf657 100644
--- a/src/imageclasses/ImgReaderOgr.cc
+++ b/src/imageclasses/ImgReaderOgr.cc
@@ -230,57 +230,59 @@ unsigned int ImgReaderOgr::readDataImageShape(std::map<std::string,Vector2d<floa
   if(verbose)
     std::cout << "reading shape file " << m_filename  << std::endl;
   try{
-    for(int ilayer=0;ilayer<m_datasource->GetLayerCount();++ilayer){
-      //only retain bands in fields
-      getFields(fields);
-      std::vector<std::string>::iterator fit=fields.begin();
-      if(verbose>1)
-	std::cout << "reading fields: ";
-      while(fit!=fields.end()){
-	if(verbose)
-	  std::cout << *fit << " ";
-	// size_t pos=(*fit).find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_ ");
-	if((*fit).substr(0,1)=="B"||(*fit).substr(0,1)=="b"){
-	  if((*fit).substr(1).find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_ ")!=std::string::npos){
-	    int theBand=atoi((*fit).substr(1).c_str());
-	    if(bands.size()){
-	      bool validBand=false;
-	      for(int iband=0;iband<bands.size();++iband){
-		if(theBand==bands[iband])
-		  validBand=true;
-	      }
-	      if(validBand)
-		++fit;
-	      else
-		fields.erase(fit);
+    //only retain bands in fields
+    getFields(fields);
+    std::vector<std::string>::iterator fit=fields.begin();
+    if(verbose>1)
+      std::cout << "reading fields: ";
+    while(fit!=fields.end()){
+      if(verbose)
+	std::cout << *fit << " ";
+      // size_t pos=(*fit).find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_ ");
+      if((*fit).substr(0,1)=="B"||(*fit).substr(0,1)=="b"){
+	if((*fit).substr(1).find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_ ")!=std::string::npos){
+	  int theBand=atoi((*fit).substr(1).c_str());
+	  if(bands.size()){
+	    bool validBand=false;
+	    for(int iband=0;iband<bands.size();++iband){
+	      if(theBand==bands[iband])
+		validBand=true;
 	    }
-	    else
+	    if(validBand)
 	      ++fit;
+	    else
+	      fields.erase(fit);
 	  }
-	  else if((*fit)=="B" || (*fit)=="b" || (*fit)=="Band")//B is only band
+	  else
 	    ++fit;
 	}
-	else
-	  fields.erase(fit);
-      }
-      if(verbose)
-	std::cout << std::endl;
-      if(verbose){
-	std::cout << "fields:";
-	for(std::vector<std::string>::iterator fit=fields.begin();fit!=fields.end();++fit)
-	  std::cout << " " << *fit;
-	std::cout << std::endl;
+	else if((*fit)=="B" || (*fit)=="b" || (*fit)=="Band")//B is only band
+	  ++fit;
       }
+      else
+	fields.erase(fit);
+    }
+    if(verbose)
+      std::cout << std::endl;
+    if(verbose){
+      std::cout << "fields:";
+      for(std::vector<std::string>::iterator fit=fields.begin();fit!=fields.end();++fit)
+	std::cout << " " << *fit;
+      std::cout << std::endl;
+    }
+    for(int ilayer=0;ilayer<m_datasource->GetLayerCount();++ilayer){
       if(!nband){
 	if(verbose)
 	  std::cout << "reading data" << std::endl;
-	for(int ilayer=0;ilayer<m_datasource->GetLayerCount();++ilayer)
-	  nband=readData(mapPixels,OFTReal,fields,label,ilayer,true,verbose==2);
+	nband=readData(mapPixels,OFTReal,fields,label,ilayer,true,verbose==2);
       }
       else{
-	for(int ilayer=0;ilayer<m_datasource->GetLayerCount();++ilayer)
-	  assert(nband==readData(mapPixels,OFTReal,fields,label,ilayer,true,false));
+	assert(nband==readData(mapPixels,OFTReal,fields,label,ilayer,true,false));
       }
+      nsample=getFeatureCount(ilayer);
+      totalSamples+=nsample;
+      if(verbose)
+	std::cout << ": " << nsample << " samples read with " << nband << " bands" << std::endl;
     }
   }
   catch(std::string e){
@@ -288,10 +290,6 @@ unsigned int ImgReaderOgr::readDataImageShape(std::map<std::string,Vector2d<floa
     estr << e << " " << m_filename;
     throw(estr.str());
   }
-  nsample=getFeatureCount();
-  totalSamples+=nsample;
-  if(verbose)
-    std::cout << ": " << nsample << " samples read with " << nband << " bands" << std::endl;
   if(verbose)
     std::cout << "total number of samples read " << totalSamples << std::endl;
   return totalSamples;
@@ -342,15 +340,19 @@ unsigned int ImgReaderOgr::readDataImageShape(std::map<std::string,Vector2d<floa
 	std::cout << " " << *fit;
       std::cout << std::endl;
     }
-    if(!nband){
-      if(verbose)
-	std::cout << "reading data" << std::endl;
-      for(int ilayer=0;ilayer<m_datasource->GetLayerCount();++ilayer)
+    for(int ilayer=0;ilayer<m_datasource->GetLayerCount();++ilayer){
+      if(!nband){
+	if(verbose)
+	  std::cout << "reading data" << std::endl;
 	nband=readData(mapPixels,OFTReal,fields,label,ilayer,true,verbose==2);
-    }
-    else{
-      for(int ilayer=0;ilayer<m_datasource->GetLayerCount();++ilayer)
+      }
+      else{
 	assert(nband==readData(mapPixels,OFTReal,fields,label,ilayer,true,false));
+      }
+      nsample=getFeatureCount(ilayer);
+      totalSamples+=nsample;
+      if(verbose)
+	std::cout << ": " << nsample << " samples read with " << nband << " bands" << std::endl;
     }
   }
   catch(std::string e){
@@ -358,8 +360,6 @@ unsigned int ImgReaderOgr::readDataImageShape(std::map<std::string,Vector2d<floa
     estr << e << " " << m_filename;
     throw(estr.str());
   }
-  nsample=getFeatureCount();
-  totalSamples+=nsample;
   if(verbose)
     std::cout << ": " << nsample << " samples read with " << nband << " bands" << std::endl;
   if(verbose)
diff --git a/src/imageclasses/ImgReaderOgr.h b/src/imageclasses/ImgReaderOgr.h
index ea210e4..36653e6 100644
--- a/src/imageclasses/ImgReaderOgr.h
+++ b/src/imageclasses/ImgReaderOgr.h
@@ -73,6 +73,7 @@ public:
   int getFields(std::vector<OGRFieldDefn*>& fields, int layer=0) const;
   OGRDataSource* getDataSource(void) {return m_datasource;};
   OGRSFDriver* getDriver(void) const {return m_datasource->GetDriver();};
+  int getLayerCount(void) const {return m_datasource->GetLayerCount();};
 //   OGRLayer *executeSql(const std::string& output,const std::string& sqlStatement, OGRGeometry* spatialFilter=NULL);
   template<typename T> int readSql(Vector2d<T>& data, const OGRFieldType& fieldType, std::vector<std::string>& fields, const std::string& sqlStatement, OGRGeometry* spatialFilter=NULL, int layer=0, bool pos=false, bool verbose=false);
   template<typename T> int readSql(std::map<int,Vector2d<T> >& data, const OGRFieldType& fieldType, std::vector<std::string>& fields, const std::string& label, const std::string& sqlStatement, OGRGeometry* spatialFilter, int layer=0, bool pos=false, bool verbose=false);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-grass/pktools.git



More information about the Pkg-grass-devel mailing list