From 41373df99a359bae25fb648d3bd8bb8a73b79a90 Mon Sep 17 00:00:00 2001
From: Emmanuel Christophe <emmanuel.christophe@orfeo-toolbox.org>
Date: Mon, 27 Apr 2009 17:16:13 +0800
Subject: [PATCH] ENH: VectorData Metadata (works for reading)

---
 Code/Common/otbDataNode.txx          |  6 +-
 Code/IO/otbSHPVectorDataIO.txx       |  2 +-
 Code/IO/otbVectorDataKeywordlist.cxx | 94 +++++++++++++++++++++++++---
 Code/IO/otbVectorDataKeywordlist.h   |  7 ++-
 4 files changed, 92 insertions(+), 17 deletions(-)

diff --git a/Code/Common/otbDataNode.txx b/Code/Common/otbDataNode.txx
index 86b41bff87..bd21d74087 100644
--- a/Code/Common/otbDataNode.txx
+++ b/Code/Common/otbDataNode.txx
@@ -227,11 +227,7 @@ DataNode<TPrecision,VDimension,TValuePrecision>
   {
     VectorDataKeywordlist kwl;
     itk::ExposeMetaData<VectorDataKeywordlist>(GetMetaDataDictionary(), MetaDataKey::VectorDataKeywordlistKey, kwl);
-    oss<< "  -> Metadata: " << kwl;
-  }
-  else
-  {
-    oss<< "  -> No metadata";
+    oss<< "\n  -> Metadata: " << kwl;
   }
   return oss.str();
 }
