Skip to content
Snippets Groups Projects
Commit 711a6b46 authored by Aurélien Bricier's avatar Aurélien Bricier
Browse files

ENH: added Set/GetFieldAsString to VectorDataKeywordList and DataNode

parent ae67b8d3
No related branches found
No related tags found
No related merge requests found
......@@ -223,6 +223,19 @@ public:
*/
int GetFieldAsInt(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 SetFieldAsDouble(std::string key, double 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.
*/
double GetFieldAsDouble(std::string key) const;
/**
* Remove the field associated with the given key, if possible.
* \param key The name of the field.
......
......@@ -274,6 +274,38 @@ DataNode<TPrecision, VDimension, TValuePrecision>
kwl);
}
template <class TPrecision, unsigned int VDimension, class TValuePrecision>
void
DataNode<TPrecision, VDimension, TValuePrecision>
::SetFieldAsDouble(std::string key, double value)
{
otb::VectorDataKeywordlist kwl;
itk::ExposeMetaData<VectorDataKeywordlist>(this->GetMetaDataDictionary(),
MetaDataKey::VectorDataKeywordlistKey,
kwl);
kwl.SetFieldAsDouble(key, value);
itk::EncapsulateMetaData<VectorDataKeywordlist>(this->GetMetaDataDictionary(),
MetaDataKey::VectorDataKeywordlistKey,
kwl);
}
template <class TPrecision, unsigned int VDimension, class TValuePrecision>
double
DataNode<TPrecision, VDimension, TValuePrecision>
::GetFieldAsDouble(std::string key) const
{
VectorDataKeywordlist keywordlist;
if (HasField(key))
{
itk::ExposeMetaData<VectorDataKeywordlist>(this->GetMetaDataDictionary(),
MetaDataKey::VectorDataKeywordlistKey, keywordlist);
return keywordlist.GetFieldAsDouble(key);
}
return 0;
}
/*
template <class TPrecision, unsigned int VDimension, class TValuePrecision>
std::string
......
......@@ -105,6 +105,68 @@ VectorDataKeywordlist
return "";
}
double
VectorDataKeywordlist
::GetFieldAsDouble(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 (double)(m_FieldList[i].second.Integer);
}
if (m_FieldList[i].first->GetType() == OFTReal)
{
return (double)(m_FieldList[i].second.Real);
}
itkExceptionMacro(
<< "This type (" << m_FieldList[i].first->GetType() <<
") is not handled (yet) by GetFieldAsDouble(), please request for it");
}
}
return 0.;
}
void
VectorDataKeywordlist
::SetFieldAsDouble(std::string key, double 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() == OFTReal)
{
OGRField field;
field.Real = value;
m_FieldList[i].second = field;
}
else
{
itkExceptionMacro(<< "This type is not of double type, can't add the element in it");
}
}
}
}
else
{
FieldType newField;
OGRFieldDefn* fieldDefn = new OGRFieldDefn(key.c_str(), OFTReal);
OGRField field;
field.Real = value;
newField.first = fieldDefn;
newField.second = field;
m_FieldList.push_back(newField);
}
}
bool
VectorDataKeywordlist
::HasField(std::string key) const
......
......@@ -77,6 +77,19 @@ public:
*/
void SetFieldAsString(std::string key, std::string 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.
*/
double GetFieldAsDouble(std::string key) const;
/**
* \param key The name of the field.
* \param value The value of the field.
*/
void SetFieldAsDouble(std::string key, double value);
/**
* \return the nth field of the node as a std::pair of (key, value).
* \param index the index of the field to return.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment