diff --git a/Code/Common/otbDataNode.h b/Code/Common/otbDataNode.h index a72057b190ec8b24139b6846e2fceeebd150c473..6b888cf6479902ead140e76bed5a3c654eb53b72 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 bd21d74087f627785758fe7cf30305c321c1169c..8f53c57d48ba5da35c41bd153f1145dfded469b0 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 77f601eeaee8a3ccb6be8cdbe29fb4a2698d2276..b0a38a71e26d85f6f18ac750b60ce78cdd9f1c53 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 9177440d3ac3e4a53f3aa3c6b02c7052b5d7d858..41e6c5ddc08acd9fa850f13796a9a8509a6a3cd2 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 cb94264e873630113e9a466415a3d432ed332106..ef5690670f920f59b89327ec55d3601b7a850cb0 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 bd0e438cfb93d7ab6240eee43dc73c4c49652722..c62ba6b31bf173615a78468aaeae2f3e8435352d 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.