From 6a38bed4469a534cf88e411feb6222e112655547 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=A9dric=20Traizet?= <cedric.traizet@c-s.fr>
Date: Wed, 31 Jul 2019 11:59:05 +0200
Subject: [PATCH] ENH: simplification of specialized field creation

---
 .../app/otbVectorClassifier.cxx               | 19 ---------------
 .../app/otbVectorRegression.cxx               | 19 ---------------
 .../include/otbVectorPrediction.h             |  4 ----
 .../include/otbVectorPrediction.hxx           | 24 ++++++++++++++++---
 4 files changed, 21 insertions(+), 45 deletions(-)

diff --git a/Modules/Applications/AppClassification/app/otbVectorClassifier.cxx b/Modules/Applications/AppClassification/app/otbVectorClassifier.cxx
index 5ccdf67e0c..5e9a245d45 100644
--- a/Modules/Applications/AppClassification/app/otbVectorClassifier.cxx
+++ b/Modules/Applications/AppClassification/app/otbVectorClassifier.cxx
@@ -100,25 +100,6 @@ VectorClassifier
   SetOfficialDocLink();
 }
 
-template<>
-void
-VectorClassifier
-::CreatePredictionField(OGRFeatureDefn & layerDefn, otb::ogr::Layer & outLayer)
-{
-  int idx = layerDefn.GetFieldIndex(GetParameterString("cfield").c_str());
-  if (idx >= 0)
-  {
-    if (layerDefn.GetFieldDefn(idx)->GetType() != OFTInteger)
-      itkExceptionMacro("Field name "<< GetParameterString("cfield") << " already exists with a different type!");
-  }
-  else
-  {
-    OGRFieldDefn predictedField(GetParameterString("cfield").c_str(), OFTInteger);
-    ogr::FieldDefn predictedFieldDef(predictedField);
-    outLayer.CreateField(predictedFieldDef);
-  }
-}
-
 }
 }
 
diff --git a/Modules/Applications/AppClassification/app/otbVectorRegression.cxx b/Modules/Applications/AppClassification/app/otbVectorRegression.cxx
index bafb8e07b2..8af7bb8b5f 100644
--- a/Modules/Applications/AppClassification/app/otbVectorRegression.cxx
+++ b/Modules/Applications/AppClassification/app/otbVectorRegression.cxx
@@ -90,25 +90,6 @@ VectorRegression
   SetOfficialDocLink();
 }
 
-template<>
-void
-VectorRegression
-::CreatePredictionField(OGRFeatureDefn & layerDefn, otb::ogr::Layer & outLayer)
-{
-  int idx = layerDefn.GetFieldIndex(GetParameterString("cfield").c_str());
-  if (idx >= 0)
-  {
-    if (layerDefn.GetFieldDefn(idx)->GetType() != OFTReal)
-      itkExceptionMacro("Field name "<< GetParameterString("cfield") << " already exists with a different type!");
-  }
-  else
-  {
-    OGRFieldDefn predictedField(GetParameterString("cfield").c_str(), OFTReal);
-    ogr::FieldDefn predictedFieldDef(predictedField);
-    outLayer.CreateField(predictedFieldDef);
-  }
-}
-
 }
 }
 
diff --git a/Modules/Applications/AppClassification/include/otbVectorPrediction.h b/Modules/Applications/AppClassification/include/otbVectorPrediction.h
index bb19171b96..14ba7193a3 100644
--- a/Modules/Applications/AppClassification/include/otbVectorPrediction.h
+++ b/Modules/Applications/AppClassification/include/otbVectorPrediction.h
@@ -98,10 +98,6 @@ private:
 
   void DoUpdateParameters() override;
 
-  /** Create the prediction field in the output layer, this template method should be specialized
-   * to create the right type of field (e.g. OGRInteger or OGRReal) */ 
-  void CreatePredictionField(OGRFeatureDefn & layerDefn, otb::ogr::Layer & outLayer);
-
   void DoExecute() override;
 
   ModelPointerType m_Model;
diff --git a/Modules/Applications/AppClassification/include/otbVectorPrediction.hxx b/Modules/Applications/AppClassification/include/otbVectorPrediction.hxx
index f6edbf2a36..74532f9e85 100644
--- a/Modules/Applications/AppClassification/include/otbVectorPrediction.hxx
+++ b/Modules/Applications/AppClassification/include/otbVectorPrediction.hxx
@@ -229,13 +229,31 @@ VectorPrediction <RegressionMode>
   OGRFeatureDefn &layerDefn = layer.GetLayerDefn();
 
   // Add the field of prediction in the output layer if field not exist
-  CreatePredictionField(layerDefn, outLayer);
-
+  
+  OGRFieldType labelType;
+  if (RegressionMode==true)
+    labelType = OFTReal;
+  else
+    labelType = OFTInteger;
+  
+  int idx = layerDefn.GetFieldIndex(GetParameterString("cfield").c_str());
+  if (idx >= 0)
+  {
+    if (layerDefn.GetFieldDefn(idx)->GetType() != labelType)
+      itkExceptionMacro("Field name "<< GetParameterString("cfield") << " already exists with a different type!");
+  }
+  else
+  {
+    OGRFieldDefn predictedField(GetParameterString("cfield").c_str(), labelType);
+    ogr::FieldDefn predictedFieldDef(predictedField);
+    outLayer.CreateField(predictedFieldDef);
+  }
+  
   // Add confidence field in the output layer
   std::string confFieldName("confidence");
   if (computeConfidenceMap)
     {
-    int idx = layerDefn.GetFieldIndex(confFieldName.c_str());
+    idx = layerDefn.GetFieldIndex(confFieldName.c_str());
     if (idx >= 0)
       {
       if (layerDefn.GetFieldDefn(idx)->GetType() != OFTReal)
-- 
GitLab