[pktools] 216/375: support of layer names

Bas Couwenberg sebastic at xs4all.nl
Wed Dec 3 21:54:15 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 009288c132afd04b5b2c76c832f101c544482672
Author: Pieter Kempeneers <kempenep at gmail.com>
Date:   Mon Mar 31 10:33:46 2014 +0200

    support of layer names
---
 src/apps/pkann.cc                |  6 +++---
 src/apps/pkextract.cc            | 12 ++++++++++--
 src/apps/pkfsann.cc              | 10 +++++-----
 src/apps/pkfssvm.cc              | 10 +++++-----
 src/apps/pkoptsvm.cc             | 10 +++++-----
 src/apps/pksvm.cc                |  6 +++---
 src/imageclasses/ImgReaderOgr.cc | 30 +++++++++++++++---------------
 src/imageclasses/ImgReaderOgr.h  | 22 +++++++++++-----------
 8 files changed, 57 insertions(+), 49 deletions(-)

diff --git a/src/apps/pkann.cc b/src/apps/pkann.cc
index 15c1464..fc8104d 100644
--- a/src/apps/pkann.cc
+++ b/src/apps/pkann.cc
@@ -40,7 +40,7 @@ int main(int argc, char *argv[])
   //--------------------------- command line options ------------------------------------
   Optionpk<string> input_opt("i", "input", "input image"); 
   Optionpk<string> training_opt("t", "training", "training vector file. A single vector file contains all training features (must be set as: B0, B1, B2,...) for all classes (class numbers identified by label option). Use multiple training files for bootstrap aggregation (alternative to the bag and bsize options, where a random subset is taken from a single training file)"); 
-  Optionpk<string> tlayer_opt("tln", "tln", "training layer name","");
+  Optionpk<string> tlayer_opt("tln", "tln", "training layer name(s)");
   Optionpk<string> label_opt("label", "label", "identifier for class label in training vector file.","label"); 
   Optionpk<unsigned int> balance_opt("bal", "balance", "balance the input data to this number of samples for each class", 0);
   Optionpk<bool> random_opt("random", "random", "in case of balance, randomize input data", true);
