[pktools] 167/375: moved readDataImageShape to ImgReaderOgr.h

Bas Couwenberg sebastic at xs4all.nl
Wed Dec 3 21:54:10 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 48f0d4284676f242fc8364a451ca65008c165049
Author: Pieter Kempeneers <kempenep at gmail.com>
Date:   Thu Dec 26 23:45:09 2013 +0100

    moved readDataImageShape to ImgReaderOgr.h
---
 src/apps/Makefile.am             |   8 +-
 src/apps/pkclassify_nn.cc        |   7 +-
 src/apps/pkclassify_nn.h         | 195 ---------------------------------------
 src/apps/pkclassify_svm.cc       |   7 +-
 src/apps/pkfs_nn.cc              |  21 +++--
 src/apps/pkfs_svm.cc             |  23 +++--
 src/apps/pkopt_svm.cc            |  24 +++--
 src/imageclasses/ImgReaderOgr.cc | 144 +++++++++++++++++++++++++++++
 src/imageclasses/ImgReaderOgr.h  |  13 +++
 9 files changed, 216 insertions(+), 226 deletions(-)

diff --git a/src/apps/Makefile.am b/src/apps/Makefile.am
index eeafcff..16e7ed1 100644
--- a/src/apps/Makefile.am
+++ b/src/apps/Makefile.am
@@ -13,7 +13,7 @@ bin_PROGRAMS = pkinfo pkcrop pkreclass pkgetmask pksetmask pkcreatect pkdumpimg
 
 if USE_FANN
 bin_PROGRAMS += pkclassify_nn pkfs_nn pkregression_nn
-pkclassify_nn_SOURCES = $(top_srcdir)/src/algorithms/myfann_cpp.h pkclassify_nn.h pkclassify_nn.cc
+pkclassify_nn_SOURCES = $(top_srcdir)/src/algorithms/myfann_cpp.h pkclassify_nn.cc
 pkclassify_nn_CXXFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/src/base $(FANN_CFLAGS) -I$(top_srcdir)/src/algorithms $(AM_CXXFLAGS)
 pkclassify_nn_LDADD = $(FANN_LIBS) $(FANN_CFLAGS) $(AM_LDFLAGS)
 pkfs_nn_SOURCES = $(top_srcdir)/src/algorithms/myfann_cpp.h pkfs_nn.cc
