From 7bafe4e0057511e47d8525104f93e1e5b6de2b0a Mon Sep 17 00:00:00 2001
From: Marina Bertolino <marina.bertolino@c-s.fr>
Date: Wed, 16 Aug 2017 12:21:09 +0200
Subject: [PATCH] REFAC: remove ogr() in KMeansClassification app

---
 .../app/otbKMeansClassification.cxx           | 26 ++++++++++---------
 1 file changed, 14 insertions(+), 12 deletions(-)

diff --git a/Modules/Applications/AppClassification/app/otbKMeansClassification.cxx b/Modules/Applications/AppClassification/app/otbKMeansClassification.cxx
index 1ba889c78b..5e10f20a67 100644
--- a/Modules/Applications/AppClassification/app/otbKMeansClassification.cxx
+++ b/Modules/Applications/AppClassification/app/otbKMeansClassification.cxx
@@ -160,19 +160,21 @@ protected:
     ogrDS = otb::ogr::DataSource::New(vectorFileName, otb::ogr::DataSource::Modes::Update_LayerUpdate);
     otb::ogr::Layer layer = ogrDS->GetLayer(0);
 
-    OGRFieldDefn confidenceField(fieldName.c_str(), OFTInteger);
-    confidenceField.SetWidth(confidenceField.GetWidth());
-    confidenceField.SetPrecision(confidenceField.GetPrecision());
-    ogr::FieldDefn confFieldDefn(confidenceField);
-    layer.CreateField(confFieldDefn);
-
-    // Complete field
-    layer.ogr().ResetReading();
-    otb::ogr::Feature feature = layer.ogr().GetNextFeature();
-    if(feature.addr())
+    OGRFieldDefn classField(fieldName.c_str(), OFTInteger);
+    classField.SetWidth(classField.GetWidth());
+    classField.SetPrecision(classField.GetPrecision());
+    ogr::FieldDefn classFieldDefn(classField);
+    layer.CreateField(classFieldDefn);
+
+    otb::ogr::Layer::const_iterator it = layer.cbegin();
+    otb::ogr::Layer::const_iterator itEnd = layer.cend();
+    for( ; it!=itEnd ; ++it)
     {
-      feature.ogr().SetField(fieldName.c_str(), 0);
-      layer.SetFeature(feature);
+      ogr::Feature dstFeature(layer.GetLayerDefn());
+      dstFeature.SetFrom( *it, TRUE);
+      dstFeature.SetFID(it->GetFID());
+      dstFeature[fieldName].SetValue<int>(0);
+      layer.SetFeature(dstFeature);
     }
     const OGRErr err = layer.ogr().CommitTransaction();
     if (err != OGRERR_NONE)
-- 
GitLab