@@ -213,9 +213,9 @@ int main(int argc, char *argv[])
       try{
 	ImgReaderOgr trainingReaderBag(training_opt[ibag]);
         if(band_opt.size())
-          totalSamples=trainingReaderBag.readDataImageOgr(trainingMap,fields,band_opt,label_opt[0],tlayer_opt[0],verbose_opt[0]);
+          totalSamples=trainingReaderBag.readDataImageOgr(trainingMap,fields,band_opt,label_opt[0],tlayer_opt,verbose_opt[0]);
         else
-          totalSamples=trainingReaderBag.readDataImageOgr(trainingMap,fields,start_opt[0],end_opt[0],label_opt[0],tlayer_opt[0],verbose_opt[0]);
+          totalSamples=trainingReaderBag.readDataImageOgr(trainingMap,fields,start_opt[0],end_opt[0],label_opt[0],tlayer_opt,verbose_opt[0]);
         if(trainingMap.size()<2){
           string errorstring="Error: could not read at least two classes from training file, did you provide class labels in training sample (see option label)?";
           throw(errorstring);
diff --git a/src/apps/pkextract.cc b/src/apps/pkextract.cc
index 889faf4..fa16986 100644
--- a/src/apps/pkextract.cc
+++ b/src/apps/pkextract.cc
@@ -44,6 +44,7 @@ int main(int argc, char *argv[])
 {
   Optionpk<string> image_opt("i", "image", "Input image file");
   Optionpk<string> sample_opt("s", "sample", "Input sample vector file or class file (e.g. Corine CLC) if class option is set");
+  Optionpk<string> layer_opt("ln", "ln", "layer name(s) in sample (leave empty to select all)");
   Optionpk<string> mask_opt("m", "mask", "Mask image file");
   Optionpk<int> msknodata_opt("msknodata", "msknodata", "Mask value where image is invalid. If a single mask is used, more nodata values can be set. If more masks are used, use one value for each mask.", 1);
   Optionpk<int> class_opt("c", "class", "Class(es) to extract from input sample image. Leave empty to extract all valid data pixels from sample file");
@@ -72,6 +73,7 @@ int main(int argc, char *argv[])
   try{
     doProcess=image_opt.retrieveOption(argc,argv);
     sample_opt.retrieveOption(argc,argv);
+    layer_opt.retrieveOption(argc,argv);
     mask_opt.retrieveOption(argc,argv);
     msknodata_opt.retrieveOption(argc,argv);
     class_opt.retrieveOption(argc,argv);
@@ -773,7 +775,11 @@ int main(int argc, char *argv[])
       
     for(int ilayer=0;ilayer<nlayer;++ilayer){
       OGRLayer *readLayer=sampleReaderOgr.getLayer(ilayer);
-      cout << "processing layer " << readLayer->GetName() << endl;
+      string currentLayername=readLayer->GetName();
+      if(layer_opt.size())
+	if(find(layer_opt.begin(),layer_opt.end(),currentLayername)==layer_opt.end())
+	  continue;
+      cout << "processing layer " << currentLayername << endl;
       readLayer->ResetReading();
       OGRLayer *writeLayer;
       OGRLayer *writeTestLayer;
@@ -798,9 +804,11 @@ int main(int argc, char *argv[])
 	}
       }
       if(verbose_opt[0])
-	std::cout << "copy fields" << std::flush << std::endl;
+	std::cout << "copy fields from layer " << ilayer << std::flush << std::endl;
       ogrWriter.copyFields(sampleReaderOgr,ilayer);
 
+      //hiero test: todo does not work
+      cout << "debug0" << endl;
       if(test_opt.size()){
 	if(verbose_opt[0])
 	  std::cout << "copy fields test writer" << std::flush << std::endl;
diff --git a/src/apps/pkfsann.cc b/src/apps/pkfsann.cc
index b115dcb..44ddc5e 100644
--- a/src/apps/pkfsann.cc
+++ b/src/apps/pkfsann.cc
@@ -198,7 +198,7 @@ int main(int argc, char *argv[])
   //--------------------------- command line options ------------------------------------
   Optionpk<string> input_opt("i", "input", "input test set (leave empty to perform a cross validation based on training only)"); 
   Optionpk<string> training_opt("t", "training", "training vector file. A single vector file contains all training features (must be set as: B0, B1, B2,...) for all classes (class numbers identified by label option). Use multiple training files for bootstrap aggregation (alternative to the bag and bsize options, where a random subset is taken from a single training file)"); 
-  Optionpk<string> tlayer_opt("tln", "tln", "training layer name","");
+  Optionpk<string> tlayer_opt("tln", "tln", "training layer name(s)");
   Optionpk<string> label_opt("\0", "label", "identifier for class label in training vector file.","label"); 
   Optionpk<unsigned short> maxFeatures_opt("n", "nf", "number of features to select (0 to select optimal number, see also ecost option)", 0);
   Optionpk<unsigned int> balance_opt("\0", "balance", "balance the input data to this number of samples for each class", 0);
@@ -310,18 +310,18 @@ int main(int argc, char *argv[])
   try{
     ImgReaderOgr trainingReader(training_opt[0]);
     if(band_opt.size()){
-      totalSamples=trainingReader.readDataImageOgr(trainingMap,fields,band_opt,label_opt[0],tlayer_opt[0],verbose_opt[0]);
+      totalSamples=trainingReader.readDataImageOgr(trainingMap,fields,band_opt,label_opt[0],tlayer_opt,verbose_opt[0]);
       if(input_opt.size()){
 	ImgReaderOgr inputReader(input_opt[0]);
-	totalTestSamples=trainingReader.readDataImageOgr(testMap,fields,band_opt,label_opt[0],tlayer_opt[0],verbose_opt[0]);
+	totalTestSamples=trainingReader.readDataImageOgr(testMap,fields,band_opt,label_opt[0],tlayer_opt,verbose_opt[0]);
 	inputReader.close();
       }
     }
     else{
-      totalSamples=trainingReader.readDataImageOgr(trainingMap,fields,start_opt[0],end_opt[0],label_opt[0],tlayer_opt[0],verbose_opt[0]);
+      totalSamples=trainingReader.readDataImageOgr(trainingMap,fields,start_opt[0],end_opt[0],label_opt[0],tlayer_opt,verbose_opt[0]);
       if(input_opt.size()){
 	ImgReaderOgr inputReader(input_opt[0]);
-	totalTestSamples=trainingReader.readDataImageOgr(testMap,fields,start_opt[0],end_opt[0],label_opt[0],tlayer_opt[0],verbose_opt[0]);
+	totalTestSamples=trainingReader.readDataImageOgr(testMap,fields,start_opt[0],end_opt[0],label_opt[0],tlayer_opt,verbose_opt[0]);
 	inputReader.close();
       }
     }
diff --git a/src/apps/pkfssvm.cc b/src/apps/pkfssvm.cc
index 4dcf05b..ed7f282 100644
--- a/src/apps/pkfssvm.cc
+++ b/src/apps/pkfssvm.cc
@@ -222,7 +222,7 @@ int main(int argc, char *argv[])
   //--------------------------- command line options ------------------------------------
   Optionpk<string> input_opt("i", "input", "input test set (leave empty to perform a cross validation based on training only)"); 
   Optionpk<string> training_opt("t", "training", "training vector file. A single vector file contains all training features (must be set as: B0, B1, B2,...) for all classes (class numbers identified by label option)."); 
-  Optionpk<string> tlayer_opt("tln", "tln", "training layer name","");
+  Optionpk<string> tlayer_opt("tln", "tln", "training layer name(s)");
   Optionpk<string> label_opt("\0", "label", "identifier for class label in training vector file.","label"); 
   Optionpk<unsigned short> maxFeatures_opt("n", "nf", "number of features to select (0 to select optimal number, see also ecost option)", 0);
   Optionpk<unsigned int> balance_opt("\0", "balance", "balance the input data to this number of samples for each class", 0);
@@ -348,18 +348,18 @@ int main(int argc, char *argv[])
   try{
     ImgReaderOgr trainingReader(training_opt[0]);
     if(band_opt.size()){
-      totalSamples=trainingReader.readDataImageOgr(trainingMap,fields,band_opt,label_opt[0],tlayer_opt[0],verbose_opt[0]);
+      totalSamples=trainingReader.readDataImageOgr(trainingMap,fields,band_opt,label_opt[0],tlayer_opt,verbose_opt[0]);
       if(input_opt.size()){
 	ImgReaderOgr inputReader(input_opt[0]);
-	totalTestSamples=inputReader.readDataImageOgr(testMap,fields,band_opt,label_opt[0],tlayer_opt[0],verbose_opt[0]);
+	totalTestSamples=inputReader.readDataImageOgr(testMap,fields,band_opt,label_opt[0],tlayer_opt,verbose_opt[0]);
 	inputReader.close();
       }
     }
     else{
-      totalSamples=trainingReader.readDataImageOgr(trainingMap,fields,start_opt[0],end_opt[0],label_opt[0],tlayer_opt[0],verbose_opt[0]);
+      totalSamples=trainingReader.readDataImageOgr(trainingMap,fields,start_opt[0],end_opt[0],label_opt[0],tlayer_opt,verbose_opt[0]);
       if(input_opt.size()){
 	ImgReaderOgr inputReader(input_opt[0]);
-	totalTestSamples=inputReader.readDataImageOgr(testMap,fields,start_opt[0],end_opt[0],label_opt[0],tlayer_opt[0],verbose_opt[0]);
+	totalTestSamples=inputReader.readDataImageOgr(testMap,fields,start_opt[0],end_opt[0],label_opt[0],tlayer_opt,verbose_opt[0]);
 	inputReader.close();
       }
     }
diff --git a/src/apps/pkoptsvm.cc b/src/apps/pkoptsvm.cc
index ce77a17..44b2940 100644
--- a/src/apps/pkoptsvm.cc
+++ b/src/apps/pkoptsvm.cc
@@ -232,7 +232,7 @@ int main(int argc, char *argv[])
   vector<int> vreclass;
   Optionpk<string> input_opt("i", "input", "input image"); 
   Optionpk<string> training_opt("t", "training", "training vector file. A single vector file contains all training features (must be set as: B0, B1, B2,...) for all classes (class numbers identified by label option)."); 
-  Optionpk<string> tlayer_opt("tln", "tln", "training layer name","");
+  Optionpk<string> tlayer_opt("tln", "tln", "training layer name(s)");
   Optionpk<string> label_opt("\0", "label", "identifier for class label in training vector file.","label"); 
   // Optionpk<unsigned short> reclass_opt("\0", "rc", "reclass code (e.g. --rc=12 --rc=23 to reclass first two classes to 12 and 23 resp.).", 0);
   Optionpk<unsigned int> balance_opt("\0", "balance", "balance the input data to this number of samples for each class", 0);
@@ -357,18 +357,18 @@ int main(int argc, char *argv[])
   try{
     ImgReaderOgr trainingReader(training_opt[0]);
     if(band_opt.size()){
-      totalSamples=trainingReader.readDataImageOgr(trainingMap,fields,band_opt,label_opt[0],tlayer_opt[0],verbose_opt[0]);
+      totalSamples=trainingReader.readDataImageOgr(trainingMap,fields,band_opt,label_opt[0],tlayer_opt,verbose_opt[0]);
       if(input_opt.size()){
 	ImgReaderOgr inputReader(input_opt[0]);
-	totalTestSamples=inputReader.readDataImageOgr(testMap,fields,band_opt,label_opt[0],tlayer_opt[0],verbose_opt[0]);
+	totalTestSamples=inputReader.readDataImageOgr(testMap,fields,band_opt,label_opt[0],tlayer_opt,verbose_opt[0]);
 	inputReader.close();
       }
     }
     else{
-      totalSamples=trainingReader.readDataImageOgr(trainingMap,fields,start_opt[0],end_opt[0],label_opt[0],tlayer_opt[0],verbose_opt[0]);
+      totalSamples=trainingReader.readDataImageOgr(trainingMap,fields,start_opt[0],end_opt[0],label_opt[0],tlayer_opt,verbose_opt[0]);
       if(input_opt.size()){
 	ImgReaderOgr inputReader(input_opt[0]);
-	totalTestSamples=inputReader.readDataImageOgr(testMap,fields,start_opt[0],end_opt[0],label_opt[0],tlayer_opt[0],verbose_opt[0]);
+	totalTestSamples=inputReader.readDataImageOgr(testMap,fields,start_opt[0],end_opt[0],label_opt[0],tlayer_opt,verbose_opt[0]);
 	inputReader.close();
       }
       trainingReader.close();
diff --git a/src/apps/pksvm.cc b/src/apps/pksvm.cc
index 38e835f..9e396ed 100644
--- a/src/apps/pksvm.cc
+++ b/src/apps/pksvm.cc
@@ -50,7 +50,7 @@ int main(int argc, char *argv[])
   //--------------------------- command line options ------------------------------------
   Optionpk<string> input_opt("i", "input", "input image"); 
   Optionpk<string> training_opt("t", "training", "training vector file. A single vector file contains all training features (must be set as: B0, B1, B2,...) for all classes (class numbers identified by label option). Use multiple training files for bootstrap aggregation (alternative to the bag and bsize options, where a random subset is taken from a single training file)");
-  Optionpk<string> tlayer_opt("tln", "tln", "training layer name","");
+  Optionpk<string> tlayer_opt("tln", "tln", "training layer name(s)");
   Optionpk<string> label_opt("label", "label", "identifier for class label in training vector file.","label"); 
   Optionpk<unsigned int> balance_opt("bal", "balance", "balance the input data to this number of samples for each class", 0);
   Optionpk<bool> random_opt("random", "random", "in case of balance, randomize input data", true);
@@ -255,9 +255,9 @@ int main(int argc, char *argv[])
       try{
 	ImgReaderOgr trainingReaderBag(training_opt[ibag]);
         if(band_opt.size())
-          totalSamples=trainingReaderBag.readDataImageOgr(trainingMap,fields,band_opt,label_opt[0],tlayer_opt[0],verbose_opt[0]);
+          totalSamples=trainingReaderBag.readDataImageOgr(trainingMap,fields,band_opt,label_opt[0],tlayer_opt,verbose_opt[0]);
         else
-          totalSamples=trainingReaderBag.readDataImageOgr(trainingMap,fields,start_opt[0],end_opt[0],tlayer_opt[0],label_opt[0],verbose_opt[0]);
+          totalSamples=trainingReaderBag.readDataImageOgr(trainingMap,fields,start_opt[0],end_opt[0],label_opt[0],tlayer_opt,verbose_opt[0]);
         if(trainingMap.size()<2){
           string errorstring="Error: could not read at least two classes from training file, did you provide class labels in training sample (see option label)?";
           throw(errorstring);
diff --git a/src/imageclasses/ImgReaderOgr.cc b/src/imageclasses/ImgReaderOgr.cc
index 8472796..db5d7cc 100644
--- a/src/imageclasses/ImgReaderOgr.cc
+++ b/src/imageclasses/ImgReaderOgr.cc
@@ -218,11 +218,11 @@ std::ostream& operator<<(std::ostream& theOstream, ImgReaderOgr& theImageReader)
 // }
 
 unsigned int ImgReaderOgr::readDataImageOgr(std::map<std::string,Vector2d<float> > &mapPixels, //[classNr][pixelNr][bandNr],
-					      std::vector<std::string>& fields,
-					      const std::vector<short>& bands,
-					      const std::string& label,
-					      const std::string& layername,
-					      int verbose)
+					    std::vector<std::string>& fields,
+					    const std::vector<short>& bands,
+					    const std::string& label,
+					    const std::vector<std::string>& layers,
+					    int verbose)
 {
   mapPixels.clear();
   int nsample=0;
@@ -232,8 +232,8 @@ unsigned int ImgReaderOgr::readDataImageOgr(std::map<std::string,Vector2d<float>
     std::cout << "reading shape file " << m_filename  << std::endl;
   for(int ilayer=0;ilayer<getLayerCount();++ilayer){
     std::string currentLayername=getLayer(ilayer)->GetName();
-    if(!layername.empty())
-      if(currentLayername!=layername)
+    if(layers.size())
+      if(find(layers.begin(),layers.end(),currentLayername)==layers.end())
 	continue;
     try{
       //only retain bands in fields
@@ -301,12 +301,12 @@ unsigned int ImgReaderOgr::readDataImageOgr(std::map<std::string,Vector2d<float>
 }
 
 unsigned int ImgReaderOgr::readDataImageOgr(std::map<std::string,Vector2d<float> > &mapPixels, //[classNr][pixelNr][bandNr],
-					      std::vector<std::string>& fields,
-					      double start,
-					      double end,
-					      const std::string& label,
-					      const std::string& layername,
-					      int verbose)
+					    std::vector<std::string>& fields,
+					    double start,
+					    double end,
+					    const std::string& label,
+					    const std::vector<std::string>& layers,
+					    int verbose)
 {
   mapPixels.clear();
   int nsample=0;
@@ -316,8 +316,8 @@ unsigned int ImgReaderOgr::readDataImageOgr(std::map<std::string,Vector2d<float>
     std::cout << "reading shape file " << m_filename  << std::endl;
   for(int ilayer=0;ilayer<getLayerCount();++ilayer){
     std::string currentLayername=getLayer(ilayer)->GetName();
-    if(!layername.empty())
-      if(currentLayername!=layername)
+    if(layers.size())
+      if(find(layers.begin(),layers.end(),currentLayername)==layers.end())
 	continue;
     try{
       //only retain bands in fields
diff --git a/src/imageclasses/ImgReaderOgr.h b/src/imageclasses/ImgReaderOgr.h
index 12e48c4..f15f905 100644
--- a/src/imageclasses/ImgReaderOgr.h
+++ b/src/imageclasses/ImgReaderOgr.h
@@ -49,19 +49,19 @@ public:
   template <typename T> int readData(std::map<int,Vector2d<T> >& data, const OGRFieldType& fieldType, std::vector<std::string>& fields, const std::string& label, int layer=0, bool pos=false, bool verbose=false);//default layer 0 and no pos information in data
   template <typename T> int readData(std::map<std::string,Vector2d<T> >& data, const OGRFieldType& fieldType, std::vector<std::string>& fields, const std::string& label, int layer=0, bool pos=false, bool verbose=false);//default layer 0 and no pos information in data
   unsigned int readDataImageOgr(std::map<std::string,Vector2d<float> > &mapPixels, //[classNr][pixelNr][bandNr],
-				  std::vector<std::string>& fields,
-				  const std::vector<short>& bands,
-				  const std::string& label,
-				  const std::string& layername=std::string(),
-				  int verbose=false);
+				std::vector<std::string>& fields,
+				const std::vector<short>& bands,
+				const std::string& label,
+				const std::vector<std::string>& layers,
+				int verbose=false);
 
   unsigned int readDataImageOgr(std::map<std::string,Vector2d<float> > &mapPixels, //[classNr][pixelNr][bandNr],
-				  std::vector<std::string>& fields,
-				  double start,
-				  double end,
-				  const std::string& label,
-				  const std::string& layername=std::string(),
-				  int verbose=false);
+				std::vector<std::string>& fields,
+				double start,
+				double end,
+				const std::string& label,
+				const std::vector<std::string>& layers,
+				int verbose=false);
 
   void shape2ascii(std::ostream& theOstream, const std::string& pointname, int layer=0, bool verbose=false);
   unsigned long int getFeatureCount(int layer=0) const;

-- 
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