@@ -31,7 +31,7 @@ pklas2img_LDADD = $(top_srcdir)/src/lasclasses/liblasClasses.la -llas $(AM_LDFLA
 endif
 if USE_NLOPT
 bin_PROGRAMS += pkopt_svm
-pkopt_svm_SOURCES = $(top_srcdir)/src/algorithms/OptFactory.h pkclassify_nn.h pkopt_svm.cc
+pkopt_svm_SOURCES = $(top_srcdir)/src/algorithms/OptFactory.h pkopt_svm.cc
 pkopt_svm_LDADD = $(GSL_LIBS) $(AM_LDFLAGS) -lnlopt
 endif
 # list of sources for the binaries
@@ -62,8 +62,8 @@ pkmosaic_SOURCES = pkmosaic.cc
 pkndvi_SOURCES = pkndvi.cc
 pkpolygonize_SOURCES = pkpolygonize.cc
 pkdiff_SOURCES = pkdiff.cc
-pkclassify_svm_SOURCES = $(top_srcdir)/src/algorithms/svm.h $(top_srcdir)/src/algorithms/svm.cpp pkclassify_nn.h pkclassify_svm.cc
-pkfs_svm_SOURCES = $(top_srcdir)/src/algorithms/svm.h $(top_srcdir)/src/algorithms/svm.cpp pkclassify_nn.h pkfs_svm.cc
+pkclassify_svm_SOURCES = $(top_srcdir)/src/algorithms/svm.h $(top_srcdir)/src/algorithms/svm.cpp  pkclassify_svm.cc
+pkfs_svm_SOURCES = $(top_srcdir)/src/algorithms/svm.h $(top_srcdir)/src/algorithms/svm.cpp pkfs_svm.cc
 pkfs_svm_LDADD = $(GSL_LIBS) $(AM_LDFLAGS)
 pkascii2img_SOURCES = pkascii2img.cc
 pkascii2ogr_SOURCES = pkascii2ogr.cc
diff --git a/src/apps/pkclassify_nn.cc b/src/apps/pkclassify_nn.cc
index 7c2ec41..7134308 100644
--- a/src/apps/pkclassify_nn.cc
+++ b/src/apps/pkclassify_nn.cc
@@ -17,7 +17,6 @@ GNU General Public License for more details.
 You should have received a copy of the GNU General Public License
 along with pktools.  If not, see <http://www.gnu.org/licenses/>.
 ***********************************************************************/
-#include "pkclassify_nn.h"
 #include <vector>
 #include <map>
 #include <algorithm>
@@ -203,14 +202,16 @@ int main(int argc, char *argv[])
       if(verbose_opt[0]>=1)
         cout << "reading imageShape file " << training_opt[0] << endl;
       try{
+	ImgReaderOgr trainingReaderBag(training_opt[ibag]);
         if(band_opt.size())
-          totalSamples=readDataImageShape(training_opt[ibag],trainingMap,fields,band_opt,label_opt[0],verbose_opt[0]);
+          totalSamples=trainingReaderBag.readDataImageShape(trainingMap,fields,band_opt,label_opt[0],verbose_opt[0]);
         else
-          totalSamples=readDataImageShape(training_opt[ibag],trainingMap,fields,start_opt[0],end_opt[0],label_opt[0],verbose_opt[0]);
+          totalSamples=trainingReaderBag.readDataImageShape(trainingMap,fields,start_opt[0],end_opt[0],label_opt[0],verbose_opt[0]);
         if(trainingMap.size()<2){
           string errorstring="Error: could not read at least two classes from training file";
           throw(errorstring);
         }
+	trainingReaderBag.close();
       }
       catch(string error){
         cerr << error << std::endl;
diff --git a/src/apps/pkclassify_nn.h b/src/apps/pkclassify_nn.h
deleted file mode 100644
index 28ed4ec..0000000
--- a/src/apps/pkclassify_nn.h
+++ /dev/null
@@ -1,195 +0,0 @@
-/**********************************************************************
-pkclassify_nn.h: classify raster image using Artificial Neural Network
-Copyright (C) 2008-2012 Pieter Kempeneers
-
-This file is part of pktools
-
-pktools is free software: you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
-
-pktools is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with pktools.  If not, see <http://www.gnu.org/licenses/>.
-***********************************************************************/
-#include <map>
-#include "imageclasses/ImgReaderOgr.h"
-#include "base/Vector2d.h"
-
-#ifndef _PKCLASSIFY_NN_H_
-#define _PKCLASSIFY_NN_H_
-
-using namespace std;
-
-template<typename T> unsigned int readDataImageShape(const string &filename,
-                                                     map<string,Vector2d<T> > &mapPixels, //[classNr][pixelNr][bandNr],
-                                                     vector<string>& fields,
-                                                     const vector<short>& bands,
-                                                     const string& label,
-                                                     int verbose=false);
-
-template<typename T> unsigned int readDataImageShape(const string &filename,
-                                                     map<string,Vector2d<T> > &mapPixels, //[classNr][pixelNr][bandNr],
-                                                     vector<string>& fields,
-                                                     double start,
-                                                     double end,
-                                                     const string& label,
-                                                     int verbose=false);
-
-template<typename T> unsigned int readDataImageShape(const string &filename,
-                                                     map<string,Vector2d<T> > &mapPixels, //[classNr][pixelNr][bandNr],
-                                                     vector<string>& fields,
-                                                     const vector<short>& bands,
-                                                     const string& label,
-                                                     int verbose)
-{
-  mapPixels.clear();
-  int nsample=0;
-  int totalSamples=0;  
-  int nband=0;
-  if(verbose)
-    cout << "reading shape file " << filename  << endl;
-  ImgReaderOgr imgReaderShape;
-  try{
-    imgReaderShape.open(filename);
-    //only retain bands in fields
-    imgReaderShape.getFields(fields);
-    vector<string>::iterator fit=fields.begin();
-    if(verbose>1)
-      cout << "reading fields: ";
-    while(fit!=fields.end()){
-      if(verbose)
-        cout << *fit << " ";
-      // size_t pos=(*fit).find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_ ");
-      if((*fit).substr(0,1)=="B"){
-	if((*fit).substr(1).find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_ ")!=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);
-	  }
-	  else
-	    ++fit;
-	}
-	else if((*fit)=="B" || (*fit)=="Band")//B is only band
-	  ++fit;
-      }
-      else
-        fields.erase(fit);
-    }
-    if(verbose)
-      cout << endl;
-    if(verbose){
-      cout << "fields:";
-      for(vector<string>::iterator fit=fields.begin();fit!=fields.end();++fit)
-        cout << " " << *fit;
-      cout << endl;
-    }
-    if(!nband){
-      if(verbose)
-        cout << "reading data" << endl;
-      nband=imgReaderShape.readData(mapPixels,OFTReal,fields,label,0,true,verbose==2);
-
-    }
-    else
-      assert(nband==imgReaderShape.readData(mapPixels,OFTReal,fields,label,0,true,false));
-  }
-  catch(string e){
-    ostringstream estr;
-    estr << e << " " << filename;
-    throw(estr.str());
-  }
-  nsample=imgReaderShape.getFeatureCount();
-  totalSamples+=nsample;
-  if(verbose)
-    cout << ": " << nsample << " samples read with " << nband << " bands" << endl;
-  imgReaderShape.close();
-  if(verbose)
-    cout << "total number of samples read " << totalSamples << endl;
-  return totalSamples;
-}
-
-template<typename T> unsigned int readDataImageShape(const string &filename,
-                                                     map<string,Vector2d<T> > &mapPixels, //[classNr][pixelNr][bandNr],
-                                                     vector<string>& fields,
-                                                     double start,
-                                                     double end,
-                                                     const string& label,
-                                                     int verbose)
-{
-  mapPixels.clear();
-  int nsample=0;
-  int totalSamples=0;  
-  int nband=0;
-  if(verbose)
-    cout << "reading shape file " << filename  << endl;
-  ImgReaderOgr imgReaderShape;
-  try{
-    imgReaderShape.open(filename);
-    //only retain bands in fields
-    imgReaderShape.getFields(fields);
-    vector<string>::iterator fit=fields.begin();
-    if(verbose)
-      cout << "reading fields: ";
-    while(fit!=fields.end()){
-      if(verbose)
-        cout << *fit << " ";
-      // size_t pos=(*fit).find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_ ");
-      if((*fit).substr(0,1)=="B"){
-	if((*fit).substr(1).find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_ ")!=string::npos){
-	  int iband=atoi((*fit).substr(1).c_str());
-	  if((start||end)&&(iband<start||iband>end))
-	    fields.erase(fit);
-	  else
-	    ++fit;
-	}
-	else if(*fit=="B" || *fit=="Band")
-	  ++fit;
-      }
-      else
-        fields.erase(fit);
-    }
-    if(verbose)
-      cout << endl;
-    if(verbose){
-      cout << "fields:";
-      for(vector<string>::iterator fit=fields.begin();fit!=fields.end();++fit)
-        cout << " " << *fit;
-      cout << endl;
-    }
-    if(!nband){
-      if(verbose)
-        cout << "reading data" << endl;
-      nband=imgReaderShape.readData(mapPixels,OFTReal,fields,label,0,true,verbose==2);
-
-    }
-    else
-      assert(nband==imgReaderShape.readData(mapPixels,OFTReal,fields,label,0,true,false));
-  }
-  catch(string e){
-    ostringstream estr;
-    estr << e << " " << filename;
-    throw(estr.str());
-  }
-  nsample=imgReaderShape.getFeatureCount();
-  totalSamples+=nsample;
-  if(verbose)
-    cout << ": " << nsample << " samples read with " << nband << " bands" << endl;
-  imgReaderShape.close();
-  if(verbose)
-    cout << "total number of samples read " << totalSamples << endl;
-  return totalSamples;
-}
-#endif //_PKCLASSIFY_NN_H_
diff --git a/src/apps/pkclassify_svm.cc b/src/apps/pkclassify_svm.cc
index d505cd1..9a0426e 100644
--- a/src/apps/pkclassify_svm.cc
+++ b/src/apps/pkclassify_svm.cc
@@ -28,7 +28,6 @@ along with pktools.  If not, see <http://www.gnu.org/licenses/>.
 #include "base/PosValue.h"
 #include "algorithms/ConfusionMatrix.h"
 #include "algorithms/svm.h"
-#include "pkclassify_nn.h"
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
@@ -245,14 +244,16 @@ int main(int argc, char *argv[])
       if(verbose_opt[0]>=1)
         std::cout << "reading imageShape file " << training_opt[0] << std::endl;
       try{
+	ImgReaderOgr trainingReaderBag(training_opt[ibag]);
         if(band_opt.size())
-          totalSamples=readDataImageShape(training_opt[ibag],trainingMap,fields,band_opt,label_opt[0],verbose_opt[0]);
+          totalSamples=trainingReaderBag.readDataImageShape(trainingMap,fields,band_opt,label_opt[0],verbose_opt[0]);
         else
-          totalSamples=readDataImageShape(training_opt[ibag],trainingMap,fields,start_opt[0],end_opt[0],label_opt[0],verbose_opt[0]);
+          totalSamples=trainingReaderBag.readDataImageShape(trainingMap,fields,start_opt[0],end_opt[0],label_opt[0],verbose_opt[0]);
         if(trainingMap.size()<2){
           string errorstring="Error: could not read at least two classes from training file";
           throw(errorstring);
         }
+	trainingReaderBag.close();
       }
       catch(string error){
         cerr << error << std::endl;
diff --git a/src/apps/pkfs_nn.cc b/src/apps/pkfs_nn.cc
index 53928ff..eba12e4 100644
--- a/src/apps/pkfs_nn.cc
+++ b/src/apps/pkfs_nn.cc
@@ -30,7 +30,6 @@ along with pktools.  If not, see <http://www.gnu.org/licenses/>.
 #include "algorithms/myfann_cpp.h"
 #include "algorithms/ConfusionMatrix.h"
 #include "algorithms/FeatureSelector.h"
-#include "pkclassify_nn.h"
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
@@ -317,15 +316,22 @@ int main(int argc, char *argv[])
   if(verbose_opt[0]>=1)
     std::cout << "reading imageShape file " << training_opt[0] << std::endl;
   try{
+    ImgReaderOgr trainingReader(training_opt[0]);
     if(band_opt.size()){
-      totalSamples=readDataImageShape(training_opt[0],trainingMap,fields,band_opt,label_opt[0],verbose_opt[0]);
-      if(input_opt.size())
-	totalTestSamples=readDataImageShape(input_opt[0],testMap,fields,band_opt,label_opt[0],verbose_opt[0]);
+      totalSamples=trainingReader.readDataImageShape(trainingMap,fields,band_opt,label_opt[0],verbose_opt[0]);
+      if(input_opt.size()){
+	ImgReaderOgr inputReader(input_opt[0]);
+	totalTestSamples=trainingReader.readDataImageShape(testMap,fields,band_opt,label_opt[0],verbose_opt[0]);
+	inputReader.close();
+      }
     }
     else{
-      totalSamples=readDataImageShape(training_opt[0],trainingMap,fields,start_opt[0],end_opt[0],label_opt[0],verbose_opt[0]);
-      if(input_opt.size())
-	totalTestSamples=readDataImageShape(input_opt[0],testMap,fields,start_opt[0],end_opt[0],label_opt[0],verbose_opt[0]);
+      totalSamples=trainingReader.readDataImageShape(trainingMap,fields,start_opt[0],end_opt[0],label_opt[0],verbose_opt[0]);
+      if(input_opt.size()){
+	ImgReaderOgr inputReader(input_opt[0]);
+	totalTestSamples=trainingReader.readDataImageShape(testMap,fields,start_opt[0],end_opt[0],label_opt[0],verbose_opt[0]);
+	inputReader.close();
+      }
     }
     if(trainingMap.size()<2){
       string errorstring="Error: could not read at least two classes from training file";
@@ -335,6 +341,7 @@ int main(int argc, char *argv[])
       string errorstring="Error: could not read at least two classes from test input file";
       throw(errorstring);
     }
+    trainingReader.close();
   }
   catch(string error){
     cerr << error << std::endl;
diff --git a/src/apps/pkfs_svm.cc b/src/apps/pkfs_svm.cc
index 1b12dd0..bc839cf 100644
--- a/src/apps/pkfs_svm.cc
+++ b/src/apps/pkfs_svm.cc
@@ -25,7 +25,7 @@ along with pktools.  If not, see <http://www.gnu.org/licenses/>.
 #include "algorithms/ConfusionMatrix.h"
 #include "algorithms/FeatureSelector.h"
 #include "algorithms/svm.h"
-#include "pkclassify_nn.h"
+#include "imageclasses/ImgReaderOgr.h"
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
@@ -38,6 +38,7 @@ namespace svm{
 
 enum SelectorValue  { NA=0, SFFS=1, SFS=2, SBS=3, BFS=4 };
 
+using namespace std;
 
 #define Malloc(type,n) (type *)malloc((n)*sizeof(type))
 
@@ -340,15 +341,22 @@ int main(int argc, char *argv[])
   if(verbose_opt[0]>=1)
     std::cout << "reading training file " << training_opt[0] << std::endl;
   try{
+    ImgReaderOgr trainingReader(training_opt[0]);
     if(band_opt.size()){
-      totalSamples=readDataImageShape(training_opt[0],trainingMap,fields,band_opt,label_opt[0],verbose_opt[0]);
-      if(input_opt.size())
-	totalTestSamples=readDataImageShape(input_opt[0],testMap,fields,band_opt,label_opt[0],verbose_opt[0]);
+      totalSamples=trainingReader.readDataImageShape(trainingMap,fields,band_opt,label_opt[0],verbose_opt[0]);
+      if(input_opt.size()){
+	ImgReaderOgr inputReader(input_opt[0]);
+	totalTestSamples=inputReader.readDataImageShape(testMap,fields,band_opt,label_opt[0],verbose_opt[0]);
+	inputReader.close();
+      }
     }
     else{
-      totalSamples=readDataImageShape(training_opt[0],trainingMap,fields,start_opt[0],end_opt[0],label_opt[0],verbose_opt[0]);
-      if(input_opt.size())
-	totalTestSamples=readDataImageShape(input_opt[0],testMap,fields,start_opt[0],end_opt[0],label_opt[0],verbose_opt[0]);
+      totalSamples=trainingReader.readDataImageShape(trainingMap,fields,start_opt[0],end_opt[0],label_opt[0],verbose_opt[0]);
+      if(input_opt.size()){
+	ImgReaderOgr inputReader(input_opt[0]);
+	totalTestSamples=inputReader.readDataImageShape(testMap,fields,start_opt[0],end_opt[0],label_opt[0],verbose_opt[0]);
+	inputReader.close();
+      }
     }
     if(trainingMap.size()<2){
       string errorstring="Error: could not read at least two classes from training input file";
@@ -358,6 +366,7 @@ int main(int argc, char *argv[])
       string errorstring="Error: could not read at least two classes from test input file";
       throw(errorstring);
     }
+    trainingReader.close();
   }
   catch(string error){
     cerr << error << std::endl;
diff --git a/src/apps/pkopt_svm.cc b/src/apps/pkopt_svm.cc
index d8b6f47..0a8ff6b 100644
--- a/src/apps/pkopt_svm.cc
+++ b/src/apps/pkopt_svm.cc
@@ -29,7 +29,7 @@ along with pktools.  If not, see <http://www.gnu.org/licenses/>.
 #include "algorithms/FeatureSelector.h"
 #include "algorithms/OptFactory.h"
 #include "algorithms/svm.h"
-#include "pkclassify_nn.h"
+#include "imageclasses/ImgReaderOgr.h"
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
@@ -40,6 +40,8 @@ namespace svm{
   enum KERNEL_TYPE {linear=0,polynomial=1,radial=2,sigmoid=3};
 }
 
+using namespace std;
+
 #define Malloc(type,n) (type *)malloc((n)*sizeof(type))
                                     //declare objective function
 double objFunction(const std::vector<double> &x, std::vector<double> &grad, void *my_func_data);
@@ -349,15 +351,23 @@ int main(int argc, char *argv[])
   if(verbose_opt[0]>=1)
     std::cout << "reading training file " << training_opt[0] << std::endl;
   try{
+    ImgReaderOgr trainingReader(training_opt[0]);
     if(band_opt.size()){
-      totalSamples=readDataImageShape(training_opt[0],trainingMap,fields,band_opt,label_opt[0],verbose_opt[0]);
-      if(input_opt.size())
-	totalTestSamples=readDataImageShape(input_opt[0],testMap,fields,band_opt,label_opt[0],verbose_opt[0]);
+      totalSamples=trainingReader.readDataImageShape(trainingMap,fields,band_opt,label_opt[0],verbose_opt[0]);
+      if(input_opt.size()){
+	ImgReaderOgr inputReader(input_opt[0]);
+	totalTestSamples=inputReader.readDataImageShape(testMap,fields,band_opt,label_opt[0],verbose_opt[0]);
+	inputReader.close();
+      }
     }
     else{
-      totalSamples=readDataImageShape(training_opt[0],trainingMap,fields,start_opt[0],end_opt[0],label_opt[0],verbose_opt[0]);
-      if(input_opt.size())
-	totalTestSamples=readDataImageShape(input_opt[0],testMap,fields,start_opt[0],end_opt[0],label_opt[0],verbose_opt[0]);
+      totalSamples=trainingReader.readDataImageShape(trainingMap,fields,start_opt[0],end_opt[0],label_opt[0],verbose_opt[0]);
+      if(input_opt.size()){
+	ImgReaderOgr inputReader(input_opt[0]);
+	totalTestSamples=inputReader.readDataImageShape(testMap,fields,start_opt[0],end_opt[0],label_opt[0],verbose_opt[0]);
+	inputReader.close();
+      }
+      trainingReader.close();
     }
     if(trainingMap.size()<2){
       string errorstring="Error: could not read at least two classes from training input file";
diff --git a/src/imageclasses/ImgReaderOgr.cc b/src/imageclasses/ImgReaderOgr.cc
index fe172ba..ff901cb 100644
--- a/src/imageclasses/ImgReaderOgr.cc
+++ b/src/imageclasses/ImgReaderOgr.cc
@@ -216,3 +216,147 @@ std::ostream& operator<<(std::ostream& theOstream, ImgReaderOgr& theImageReader)
 //     imgWriter.close();
 //   }
 // }
+
+unsigned int ImgReaderOgr::readDataImageShape(std::map<std::string,Vector2d<float> > &mapPixels, //[classNr][pixelNr][bandNr],
+					      std::vector<std::string>& fields,
+					      const std::vector<short>& bands,
+					      const std::string& label,
+					      int verbose)
+{
+  mapPixels.clear();
+  int nsample=0;
+  int totalSamples=0;  
+  int nband=0;
+  if(verbose)
+    std::cout << "reading shape file " << m_filename  << std::endl;
+  try{
+    //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"){
+	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);
+	  }
+	  else
+	    ++fit;
+	}
+	else if((*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;
+    }
+    if(!nband){
+      if(verbose)
+        std::cout << "reading data" << std::endl;
+      nband=readData(mapPixels,OFTReal,fields,label,0,true,verbose==2);
+
+    }
+    else
+      assert(nband==readData(mapPixels,OFTReal,fields,label,0,true,false));
+  }
+  catch(std::string e){
+    std::ostringstream estr;
+    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;
+}
+
+unsigned int ImgReaderOgr::readDataImageShape(std::map<std::string,Vector2d<float> > &mapPixels, //[classNr][pixelNr][bandNr],
+					      std::vector<std::string>& fields,
+					      double start,
+					      double end,
+					      const std::string& label,
+					      int verbose)
+{
+  mapPixels.clear();
+  int nsample=0;
+  int totalSamples=0;  
+  int nband=0;
+  if(verbose)
+    std::cout << "reading shape file " << m_filename  << std::endl;
+  try{
+    //only retain bands in fields
+    getFields(fields);
+    std::vector<std::string>::iterator fit=fields.begin();
+    if(verbose)
+      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"){
+	if((*fit).substr(1).find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_ ")!=std::string::npos){
+	  int iband=atoi((*fit).substr(1).c_str());
+	  if((start||end)&&(iband<start||iband>end))
+	    fields.erase(fit);
+	  else
+	    ++fit;
+	}
+	else if(*fit=="B" || *fit=="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;
+    }
+    if(!nband){
+      if(verbose)
+        std::cout << "reading data" << std::endl;
+      nband=readData(mapPixels,OFTReal,fields,label,0,true,verbose==2);
+
+    }
+    else
+      assert(nband==readData(mapPixels,OFTReal,fields,label,0,true,false));
+  }
+  catch(std::string e){
+    std::ostringstream estr;
+    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;
+}
diff --git a/src/imageclasses/ImgReaderOgr.h b/src/imageclasses/ImgReaderOgr.h
index 27890fd..171bcfb 100644
--- a/src/imageclasses/ImgReaderOgr.h
+++ b/src/imageclasses/ImgReaderOgr.h
@@ -48,6 +48,19 @@ public:
   template <typename T> int readData(Vector2d<T>& data, const OGRFieldType& fieldType, std::vector<std::string>& fields, 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<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 readDataImageShape(std::map<std::string,Vector2d<float> > &mapPixels, //[classNr][pixelNr][bandNr],
+						       std::vector<std::string>& fields,
+						       const std::vector<short>& bands,
+						       const std::string& label,
+						       int verbose=false);
+
+  unsigned int readDataImageShape(std::map<std::string,Vector2d<float> > &mapPixels, //[classNr][pixelNr][bandNr],
+						       std::vector<std::string>& fields,
+						       double start,
+						       double end,
+						       const std::string& label,
+						       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;
   int getFieldCount(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