From: asl Date: Tue, 13 Oct 2015 14:27:26 +0000 (+0300) Subject: refs #649: land cover data object X-Git-Tag: v1.5~136 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=efaf298dce499879bcd94ea126314badda74f3b5;p=modules%2Fhydro.git refs #649: land cover data object --- diff --git a/src/HYDROData/HYDROData_LandCoverMap.cxx b/src/HYDROData/HYDROData_LandCoverMap.cxx index 55783a21..5c1f9e39 100644 --- a/src/HYDROData/HYDROData_LandCoverMap.cxx +++ b/src/HYDROData/HYDROData_LandCoverMap.cxx @@ -16,117 +16,257 @@ // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // -#include "HYDROData_LandCoverMap.h" -#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include #include +#include #include + #include IMPLEMENT_STANDARD_HANDLE(HYDROData_LandCoverMap, HYDROData_Entity) IMPLEMENT_STANDARD_RTTIEXT(HYDROData_LandCoverMap, HYDROData_Entity) -HYDROData_LandCoverMap::HYDROData_LandCoverMap() +class HYDROData_MapOfShapeToStricklerType : public NCollection_IndexedDataMap { +}; + +/** + Constructor + @param theMap the land cover map to iterate through +*/ +HYDROData_LandCoverMap::Iterator::Iterator( const HYDROData_LandCoverMap& theMap ) +{ + Init( theMap ); } -HYDROData_LandCoverMap::~HYDROData_LandCoverMap() +/** + Initialize the iterator + @param theMap the land cover map to iterate through +*/ +void HYDROData_LandCoverMap::Iterator::Init( const HYDROData_LandCoverMap& theMap ) { + myIterator = new TopoDS_Iterator( theMap.GetShape() ); + myIndex = 0; + theMap.myLab.FindChild( DataTag_Types ).FindAttribute( TDataStd_ExtStringArray::GetID(), myArray ); } -int HYDROData_LandCoverMap::GetNbFaces() const +/** + Destructor +*/ +HYDROData_LandCoverMap::Iterator::~Iterator() { - //TODO - return 0; + delete myIterator; } -TopoDS_Face HYDROData_LandCoverMap::GetFace( int theIndex ) const +/** + Return if the iterator has more elements + @return if the iterator has more elements +*/ +bool HYDROData_LandCoverMap::Iterator::More() const { - //TODO - return TopoDS_Face(); + return !myArray.IsNull() && myIterator->More(); } -QString HYDROData_LandCoverMap::GetStricklerType( int theIndex ) const +/** + Move iterator to the next element +*/ +void HYDROData_LandCoverMap::Iterator::Next() { - Handle(TDataStd_ExtStringArray) aTypesArray; - if( !myLab.FindChild( DataTag_Types ).FindAttribute( TDataStd_ExtStringArray::GetID(), aTypesArray ) ) - return ""; + myIterator->Next(); +} - TCollection_ExtendedString aType = aTypesArray->Value( theIndex ); - TCollection_AsciiString anAscii = aType; - return anAscii.ToCString(); +/** + Get the current land cover (face) + @return the land cover's face +*/ +TopoDS_Face HYDROData_LandCoverMap::Iterator::Face() const +{ + return TopoDS::Face( myIterator->Value() ); } -void HYDROData_LandCoverMap::SetStricklerType( int theIndex, const QString& theType ) +/** + Get the current land cover's Strickler type + @return the land cover's Strickler type +*/ +QString HYDROData_LandCoverMap::Iterator::StricklerType() const { - Handle(TDataStd_ExtStringArray) aTypesArray; - if( !myLab.FindChild( DataTag_Types ).FindAttribute( TDataStd_ExtStringArray::GetID(), aTypesArray ) ) - return; + if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() ) + return ""; + else + return HYDROData_Tool::toQString( myArray->Value( myIndex ) ); +} - std::string aType = theType.toStdString(); - aTypesArray->SetValue( theIndex, aType.c_str() ); +/** + Constructor +*/ +HYDROData_LandCoverMap::HYDROData_LandCoverMap() +{ } +/** + Destructor +*/ +HYDROData_LandCoverMap::~HYDROData_LandCoverMap() +{ +} + +/** + Import the land cover map from QGIS + @param theFileName the name of file + @return if the import is successful +*/ bool HYDROData_LandCoverMap::ImportQGIS( const QString& theFileName ) { //TODO return false; } +/** + Export the land cover map to QGIS + @param theFileName the name of file + @return if the export is successful +*/ bool HYDROData_LandCoverMap::ExportQGIS( const QString& theFileName ) const { //TODO return false; } +/** + Export the land cover map for the solver (Telemac) + @param theFileName the name of file + @return if the export is successful +*/ bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName ) const { //TODO return false; } -bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )&, const QString& theType ) +/** + Add a new object as land cover + @param theObject the object to add as land cover + @param theType the Strickler type for the new land cover + @return if the addition is successful +*/ +bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )& theObject, const QString& theType ) { //TODO return false; } -bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Polyline )&, const QString& theType ) +/** + Add a new polyline as land cover + @param thePolyline the polyline to add as land cover + @param theType the Strickler type for the new land cover + @return if the addition is successful +*/ +bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Polyline )& thePolyline, const QString& theType ) { //TODO return false; } -bool HYDROData_LandCoverMap::Add( const TopoDS_Face&, const QString& theType ) +/** + Remove the given face from land cover map + @param theFace the face to be removed + @return if the removing is successful +*/ +bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace ) { //TODO return false; } -bool HYDROData_LandCoverMap::Remove( int theIndex ) +/** + Split the land cover map by the given polyline + @param thePolyline the tool polyline to split the land cover map + @return if the removing is successful +*/ +bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_Polyline )& thePolyline ) { //TODO return false; } -bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_Polyline )& ) +/** + Merge the given faces in the land cover + @param theFaces the faces to merge in the land cover map + @param theType the Strickler type for the merged land cover + @return if the merge is successful +*/ +bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType ) { //TODO return false; } -bool HYDROData_LandCoverMap::Merge( const QList&, const QString& theType ) +/** + Get the shape of the land cover map +*/ +TopoDS_Shape HYDROData_LandCoverMap::GetShape() const { - //TODO - return false; + TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false ); + if ( !aLabel.IsNull() ) + { + Handle(TNaming_NamedShape) aNamedShape; + if( aLabel.FindAttribute( TNaming_NamedShape::GetID(), aNamedShape ) ) + return aNamedShape->Get(); + } + return TopoDS_Shape(); } -TopoDS_Shell HYDROData_LandCoverMap::GetShape() const +/** + Set the shape of the land cover map + @param theShape the new shape for the land cover map +*/ +void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape ) { - //TODO - return TopoDS_Shell(); + TNaming_Builder aBuilder( myLab.FindChild( DataTag_Shape ) ); + aBuilder.Generated( theShape ); } -void HYDROData_LandCoverMap::SetShape( const TopoDS_Shell& ) +/** + Perform the local partition algorithm on the land cover + @param theNewShape the new shape to add into the land cover + @param theNewType the new Strickler type for the new land cover + @return if the local partition is successful +*/ +bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType ) { //TODO } + +/** + Replace the set of land covers in the land cover map + @param theMap the map of shape (face) to Strickler type (string) +*/ +void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfShapeToStricklerType& theMap ) +{ + TopoDS_Compound aCompound; + BRep_Builder aCompoundBuilder; + aCompoundBuilder.MakeCompound( aCompound ); + + int n = theMap.Size(); + Handle( TDataStd_ExtStringArray ) aTypes = + TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, n-1, Standard_True ); + HYDROData_MapOfShapeToStricklerType::Iterator aNFIt( theMap ); + for( int i=0; aNFIt.More(); aNFIt.Next(), i++ ) + { + aCompoundBuilder.Add( aCompound, aNFIt.Key() ); + aTypes->SetValue( i, HYDROData_Tool::toExtString( aNFIt.Value() ) ); + } + + SetShape( aCompound ); +} diff --git a/src/HYDROData/HYDROData_LandCoverMap.h b/src/HYDROData/HYDROData_LandCoverMap.h index f479c428..7d9939cf 100644 --- a/src/HYDROData/HYDROData_LandCoverMap.h +++ b/src/HYDROData/HYDROData_LandCoverMap.h @@ -20,13 +20,17 @@ #define HYDROData_LANDCOVER_MAP_HeaderFile #include +#include DEFINE_STANDARD_HANDLE( HYDROData_LandCoverMap, HYDROData_Entity ) class TopoDS_Face; -class TopoDS_Shell; +class TopoDS_Shape; +class TopoDS_Iterator; +class TopTools_ListOfShape; class Handle( HYDROData_Polyline ); class Handle( HYDROData_Object ); +class HYDROData_MapOfShapeToStricklerType; class HYDROData_LandCoverMap : public HYDROData_Entity { @@ -40,15 +44,28 @@ protected: DataTag_Types, }; + class Iterator + { + public: + Iterator( const HYDROData_LandCoverMap& ); + ~Iterator(); + + void Init( const HYDROData_LandCoverMap& ); + bool More() const; + void Next(); + + TopoDS_Face Face() const; + QString StricklerType() const; + + private: + TopoDS_Iterator* myIterator; + int myIndex; + Handle(TDataStd_ExtStringArray) myArray; + }; + HYDROData_LandCoverMap(); ~HYDROData_LandCoverMap(); - int GetNbFaces() const; - TopoDS_Face GetFace( int theIndex ) const; - - QString GetStricklerType( int theIndex ) const; - void SetStricklerType( int theIndex, const QString& theType ); - bool ImportQGIS( const QString& theFileName ); bool ExportQGIS( const QString& theFileName ) const; bool ExportTelemac( const QString& theFileName ) const; @@ -56,18 +73,23 @@ protected: bool Add( const Handle( HYDROData_Object )&, const QString& theType ); bool Add( const Handle( HYDROData_Polyline )&, const QString& theType ); - bool Remove( int theIndex ); + bool Remove( const TopoDS_Face& ); bool Split( const Handle( HYDROData_Polyline )& ); - bool Merge( const QList&, const QString& theType ); + bool Merge( const TopTools_ListOfShape&, const QString& theType ); protected: - TopoDS_Shell GetShape() const; - void SetShape( const TopoDS_Shell& ); - bool Add( const TopoDS_Face&, const QString& theType ); + TopoDS_Shape GetShape() const; + void SetShape( const TopoDS_Shape& ); + + bool LocalPartition( const TopoDS_Shape&, const QString& theNewType ); + void StoreLandCovers( const HYDROData_MapOfShapeToStricklerType& ); public: DEFINE_STANDARD_RTTI( HYDROData_LandCoverMap ); + +private: + friend class Iterator; }; #endif diff --git a/src/HYDROData/HYDROData_StricklerTable.cxx b/src/HYDROData/HYDROData_StricklerTable.cxx index d206c59c..212f9543 100644 --- a/src/HYDROData/HYDROData_StricklerTable.cxx +++ b/src/HYDROData/HYDROData_StricklerTable.cxx @@ -17,6 +17,7 @@ // #include +#include #include @@ -85,10 +86,10 @@ bool HYDROData_StricklerTable::Import( const QString& theFileName ) int aColorInt = aParts[1].toInt( 0, 16 ); QString anAttrValue = aParts.size()>2 ? aParts[2] : QString(); - TCollection_ExtendedString aTypeExt = toExtString( aType ); + TCollection_ExtendedString aTypeExt = HYDROData_Tool::toExtString( aType ); aMap->SetReal( aTypeExt, aCoeff ); aMap->SetInteger( aTypeExt, aColorInt ); - aMap->SetString( aTypeExt, toExtString( anAttrValue ) ); + aMap->SetString( aTypeExt, HYDROData_Tool::toExtString( anAttrValue ) ); } i++; } @@ -117,13 +118,13 @@ bool HYDROData_StricklerTable::Export( const QString& theFileName ) QStringList aTypes = GetTypes(); foreach( QString aType, aTypes ) { - TCollection_ExtendedString aTypeExt = toExtString( aType ); + TCollection_ExtendedString aTypeExt = HYDROData_Tool::toExtString( aType ); aStream << "\"" << aType << "\" " << Get( aType, 0.0 ); QString aColor = QString::number( aMap->GetInteger( aTypeExt ), 16 ).toUpper(); aColor = QString( 6-aColor.length(), '0' ) + aColor; - QString anAttrValue = toQString( aMap->GetString( aTypeExt ) ); + QString anAttrValue = HYDROData_Tool::toQString( aMap->GetString( aTypeExt ) ); aStream << " " << aColor; if( !anAttrValue.isEmpty() ) @@ -138,7 +139,7 @@ bool HYDROData_StricklerTable::Export( const QString& theFileName ) double HYDROData_StricklerTable::Get( const QString& theType, double theDefault ) const { Handle( TDataStd_NamedData ) aMap = Map(); - TCollection_ExtendedString aType = toExtString( theType ); + TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType ); if( aMap->HasReal( aType ) ) return aMap->GetReal( aType ); else @@ -148,7 +149,7 @@ double HYDROData_StricklerTable::Get( const QString& theType, double theDefault void HYDROData_StricklerTable::Set( const QString& theType, double theCoefficient ) { Handle(TDataStd_NamedData) aMap = Map(); - aMap->SetReal( toExtString( theType ), theCoefficient ); + aMap->SetReal( HYDROData_Tool::toExtString( theType ), theCoefficient ); } void HYDROData_StricklerTable::GetCoefficientRange( double& theMin, double& theMax ) const @@ -179,7 +180,7 @@ QStringList HYDROData_StricklerTable::GetTypes() const if( !aMap.IsNull() ) { for( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() ) - aSeq.append( toQString( it.Key() ) ); + aSeq.append( HYDROData_Tool::toQString( it.Key() ) ); } aSeq.sort(); return aSeq; @@ -189,7 +190,7 @@ bool HYDROData_StricklerTable::HasType( const QString& theType ) const { Handle(TDataStd_NamedData) aMap = Map(); - TCollection_ExtendedString aType = toExtString( theType ); + TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType ); return !aMap.IsNull() && aMap->HasReal( aType ); } @@ -231,25 +232,6 @@ Handle(TDataStd_NamedData) HYDROData_StricklerTable::Map() const return aMap; } -TCollection_ExtendedString HYDROData_StricklerTable::toExtString( const QString& theStr ) const -{ - TCollection_ExtendedString aRes; - if( !theStr.isEmpty() ) - { - Standard_ExtString extStr = new Standard_ExtCharacter[ ( theStr.length() + 1 ) * 2 ]; - memcpy( (void*)extStr, theStr.unicode(), theStr.length() * 2 ); - ((short*)extStr)[theStr.length()] = '\0'; - aRes = TCollection_ExtendedString( extStr ); - delete [] extStr; - } - return aRes; -} - -QString HYDROData_StricklerTable::toQString( const TCollection_ExtendedString& theStr ) const -{ - return QString( (QChar*)theStr.ToExtString(), theStr.Length() ); -} - QString HYDROData_StricklerTable::GetAttrName() const { Handle(TDataStd_AsciiString) aName; @@ -261,15 +243,15 @@ QString HYDROData_StricklerTable::GetAttrName() const void HYDROData_StricklerTable::SetAttrName( const QString& theAttrName ) const { - TDataStd_AsciiString::Set( myLab.FindChild( DataTag_AttrName ), toExtString( theAttrName ) ); + TDataStd_AsciiString::Set( myLab.FindChild( DataTag_AttrName ), HYDROData_Tool::toExtString( theAttrName ) ); } QString HYDROData_StricklerTable::GetAttrValue( const QString& theType ) const { Handle( TDataStd_NamedData ) aMap = Map(); - TCollection_ExtendedString aType = toExtString( theType ); + TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType ); if( aMap->HasString( aType ) ) - return toQString( aMap->GetString( aType ) ); + return HYDROData_Tool::toQString( aMap->GetString( aType ) ); else return ""; } @@ -277,19 +259,19 @@ QString HYDROData_StricklerTable::GetAttrValue( const QString& theType ) const void HYDROData_StricklerTable::SetAttrValue( const QString& theType, const QString& theAttrValue ) const { Handle( TDataStd_NamedData ) aMap = Map(); - TCollection_ExtendedString aType = toExtString( theType ); - aMap->SetString( aType, toExtString( theAttrValue ) ); + TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType ); + aMap->SetString( aType, HYDROData_Tool::toExtString( theAttrValue ) ); } QString HYDROData_StricklerTable::GetType( const QString& theAttrValue ) const { Handle( TDataStd_NamedData ) aMap = Map(); - TCollection_ExtendedString anAttrValue = toExtString( theAttrValue ); + TCollection_ExtendedString anAttrValue = HYDROData_Tool::toExtString( theAttrValue ); for( TDataStd_DataMapIteratorOfDataMapOfStringString it( aMap->GetStringsContainer() ); it.More(); it.Next() ) { if( it.Value() == anAttrValue ) { - QString aType = toQString( it.Key() ); + QString aType = HYDROData_Tool::toQString( it.Key() ); return aType; } } @@ -299,7 +281,7 @@ QString HYDROData_StricklerTable::GetType( const QString& theAttrValue ) const QColor HYDROData_StricklerTable::GetColor( const QString& theType ) const { Handle( TDataStd_NamedData ) aMap = Map(); - TCollection_ExtendedString aType = toExtString( theType ); + TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType ); if( aMap->HasInteger( aType ) ) { int aColorInt = aMap->GetInteger( aType ); @@ -315,7 +297,7 @@ QColor HYDROData_StricklerTable::GetColor( const QString& theType ) const void HYDROData_StricklerTable::SetColor( const QString& theType, const QColor& theColor ) const { Handle( TDataStd_NamedData ) aMap = Map(); - TCollection_ExtendedString aType = toExtString( theType ); + TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType ); int r = theColor.red(); int g = theColor.green(); int b = theColor.blue(); diff --git a/src/HYDROData/HYDROData_StricklerTable.h b/src/HYDROData/HYDROData_StricklerTable.h index 62323e32..34d2b304 100644 --- a/src/HYDROData/HYDROData_StricklerTable.h +++ b/src/HYDROData/HYDROData_StricklerTable.h @@ -72,10 +72,6 @@ public: HYDRODATA_EXPORT QColor GetColor( const QString& theType ) const; HYDRODATA_EXPORT void SetColor( const QString& theType, const QColor& theColor ) const; -private: - TCollection_ExtendedString toExtString( const QString& ) const; - QString toQString( const TCollection_ExtendedString& ) const; - private: Handle(TDataStd_NamedData) Map() const; }; diff --git a/src/HYDROData/HYDROData_Tool.cxx b/src/HYDROData/HYDROData_Tool.cxx index c30bff5e..48b9340f 100644 --- a/src/HYDROData/HYDROData_Tool.cxx +++ b/src/HYDROData/HYDROData_Tool.cxx @@ -284,4 +284,23 @@ TopoDS_Shape HYDROData_Tool::getFirstShapeFromGroup( const HYDROData_SequenceOfO aResShape = aGroupShapes.First(); return aResShape; -} \ No newline at end of file +} + +TCollection_ExtendedString HYDROData_Tool::toExtString( const QString& theStr ) +{ + TCollection_ExtendedString aRes; + if( !theStr.isEmpty() ) + { + Standard_ExtString extStr = new Standard_ExtCharacter[ ( theStr.length() + 1 ) * 2 ]; + memcpy( (void*)extStr, theStr.unicode(), theStr.length() * 2 ); + ((short*)extStr)[theStr.length()] = '\0'; + aRes = TCollection_ExtendedString( extStr ); + delete [] extStr; + } + return aRes; +} + +QString HYDROData_Tool::toQString( const TCollection_ExtendedString& theStr ) +{ + return QString( (QChar*)theStr.ToExtString(), theStr.Length() ); +} diff --git a/src/HYDROData/HYDROData_Tool.h b/src/HYDROData/HYDROData_Tool.h index 4bb77866..93de80d5 100644 --- a/src/HYDROData/HYDROData_Tool.h +++ b/src/HYDROData/HYDROData_Tool.h @@ -116,6 +116,9 @@ public: */ static TopoDS_Shape getFirstShapeFromGroup( const HYDROData_SequenceOfObjects& theGroups, const int theGroupId ); + + static TCollection_ExtendedString toExtString( const QString& ); + static QString toQString( const TCollection_ExtendedString& ); }; inline bool ValuesEquals( const double& theFirst, const double& theSecond )