1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include <HYDROData_LandCoverMap.h>
20 #include <HYDROData_Object.h>
21 #include <HYDROData_PolylineXY.h>
22 #include <HYDROData_Tool.h>
23 #include <HYDROData_ShapeFile.h>
24 #include <HYDROData_Document.h>
25 #include <HYDROData_StricklerTable.h>
27 #include <BOPAlgo_BOP.hxx>
28 #include <BOPAlgo_Builder.hxx>
29 #include <BOPAlgo_PaveFiller.hxx>
30 #include <BOPCol_ListOfShape.hxx>
31 #include <BRep_Builder.hxx>
32 #include <BRepAdaptor_Curve.hxx>
33 #include <BRepAlgoAPI_Fuse.hxx>
34 #include <BRepBuilderAPI_MakeFace.hxx>
35 #include <GCPnts_QuasiUniformDeflection.hxx>
37 #include <TopoDS_Compound.hxx>
38 #include <TopoDS_Edge.hxx>
39 #include <TopoDS_Face.hxx>
40 #include <TopoDS_Iterator.hxx>
41 #include <TopoDS_Shell.hxx>
42 #include <TopExp_Explorer.hxx>
43 #include <TopTools_ListIteratorOfListOfShape.hxx>
44 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
45 #include <BOPAlgo_PaveFiller.hxx>
46 #include <BRepTools.hxx>
47 #include <TopExp_Explorer.hxx>
48 #include <ShapeUpgrade_UnifySameDomain.hxx>
50 #include <TDataStd_Real.hxx>
51 #include <TopTools_IndexedMapOfShape.hxx>
52 #include <ShapeBuild_ReShape.hxx>
53 #include <ShapeFix_Shape.hxx>
58 #include <QTextStream>
60 const char TELEMAC_FORMAT = 'f';
61 const int TELEMAC_PRECISION = 3;
64 IMPLEMENT_STANDARD_HANDLE(HYDROData_LandCoverMap, HYDROData_Entity)
65 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_LandCoverMap, HYDROData_Entity)
69 @param theMap the land cover map to iterate through
71 HYDROData_LandCoverMap::Iterator::Iterator( const HYDROData_LandCoverMap& theMap )
76 HYDROData_LandCoverMap::Iterator::Iterator( const Handle( HYDROData_LandCoverMap )& theMap )
88 Initialize the iterator
89 @param theMap the land cover map to iterate through
91 void HYDROData_LandCoverMap::Iterator::Init( const HYDROData_LandCoverMap& theMap )
93 TopoDS_Shape aShape = theMap.GetShape();
97 myIterator = new TopoDS_Iterator( aShape );
99 theMap.myLab.FindChild( DataTag_Types ).FindAttribute( TDataStd_ExtStringArray::GetID(), myArray );
100 if( myArray.IsNull() )
103 myIndex = myArray->Lower();
109 HYDROData_LandCoverMap::Iterator::~Iterator()
115 Return the current 0-based index of the iterator
116 @return the current index
118 int HYDROData_LandCoverMap::Iterator::Index() const
120 if( myArray.IsNull() )
123 return myIndex - myArray->Lower();
127 Return if the iterator has more elements
128 @return if the iterator has more elements
130 bool HYDROData_LandCoverMap::Iterator::More() const
132 return !myArray.IsNull() && myIterator && myIterator->More();
136 Move iterator to the next element
138 void HYDROData_LandCoverMap::Iterator::Next()
148 Get the current land cover (face)
149 @return the land cover's face
151 TopoDS_Face HYDROData_LandCoverMap::Iterator::Face() const
154 return TopoDS::Face( myIterator->Value() );
156 return TopoDS_Face();
160 Get the current land cover's Strickler type
161 @return the land cover's Strickler type
163 QString HYDROData_LandCoverMap::Iterator::StricklerType() const
165 if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
168 return HYDROData_Tool::toQString( myArray->Value( myIndex ) );
172 Set the Strickler type for the current land cover
173 @param theType the Strickler type
175 void HYDROData_LandCoverMap::Iterator::SetStricklerType( const QString& theType )
177 if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
180 myArray->SetValue( myIndex, HYDROData_Tool::toExtString( theType ) );
186 HYDROData_LandCoverMap::HYDROData_LandCoverMap()
187 : HYDROData_Entity( Geom_No )
194 HYDROData_LandCoverMap::~HYDROData_LandCoverMap()
200 @return object's kind
202 const ObjectKind HYDROData_LandCoverMap::GetKind() const
204 return KIND_LAND_COVER_MAP;
207 int HYDROData_LandCoverMap::GetLCCount() const
209 Iterator anIt( *this );
211 for( ; anIt.More(); anIt.Next() )
216 bool HYDROData_LandCoverMap::IsEmpty() const
218 Iterator anIt( *this );
226 Load attributes from DBF File
229 HYDROData_LandCoverMap::DBFStatus HYDROData_LandCoverMap::ImportDBF( const QString& theDBFFileName,
230 const QString& theFieldName,
231 const QStringList& theDBFValues,
232 const QStringList& theStricklerTypes,
233 const QList<int>& theIndices )
235 if (theDBFValues.size() != theStricklerTypes.size())
236 return DBFStatus_DIFF_SIZE_ERROR;
237 HYDROData_ShapeFile aDBFImporter;
238 if (!aDBFImporter.DBF_OpenDBF(theDBFFileName))
239 return DBFStatus_OPEN_FILE_ERROR; //cant open file
241 QStringList FieldList = aDBFImporter.DBF_GetFieldList();
242 int FieldNameIndex = FieldList.indexOf(theFieldName);
243 if (FieldNameIndex == -1)
244 return DBFStatus_NO_SUCH_FIELD_ERROR; //no such field
246 std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
247 aDBFImporter.DBF_GetAttributeList(FieldNameIndex, theAttrV );
250 Iterator anIt( *this );
251 for( ; anIt.More(); anIt.Next() )
253 int CurIndex = anIt.Index();
254 int anIndex = CurIndex;
255 if( !theIndices.isEmpty() )
256 anIndex = theIndices[CurIndex];
258 HYDROData_ShapeFile::DBF_AttrValue AValue = theAttrV[anIndex];
259 int StricklerTypesInd = theDBFValues.indexOf( QString( AValue.myStrVal ) );
260 if ( StricklerTypesInd != -1)
261 anIt.SetStricklerType( theStricklerTypes[StricklerTypesInd] );
268 return DBFStatus_NO_DBFVALUES_CORRESPONDENCE_WARNING;
272 Export attributes to DBF File
275 void HYDROData_LandCoverMap::ExportDBF( const QString& theDBFFileName,
276 const QString& theFieldName,
277 const QStringList& theDBFValues,
278 const QStringList& theStricklerTypes) const
280 if (theDBFValues.size() != theStricklerTypes.size())
282 HYDROData_ShapeFile anExporter;
283 std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
284 Iterator anIt( *this );
285 for( ; anIt.More(); anIt.Next() )
287 QString CurST = anIt.StricklerType();
288 HYDROData_ShapeFile::DBF_AttrValue aCurAttrV;
289 aCurAttrV.myIsNull = false;
290 int StricklerTypesInd = theStricklerTypes.indexOf(CurST);
291 if (StricklerTypesInd != -1)
293 aCurAttrV.myStrVal = theDBFValues[StricklerTypesInd];
294 aCurAttrV.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String;
295 theAttrV.push_back(aCurAttrV);
298 aCurAttrV.myIsNull = true;
300 //use actual str value; not the raw value
301 anExporter.DBF_WriteFieldAndValues(theDBFFileName, theFieldName, HYDROData_ShapeFile::DBF_FieldType_String, theAttrV, false);
305 int HashCode( const gp_Pnt& thePoint, const Standard_Integer theUpper )
307 int aHashX = HashCode( thePoint.X(), theUpper );
308 int aHashY = HashCode( thePoint.Y(), theUpper );
309 return (aHashX^aHashY)%theUpper;
312 bool operator == ( const gp_Pnt& thePoint1, const gp_Pnt& thePoint2 )
314 return thePoint1.IsEqual( thePoint2, Precision::Confusion() );
317 bool EdgeDiscretization( const TopoDS_Edge& theEdge,
318 Standard_Real theDeflection,
319 NCollection_IndexedMap<gp_Pnt>& theVerticesMap,
320 QList<int>& theVerticesIds )
322 BRepAdaptor_Curve aCurve( theEdge );
323 GCPnts_QuasiUniformDeflection aDiscrete( aCurve, theDeflection );
324 if( !aDiscrete.IsDone() )
327 int n = aDiscrete.NbPoints();
328 for( int i=1; i<=n; i++ )
330 gp_Pnt aPnt = aDiscrete.Value( i );
332 if( theVerticesMap.Contains( aPnt ) )
333 anId = theVerticesMap.FindIndex( aPnt );
336 anId = theVerticesMap.Size();
337 theVerticesMap.Add( aPnt );
339 theVerticesIds.append( anId );
345 Export the land cover map for the solver (Telemac)
346 @param theFileName the name of file
347 @return if the export is successful
349 bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName,
350 double theDeflection,
351 const Handle(HYDROData_StricklerTable)& theTable ) const
353 TopoDS_Shape aLandCoverMapShape = GetShape();
354 TopTools_ListOfShape aListOfFaces;
355 Iterator anIt( *this );
356 QMap<Handle(TopoDS_TShape), QString> aTypesMap;
357 for( ; anIt.More(); anIt.Next() )
359 aListOfFaces.Append( anIt.Face() );
360 aTypesMap.insert( anIt.Face().TShape(), anIt.StricklerType() );
363 TopoDS_Shape aShape = MergeFaces( aListOfFaces, false );
365 NCollection_IndexedMap<gp_Pnt> aVerticesMap;
366 NCollection_IndexedDataMap< TopoDS_Edge, QList<int> > anEdgesMap;
367 typedef QPair< QString, QList<int> > FaceData;
368 NCollection_IndexedDataMap< TopoDS_Face, FaceData > aFacesMap;
370 // add into the map all edges existing in the shell
371 TopExp_Explorer anExp1( aShape, TopAbs_EDGE );
372 for( ; anExp1.More(); anExp1.Next() )
374 TopoDS_Edge anEdge = TopoDS::Edge( anExp1.Current() );
375 QList<int> aVerticesIdsList;
376 if( EdgeDiscretization( anEdge, theDeflection, aVerticesMap, aVerticesIdsList ) )
377 anEdgesMap.Add( anEdge, aVerticesIdsList );
380 // add into the map all faces existing in the shell and correspondence between face and edges ids
381 TopExp_Explorer anExp2( aShape, TopAbs_FACE );
382 for( ; anExp2.More(); anExp2.Next() )
384 TopoDS_Face aFace = TopoDS::Face( anExp2.Current() );
385 TopExp_Explorer anExp3( aFace, TopAbs_EDGE );
386 QList<int> anEdgesIdsList;
387 for( ; anExp3.More(); anExp3.Next() )
389 TopoDS_Edge anEdge = TopoDS::Edge( anExp3.Current() );
390 int anEdgeId = anEdgesMap.FindIndex( anEdge );
391 anEdgesIdsList.append( anEdgeId );
395 aData.first = aTypesMap[aFace.TShape()];
396 aData.second = anEdgesIdsList;
397 aFacesMap.Add( aFace, aData );
400 QFile aFile( theFileName );
401 if( !aFile.open( QFile::WriteOnly | QFile::Text ) )
404 QTextStream aStream( &aFile );
405 aStream << "# nodes\n";
406 NCollection_IndexedMap<gp_Pnt>::Iterator anIt1( aVerticesMap );
407 for( ; anIt1.More(); anIt1.Next() )
409 gp_Pnt aPnt = anIt1.Value();
410 aStream << QString::number( aPnt.X(), TELEMAC_FORMAT, TELEMAC_PRECISION );
412 aStream << QString::number( aPnt.Y(), TELEMAC_FORMAT, TELEMAC_PRECISION );
414 aStream << QString::number( aPnt.Z(), TELEMAC_FORMAT, TELEMAC_PRECISION );
419 aStream << "# edges\n";
420 NCollection_IndexedDataMap< TopoDS_Edge, QList<int> >::Iterator anIt2( anEdgesMap );
421 for( ; anIt2.More(); anIt2.Next() )
423 QList<int> aVerticesIds = anIt2.Value();
424 foreach( int anId, aVerticesIds )
425 aStream << anId << " ";
430 aStream << "# faces\n";
431 NCollection_IndexedDataMap< TopoDS_Face, FaceData >::Iterator anIt3( aFacesMap );
432 for( ; anIt3.More(); anIt3.Next() )
434 QString aType = anIt3.Value().first;
435 double aCoeff = theTable->Get( aType, 0.0 );
436 QList<int> anEdgesIds = anIt3.Value().second;
437 //aStream << "\"" << aType << "\" ";
438 aStream << QString::number( aCoeff, TELEMAC_FORMAT, TELEMAC_PRECISION ) << " ";
439 foreach( int anId, anEdgesIds )
440 aStream << anId << " ";
450 Add a new object as land cover
451 @param theObject the object to add as land cover
452 @param theType the Strickler type for the new land cover
453 @return if the addition is successful
455 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )& theObject, const QString& theType )
457 if( theObject.IsNull() )
460 TopoDS_Shape aShape = theObject->GetTopShape();
461 if( aShape.ShapeType()!=TopAbs_FACE )
464 TopoDS_Face aFace = TopoDS::Face( aShape );
465 return LocalPartition( aFace, theType );
468 bool HYDROData_LandCoverMap::Add( const TopoDS_Wire& theWire, const QString& theType )
470 if( !theWire.Closed() )
473 TopoDS_Face aFace = BRepBuilderAPI_MakeFace( theWire, Standard_True ).Face();
474 return LocalPartition( aFace, theType );
478 Add a new polyline as land cover
479 @param thePolyline the polyline to add as land cover
480 @param theType the Strickler type for the new land cover
481 @return if the addition is successful
483 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_PolylineXY )& thePolyline, const QString& theType )
485 if( thePolyline.IsNull() )
488 TopoDS_Shape aShape = thePolyline->GetShape();
489 if( aShape.ShapeType()==TopAbs_WIRE )
490 return Add( TopoDS::Wire( aShape ), theType );
492 if( aShape.ShapeType()==TopAbs_COMPOUND )
494 TopExp_Explorer anExp( aShape, TopAbs_WIRE );
495 for( ; anExp.More(); anExp.Next() )
497 TopoDS_Wire aPart = TopoDS::Wire( anExp.Current() );
498 if( !Add( aPart, theType ) )
508 Remove the given face from land cover map
509 @param theFace the face to be removed
510 @return if the removing is successful
512 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
514 TopTools_ListOfShape aList;
515 aList.Append( theFace );
516 return Remove( aList );
520 Remove the given faces from land cover map
521 @param theFacesToRemove the face list to be removed
522 @return if the removing is successful
524 bool HYDROData_LandCoverMap::Remove( const TopTools_ListOfShape& theFacesToRemove )
526 HYDROData_MapOfFaceToStricklerType aFacesToRemove, aNewFaces;
527 TopTools_ListIteratorOfListOfShape aFIt( theFacesToRemove );
528 for( ; aFIt.More(); aFIt.Next() )
530 TopoDS_Shape aShape = aFIt.Value();
531 if( aShape.ShapeType()==TopAbs_FACE )
532 aFacesToRemove.Add( TopoDS::Face( aShape ), "" );
535 Iterator anIt( *this );
536 for( ; anIt.More(); anIt.Next() )
537 if( !aFacesToRemove.Contains( anIt.Face() ) )
538 aNewFaces.Add( anIt.Face(), anIt.StricklerType() );
540 if ( aNewFaces.IsEmpty() )
543 StoreLandCovers( aNewFaces );
548 Split the land cover map by the given polyline
549 @param thePolyline the tool polyline to split the land cover map
550 @return if the removing is successful
552 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_PolylineXY )& thePolyline )
554 if( thePolyline.IsNull() )
557 TopoDS_Shape aShape = thePolyline->GetShape();
558 return Split( aShape );
563 Split the land cover map by the given polyline
564 @param theShape the tool polyline to split the land cover map
565 @return if the removing is successful
567 bool HYDROData_LandCoverMap::Split( const TopoDS_Shape& theShape )
569 return LocalPartition( theShape, "" );
574 Merge the given faces in the land cover
575 @param theFaces the faces to merge in the land cover map
576 @param theType the Strickler type for the merged land cover
577 @return if the merge is successful
579 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
581 // 1. to fuse the faces into the new face
582 TopoDS_Shape aMergedFace = MergeFaces( theFaces, true );
583 if( !aMergedFace.IsNull() && aMergedFace.ShapeType()==TopAbs_FACE )
585 // 2. to remove the merged faces from the current map
588 // 3. to add the face into the map
589 return LocalPartition( TopoDS::Face( aMergedFace ), theType );
595 Merge the given faces into the shell/face
596 @param theFaces the faces to merge
597 @param IsToUnify if the common edges should be removed (fused)
598 @param theTolerance the operation's tolerance
599 @return result shape (face or shell)
601 TopoDS_Shape HYDROData_LandCoverMap::MergeFaces( const TopTools_ListOfShape& theFaces,
602 bool IsToUnify, double theTolerance )
605 TopTools_ListIteratorOfListOfShape anIt;
606 BOPCol_ListOfShape aLC;
607 anIt.Initialize(theFaces);
608 for( ; anIt.More(); anIt.Next() )
610 if (anIt.Value().ShapeType() != TopAbs_FACE)
611 return TopoDS_Shape();
612 aLC.Append( anIt.Value() );
615 BOPAlgo_PaveFiller aPF;
616 aPF.SetArguments( aLC );
617 aPF.SetRunParallel( Standard_False );
618 aPF.SetFuzzyValue( theTolerance );
621 anError = aPF.ErrorStatus();
623 return TopoDS_Shape();
625 BOPAlgo_Builder anAlgo;
626 anIt.Initialize( theFaces );
627 for( ; anIt.More(); anIt.Next() )
628 anAlgo.AddArgument( anIt.Value() );
630 anAlgo.PerformWithFiller( aPF );
631 anError = anAlgo.ErrorStatus();
633 return TopoDS_Shape();
635 const TopoDS_Shape& aMergedShape = anAlgo.Shape();
637 BRep_Builder aBuilder;
639 aBuilder.MakeShell( aShell );
640 aShell.Closed( Standard_False );
641 TopExp_Explorer anExplorer( aMergedShape, TopAbs_FACE );
642 for( ; anExplorer.More(); anExplorer.Next() )
644 const TopoDS_Face& aFace = TopoDS::Face(anExplorer.Current());
647 if( aFace.ShapeType() == TopAbs_FACE )
649 aBuilder.Add( aShell, aFace );
650 aShell.Closed( Standard_False );
654 TopoDS_Shape aResult;
657 ShapeUpgrade_UnifySameDomain aUSD;
658 aUSD.Initialize( aShell );
660 aResult = aUSD.Shape();
665 anExplorer.Init( aResult, TopAbs_FACE );
667 TopoDS_Face anOneFace;
668 for( ; anExplorer.More(); anExplorer.Next(), n++ )
669 anOneFace = TopoDS::Face( anExplorer.Current() );
678 Change Strickler type for the list of faces to the given one
679 @param theFaces the faces to change type
680 @param theType the Strickler type for the given land cover(s)
681 @return if the change type operation is successful
683 bool HYDROData_LandCoverMap::ChangeType( const TopTools_ListOfShape& theFaces, const QString& theType )
685 HYDROData_MapOfFaceToStricklerType aFacesToChangeType;
686 TopTools_ListIteratorOfListOfShape aFIt( theFaces );
687 for( ; aFIt.More(); aFIt.Next() )
689 TopoDS_Shape aShape = aFIt.Value();
690 if( aShape.ShapeType()==TopAbs_FACE )
691 aFacesToChangeType.Add( TopoDS::Face( aShape ), "" );
695 Iterator anIt( *this );
696 for( ; anIt.More(); anIt.Next() )
697 if( aFacesToChangeType.Contains( anIt.Face() ) )
699 anIt.SetStricklerType( theType );
702 if ( aNbChanges != theFaces.Extent() )
709 Get the shape of the land cover map
711 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
713 return HYDROData_Entity::GetShape( DataTag_Shape );
717 Get Strickler type of the given land cover
718 @param theLandCover the land cover to get Strickler type of
719 @return name of Strickler type
721 QString HYDROData_LandCoverMap::StricklerType( const TopoDS_Face& theLandCover ) const
725 Iterator anIt( *this );
726 for( ; anIt.More(); anIt.Next() )
727 if( anIt.Face().IsEqual( theLandCover) )
729 aType = anIt.StricklerType();
737 Set the shape of the land cover map
738 @param theShape the new shape for the land cover map
740 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
742 HYDROData_Entity::SetShape( DataTag_Shape, theShape );
746 Perform the local partition algorithm on the land cover
747 @param theNewShape the new shape to add into the land cover
748 @param theNewType the new Strickler type for the new land cover
749 @return if the local partition is successful
751 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
753 if( theNewShape.IsNull() )
756 BOPCol_ListOfShape aShapesList;
757 BOPAlgo_PaveFiller aPaveFiller;
758 HYDROData_MapOfFaceToStricklerType aNewFaces;
760 // add faces to shapes list
761 Iterator anIt( *this );
762 for( ; anIt.More(); anIt.Next() )
763 aShapesList.Append( anIt.Face() );
764 aShapesList.Append( theNewShape );
766 if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
768 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
769 StoreLandCovers( aNewFaces );
773 // prepare pave filler
774 aPaveFiller.SetArguments( aShapesList );
775 aPaveFiller.Perform();
776 Standard_Integer anError = aPaveFiller.ErrorStatus();
780 // add faces to builder
781 BOPAlgo_Builder aBuilder;
783 for( ; anIt.More(); anIt.Next() )
784 aBuilder.AddArgument( anIt.Face() );
785 aBuilder.AddArgument( theNewShape );
787 // perform the partition with the pave filler
788 aBuilder.PerformWithFiller( aPaveFiller );
789 anError = aBuilder.ErrorStatus();
793 //std::cout << "History:" << std::endl;
794 // analysis of the history
795 // a. to fill map of shapes which come from the new face
796 NCollection_IndexedMap<int> aShapesFromNewFace;
797 //std::cout << "from NEW " << theNewShape << ":" << theNewType << std::endl;
798 TopTools_ListOfShape aModified = aBuilder.Modified( theNewShape );
799 TopTools_ListIteratorOfListOfShape aMIt( aModified );
800 for( ; aMIt.More(); aMIt.Next() )
802 //std::cout << " " << aMIt.Value() << std::endl;
803 int aKey = (int)(uintptr_t)aMIt.Value().TShape().operator->();
804 aShapesFromNewFace.Add( aKey );
807 // b. to fill map of parts except parts from new face
809 for( ; anIt.More(); anIt.Next() )
811 QString aSType = anIt.StricklerType();
812 //std::cout << "from " << anIt.Face() << ": " << anIt.StricklerType() << std::endl;
813 TopTools_ListOfShape aModified = aBuilder.Modified( anIt.Face() );
814 if( aModified.Extent() == 0 )
815 aModified.Append( anIt.Face() );
817 TopTools_ListIteratorOfListOfShape aMIt( aModified );
818 for( ; aMIt.More(); aMIt.Next() )
820 TopoDS_Shape aShape = aMIt.Value();
821 bool isFace = aShape.ShapeType()==TopAbs_FACE;
822 int aKey = (int)(uintptr_t)aShape.TShape().operator->();
823 bool isAlsoFromNew = aShapesFromNewFace.Contains( aKey );
824 //std::cout << " " << aShape << " " << isAlsoFromNew << std::endl;
825 if( isFace && !isAlsoFromNew )
826 aNewFaces.Add( TopoDS::Face( aShape ), aSType );
830 // c. add the new shape if it is face with its type
831 if( theNewShape.ShapeType()==TopAbs_FACE )
832 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
834 // convert map of shape to type to compound and list of types
835 StoreLandCovers( aNewFaces );
840 Replace the set of land covers in the land cover map
841 @param theMap the map of shape (face) to Strickler type (string)
843 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
845 TopTools_ListOfShape aListOfFaces;
847 int n = theMap.Size();
849 Handle( TDataStd_ExtStringArray ) aTypes =
850 TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, n-1, Standard_True );
852 HYDROData_MapOfFaceToStricklerType::Iterator aNFIt( theMap );
853 for( int i=0; aNFIt.More(); aNFIt.Next(), i++ )
855 TopoDS_Face aFace = aNFIt.Key();
858 QString aType = aNFIt.Value();
859 aListOfFaces.Append(aFace);
860 aTypes->SetValue( i, HYDROData_Tool::toExtString( aType ) );
863 TopoDS_Shape aResult;
864 if( aListOfFaces.Extent() == 1 )
867 BRep_Builder aShellBuilder;
868 aShellBuilder.MakeShell( aShell );
869 aShell.Closed( Standard_False );
870 aShellBuilder.Add( aShell, aListOfFaces.First() );
873 else if( aListOfFaces.Extent() > 1 )
874 aResult = MergeFaces( aListOfFaces, false );
876 //remove internal edges
877 aResult = RemoveInternal(aResult);
882 Checks that object has 2D presentation. Reimlemented to retun true.
884 bool HYDROData_LandCoverMap::IsHas2dPrs() const
890 Find the land cover for the given point
891 @param thePoint the point laying in some land cover
892 @param theType the returned type
893 @return the found land cover's face
895 TopoDS_Face HYDROData_LandCoverMap::FindByPoint( const gp_Pnt2d& thePoint, QString& theType ) const
897 //TODO: some more optimal algorithm
898 Iterator anIt( *this );
899 for( ; anIt.More(); anIt.Next() )
900 if( HYDROData_Tool::ComputePointState( thePoint.XY(), anIt.Face() ) == TopAbs_IN )
902 theType = anIt.StricklerType();
907 return TopoDS_Face();
910 void Dump( const QString& theName, const QStringList& theList, QStringList& theLines )
912 theLines.append( QString( "%1 = QStringList()" ).arg( theName ) );
913 foreach( QString anItem, theList )
914 theLines.append( QString( "%1.append( u\"%2\" )" ).arg( theName ).arg( anItem ) );
919 @param theTreatedObjects the map of treated objects
921 QStringList HYDROData_LandCoverMap::DumpToPython( const QString& thePyScriptPath,
922 MapOfTreatedObjects& theTreatedObjects ) const
924 QStringList aResList = dumpObjectCreation( theTreatedObjects );
925 QString aName = GetObjPyName();
927 QString aShapeFileName = thePyScriptPath;
928 aShapeFileName.replace( ".py", ".shp" );
929 QString aDbfFileName = thePyScriptPath;
930 aDbfFileName.replace( ".py", ".dbf" );
932 //TODO: export shape file
934 QString anAttr = "CODE_06"; //TODO: some custom choice
935 QStringList anAttrValues, aTypes;
936 HYDROData_Document::Document( myLab )->CollectQGISValues( anAttr, anAttrValues, aTypes );
937 ExportDBF( aDbfFileName, anAttr, anAttrValues, aTypes );
939 //TODO: insert command to import shape file
941 Dump( "attr_values", anAttrValues, aResList );
942 Dump( "types", aTypes, aResList );
943 aResList << QString( "%1.ImportDBF( '%2', '%3', attr_values, types )" ).
944 arg( aName ).arg( aDbfFileName ).arg( anAttr );
949 TopoDS_Shape HYDROData_LandCoverMap::RemoveInternal(const TopoDS_Shape& InSh)
951 //Shape must be topologically correct
952 TopExp_Explorer anExp(InSh, TopAbs_EDGE);
953 TopTools_ListOfShape anEdgesToRemove;
955 for(; anExp.More(); anExp.Next() )
957 TopoDS_Edge CurEdge = TopoDS::Edge(anExp.Current());
958 if (CurEdge.Orientation() == TopAbs_INTERNAL)
959 anEdgesToRemove.Append(CurEdge);
962 Handle_ShapeBuild_ReShape aReshape = new ShapeBuild_ReShape();
963 TopoDS_Shape OutSh = aReshape->Apply(InSh);
964 TopTools_ListIteratorOfListOfShape aIt(anEdgesToRemove);
965 for (; aIt.More(); aIt.Next())
966 aReshape->Remove(aIt.Value());
967 OutSh = aReshape->Apply(InSh);
969 Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
976 void HYDROData_LandCoverMap::SetTransparency( double theTransparency )
978 Handle(TDataStd_Real) anAttr;
979 TDF_Label aLabel = myLab.FindChild( DataTag_Transparency );
980 if( !aLabel.FindAttribute( TDataStd_Real::GetID(), anAttr ) )
981 aLabel.AddAttribute( anAttr = new TDataStd_Real() );
982 anAttr->Set( theTransparency );
985 double HYDROData_LandCoverMap::GetTransparency() const
987 Handle(TDataStd_Real) anAttr;
988 TDF_Label aLabel = myLab.FindChild( DataTag_Transparency );
989 if( !aLabel.FindAttribute( TDataStd_Real::GetID(), anAttr ) )
992 return anAttr->Get();