diff --git a/Code/Common/otbDataNode.h b/Code/Common/otbDataNode.h index 52ee5086c90ec619f16616a19ab8b8dbc6965231..257230d100108bba223d65174373527835551689 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 8f53c57d48ba5da35c41bd153f1145dfded469b0..405690df2fa87f7b4bf968cdc9f19cb902000bf6 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 a6145b34ad122ce277186a83cc7c5a6a53b406c8..ebeb087add6e636be6025f6c1ae40a98ce2b41a7 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 c62ba6b31bf173615a78468aaeae2f3e8435352d..3ce6ac94f0affa6b708d45384dcd3a7bb5414b25 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.