From 3e865a760f79a9b3085e653fc7ea09e2d1b6f570 Mon Sep 17 00:00:00 2001 From: asl Date: Mon, 12 Oct 2015 16:42:34 +0300 Subject: [PATCH] new attributes in the Strickler table --- src/HYDROData/HYDROData_LandCoverMap.cxx | 89 +++++ src/HYDROData/HYDROData_LandCoverMap.h | 1 + src/HYDROData/HYDROData_StricklerTable.cxx | 324 +++++++++++------- src/HYDROData/HYDROData_StricklerTable.h | 16 +- .../resources/def_strickler_table.txt | 17 +- 5 files changed, 322 insertions(+), 125 deletions(-) diff --git a/src/HYDROData/HYDROData_LandCoverMap.cxx b/src/HYDROData/HYDROData_LandCoverMap.cxx index 7d8ee63f..55783a21 100644 --- a/src/HYDROData/HYDROData_LandCoverMap.cxx +++ b/src/HYDROData/HYDROData_LandCoverMap.cxx @@ -18,8 +18,33 @@ #include "HYDROData_LandCoverMap.h" #include +#include +#include #include +IMPLEMENT_STANDARD_HANDLE(HYDROData_LandCoverMap, HYDROData_Entity) +IMPLEMENT_STANDARD_RTTIEXT(HYDROData_LandCoverMap, HYDROData_Entity) + +HYDROData_LandCoverMap::HYDROData_LandCoverMap() +{ +} + +HYDROData_LandCoverMap::~HYDROData_LandCoverMap() +{ +} + +int HYDROData_LandCoverMap::GetNbFaces() const +{ + //TODO + return 0; +} + +TopoDS_Face HYDROData_LandCoverMap::GetFace( int theIndex ) const +{ + //TODO + return TopoDS_Face(); +} + QString HYDROData_LandCoverMap::GetStricklerType( int theIndex ) const { Handle(TDataStd_ExtStringArray) aTypesArray; @@ -41,3 +66,67 @@ void HYDROData_LandCoverMap::SetStricklerType( int theIndex, const QString& theT aTypesArray->SetValue( theIndex, aType.c_str() ); } +bool HYDROData_LandCoverMap::ImportQGIS( const QString& theFileName ) +{ + //TODO + return false; +} + +bool HYDROData_LandCoverMap::ExportQGIS( const QString& theFileName ) const +{ + //TODO + return false; +} + +bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName ) const +{ + //TODO + return false; +} + +bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )&, const QString& theType ) +{ + //TODO + return false; +} + +bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Polyline )&, const QString& theType ) +{ + //TODO + return false; +} + +bool HYDROData_LandCoverMap::Add( const TopoDS_Face&, const QString& theType ) +{ + //TODO + return false; +} + +bool HYDROData_LandCoverMap::Remove( int theIndex ) +{ + //TODO + return false; +} + +bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_Polyline )& ) +{ + //TODO + return false; +} + +bool HYDROData_LandCoverMap::Merge( const QList&, const QString& theType ) +{ + //TODO + return false; +} + +TopoDS_Shell HYDROData_LandCoverMap::GetShape() const +{ + //TODO + return TopoDS_Shell(); +} + +void HYDROData_LandCoverMap::SetShape( const TopoDS_Shell& ) +{ + //TODO +} diff --git a/src/HYDROData/HYDROData_LandCoverMap.h b/src/HYDROData/HYDROData_LandCoverMap.h index aa6e6a92..f479c428 100644 --- a/src/HYDROData/HYDROData_LandCoverMap.h +++ b/src/HYDROData/HYDROData_LandCoverMap.h @@ -64,6 +64,7 @@ protected: protected: TopoDS_Shell GetShape() const; void SetShape( const TopoDS_Shell& ); + bool Add( const TopoDS_Face&, const QString& theType ); public: DEFINE_STANDARD_RTTI( HYDROData_LandCoverMap ); diff --git a/src/HYDROData/HYDROData_StricklerTable.cxx b/src/HYDROData/HYDROData_StricklerTable.cxx index d3ef0b86..7a165907 100644 --- a/src/HYDROData/HYDROData_StricklerTable.cxx +++ b/src/HYDROData/HYDROData_StricklerTable.cxx @@ -21,18 +21,27 @@ #include #include +#include +#include #include +#include +#include #include #include -#include -#include +#include +#include +#include +#include +#include IMPLEMENT_STANDARD_HANDLE( HYDROData_StricklerTable, HYDROData_Entity ) - IMPLEMENT_STANDARD_RTTIEXT( HYDROData_StricklerTable, HYDROData_Entity ) +IMPLEMENT_STANDARD_RTTIEXT( HYDROData_StricklerTable, HYDROData_Entity ) - HYDROData_StricklerTable::HYDROData_StricklerTable() +const char ATTR_SUFFIX[] = " attr_value"; + +HYDROData_StricklerTable::HYDROData_StricklerTable() { } @@ -42,93 +51,101 @@ HYDROData_StricklerTable::~HYDROData_StricklerTable() const ObjectKind HYDROData_StricklerTable::GetKind() const { - return KIND_STRICKLER_TABLE; + return KIND_STRICKLER_TABLE; } bool HYDROData_StricklerTable::Import( const TCollection_AsciiString& theFileName ) { - std::ifstream aStream( theFileName.ToCString(), std::ios::in | std::ios::binary ); - if ( !aStream ) - return false; - - bool aRes = true; - - const int aBufLen = 512; - char aBuf[aBufLen]; - while( !aStream.eof() && aRes ) + Handle(TDataStd_NamedData) aMap = Map(); + if( aMap.IsNull() ) + return false; + + QFile aFile( theFileName.ToCString() ); + if( !aFile.open( QFile::ReadOnly | QFile::Text ) ) + return false; + + aMap->ChangeReals( TDataStd_DataMapOfStringReal() ); + aMap->ChangeStrings( TDataStd_DataMapOfStringString() ); + aMap->ChangeIntegers( TColStd_DataMapOfStringInteger() ); + + QTextStream aStream( &aFile ); + int i=0; + while( !aStream.atEnd() ) + { + QString aLine = aStream.readLine(); + if( i==0 ) + { + SetAttrName( aLine ); + } + else { - aStream.getline( aBuf, aBufLen ); - TCollection_ExtendedString aStr( aBuf, Standard_True ); - - Standard_Integer aPos = aStr.SearchFromEnd( " " ); - if ( aPos < 0 ) - aRes = false; - else - { - TCollection_ExtendedString aValueStr = aStr.Split( aPos ); - while ( aStr.Length() > 0 && ( aStr.Value( aStr.Length() ) == ' ' || aStr.Value( aStr.Length() ) == '\t' ) ) - aStr.Remove( aStr.Length() ); - while ( aStr.Length() > 0 && ( aStr.Value( 1 ) == ' ' || aStr.Value( 1 ) == '\t' ) || aStr.Value( 1 ) < 0 ) - aStr.Remove( 1 ); - - if ( aStr.Length() > 0 && aStr.Value( aStr.Length() ) == '\"' ) - aStr.Remove( aStr.Length() ); - if ( aStr.Length() > 0 && aStr.Value( 1 ) == '\"' ) - aStr.Remove( 1 ); - - if ( aValueStr.IsEmpty() || aStr.IsEmpty() ) - aRes = false; - else - { - Standard_Real aValue = TCollection_AsciiString( aValueStr ).RealValue(); - Set( QString( (QChar*)aStr.ToExtString(), aStr.Length() ), aValue ); - } - } + int p1 = aLine.indexOf( '"' ); + int p2 = aLine.indexOf( '"', p1+1 ); + QString aType = aLine.mid( p1+1, p2-p1-1 ); + QStringList aParts = aLine.mid( p2+1 ).split( ' ', QString::SkipEmptyParts ); + double aCoeff = aParts[0].toDouble(); + int aColorInt = aParts[1].toInt( 0, 16 ); + QString anAttrValue = aParts.size()>2 ? aParts[2] : QString(); + + TCollection_ExtendedString aTypeExt = toExtString( aType ); + aMap->SetReal( aTypeExt, aCoeff ); + aMap->SetInteger( aTypeExt, aColorInt ); + aMap->SetString( aTypeExt, toExtString( anAttrValue ) ); } + i++; + } + aFile.close(); - aStream.close(); - return aRes; + return true; } bool HYDROData_StricklerTable::Export( const TCollection_AsciiString& theFileName ) { - Handle(TDataStd_NamedData) aMap = Map(); - if ( aMap.IsNull() ) - return false; - - std::ofstream aStream( theFileName.ToCString(), std::ios::out | std::ios::binary ); - if ( !aStream ) - return false; - - bool aRes = true; - for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More() && aRes; it.Next() ) - { - TCollection_ExtendedString aLine = TCollection_ExtendedString( '\"' ) + it.Key() + TCollection_ExtendedString( '\"' ) + - TCollection_ExtendedString( ' ' ) + TCollection_ExtendedString( it.Value() ); - Standard_PCharacter aBuf = (Standard_PCharacter)malloc( aLine.LengthOfCString() + 1 ); - aStream.write( aBuf, aLine.ToUTF8CString( aBuf ) ); - aStream.write( "\r\n", 2 ); - free( aBuf ); - } + Handle(TDataStd_NamedData) aMap = Map(); + if( aMap.IsNull() ) + return false; + + std::ofstream aStream( theFileName.ToCString(), std::ios::out | std::ios::binary ); + if( !aStream ) + return false; + + aStream << GetAttrName().toStdString() << "\r\n"; + + bool aRes = true; + for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More() && aRes; it.Next() ) + { + TCollection_ExtendedString aType = it.Key(); + TCollection_ExtendedString aLine = TCollection_ExtendedString( '\"' ) + aType + TCollection_ExtendedString( '\"' ) + + TCollection_ExtendedString( ' ' ) + TCollection_ExtendedString( it.Value() ); + Standard_PCharacter aBuf = (Standard_PCharacter)malloc( aLine.LengthOfCString() + 1 ); + aStream.write( aBuf, aLine.ToUTF8CString( aBuf ) ); + + QString aColor = QString::number( aMap->GetInteger( aType ), 16 ); + QString anAttrValue = toQString( aMap->GetString( aType ) ); + + aStream << " " << aColor.toStdString() << " " << anAttrValue.toStdString(); + aStream.write( "\r\n", 2 ); + free( aBuf ); + } - aStream.close(); - return aRes; + aStream.close(); + return aRes; } double HYDROData_StricklerTable::Get( const QString& theType, double theDefault ) const { - Handle( TDataStd_NamedData ) aMap = Map(); - TCollection_ExtendedString aType = toExtString( theType ); - if( aMap->HasReal( aType ) ) - return aMap->GetReal( aType ); - else - return theDefault; + Handle( TDataStd_NamedData ) aMap = Map(); + TCollection_ExtendedString aType = toExtString( theType ); + if( aMap->HasReal( aType ) ) + return aMap->GetReal( aType ); + else + return theDefault; } void HYDROData_StricklerTable::Set( const QString& theType, double theCoefficient ) { - Handle(TDataStd_NamedData) aMap = Map(); - aMap->SetReal( toExtString( theType ), theCoefficient ); + Handle(TDataStd_NamedData) aMap = Map(); + aMap->SetReal( toExtString( theType ), theCoefficient ); } void HYDROData_StricklerTable::GetCoefficientRange( double& theMin, double& theMax ) const @@ -138,12 +155,15 @@ void HYDROData_StricklerTable::GetCoefficientRange( double& theMin, double& theM Handle(TDataStd_NamedData) aMap = Map(); Standard_Boolean isFirst = Standard_True; - for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() ) { + for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() ) + { Standard_Real aValue = it.Value(); - if ( theMin == 0 || aValue < theMin ) { + if ( theMin == 0 || aValue < theMin ) + { theMin = aValue; } - if ( theMax == 0 || aValue > theMax ) { + if ( theMax == 0 || aValue > theMax ) + { theMax = aValue; } } @@ -151,14 +171,14 @@ void HYDROData_StricklerTable::GetCoefficientRange( double& theMin, double& theM QStringList HYDROData_StricklerTable::GetTypes() const { - QStringList aSeq; - Handle(TDataStd_NamedData) aMap = Map(); - if ( !aMap.IsNull() ) - { - for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() ) - aSeq.append( toQString( it.Key() ) ); - } - return aSeq; + QStringList aSeq; + Handle(TDataStd_NamedData) aMap = Map(); + if( !aMap.IsNull() ) + { + for( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() ) + aSeq.append( toQString( it.Key() ) ); + } + return aSeq; } bool HYDROData_StricklerTable::HasType( const QString& theType ) const @@ -171,58 +191,132 @@ bool HYDROData_StricklerTable::HasType( const QString& theType ) const void HYDROData_StricklerTable::Clear() { - Handle(TDataStd_NamedData) aMap = Map(); - if ( !aMap.IsNull() ) - aMap->ChangeReals( TDataStd_DataMapOfStringReal() ); + Handle(TDataStd_NamedData) aMap = Map(); + if( !aMap.IsNull() ) + aMap->ChangeReals( TDataStd_DataMapOfStringReal() ); } QStringList HYDROData_StricklerTable::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const { - QStringList aResList = dumpObjectCreation( theTreatedObjects ); - QString aPyName = GetObjPyName(); + QStringList aResList = dumpObjectCreation( theTreatedObjects ); + QString aPyName = GetObjPyName(); - aResList << QString( "" ); - Handle(TDataStd_NamedData) aMap = Map(); - if ( !aMap.IsNull() ) + aResList << QString( "" ); + Handle(TDataStd_NamedData) aMap = Map(); + if( !aMap.IsNull() ) + { + for( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() ) { - for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() ) - { - TCollection_ExtendedString aType = it.Key(); - Standard_Real aValue = it.Value(); - aResList << QString( "%1.Set( \"%2\", %3 );" ).arg( aPyName ).arg( QString( (QChar*)aType.ToExtString(), aType.Length() ) ).arg( aValue ); - } + TCollection_ExtendedString aType = it.Key(); + Standard_Real aValue = it.Value(); + aResList << QString( "%1.Set( \"%2\", %3 );" ).arg( aPyName ).arg( QString( (QChar*)aType.ToExtString(), aType.Length() ) ).arg( aValue ); } - aResList << QString( "" ); - aResList << QString( "%1.Update();" ).arg( aPyName ); + } + aResList << QString( "" ); + aResList << QString( "%1.Update();" ).arg( aPyName ); - return aResList; + return aResList; } Handle(TDataStd_NamedData) HYDROData_StricklerTable::Map() const { - TDF_Label aLabel = myLab.FindChild( DataTag_Table ); - Handle( TDataStd_NamedData ) aMap; - if( !aLabel.FindAttribute( TDataStd_NamedData::GetID(), aMap ) ) - aMap = TDataStd_NamedData::Set( aLabel ); - return aMap; + TDF_Label aLabel = myLab.FindChild( DataTag_Table ); + Handle( TDataStd_NamedData ) aMap; + if( !aLabel.FindAttribute( TDataStd_NamedData::GetID(), aMap ) ) + aMap = TDataStd_NamedData::Set( aLabel ); + 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'; + 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; + if( myLab.FindChild( DataTag_AttrName ).FindAttribute(TDataStd_AsciiString::GetID(), aName)) { + TCollection_AsciiString aStr(aName->Get()); + return QString(aStr.ToCString()); + } + return QString();} + +void HYDROData_StricklerTable::SetAttrName( const QString& theAttrName ) const +{ + TDataStd_AsciiString::Set( myLab.FindChild( DataTag_AttrName ), toExtString( theAttrName ) ); +} + +QString HYDROData_StricklerTable::GetAttrValue( const QString& theType ) const +{ + Handle( TDataStd_NamedData ) aMap = Map(); + TCollection_ExtendedString aType = toExtString( theType ) + ATTR_SUFFIX; + if( aMap->HasString( aType ) ) + return toQString( aMap->GetString( aType ) ); + else + return ""; +} - aRes = TCollection_ExtendedString( extStr ); - delete [] extStr; +void HYDROData_StricklerTable::SetAttrValue( const QString& theType, const QString& theAttrValue ) const +{ + Handle( TDataStd_NamedData ) aMap = Map(); + TCollection_ExtendedString aType = toExtString( theType ) + ATTR_SUFFIX; + aMap->SetString( aType, toExtString( theAttrValue ) ); +} + +QString HYDROData_StricklerTable::GetType( const QString& theAttrValue ) const +{ + Handle( TDataStd_NamedData ) aMap = Map(); + TCollection_ExtendedString anAttrValue = toExtString( theAttrValue ); + int l = strlen( ATTR_SUFFIX ); + for( TDataStd_DataMapIteratorOfDataMapOfStringString it( aMap->GetStringsContainer() ); it.More(); it.Next() ) + { + if( it.Value() == anAttrValue ) + { + QString aType = toQString( it.Key() ); + aType.remove( aType.length() - l, l ); + return aType; } - return aRes; + } + return ""; } -QString HYDROData_StricklerTable::toQString( const TCollection_ExtendedString& theStr ) const +QColor HYDROData_StricklerTable::GetColor( const QString& theType ) const { - return QString( (QChar*)theStr.ToExtString(), theStr.Length() ); + Handle( TDataStd_NamedData ) aMap = Map(); + TCollection_ExtendedString aType = toExtString( theType ); + if( aMap->HasInteger( aType ) ) + { + int aColorInt = aMap->GetInteger( aType ); + int b = aColorInt % 256; + int g = (aColorInt>>8) % 256; + int r = (aColorInt>>16) % 256; + return QColor( r, g, b ); + } + else + return QColor(); +} + +void HYDROData_StricklerTable::SetColor( const QString& theType, const QColor& theColor ) const +{ + Handle( TDataStd_NamedData ) aMap = Map(); + TCollection_ExtendedString aType = toExtString( theType ); + int r = theColor.red(); + int g = theColor.green(); + int b = theColor.blue(); + int aColorInt = ( r<<16 ) + ( g<<8 ) + b; + aMap->SetInteger( aType, aColorInt ); } diff --git a/src/HYDROData/HYDROData_StricklerTable.h b/src/HYDROData/HYDROData_StricklerTable.h index 76a7241e..5cf2e7f5 100644 --- a/src/HYDROData/HYDROData_StricklerTable.h +++ b/src/HYDROData/HYDROData_StricklerTable.h @@ -35,6 +35,7 @@ protected: enum DataTag { DataTag_Table = HYDROData_Entity::DataTag_First + 100, ///< first tag, to reserve + DataTag_AttrName, }; HYDRODATA_EXPORT HYDROData_StricklerTable(); @@ -60,9 +61,20 @@ public: HYDRODATA_EXPORT virtual QStringList DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const; + HYDRODATA_EXPORT QString GetAttrName() const; + HYDRODATA_EXPORT void SetAttrName( const QString& ) const; + + HYDRODATA_EXPORT QString GetAttrValue( const QString& theType ) const; + HYDRODATA_EXPORT void SetAttrValue( const QString& theType, const QString& theAttrValue ) const; + + HYDRODATA_EXPORT QString GetType( const QString& theAttrValue ) const; + + 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; + TCollection_ExtendedString toExtString( const QString& ) const; + QString toQString( const TCollection_ExtendedString& ) const; private: Handle(TDataStd_NamedData) Map() const; diff --git a/src/HYDROGUI/resources/def_strickler_table.txt b/src/HYDROGUI/resources/def_strickler_table.txt index 39ee171b..29c9c231 100644 --- a/src/HYDROGUI/resources/def_strickler_table.txt +++ b/src/HYDROGUI/resources/def_strickler_table.txt @@ -1,8 +1,9 @@ -"Zones de champs, prairies, sans cultures" 20.0 -"Zones de champs cultivé à végétation basse" 17.5 -"Zones de champs cultivé à végétation haute" 12.5 -"Zones d'arbustes, de sous-bois" 10.0 -"Zones à faible urbanization (bourg)" 9.0 -"Zones à forte urbanization (agglomération)" 9.0 -"Canaux naturels" 35.0 -"Canaux artificiels en béton" 65.0 +CODE_06 +"Zones de champs, prairies, sans cultures" 20.0 FF0000 511 +"Zones de champs cultivé à végétation basse" 17.5 FFFF00 512 +"Zones de champs cultivé à végétation haute" 12.5 00FF00 +"Zones d'arbustes, de sous-bois" 10.0 00FFFF +"Zones à faible urbanization (bourg)" 9.0 0000FF +"Zones à forte urbanization (agglomération)" 9.0 123456 +"Canaux naturels" 35.0 FF00FF +"Canaux artificiels en béton" 65.0 888888 -- 2.39.2