From a2d6cf59f56c2b23e257f0b1668ed9d817c814cc Mon Sep 17 00:00:00 2001
From: Julien Malik <julien.malik@c-s.fr>
Date: Mon, 25 Jun 2012 18:02:41 +0200
Subject: [PATCH] BUG: in UpdateLayer mode, don't create field if it does not
 exists

---
 Applications/Segmentation/otbSegmentation.cxx | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/Applications/Segmentation/otbSegmentation.cxx b/Applications/Segmentation/otbSegmentation.cxx
index f00a42b35c..c03a82cfff 100644
--- a/Applications/Segmentation/otbSegmentation.cxx
+++ b/Applications/Segmentation/otbSegmentation.cxx
@@ -491,9 +491,15 @@ private:
         layer = ogrDS->CreateLayer(GetParameterString("mode.vector.layername"),
                                    &oSRS,wkbMultiPolygon,
                                    options);
-        // And create the field
-        OGRFieldDefn field(this->GetParameterString("mode.vector.fieldname").c_str(),OFTInteger);
-        layer.CreateField(field,true);
+
+        // And create the field if necessary
+        std::string fieldName = this->GetParameterString("mode.vector.fieldname");
+        OGRFeatureDefn & ogrFeatureDfn = layer.GetLayerDefn();
+        if (-1 != ogrFeatureDfn.GetFieldIndex(fieldName.c_str()))
+          {
+          OGRFieldDefn field(fieldName.c_str(),OFTInteger);
+          layer.CreateField(field,true);
+          }
 
         }
       else if(outmode == "ulco")
-- 
GitLab