[pktools] 111/375: string option for pkmosaic.cc

Bas Couwenberg sebastic at xs4all.nl
Wed Dec 3 21:54:05 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 3bc4208fc5301b9cd5ed282edd05e966c39368a8
Author: Pieter Kempeneers <kempenep at gmail.com>
Date:   Fri May 17 15:08:32 2013 +0200

    string option for pkmosaic.cc
---
 src/apps/pkextract.cc | 214 +++++++++++++++++++++++++++++++++++---------------
 src/apps/pkmosaic.cc  |  94 +++++++++++++---------
 2 files changed, 208 insertions(+), 100 deletions(-)

diff --git a/src/apps/pkextract.cc b/src/apps/pkextract.cc
index 74c5b44..09be56b 100644
--- a/src/apps/pkextract.cc
+++ b/src/apps/pkextract.cc
@@ -33,6 +33,9 @@ along with pktools.  If not, see <http://www.gnu.org/licenses/>.
 #define PI 3.1415926535897932384626433832795
 #endif
 
+namespace rule{
+  enum RULE_TYPE {point=0, mean=1, proportion=2, custom=3, minimum=4, maximum=5, maxvote=6};
+}
 
 int main(int argc, char *argv[])
 {
@@ -40,7 +43,7 @@ int main(int argc, char *argv[])
   Optionpk<string> sample_opt("s", "sample", "Input sample file (shape) or class file (e.g. Corine CLC) if class option is set", "");
   Optionpk<string> mask_opt("m", "mask", "Mask image file", "");
   Optionpk<int> invalid_opt("f", "flag", "Mask value where image is invalid. If a single mask is used, more flags 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. Use -1 to process every class in sample image, or 0 to extract all non-flagged pixels from sample file", 0);
+  Optionpk<int> class_opt("c", "class", "Class(es) to extract from input sample image. Use -1 to process every class in sample image, or leave empty to extract all non-flagged pixels from sample file");
   Optionpk<string> output_opt("o", "output", "Output sample file (image file)", "");
   Optionpk<string> test_opt("test", "test", "Test sample file (use this option in combination with threshold<100 to create a training (output) and test set");
   Optionpk<bool> keepFeatures_opt("k", "keep", "Keep original features in output vector file", false);
@@ -59,7 +62,7 @@ int main(int argc, char *argv[])
   Optionpk<string> label_opt("cn", "cname", "name of the class label in the output vector file", "label");
   Optionpk<bool> polygon_opt("l", "line", "create OGRPolygon as geometry instead of OGRPoint. Only if sample option is also of polygon type.", false);
   Optionpk<int> band_opt("b", "band", "band index to crop. Use -1 to use all bands)", -1);
-  Optionpk<short> rule_opt("r", "rule", "rule how to report image information per feature. 0: value at each point (or at centroid of the polygon if line is not set), 1: mean value (written to centroid of polygon if line is not set), 2: proportion classes, 3: custom, 4: minimum of polygon).", 0);
+  Optionpk<string> rule_opt("r", "rule", "rule how to report image information per feature. point (value at each point or at centroid of the polygon if line is set), mean (mean value written to centroid of polygon if line is set), proportion, minimum (of polygon), maximum (of polygon), maxvote.", "point");
   Optionpk<short> verbose_opt("v", "verbose", "verbose mode if > 0", 0);
 
   bool doProcess;//stop process when program was invoked with help option (-h --help)
@@ -99,6 +102,14 @@ int main(int argc, char *argv[])
     exit(0);//help was invoked, stop processing
   }
 
+  std::map<std::string, rule::RULE_TYPE> ruleMap;
+  //initialize ruleMap
+  ruleMap["point"]=rule::point;
+  ruleMap["mean"]=rule::mean;
+  ruleMap["proportion"]=rule::proportion;
+  ruleMap["custom"]=rule::custom;
+  ruleMap["maxvote"]=rule::maxvote;
+
   if(verbose_opt[0])
     std::cout << class_opt << std::endl;
   statfactory::StatFactory stat;
