From cb57a4f513cdacdb5cfdc49d549e72583926d032 Mon Sep 17 00:00:00 2001 From: Emmanuel Christophe <emmanuel.christophe@orfeo-toolbox.org> Date: Thu, 30 Apr 2009 11:07:53 +0800 Subject: [PATCH] ENH: Clearing the field structure in DataNote --- Code/Common/otbDataNode.h | 20 +++++----- Code/Common/otbDataNode.txx | 57 +++++++++++++++++++++++++++- Code/IO/otbKMLVectorDataIO.txx | 8 ++-- Code/IO/otbSHPVectorDataIO.txx | 26 ++++++------- Code/IO/otbVectorDataKeywordlist.cxx | 19 ++++++++++ Code/IO/otbVectorDataKeywordlist.h | 1 + 6 files changed, 102 insertions(+), 29 deletions(-) diff --git a/Code/Common/otbDataNode.h b/Code/Common/otbDataNode.h index a72057b190..6b888cf647 100644 --- a/Code/Common/otbDataNode.h +++ b/Code/Common/otbDataNode.h @@ -99,8 +99,8 @@ public: typedef typename PolygonListType::ConstPointer PolygonListConstPointerType; /** Fields typedef */ - typedef std::map<std::string,std::string> FieldMapType; - typedef std::pair<std::string,std::string> FieldType; +// typedef std::map<std::string,std::string> FieldMapType; +// typedef std::pair<std::string,std::string> FieldType; /** Accessors */ itkGetMacro(NodeType,NodeType); @@ -203,18 +203,18 @@ public: * \param key The name of the field. * \param value The value of the field. */ - void SetField(std::string key, std::string value); + 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 retuned if the key was not found. + * \return The value of the field. A default value is returned if the key was not found. */ - std::string GetField(std::string key) const; + std::string GetFieldAsString(std::string key) const; /** * Remove the field associated with the given key, if possible. * \param key The name of the field. */ - void RemoveField(std::string key); +// void RemoveField(std::string key); /** * \return True if the node contains the field named after the given key. * \param key The name of the field. @@ -224,15 +224,15 @@ public: * \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; +// FieldType GetNthField(unsigned int index) const; /** * \return the number of fields in the node. */ - unsigned int GetNumberOfFields() const; +// unsigned int GetNumberOfFields() const; /** * Clear all fields. */ - void ClearFields(); +// void ClearFields(); protected: /** Constructor */ @@ -266,7 +266,7 @@ private: DataType m_Data; /** The fields map */ - FieldMapType m_FieldMap; +// FieldMapType m_FieldMap; }; } // end namespace diff --git a/Code/Common/otbDataNode.txx b/Code/Common/otbDataNode.txx index bd21d74087..8f53c57d48 100644 --- a/Code/Common/otbDataNode.txx +++ b/Code/Common/otbDataNode.txx @@ -231,7 +231,7 @@ DataNode<TPrecision,VDimension,TValuePrecision> } return oss.str(); } - +/* template <class TPrecision, unsigned int VDimension, class TValuePrecision> void DataNode<TPrecision,VDimension,TValuePrecision> @@ -239,6 +239,24 @@ DataNode<TPrecision,VDimension,TValuePrecision> { m_FieldMap[key] = value; } +*/ + +template <class TPrecision, unsigned int VDimension, class TValuePrecision> + void + DataNode<TPrecision,VDimension,TValuePrecision> + ::SetFieldAsString(std::string key, std::string value) +{ + otb::VectorDataKeywordlist kwl; + itk::ExposeMetaData< VectorDataKeywordlist >(this->GetMetaDataDictionary(), + MetaDataKey::VectorDataKeywordlistKey, + kwl); + kwl.AddField(key, value); + itk::EncapsulateMetaData< VectorDataKeywordlist >(this->GetMetaDataDictionary(), + MetaDataKey::VectorDataKeywordlistKey, + kwl); +} + +/* template <class TPrecision, unsigned int VDimension, class TValuePrecision> std::string DataNode<TPrecision,VDimension,TValuePrecision> @@ -252,7 +270,24 @@ DataNode<TPrecision,VDimension,TValuePrecision> { return "Unknown Key"; } +}*/ + +template <class TPrecision, unsigned int VDimension, class TValuePrecision> + std::string + DataNode<TPrecision,VDimension,TValuePrecision> + ::GetFieldAsString(std::string key) const +{ + VectorDataKeywordlist keywordlist; + if (HasField(key)) + { + itk::ExposeMetaData< VectorDataKeywordlist >(this->GetMetaDataDictionary(), + MetaDataKey::VectorDataKeywordlistKey, keywordlist); + return keywordlist.GetFieldAsString(key); + } + return ""; } + +/* template <class TPrecision, unsigned int VDimension, class TValuePrecision> void DataNode<TPrecision,VDimension,TValuePrecision> @@ -267,6 +302,24 @@ DataNode<TPrecision,VDimension,TValuePrecision> { return (m_FieldMap.find(key)!=m_FieldMap.end()); } +*/ + +template <class TPrecision, unsigned int VDimension, class TValuePrecision> + bool + DataNode<TPrecision,VDimension,TValuePrecision> + ::HasField(std::string key) const +{ + VectorDataKeywordlist keywordlist; + if (this->GetMetaDataDictionary().HasKey(MetaDataKey::VectorDataKeywordlistKey)) + { + itk::ExposeMetaData< VectorDataKeywordlist >(this->GetMetaDataDictionary(), + MetaDataKey::VectorDataKeywordlistKey, keywordlist); + return keywordlist.HasField(key); + } + return false; +} + +/* template <class TPrecision, unsigned int VDimension, class TValuePrecision> typename DataNode<TPrecision,VDimension,TValuePrecision> ::FieldType @@ -298,7 +351,7 @@ DataNode<TPrecision,VDimension,TValuePrecision> ::ClearFields() { m_FieldMap.clear(); -} +}*/ template <class TPrecision, unsigned int VDimension, class TValuePrecision> bool DataNode<TPrecision,VDimension,TValuePrecision> diff --git a/Code/IO/otbKMLVectorDataIO.txx b/Code/IO/otbKMLVectorDataIO.txx index 77f601eeae..b0a38a71e2 100644 --- a/Code/IO/otbKMLVectorDataIO.txx +++ b/Code/IO/otbKMLVectorDataIO.txx @@ -163,7 +163,7 @@ KMLVectorDataIO<TData>::WalkContainer(const ContainerPtr& container, DataNodePoi document->SetNodeId(feature->get_id()); if (feature->has_name()) { - document->SetField("name",feature->get_name()); + document->SetFieldAsString("name",feature->get_name()); } m_Tree->Add(document,father); WalkFeature(feature,document); @@ -176,7 +176,7 @@ KMLVectorDataIO<TData>::WalkContainer(const ContainerPtr& container, DataNodePoi folder->SetNodeId(feature->get_id()); if (feature->has_name()) { - folder->SetField("name",feature->get_name()); + folder->SetFieldAsString("name",feature->get_name()); } m_Tree->Add(folder,father); WalkFeature(feature,folder); @@ -635,7 +635,7 @@ template<class TData> DocumentPtr document = factory->CreateDocument(); if (dataNode->HasField("name")) { - std::string fieldname = dataNode->GetField("name"); + std::string fieldname = dataNode->GetFieldAsString("name"); document->set_name(fieldname); } kml->set_feature(document); @@ -646,7 +646,7 @@ template<class TData> case FOLDER: { FolderPtr folder = factory->CreateFolder(); - std::string fieldname = dataNode->GetField("name"); + std::string fieldname = dataNode->GetFieldAsString("name"); folder->set_name(fieldname); currentDocument->add_feature(folder); currentFolder = folder; diff --git a/Code/IO/otbSHPVectorDataIO.txx b/Code/IO/otbSHPVectorDataIO.txx index 9177440d3a..41e6c5ddc0 100644 --- a/Code/IO/otbSHPVectorDataIO.txx +++ b/Code/IO/otbSHPVectorDataIO.txx @@ -156,13 +156,13 @@ SHPVectorDataIO<TData> document->SetNodeId(dfn->GetName()); /** Retrieving the fields types */ - OGRFieldDefn * field; - for (int fieldIndex = 0; fieldIndex<dfn->GetFieldCount();++fieldIndex) - { - field = dfn->GetFieldDefn(fieldIndex); - document->SetField(field->GetNameRef(),OGRFieldDefn::GetFieldTypeName(field->GetType())); - // std::cout<<"Document "<<document->GetNodeId()<<": Adding field "<<field->GetNameRef()<<" "<<OGRFieldDefn::GetFieldTypeName(field->GetType())<<std::endl; - } +// OGRFieldDefn * field; +// for (int fieldIndex = 0; fieldIndex<dfn->GetFieldCount();++fieldIndex) +// { +// field = dfn->GetFieldDefn(fieldIndex); +// document->SetField(field->GetNameRef(),OGRFieldDefn::GetFieldTypeName(field->GetType())); +// // std::cout<<"Document "<<document->GetNodeId()<<": Adding field "<<field->GetNameRef()<<" "<<OGRFieldDefn::GetFieldTypeName(field->GetType())<<std::endl; +// } /** Adding the layer to the data tree */ tree->Add(document,root); @@ -189,12 +189,12 @@ SHPVectorDataIO<TData> folder->SetNodeId(feature->GetDefnRef()->GetName()); // Reading fields - for (int fieldIndex = 0; fieldIndex<dfn->GetFieldCount();++fieldIndex) - { - OGRFieldDefn * field = dfn->GetFieldDefn(fieldIndex); - folder->SetField(field->GetNameRef(),feature->GetFieldAsString(fieldIndex)); - // std::cout<<"Folder "<<folder->GetNodeId()<<": Adding field "<<field->GetNameRef()<<" "<<feature->GetFieldAsString(fieldIndex)<<std::endl; - } +// for (int fieldIndex = 0; fieldIndex<dfn->GetFieldCount();++fieldIndex) +// { +// OGRFieldDefn * field = dfn->GetFieldDefn(fieldIndex); +// folder->SetField(field->GetNameRef(),feature->GetFieldAsString(fieldIndex)); +// // std::cout<<"Folder "<<folder->GetNodeId()<<": Adding field "<<field->GetNameRef()<<" "<<feature->GetFieldAsString(fieldIndex)<<std::endl; +// } // Add the folder to the document diff --git a/Code/IO/otbVectorDataKeywordlist.cxx b/Code/IO/otbVectorDataKeywordlist.cxx index cb94264e87..ef5690670f 100644 --- a/Code/IO/otbVectorDataKeywordlist.cxx +++ b/Code/IO/otbVectorDataKeywordlist.cxx @@ -56,6 +56,25 @@ VectorDataKeywordlist m_FieldList.push_back(CopyOgrField(newField)); }; + +void + VectorDataKeywordlist + ::AddField(std::string key,std::string value) +{ + FieldType newField; + + OGRFieldDefn* fieldDefn = new OGRFieldDefn(key.c_str(), OFTString); + + OGRField field; + char * cstr = new char[value.length() + 1]; + strcpy(cstr, value.c_str()); + field.String = cstr; + + newField.first=fieldDefn; + newField.second=field; + m_FieldList.push_back(newField); +} + std::string VectorDataKeywordlist ::GetFieldAsString(std::string key) const diff --git a/Code/IO/otbVectorDataKeywordlist.h b/Code/IO/otbVectorDataKeywordlist.h index bd0e438cfb..c62ba6b31b 100644 --- a/Code/IO/otbVectorDataKeywordlist.h +++ b/Code/IO/otbVectorDataKeywordlist.h @@ -53,6 +53,7 @@ class VectorDataKeywordlist void AddField(OGRFieldDefn* fieldDefn, OGRField* field); + void AddField(std::string key,std::string value); /** * Returns the value associated with a field name. -- GitLab