From 0a9852941de944cca72b91c052e0495d8380bf8a Mon Sep 17 00:00:00 2001 From: Emmanuel Christophe <emmanuel.christophe@orfeo-toolbox.org> Date: Tue, 28 Apr 2009 10:45:33 +0800 Subject: [PATCH] ENH: adding accessor --- Code/IO/otbImageKeywordlist.h | 3 ++ Code/IO/otbVectorDataKeywordlist.cxx | 71 +++++++++++++++++++++++----- Code/IO/otbVectorDataKeywordlist.h | 34 ++++++++++++- 3 files changed, 95 insertions(+), 13 deletions(-) diff --git a/Code/IO/otbImageKeywordlist.h b/Code/IO/otbImageKeywordlist.h index 201a9bc523..e65134de21 100644 --- a/Code/IO/otbImageKeywordlist.h +++ b/Code/IO/otbImageKeywordlist.h @@ -44,6 +44,9 @@ public: /** Standard class typedefs. */ typedef ImageKeywordlist Self; + virtual const char *GetNameOfClass() const + {return "ImageKeywordlist";} + typedef std::map<ossimString, ossimString> KeywordlistMap; void SetKeywordlist(const ossimKeywordlist& kwl) diff --git a/Code/IO/otbVectorDataKeywordlist.cxx b/Code/IO/otbVectorDataKeywordlist.cxx index 8e276f148b..cb94264e87 100644 --- a/Code/IO/otbVectorDataKeywordlist.cxx +++ b/Code/IO/otbVectorDataKeywordlist.cxx @@ -42,8 +42,9 @@ VectorDataKeywordlist } } -void VectorDataKeywordlist:: - AddField(OGRFieldDefn* fieldDefn, OGRField* field) +void +VectorDataKeywordlist + ::AddField(OGRFieldDefn* fieldDefn, OGRField* field) { FieldType newField; newField.first = fieldDefn; @@ -55,9 +56,55 @@ void VectorDataKeywordlist:: m_FieldList.push_back(CopyOgrField(newField)); }; +std::string +VectorDataKeywordlist + ::GetFieldAsString(std::string key) const +{ + for (unsigned int i = 0; i < m_FieldList.size(); ++i) + { + if (key.compare(m_FieldList[i].first->GetNameRef()) == 0) + { + return m_FieldList[i].second.String; + } + } + return ""; +} + +bool +VectorDataKeywordlist + ::HasField(std::string key) const +{ + for (unsigned int i = 0; i < m_FieldList.size(); ++i) + { + if (key.compare(m_FieldList[i].first->GetNameRef()) == 0) + { + return true; + } + } + return false; +} + +VectorDataKeywordlist::FieldType +VectorDataKeywordlist + ::GetNthField(unsigned int index) const +{ + if (index > m_FieldList.size()) + { + itkExceptionMacro(<<" Accessing out-of-range metadata "); + } + return m_FieldList[index]; +} + +unsigned int +VectorDataKeywordlist + ::GetNumberOfFields() const +{ + return m_FieldList.size(); +} + void -VectorDataKeywordlist:: - operator=(const Self& p) +VectorDataKeywordlist + ::operator=(const Self& p) { for (unsigned int i = 0; i < p.m_FieldList.size(); ++i) { @@ -66,15 +113,15 @@ VectorDataKeywordlist:: } void -VectorDataKeywordlist:: - Print(std::ostream& os, itk::Indent indent) const +VectorDataKeywordlist + ::Print(std::ostream& os, itk::Indent indent) const { this->PrintSelf(os, indent.GetNextIndent()); } void -VectorDataKeywordlist:: - PrintSelf(std::ostream& os, itk::Indent indent) const +VectorDataKeywordlist + ::PrintSelf(std::ostream& os, itk::Indent indent) const { os << indent << " VectorData Keyword list: "; os << indent << " - Size: " << m_FieldList.size() << std::endl; @@ -85,8 +132,8 @@ VectorDataKeywordlist:: } std::string -VectorDataKeywordlist:: - PrintField(FieldType field) const +VectorDataKeywordlist + ::PrintField(FieldType field) const { std::stringstream output; output << std::setprecision(15); @@ -165,8 +212,8 @@ VectorDataKeywordlist:: VectorDataKeywordlist::FieldType -VectorDataKeywordlist:: - CopyOgrField(FieldType field) +VectorDataKeywordlist + ::CopyOgrField(FieldType field) { FieldType outField; outField.first = new OGRFieldDefn(field.first); diff --git a/Code/IO/otbVectorDataKeywordlist.h b/Code/IO/otbVectorDataKeywordlist.h index 606ee31b6c..bd0e438cfb 100644 --- a/Code/IO/otbVectorDataKeywordlist.h +++ b/Code/IO/otbVectorDataKeywordlist.h @@ -33,7 +33,7 @@ namespace otb * a vector object. This information is retrieved from the input file (a * shapefile for example) and propagated along the pipeline with the object. * - * This is the equivalent of the otbOssimKeywordlist class but for OGR information. + * This is the equivalent of the otbImageKeywordlist class but for OGR information. * * \todo add the accessor to enable modifying/updating the data. * @@ -48,12 +48,44 @@ class VectorDataKeywordlist typedef std::pair<OGRFieldDefn*,OGRField> FieldType; typedef std::vector< FieldType > FieldListType; + virtual const char *GetNameOfClass() const + {return "VectorDataKeywordlist";} + void AddField(OGRFieldDefn* fieldDefn, OGRField* field); + /** + * 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 retuned if the key was not found. + */ + std::string GetFieldAsString(std::string key) const; + + /** + * \return True if the node contains the field named after the given key. + * \param key The name of the field. + */ + bool HasField(std::string key) const; + + /** + * \return the nth field of the node as a std::pair of (key,value). + * \param index the index of the field to return. + */ + FieldType GetNthField(unsigned int index) const; + + /** + * \return the number of fields in the node. + */ + unsigned int GetNumberOfFields() const; + + /** + * Print the keyword list + */ virtual void Print(std::ostream& os, itk::Indent indent=0) const; + /** Constructor */ VectorDataKeywordlist(); + /** Destructor */ virtual ~VectorDataKeywordlist(); /** Constructor by copy (deep copy)*/ -- GitLab