@@ -216,7 +227,7 @@ int main(int argc, char *argv[])
 
   assert(sample_opt[0]!="");
   if((sample_opt[0].find(".tif"))!=std::string::npos){//raster file
-    if(!class_opt[0]){
+    if(class_opt.empty()){
       std::cout << "Warning: no classes selected, if classes must be extracted, set to -1 for all classes using option -c -1" << std::endl;
       ImgReaderGdal classReader;
       ImgWriterOgr ogrWriter;
@@ -753,8 +764,10 @@ int main(int argc, char *argv[])
       sampleReader.getFields(fieldnames);
       assert(fieldnames.size()==ogrWriter.getFieldCount());
       map<std::string,double> pointAttributes;
-      switch(rule_opt[0]){
-      case(2):{//proportion for each class
+
+      switch(ruleMap[rule_opt[0]]){
+        // switch(rule_opt[0]){
+      case(rule::proportion):{//proportion for each class
         for(int iclass=0;iclass<class_opt.size();++iclass){
           ostringstream cs;
           cs << class_opt[iclass];
@@ -764,14 +777,16 @@ int main(int argc, char *argv[])
           ogrWriter.createField("origId",OFTInteger);//the fieldId of the original feature
         break;
       }
-      case(3):
-      case(4):
+      case(rule::custom):
+      case(rule::minimum):
+      case(rule::maximum):
+      case(rule::maxvote):
         ogrWriter.createField(label_opt[0],fieldType);
       if(test_opt.size())
 	ogrTestWriter.createField(label_opt[0],fieldType);
         break;
-      case(0):
-      case(1):
+      case(rule::point):
+      case(rule::mean):
       default:{
         if(keepFeatures_opt[0]){
           ogrWriter.createField("origId",OFTInteger);//the fieldId of the original feature
@@ -1122,7 +1137,7 @@ int main(int argc, char *argv[])
 
             double ulx,uly,lrx,lry;
             double uli,ulj,lri,lrj;
-            if(polygon_opt[0]&&rule_opt[0]==0){
+            if(polygon_opt[0]&&ruleMap[rule_opt[0]]==rule::point){
               ulx=writeCentroidPoint.getX();
               uly=writeCentroidPoint.getY();
               lrx=ulx;
@@ -1169,7 +1184,7 @@ int main(int argc, char *argv[])
 	      else
 		writePolygonFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
             }
-            else if(rule_opt[0]==1){
+            else if(ruleMap[rule_opt[0]]==rule::mean){
 	      if(writeTest)
 		writeCentroidFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
 	      else
@@ -1177,15 +1192,18 @@ int main(int argc, char *argv[])
 	    }
             //previously here
             vector<double> polyValues;
-            switch(rule_opt[0]){
-            case(0):
-            case(1):
+            switch(ruleMap[rule_opt[0]]){
+            case(rule::point):
+            case(rule::mean):
             default:
               polyValues.resize(nband);
             break;
-            case(2):
-            case(3):
-            case(4):
+            case(rule::proportion):
+            case(rule::custom):
+            case(rule::minimum):
+            case(rule::maximum):
+            case(rule::maxvote):
+              assert(class_opt.size());
               polyValues.resize(class_opt.size());
             break;
             }
@@ -1293,7 +1311,7 @@ int main(int argc, char *argv[])
                   OGRFeature *writePointFeature;
                   if(!polygon_opt[0]){
                     //create feature
-                    if(rule_opt[0]!=1){//do not create in case of mean value (only create point at centroid
+                    if(ruleMap[rule_opt[0]]!=rule::mean){//do not create in case of mean value (only create point at centroid
 		      if(writeTest)
 			writePointFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
 		      else
@@ -1318,19 +1336,21 @@ int main(int argc, char *argv[])
                     imgReader.readData(value,GDT_Float64,i,j,theBand);
                     if(verbose_opt[0]>1)
                       std::cout << ": " << value << std::endl;
-                    if(polygon_opt[0]||rule_opt[0]==1){
+                    if(polygon_opt[0]||ruleMap[rule_opt[0]]==rule::mean){
                       int iclass=0;
-                      switch(rule_opt[0]){
-                      case(0)://in centroid if polygon_opt==true or all values as points if polygon_opt!=true
+                      switch(ruleMap[rule_opt[0]]){
+                      case(rule::point)://in centroid if polygon_opt==true or all values as points if polygon_opt!=true
                       default:
                         polyValues[iband]=value;
                         break;
-                      case(1)://mean as polygon if polygon_opt==true or as point in centroid if polygon_opt!=true
+                      case(rule::mean)://mean as polygon if polygon_opt==true or as point in centroid if polygon_opt!=true
                         polyValues[iband]+=value;
                       break;
-                      case(2):
-                      case(3):
-                      case(4):
+                      case(rule::proportion):
+                      case(rule::custom):
+                      case(rule::minimum):
+                      case(rule::maximum):
+                      case(rule::maxvote):
                         for(iclass=0;iclass<class_opt.size();++iclass){
                           if(value==class_opt[iclass]){
                             assert(polyValues.size()>iclass);
@@ -1383,7 +1403,7 @@ int main(int argc, char *argv[])
                   if(!polygon_opt[0]){
                     // if(keepFeatures_opt[0])
                     //   writePointFeature->SetField("origId",static_cast<int>(readFeature->GetFID()));
-                    if(rule_opt[0]!=1){//do not create in case of mean value (only at centroid)
+                    if(ruleMap[rule_opt[0]]!=rule::mean){//do not create in case of mean value (only at centroid)
                       if(keepFeatures_opt[0])
                         writePointFeature->SetField("origId",static_cast<int>(readFeature->GetFID()));
                       //write feature
@@ -1414,7 +1434,7 @@ int main(int argc, char *argv[])
 	    }
             // //test
             // std::cout << "before write polygon" << std::endl;
-            if(polygon_opt[0]||rule_opt[0]==1){
+            if(polygon_opt[0]||ruleMap[rule_opt[0]]==rule::mean){
               //do not create if no points found within polygon
               if(!nPointPolygon)
                 continue;
@@ -1432,7 +1452,7 @@ int main(int argc, char *argv[])
                   std::cout << "write feature has " << writePolygonFeature->GetFieldCount() << " fields" << std::endl;
                 //write polygon feature
               }
-              else{//write mean value of polygon to centroid point (rule_opt[0]==1)
+              else{//write mean value of polygon to centroid point (ruleMap[rule_opt[0]]==rule::mean)
                 //create feature
                 if(verbose_opt[0]>1)
                   std::cout << "copying fields from polygons " << sample_opt[0] << std::endl;
@@ -1445,8 +1465,8 @@ int main(int argc, char *argv[])
                 if(verbose_opt[0]>1)
                   std::cout << "write feature has " << writeCentroidFeature->GetFieldCount() << " fields" << std::endl;
               }
-              switch(rule_opt[0]){
-              case(0)://value at each point (or at centroid of polygon if line is not set
+              switch(ruleMap[rule_opt[0]]){
+              case(rule::point)://value at each point (or at centroid of polygon if line is not set
               default:{
                 if(verbose_opt[0])
                   std::cout << "number of points in polygon: " << nPointPolygon << std::endl;
@@ -1518,7 +1538,7 @@ int main(int argc, char *argv[])
                 }
                 break;
               }
-              case(1):{//mean value (written to centroid of polygon if line is not set
+              case(rule::mean):{//mean value (written to centroid of polygon if line is not set
                 if(verbose_opt[0])
                   std::cout << "number of points in polygon: " << nPointPolygon << std::endl;
                 for(int index=0;index<polyValues.size();++index){
@@ -1587,7 +1607,7 @@ int main(int argc, char *argv[])
                 }
                 break;
               }
-              case(2):{//proportion classes
+              case(rule::proportion):{//proportion classes
                 if(verbose_opt[0])
                   std::cout << "number of points in polygon: " << nPointPolygon << std::endl;
                 stat.normalize_pct(polyValues);
@@ -1603,7 +1623,7 @@ int main(int argc, char *argv[])
                 }
                 break;
               }
-              case(3):{//custom
+              case(rule::custom):{//custom
                 assert(polygon_opt[0]);//not implemented for points
                 if(verbose_opt[0])
                   std::cout << "number of points in polygon: " << nPointPolygon << std::endl;
@@ -1628,13 +1648,13 @@ int main(int argc, char *argv[])
                 }
                 break;
               }
-              case(4):{//minimum of polygon
+              case(rule::minimum):{
+                //minimum of polygon
                 assert(polygon_opt[0]);//not implemented for points
                 if(verbose_opt[0])
                   std::cout << "number of points in polygon: " << nPointPolygon << std::endl;
                 //search for min class
-                //todo: change to minClass=stat.max(class_opt) once Optionpk is implemented...
-                int minClass=class_opt[class_opt.size()-1];//!hard coded for now, maximum class is last entry in class_opt
+                int minClass=stat.max(class_opt);
                 for(int iclass=0;iclass<class_opt.size();++iclass){
                   if(polyValues[iclass]>0){
                     if(verbose_opt[0]>1)
@@ -1648,6 +1668,42 @@ int main(int argc, char *argv[])
                 writePolygonFeature->SetField(label_opt[0].c_str(),minClass);
                 break;
               }
+              case(rule::maximum):{
+                //maximum of polygon
+                assert(polygon_opt[0]);//not implemented for points
+                if(verbose_opt[0])
+                  std::cout << "number of points in polygon: " << nPointPolygon << std::endl;
+                //search for max class
+                int maxClass=stat.min(class_opt);
+                for(int iclass=0;iclass<class_opt.size();++iclass){
+                  if(polyValues[iclass]>0){
+                    if(verbose_opt[0]>1)
+                      std::cout << class_opt[iclass] << ": " << polyValues[iclass] << std::endl;
+                    if(class_opt[iclass]>maxClass)
+                      maxClass=class_opt[iclass];
+                  }
+                }
+                if(verbose_opt[0]>0)
+                  std::cout << "maxClass: " << maxClass << std::endl;
+                writePolygonFeature->SetField(label_opt[0].c_str(),maxClass);
+                break;
+              }
+              case(rule::maxvote):{
+                //maximum votes in polygon
+                assert(polygon_opt[0]);//not implemented for points
+                if(verbose_opt[0])
+                  std::cout << "number of points in polygon: " << nPointPolygon << std::endl;
+                //search for class with maximum votes
+                int maxClass=stat.min(class_opt);
+                vector<double>::iterator maxit;
+                maxit=stat.max(polyValues,polyValues.begin(),polyValues.end());
+                int maxIndex=distance(polyValues.begin(),maxit);
+                maxClass=class_opt[maxIndex];
+                if(verbose_opt[0]>0)
+                  std::cout << "maxClass: " << maxClass << std::endl;
+                writePolygonFeature->SetField(label_opt[0].c_str(),maxClass);
+                break;
+              }
               }
               if(polygon_opt[0]){
                 if(keepFeatures_opt[0])
@@ -1695,7 +1751,7 @@ int main(int argc, char *argv[])
 
             double ulx,uly,lrx,lry;
             double uli,ulj,lri,lrj;
-            if(polygon_opt[0]&&rule_opt[0]==0){
+            if(polygon_opt[0]&&ruleMap[rule_opt[0]]==rule::point){
               ulx=writeCentroidPoint.getX();
               uly=writeCentroidPoint.getY();
               lrx=ulx;
@@ -1742,7 +1798,7 @@ int main(int argc, char *argv[])
 	      else
 		writePolygonFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn());
             }
-            else if(rule_opt[0]==1){
+            else if(ruleMap[rule_opt[0]]==rule::mean){
 	      if(writeTest)
 		writeCentroidFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
 	      else
@@ -1750,15 +1806,18 @@ int main(int argc, char *argv[])
 	    }
             //previously here
             vector<double> polyValues;
-            switch(rule_opt[0]){
-            case(0):
-            case(1):
+            switch(ruleMap[rule_opt[0]]){
+            case(rule::point):
+            case(rule::mean):
             default:
               polyValues.resize(nband);
             break;
-            case(2):
-            case(3):
-            case(4):
+            case(rule::proportion):
+            case(rule::custom):
+            case(rule::minimum):
+            case(rule::maximum):
+            case(rule::maxvote):
+              assert(class_opt.size());
               polyValues.resize(class_opt.size());
             break;
             }
@@ -1866,7 +1925,7 @@ int main(int argc, char *argv[])
                   OGRFeature *writePointFeature;
                   if(!polygon_opt[0]){
                     //create feature
-                    if(rule_opt[0]!=1){//do not create in case of mean value (only create point at centroid
+                    if(ruleMap[rule_opt[0]]!=rule::mean){//do not create in case of mean value (only create point at centroid
 		      if(writeTest)
 			writePointFeature = OGRFeature::CreateFeature(writeTestLayer->GetLayerDefn());
 		      else
@@ -1891,19 +1950,21 @@ int main(int argc, char *argv[])
                     imgReader.readData(value,GDT_Float64,i,j,theBand);
                     if(verbose_opt[0]>1)
                       std::cout << ": " << value << std::endl;
-                    if(polygon_opt[0]||rule_opt[0]==1){
+                    if(polygon_opt[0]||ruleMap[rule_opt[0]]==rule::mean){
                       int iclass=0;
-                      switch(rule_opt[0]){
-                      case(0)://in centroid if polygon_opt==true or all values as points if polygon_opt!=true
+                      switch(ruleMap[rule_opt[0]]){
+                      case(rule::point)://in centroid if polygon_opt==true or all values as points if polygon_opt!=true
                       default:
                         polyValues[iband]=value;
                         break;
-                      case(1)://mean as polygon if polygon_opt==true or as point in centroid if polygon_opt!=true
+                      case(rule::mean)://mean as polygon if polygon_opt==true or as point in centroid if polygon_opt!=true
                         polyValues[iband]+=value;
                       break;
-                      case(2):
-                      case(3):
-                      case(4):
+                      case(rule::proportion):
+                      case(rule::custom):
+                      case(rule::minimum):
+                      case(rule::maximum):
+                      case(rule::maxvote):
                         for(iclass=0;iclass<class_opt.size();++iclass){
                           if(value==class_opt[iclass]){
                             assert(polyValues.size()>iclass);
@@ -1956,7 +2017,7 @@ int main(int argc, char *argv[])
                   if(!polygon_opt[0]){
                     if(keepFeatures_opt[0])
                       writePointFeature->SetField("origId",static_cast<int>(readFeature->GetFID()));
-                    if(rule_opt[0]!=1){//do not create in case of mean value (only at centroid)
+                    if(ruleMap[rule_opt[0]]!=rule::mean){//do not create in case of mean value (only at centroid)
                       //write feature
                       if(verbose_opt[0]>1)
                         std::cout << "creating point feature" << std::endl;
@@ -1985,7 +2046,7 @@ int main(int argc, char *argv[])
             }
             // //test
             // std::cout << "before write polygon" << std::endl;
-            if(polygon_opt[0]||rule_opt[0]==1){
+            if(polygon_opt[0]||ruleMap[rule_opt[0]]==rule::mean){
               //add ring to polygon
               if(polygon_opt[0]){
                 writePolygon.addRing(&writeRing);
@@ -2000,7 +2061,7 @@ int main(int argc, char *argv[])
                   std::cout << "write feature has " << writePolygonFeature->GetFieldCount() << " fields" << std::endl;
                 //write polygon feature
               }
-              else{//write mean value of polygon to centroid point (rule_opt[0]==1)
+              else{//write mean value of polygon to centroid point (ruleMap[rule_opt[0]]==rule::mean)
                 //create feature
                 if(verbose_opt[0]>1)
                   std::cout << "copying fields from polygons " << sample_opt[0] << std::endl;
@@ -2013,7 +2074,7 @@ int main(int argc, char *argv[])
                 if(verbose_opt[0]>1)
                   std::cout << "write feature has " << writeCentroidFeature->GetFieldCount() << " fields" << std::endl;
               }
-              switch(rule_opt[0]){
+              switch(ruleMap[rule_opt[0]]){
               case(0)://value at each point (or at centroid of polygon if line is not set
               default:{
                 if(verbose_opt[0])
@@ -2082,7 +2143,7 @@ int main(int argc, char *argv[])
                 }//for index
                 break;
               }//case 0 and default
-              case(1):{//mean value (written to centroid of polygon if line is not set
+              case(rule::mean):{//mean value (written to centroid of polygon if line is not set
                 if(verbose_opt[0])
                   std::cout << "number of points in polygon: " << nPointPolygon << std::endl;
                 for(int index=0;index<polyValues.size();++index){
@@ -2148,7 +2209,7 @@ int main(int argc, char *argv[])
                 }
                 break;
               }
-              case(2):{//proportion classes
+              case(rule::proportion):{//proportion classes
                 if(verbose_opt[0])
                   std::cout << "number of points in polygon: " << nPointPolygon << std::endl;
                 stat.normalize_pct(polyValues);
@@ -2164,7 +2225,7 @@ int main(int argc, char *argv[])
                 }
                 break;
               }
-              case(3):{//custom
+              case(rule::custom):{//custom
                 assert(polygon_opt[0]);//not implemented for points
                 if(verbose_opt[0])
                   std::cout << "number of points in polygon: " << nPointPolygon << std::endl;
@@ -2189,13 +2250,12 @@ int main(int argc, char *argv[])
                 }
                 break;
               }
-              case(4):{//minimum of polygon
+              case(rule::minimum):{//minimum of polygon
                 assert(polygon_opt[0]);//not implemented for points
                 if(verbose_opt[0])
                   std::cout << "number of points in polygon: " << nPointPolygon << std::endl;
                 //search for min class
-                //todo: change to minClass=stat.max(class_opt) once Optionpk is implemented...
-                int minClass=class_opt[class_opt.size()-1];//!hard coded for now, maximum class is last entry in class_opt
+                int minClass=stat.max(class_opt);
                 for(int iclass=0;iclass<class_opt.size();++iclass){
                   if(polyValues[iclass]>0){
                     if(verbose_opt[0]>1)
@@ -2209,6 +2269,36 @@ int main(int argc, char *argv[])
                 writePolygonFeature->SetField(label_opt[0].c_str(),minClass);
                 break;
               }
+              case(rule::maximum):{//maximum of polygon
+                assert(polygon_opt[0]);//not implemented for points
+                if(verbose_opt[0])
+                  std::cout << "number of points in polygon: " << nPointPolygon << std::endl;
+                //search for max class
+                int maxClass=stat.min(class_opt);
+                for(int iclass=0;iclass<class_opt.size();++iclass){
+                  if(polyValues[iclass]>0){
+                    if(verbose_opt[0]>1)
+                      std::cout << class_opt[iclass] << ": " << polyValues[iclass] << std::endl;
+                    if(class_opt[iclass]>maxClass)
+                      maxClass=class_opt[iclass];
+                  }
+                }
+                if(verbose_opt[0]>0)
+                  std::cout << "maxClass: " << maxClass << std::endl;
+                writePolygonFeature->SetField(label_opt[0].c_str(),maxClass);
+                break;
+              }
+              case(rule::maxvote):{//maximum votes in polygon
+                assert(polygon_opt[0]);//not implemented for points
+                if(verbose_opt[0])
+                  std::cout << "number of points in polygon: " << nPointPolygon << std::endl;
+                //search for max votes
+                int maxClass=stat.min(class_opt);
+                vector<double>::iterator maxit;
+                maxit=stat.max(polyValues,polyValues.begin(),polyValues.end());
+                int maxIndex=distance(polyValues.begin(),maxit);
+                maxClass=class_opt[maxIndex];
+              }
               }
               if(polygon_opt[0]){
                 if(keepFeatures_opt[0])
diff --git a/src/apps/pkmosaic.cc b/src/apps/pkmosaic.cc
index d09f8ad..0b7f100 100644
--- a/src/apps/pkmosaic.cc
+++ b/src/apps/pkmosaic.cc
@@ -28,6 +28,10 @@ along with pktools.  If not, see <http://www.gnu.org/licenses/>.
 #include "base/Optionpk.h"
 #include "algorithms/StatFactory.h"
 
+namespace mrule{
+  enum MRULE_TYPE {overwrite=0, maxndvi=1, maxband=2, minband=3, validband=4, mean=5, maxvote=6, median=7,sum=8};
+}
+
 using namespace std;
 
 int main(int argc, char *argv[])
@@ -50,7 +54,7 @@ int main(int argc, char *argv[])
   Optionpk<short>  flag_opt("f", "flag", "Flag value to put in image if out of bounds.", 0);
   Optionpk<unsigned short>  resample_opt("r", "resample", "Resampling method (0: nearest neighbour, 1: bi-linear interpolation).", 0);
   Optionpk<string>  description_opt("\0", "description", "Set image description", "");
-  Optionpk<int> mrule_opt("m", "mrule", "Mosaic rule for mosaic (0: overwrite, 1: max ndvi, 2: max Band, 3: min Band, 4: valid Band, 5: mean value, 6: max voting (only for byte images), 7: median, 8: (weighted) sum", 0);
+  Optionpk<string> mrule_opt("m", "mrule", "Mosaic rule for mosaic (overwrite, maxndvi, maxband, minband, validband, mean, maxvote (only for byte images), median, sum", "overwrite");
   Optionpk<int> ruleBand_opt("rb", "rband", "band index used for the rule (for ndvi, use --ruleBand=redBand --ruleBand=nirBand", 0);
   Optionpk<int> validBand_opt("vb", "validBand", "valid band index(es)", 0);
   Optionpk<double> invalid_opt("t", "invalid", "invalid value for valid band", 0);
@@ -101,6 +105,20 @@ int main(int argc, char *argv[])
     exit(0);//help was invoked, stop processing
   }
 
+  std::map<std::string, mrule::MRULE_TYPE> mruleMap;
+  //initialize mruleMap
+  enum MRULE_TYPE {overwrite=0, maxndvi=1, maxband=2, minband=3, validband=4, mean=5, maxvote=6, median=7,sum=8};
+
+  mruleMap["overwrite"]=mrule::overwrite;
+  mruleMap["maxndvi"]=mrule::maxndvi;
+  mruleMap["maxband"]=mrule::maxband;
+  mruleMap["minband"]=mrule::minband;
+  mruleMap["validband"]=mrule::validband;
+  mruleMap["mean"]=mrule::mean;
+  mruleMap["maxvote"]=mrule::maxvote;
+  mruleMap["median"]=mrule::median;
+  mruleMap["sum"]=mrule::sum;
+
   while(invalid_opt.size()<validBand_opt.size())
     invalid_opt.push_back(invalid_opt[0]);
   RESAMPLE theResample;
@@ -197,33 +215,33 @@ int main(int argc, char *argv[])
       cout << "Bounding Box (ULX ULY LRX LRY): " << fixed << setprecision(6) << theULX << " " << theULY << " " << theLRX << " " << theLRY << endl;
     if(!init){
       if(verbose_opt[0]){
-        switch(mrule_opt[0]){
+        switch(mruleMap[mrule_opt[0]]){
         default:
-        case(0):
+        case(mrule::overwrite):
           cout << "Mosaic rule: overwrite" << endl;
           break;
-        case(1):
+        case(mrule::maxndvi):
           cout << "Mosaic rule: max ndvi" << endl;
           break;
-        case(2):
+        case(mrule::maxband):
           cout << "Mosaic rule: max band" << endl;
           break;
-        case(3):
+        case(mrule::minband):
           cout << "Mosaic rule: min band" << endl;
           break;
-        case(4):
+        case(mrule::validband):
           cout << "Mosaic rule: valid band" << endl;
           break;
-        case(5):
+        case(mrule::mean):
           cout << "Mosaic rule: mean value" << endl;
           break;
-        case(6):
+        case(mrule::maxvote):
           cout << "Mosaic rule: max voting (only for byte images)" << endl;
           break;
-        case(7):
+        case(mrule::median):
           cout << "Mosaic rule: median" << endl;
           break;
-        case(8):
+        case(mrule::sum):
           cout << "Mosaic rule: sum" << endl;
           break;
         }
@@ -342,7 +360,7 @@ int main(int argc, char *argv[])
   if(verbose_opt[0]){
     std::cout << weight_opt << std::endl;
   }
-  if(mrule_opt[0]==6){
+  if(mruleMap[mrule_opt[0]]==mrule::maxvote){
     nwriteBand=(file_opt[0])? class_opt.size()+1:class_opt.size();
   }
   else
@@ -384,9 +402,9 @@ int main(int argc, char *argv[])
   Vector2d<short> maxBuffer;//buffer used for maximum voting
   Vector2d<double> readBuffer(nband);
   statfactory::StatFactory stat;
-  if(mrule_opt[0]==1)//ndvi
+  if(mruleMap[mrule_opt[0]]==maxndvi)//ndvi
     assert(ruleBand_opt.size()==2);
-  if(mrule_opt[0]==6){//max voting
+  if(mruleMap[mrule_opt[0]]==mrule::maxvote){//max voting
     maxBuffer.resize(imgWriter.nrOfCol(),256);//use only byte images for max voting
     for(int iclass=0;iclass<class_opt.size();++iclass)
       assert(class_opt[iclass]<maxBuffer.size());
@@ -405,12 +423,12 @@ int main(int argc, char *argv[])
     Vector2d< vector<double> > storeBuffer;
     vector<bool> writeValid(ncol);
 
-    if(mrule_opt[0]==5||mrule_opt[0]==7||mrule_opt[0]==8)//mean, median or (weighted) sum value
+    if(mruleMap[mrule_opt[0]]==mrule::mean||mruleMap[mrule_opt[0]]==mrule::median||mruleMap[mrule_opt[0]]==mrule::sum)//mean, median or (weighted) sum value
       storeBuffer.resize(nband,ncol);
     for(int icol=0;icol<imgWriter.nrOfCol();++icol){
       writeValid[icol]=false;
       fileBuffer[icol]=0;
-      if(mrule_opt[0]==6){//max voting
+      if(mruleMap[mrule_opt[0]]==mrule::maxvote){//max voting
         for(int iclass=0;iclass<256;++iclass)
           maxBuffer[icol][iclass]=0;
       }
@@ -516,8 +534,8 @@ int main(int argc, char *argv[])
 	if(readValid){
           if(writeValid[ib]){
             int iband=0;
-	    switch(mrule_opt[0]){
-	    case(1):{//max ndvi
+	    switch(mruleMap[mrule_opt[0]]){
+	    case(mrule::maxndvi):{//max ndvi
               double red_current=writeBuffer[ruleBand_opt[0]][ib];
               double nir_current=writeBuffer[ruleBand_opt[1]][ib];
 	      double ndvi_current=0;
@@ -567,9 +585,9 @@ int main(int argc, char *argv[])
               }
 	      break;
             }
-	    case(2):
-            case(3):
-            case(4)://max,min,valid band
+	    case(mrule::maxband):
+            case(mrule::minband):
+            case(mrule::validband)://max,min,valid band
               val_current=writeBuffer[ruleBand_opt[0]][ib];
               switch(resample_opt[0]){
               case(BILINEAR):
@@ -583,7 +601,7 @@ int main(int argc, char *argv[])
                   upperCol=imgReader.nrOfCol()-1;
                 val_new=(readCol-0.5-lowerCol)*readBuffer[ruleBand_opt[0]][upperCol-startCol]+(1-readCol+0.5+lowerCol)*readBuffer[ruleBand_opt[0]][lowerCol-startCol];
                 val_new*=weight_opt[ifile];
-                if((mrule_opt[0]==2&&val_new>val_current)||(mrule_opt[0]==3&&val_new<val_current)||(mrule_opt[0]==4)){//&&val_new>minValue_opt[0]&&val_new<maxValue_opt[0])){
+                if((mruleMap[mrule_opt[0]]==mrule::maxband&&val_new>val_current)||(mruleMap[mrule_opt[0]]==mrule::minband&&val_new<val_current)||(mruleMap[mrule_opt[0]]==mrule::validband)){//&&val_new>minValue_opt[0]&&val_new<maxValue_opt[0])){
                   for(iband=0;iband<nband;++iband){
                     val_new=(readCol-0.5-lowerCol)*readBuffer[iband][upperCol-startCol]+(1-readCol+0.5+lowerCol)*readBuffer[iband][lowerCol-startCol];
                     val_new*=weight_opt[ifile];
@@ -597,7 +615,7 @@ int main(int argc, char *argv[])
                 readCol=static_cast<int>(readCol);
                 val_new=readBuffer[ruleBand_opt[0]][readCol-startCol];
                 val_new*=weight_opt[ifile];
-                if((mrule_opt[0]==2&&val_new>val_current)||(mrule_opt[0]==3&&val_new<val_current)||(mrule_opt[0]==4)){//&&val_new>minValue_opt[0]&&val_new<maxValue_opt[0])){
+                if((mruleMap[mrule_opt[0]]==mrule::maxband&&val_new>val_current)||(mruleMap[mrule_opt[0]]==mrule::minband&&val_new<val_current)||(mruleMap[mrule_opt[0]]==mrule::validband)){//&&val_new>minValue_opt[0]&&val_new<maxValue_opt[0])){
                   for(iband=0;iband<nband;++iband){
                     val_new=readBuffer[iband][readCol-startCol];
                     val_new*=weight_opt[ifile];
@@ -609,7 +627,7 @@ int main(int argc, char *argv[])
                 break;
               }
 	      break;
-            case(6)://max voting (only for Byte images)
+            case(mrule::maxvote)://max voting (only for Byte images)
               switch(resample_opt[0]){
               case(BILINEAR):
                 lowerCol=readCol-0.5;
@@ -635,9 +653,9 @@ int main(int argc, char *argv[])
                 break;
 	      }
               break;
-            case(5)://mean value
-	    case(7)://median value
-	    case(8)://sum value
+            case(mrule::mean)://mean value
+	    case(mrule::median)://median value
+	    case(mrule::sum)://sum value
               switch(resample_opt[0]){
               case(BILINEAR):
                 lowerCol=readCol-0.5;
@@ -668,7 +686,7 @@ int main(int argc, char *argv[])
               }
               ++fileBuffer[ib];
 	      break;
-	    case(0):
+	    case(mrule::overwrite):
 	    default:
               switch(resample_opt[0]){
               case(BILINEAR):
@@ -703,10 +721,10 @@ int main(int argc, char *argv[])
 	  else{
             writeValid[ib]=true;//readValid was true
             int iband=0;
-	    switch(mrule_opt[0]){
-            case(5):
-            case(7):
-            case(8):
+	    switch(mruleMap[mrule_opt[0]]){
+            case(mrule::mean):
+            case(mrule::median):
+            case(mrule::sum):
               switch(resample_opt[0]){
               case(BILINEAR):
                 lowerCol=readCol-0.5;
@@ -734,7 +752,7 @@ int main(int argc, char *argv[])
               }
               ++fileBuffer[ib];
               break;
-            case(6):
+            case(mrule::maxvote):
               switch(resample_opt[0]){
               case(BILINEAR):
                 lowerCol=readCol-0.5;
@@ -796,7 +814,7 @@ int main(int argc, char *argv[])
       }
       imgReader.close();
     }
-    if(mrule_opt[0]==6){
+    if(mruleMap[mrule_opt[0]]==mrule::maxvote){
       vector<short> classBuffer(imgWriter.nrOfCol());
       if(class_opt.size()>1){
         for(int iclass=0;iclass<class_opt.size();++iclass){
@@ -834,18 +852,18 @@ int main(int argc, char *argv[])
         // assert(writeBuffer[bands[iband]].size()==imgWriter.nrOfCol());
         assert(writeBuffer[iband].size()==imgWriter.nrOfCol());
         for(int icol=0;icol<imgWriter.nrOfCol();++icol){
-          switch(mrule_opt[0]){
-          case(5):
+          switch(mruleMap[mrule_opt[0]]){
+          case(mrule::mean):
             assert(storeBuffer[bands[iband]][icol].size()==fileBuffer[icol]);
             if(storeBuffer[bands[iband]][icol].size())
               writeBuffer[iband][icol]=stat.mean(storeBuffer[bands[iband]][icol]);
             break;
-          case(7):
+          case(mrule::median):
             assert(storeBuffer[bands[iband]][icol].size()==fileBuffer[icol]);
             if(storeBuffer[bands[iband]][icol].size())
               writeBuffer[iband][icol]=stat.median(storeBuffer[bands[iband]][icol]);
             break;
-          case(8)://sum
+          case(mrule::sum)://sum
             assert(storeBuffer[bands[iband]][icol].size()==fileBuffer[icol]);
             if(storeBuffer[bands[iband]][icol].size())
               writeBuffer[iband][icol]=stat.sum(storeBuffer[bands[iband]][icol]);

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