diff --git a/Code/IO/otbSHPVectorDataIO.txx b/Code/IO/otbSHPVectorDataIO.txx
index 476980c99a..b0a191ecc3 100644
--- a/Code/IO/otbSHPVectorDataIO.txx
+++ b/Code/IO/otbSHPVectorDataIO.txx
@@ -214,7 +214,7 @@ SHPVectorDataIO<TData>
         std::cout << "Inserting " << feature->GetFieldCount() << " fields." << std::endl;
         for (int fieldNum=0; fieldNum< feature->GetFieldCount(); ++fieldNum)
         {
-          kwl.AddField(feature->GetFieldDefnRef((fieldNum), feature->GetRawFieldRef((fieldNum));
+          kwl.AddField(feature->GetFieldDefnRef(fieldNum), feature->GetRawFieldRef(fieldNum));
         }
 
 
diff --git a/Code/IO/otbVectorDataKeywordlist.cxx b/Code/IO/otbVectorDataKeywordlist.cxx
index 411dca4318..a597e7835b 100644
--- a/Code/IO/otbVectorDataKeywordlist.cxx
+++ b/Code/IO/otbVectorDataKeywordlist.cxx
@@ -33,14 +33,16 @@ VectorDataKeywordlist
 {
   for (unsigned int i = 0; i < m_FieldList.size(); ++i)
   {
-    delete(m_FieldList[i]);
+    delete(m_FieldList[i].first);
   }
 }
 
 void VectorDataKeywordlist::
-    AddField(OGRFieldDefn* field)
+    AddField(OGRFieldDefn* fieldDefn, OGRField* field)
 {
-  OGRFieldDefn* newField = new OGRFieldDefn(field);//this ogr construction does a clone
+  FieldType newField;
+  newField.first = new OGRFieldDefn(fieldDefn);
+  newField.second = *field;
   m_FieldList.push_back(newField);
 };
 
@@ -50,7 +52,9 @@ VectorDataKeywordlist::
 {
   for (unsigned int i = 0; i < p.m_FieldList.size(); ++i)
   {
-    OGRFieldDefn* newField = new OGRFieldDefn(p.m_FieldList[i]);//this ogr construction does a clone
+    FieldType newField;
+    newField.first = new OGRFieldDefn(p.m_FieldList[i].first);
+    newField.second = p.m_FieldList[i].second;
     m_FieldList.push_back(newField);
   }
 }
@@ -66,15 +70,89 @@ void
 VectorDataKeywordlist::
     PrintSelf(std::ostream& os, itk::Indent indent) const
 {
-  os << indent << " VectorData Keyword list:"<<std::endl;
-  os << indent << "  - Size: " << m_FieldList.size() << std::endl;
+  os << indent << " VectorData Keyword list: ";
+  os << indent << " - Size: " << m_FieldList.size() << std::endl;
   for (unsigned int i = 0; i < m_FieldList.size(); ++i)
   {
-    os << indent << "    " << m_FieldList[i]->GetNameRef ();
-    os << " : " << (*(m_FieldList[i]->GetDefaultRef())).Integer;
+    os << indent << "    " << PrintField(m_FieldList[i]);
   }
 }
 
+std::string
+VectorDataKeywordlist::
+    PrintField(FieldType field) const
+{
+  std::stringstream output;
+  output << field.first->GetNameRef() << " (";
+  output << field.first->GetFieldTypeName(field.first->GetType()) << "): ";
+  switch(field.first->GetType())
+  {
+    case OFTInteger:
+    {
+      output << field.second.Integer;
+      break;
+    }
+    case OFTIntegerList:
+    {
+      output << "Type not handled for printing";
+      break;
+    }
+    case OFTReal:
+    {
+      output << field.second.Real;
+      break;
+    }
+    case OFTRealList:
+    {
+      output << "Type not handled for printing";
+      break;
+    }
+    case OFTString:
+    {
+      output << field.second.String;
+      break;
+    }
+    case OFTStringList:
+    {
+      output << "Type not handled for printing";
+      break;
+    }
+    case OFTWideString:
+    {
+      output << "Type not handled for printing";
+      break;
+    }
+    case OFTWideStringList:
+    {
+      output << "Type not handled for printing";
+      break;
+    }
+    case OFTBinary:
+    {
+      output << "Type not handled for printing";
+      break;
+    }
+    case OFTDate:
+    {
+      output << field.second.Date.Year << field.second.Date.Month << field.second.Date.Day;
+      break;
+    }
+    case OFTTime:
+    {
+      output << field.second.Date.Hour << field.second.Date.Minute << field.second.Date.Second;
+      break;
+    }
+    case OFTDateTime:
+    {
+      output << field.second.Date.Year << field.second.Date.Month << field.second.Date.Day << "-"
+          << field.second.Date.Hour << field.second.Date.Minute << field.second.Date.Second;
+      break;
+    }
+  }
+  output << std::endl;
+  return output.str();
+}
+
 std::ostream &
     operator<<(std::ostream &os, const VectorDataKeywordlist &kwl)
 {
diff --git a/Code/IO/otbVectorDataKeywordlist.h b/Code/IO/otbVectorDataKeywordlist.h
index aabb6ce724..123dfd6022 100644
--- a/Code/IO/otbVectorDataKeywordlist.h
+++ b/Code/IO/otbVectorDataKeywordlist.h
@@ -46,11 +46,11 @@ class VectorDataKeywordlist
 //     /** Run-time type information (and related methods). */
 //     itkTypeMacro(VectorDataKeywordlist, LightObject);
 
+    typedef std::pair<OGRFieldDefn*,OGRField> FieldType;
+    typedef std::vector< FieldType > FieldListType;
 
-    typedef std::vector<OGRFieldDefn*> FieldListType;
 
-
-    void AddField(OGRFieldDefn* field);
+    void AddField(OGRFieldDefn* fieldDefn, OGRField* field);
 
     virtual void Print(std::ostream& os, itk::Indent indent=0) const;
 
@@ -66,6 +66,7 @@ class VectorDataKeywordlist
 
   private:
 
+    std::string PrintField(FieldType field) const;
     FieldListType m_FieldList;
 
 
-- 
GitLab