diff --git a/Code/IO/otbVectorDataKeywordlist.cxx b/Code/IO/otbVectorDataKeywordlist.cxx index afa5fc85603a747e72213590a21bcfbb751ac645..eba5dfada216dc3848c197d742ced98619ff18e0 100644 --- a/Code/IO/otbVectorDataKeywordlist.cxx +++ b/Code/IO/otbVectorDataKeywordlist.cxx @@ -28,6 +28,12 @@ VectorDataKeywordlist //Nothing to do here } +VectorDataKeywordlist +::VectorDataKeywordlist(const Self& other) +{ + *this = other; +} + VectorDataKeywordlist ::~VectorDataKeywordlist() { @@ -129,6 +135,30 @@ VectorDataKeywordlist return 0.; } +int +VectorDataKeywordlist +::GetFieldAsInt(std::string key) const +{ + 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() == OFTInteger) + { + return (int)(m_FieldList[i].second.Integer); + } + if (m_FieldList[i].first->GetType() == OFTReal) + { + return (int)(m_FieldList[i].second.Real); + } + itkExceptionMacro( + << "This type (" << m_FieldList[i].first->GetType() << + ") is not handled (yet) by GetFieldAsInt(), please request for it"); + } + } + return 0.; +} + void VectorDataKeywordlist ::SetFieldAsDouble(std::string key, double value) @@ -164,7 +194,49 @@ VectorDataKeywordlist newField.second = field; m_FieldList.push_back(newField); } +} +void +VectorDataKeywordlist +::SetFieldAsInt(std::string key, int 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() == OFTInteger) + { + OGRField field; + field.Integer = value; + m_FieldList[i].second = field; + } + else + if (m_FieldList[i].first->GetType() == OFTReal) + { + OGRField field; + field.Real = static_cast<double>(value); + m_FieldList[i].second = field; + } + else + { + itkExceptionMacro(<< "This type is not of integer type, can't add the element in it"); + } + } + } + } + else + { + FieldType newField; + + OGRFieldDefn* fieldDefn = new OGRFieldDefn(key.c_str(), OFTInteger); + OGRField field; + field.Integer = value; + newField.first = fieldDefn; + newField.second = field; + m_FieldList.push_back(newField); + } } bool @@ -271,6 +343,35 @@ VectorDataKeywordlist } } +void +VectorDataKeywordlist::CopyFieldList(const Self& kwl) +{ + for (unsigned int idx = 0; idx < kwl.GetNumberOfFields(); ++idx) + { + switch(kwl.GetNthField(idx).first->GetType()) + { + case OFTString : + { + this->SetFieldAsString(kwl.GetNthField(idx).first->GetNameRef(), + kwl.GetNthField(idx).second.String); + break; + } + case OFTInteger : + { + this->SetFieldAsInt(kwl.GetNthField(idx).first->GetNameRef(), + kwl.GetNthField(idx).second.Integer); + break; + } + case OFTReal : + { + this->SetFieldAsDouble(kwl.GetNthField(idx).first->GetNameRef(), + kwl.GetNthField(idx).second.Real); + break; + } + } + } +} + std::string VectorDataKeywordlist ::PrintField(FieldType field) const diff --git a/Code/IO/otbVectorDataKeywordlist.h b/Code/IO/otbVectorDataKeywordlist.h index 79d95eb8093a651d9e964622eb9922a1a9911c81..4121814071d55e7c5d6e34f5811deec4a7453515 100644 --- a/Code/IO/otbVectorDataKeywordlist.h +++ b/Code/IO/otbVectorDataKeywordlist.h @@ -90,6 +90,20 @@ public: */ void SetFieldAsDouble(std::string key, double value); + /** + * Set the field as an integer + * \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; + /** * \return the nth field of the node as a std::pair of (key, value). * \param index the index of the field to return. @@ -106,6 +120,11 @@ public: */ std::vector<std::string> GetFieldList() const; + /** + * Copy all the fields from another kwl + */ + void CopyFieldList(const Self& kwl); + /** * Print the keyword list */ @@ -117,7 +136,7 @@ public: virtual ~VectorDataKeywordlist(); /** Constructor by copy (deep copy)*/ - VectorDataKeywordlist(const Self&); + VectorDataKeywordlist(const Self& other); /** Deep copy operator*/ void operator =(const Self&);