From c65bf8b93a694c1553146f7c99c53eb0b0ee815a Mon Sep 17 00:00:00 2001
From: Emmanuel Christophe <emmanuel.christophe@orfeo-toolbox.org>
Date: Mon, 31 Aug 2009 13:26:39 +0800
Subject: [PATCH] ENH: extending VectorDataKeywordlist

---
 Code/Common/otbDataNode.h            | 15 +++++++++++
 Code/Common/otbDataNode.txx          | 37 +++++++++++++++++++++++++++-
 Code/IO/otbVectorDataKeywordlist.cxx | 31 +++++++++++++++++++++++
 Code/IO/otbVectorDataKeywordlist.h   | 11 +++++++++
 4 files changed, 93 insertions(+), 1 deletion(-)

diff --git a/Code/Common/otbDataNode.h b/Code/Common/otbDataNode.h
index 52ee5086c9..257230d100 100644
--- a/Code/Common/otbDataNode.h
+++ b/Code/Common/otbDataNode.h
@@ -210,6 +210,21 @@ public:
    * \return The value of the field. A default value is returned if the key was not found.
    */
    std::string GetFieldAsString(std::string key) const;
+
+   /**
+    * Add a field to the node.
+    * \param key The name of the field.
+    * \param value The value of the field.
+    */
+   void SetFieldAsInt(std::string key, int value);
+   /**
+    * Returns the value associated with a field name.
+    * \param key The name of the field.
+    * \return The value of the field. A default value is returned if the key was not found.
+    */
+    int GetFieldAsInt(std::string key) const;
+
+
   /**
    * Remove the field associated with the given key, if possible.
    * \param key The name of the field.
diff --git a/Code/Common/otbDataNode.txx b/Code/Common/otbDataNode.txx
index 8f53c57d48..405690df2f 100644
--- a/Code/Common/otbDataNode.txx
+++ b/Code/Common/otbDataNode.txx
@@ -250,7 +250,24 @@ template <class TPrecision, unsigned int VDimension, class TValuePrecision>
   itk::ExposeMetaData< VectorDataKeywordlist >(this->GetMetaDataDictionary(),
       MetaDataKey::VectorDataKeywordlistKey,
       kwl);
-  kwl.AddField(key, value);
+  kwl.SetFieldAsString(key, value);
+  itk::EncapsulateMetaData< VectorDataKeywordlist >(this->GetMetaDataDictionary(),
+      MetaDataKey::VectorDataKeywordlistKey,
+      kwl);
+}
+
+template <class TPrecision, unsigned int VDimension, class TValuePrecision>
+    void
+        DataNode<TPrecision,VDimension,TValuePrecision>
+  ::SetFieldAsInt(std::string key, int value)
+{
+  otb::VectorDataKeywordlist kwl;
+  itk::ExposeMetaData< VectorDataKeywordlist >(this->GetMetaDataDictionary(),
+      MetaDataKey::VectorDataKeywordlistKey,
+      kwl);
+  std::ostringstream os;
+  os << value;
+  kwl.SetFieldAsString(key,os.str() );//FIXME the int is currently saved as string in the OGR data
   itk::EncapsulateMetaData< VectorDataKeywordlist >(this->GetMetaDataDictionary(),
       MetaDataKey::VectorDataKeywordlistKey,
       kwl);
@@ -287,6 +304,24 @@ template <class TPrecision, unsigned int VDimension, class TValuePrecision>
   return "";
 }
 
+template <class TPrecision, unsigned int VDimension, class TValuePrecision>
+    int
+        DataNode<TPrecision,VDimension,TValuePrecision>
+  ::GetFieldAsInt(std::string key) const
+{
+  VectorDataKeywordlist keywordlist;
+  if (HasField(key))
+  {
+    itk::ExposeMetaData< VectorDataKeywordlist >(this->GetMetaDataDictionary(),
+                                              MetaDataKey::VectorDataKeywordlistKey, keywordlist);
+    std::istringstream is(keywordlist.GetFieldAsString(key));
+    int value;
+    is >> value;
+    return value;
+  }
+  return "";
+}
+
 /*
 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
 void
diff --git a/Code/IO/otbVectorDataKeywordlist.cxx b/Code/IO/otbVectorDataKeywordlist.cxx
index a6145b34ad..ebeb087add 100644
--- a/Code/IO/otbVectorDataKeywordlist.cxx
+++ b/Code/IO/otbVectorDataKeywordlist.cxx
@@ -113,6 +113,37 @@ VectorDataKeywordlist
   return false;
 }
 
+void
+VectorDataKeywordlist
+  ::SetFieldAsString(std::string key,std::string value)
+{
+  if (HasField(key))
+  {
+    for (unsigned int i = 0; i < m_FieldList.size(); ++i)
+    {
+      if (key.compare(m_FieldList[i].first->GetNameRef()) == 0)
+      {
+        if (m_FieldList[i].first->GetType() == OFTString)
+        {
+          OGRField field;
+          char * cstr = new char[value.length() + 1];
+          strcpy(cstr, value.c_str());
+          field.String = cstr;
+          m_FieldList[i].second = field;
+        }
+        else
+        {
+          itkExceptionMacro(<<"This type is not of string type, can't add the element in it");
+        }
+      }
+    }
+  }
+  else
+  {
+    AddField(key,value);
+  }
+}
+
 VectorDataKeywordlist::FieldType
 VectorDataKeywordlist
   ::GetNthField(unsigned int index) const
diff --git a/Code/IO/otbVectorDataKeywordlist.h b/Code/IO/otbVectorDataKeywordlist.h
index c62ba6b31b..3ce6ac94f0 100644
--- a/Code/IO/otbVectorDataKeywordlist.h
+++ b/Code/IO/otbVectorDataKeywordlist.h
@@ -53,6 +53,11 @@ class VectorDataKeywordlist
 
 
     void AddField(OGRFieldDefn* fieldDefn, OGRField* field);
+
+    /**
+      * \param key The name of the field.
+      * \param value The value of the field.
+      */
     void AddField(std::string key,std::string value);
 
   /**
@@ -68,6 +73,12 @@ class VectorDataKeywordlist
     */
     bool HasField(std::string key) const;
 
+    /**
+      * \param key The name of the field.
+      * \param value The value of the field.
+      */
+    void SetFieldAsString(std::string key,std::string value);
+
   /**
     * \return the nth field of the node as a std::pair of (key,value).
     * \param index the index of the field to return.
-- 
GitLab