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 <TopTools_SequenceOfShape.hxx>
46 #include <BOPAlgo_PaveFiller.hxx>
47 #include <BRepTools.hxx>
48 #include <TopExp_Explorer.hxx>
49 #include <ShapeUpgrade_UnifySameDomain.hxx>
51 #include <TDataStd_Real.hxx>
52 #include <TopTools_IndexedMapOfShape.hxx>
53 #include <ShapeBuild_ReShape.hxx>
54 #include <ShapeFix_Shape.hxx>
55 #include <BRepCheck_Shell.hxx>
56 #include <BRepCheck_ListOfStatus.hxx>
57 #include <TopTools_SequenceOfShape.hxx>
62 #include <QTextStream>
64 const char TELEMAC_FORMAT = 'f';
65 const int TELEMAC_PRECISION = 3;
68 IMPLEMENT_STANDARD_HANDLE(HYDROData_LandCoverMap, HYDROData_Entity)
69 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_LandCoverMap, HYDROData_Entity)
73 @param theMap the land cover map to iterate through
75 HYDROData_LandCoverMap::Explorer::Explorer( const HYDROData_LandCoverMap& theMap )
80 HYDROData_LandCoverMap::Explorer::Explorer( const Handle( HYDROData_LandCoverMap )& theMap )
92 Initialize the iterator
93 @param theMap the land cover map to iterate through
95 void HYDROData_LandCoverMap::Explorer::Init( const HYDROData_LandCoverMap& theMap )
97 TopoDS_Shape aShape = theMap.GetShape();
102 myExplorer = new TopExp_Explorer();
103 myExplorer->Init( aShape, TopAbs_FACE );
106 theMap.myLab.FindChild( DataTag_Types ).FindAttribute( TDataStd_ExtStringArray::GetID(), myArray );
107 if( myArray.IsNull() )
110 myIndex = myArray->Lower();
116 HYDROData_LandCoverMap::Explorer::~Explorer()
122 Return the current 0-based index of the iterator
123 @return the current index
125 int HYDROData_LandCoverMap::Explorer::Index() const
127 if( myArray.IsNull() )
130 return myIndex - myArray->Lower();
134 Return if the iterator has more elements
135 @return if the iterator has more elements
137 bool HYDROData_LandCoverMap::Explorer::More() const
139 return !myArray.IsNull() && myExplorer && myExplorer->More();
143 Move iterator to the next element
145 void HYDROData_LandCoverMap::Explorer::Next()
155 Get the current land cover (face)
156 @return the land cover's face
158 TopoDS_Face HYDROData_LandCoverMap::Explorer::Face() const
161 return TopoDS::Face( myExplorer->Current() );
163 return TopoDS_Face();
167 Get the current land cover's Strickler type
168 @return the land cover's Strickler type
170 QString HYDROData_LandCoverMap::Explorer::StricklerType() const
172 if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
175 return HYDROData_Tool::toQString( myArray->Value( myIndex ) );
179 Set the Strickler type for the current land cover
180 @param theType the Strickler type
182 void HYDROData_LandCoverMap::Explorer::SetStricklerType( const QString& theType )
184 if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
187 myArray->SetValue( myIndex, HYDROData_Tool::toExtString( theType ) );
193 HYDROData_LandCoverMap::HYDROData_LandCoverMap()
194 : HYDROData_Entity( Geom_No )
201 HYDROData_LandCoverMap::~HYDROData_LandCoverMap()
207 @return object's kind
209 const ObjectKind HYDROData_LandCoverMap::GetKind() const
211 return KIND_LAND_COVER_MAP;
214 int HYDROData_LandCoverMap::GetLCCount() const
216 Explorer anIt( *this );
218 for( ; anIt.More(); anIt.Next() )
223 bool HYDROData_LandCoverMap::IsEmpty() const
225 Explorer anIt( *this );
233 Load attributes from DBF File
236 HYDROData_LandCoverMap::DBFStatus HYDROData_LandCoverMap::ImportDBF( const QString& theDBFFileName,
237 const QString& theFieldName,
238 const QStringList& theDBFValues,
239 const QStringList& theStricklerTypes,
240 const QList<int>& theIndices )
242 if (theDBFValues.size() != theStricklerTypes.size())
243 return DBFStatus_DIFF_SIZE_ERROR;
244 HYDROData_ShapeFile aDBFImporter;
245 if (!aDBFImporter.DBF_OpenDBF(theDBFFileName))
246 return DBFStatus_OPEN_FILE_ERROR; //cant open file
248 QStringList FieldList = aDBFImporter.DBF_GetFieldList();
249 int FieldNameIndex = FieldList.indexOf(theFieldName);
250 if (FieldNameIndex == -1)
251 return DBFStatus_NO_SUCH_FIELD_ERROR; //no such field
253 std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
254 aDBFImporter.DBF_GetAttributeList(FieldNameIndex, theAttrV );
257 Explorer anIt( *this );
258 for( ; anIt.More(); anIt.Next() )
260 int CurIndex = anIt.Index();
261 int anIndex = CurIndex;
262 if( !theIndices.isEmpty() )
263 anIndex = theIndices[CurIndex];
265 HYDROData_ShapeFile::DBF_AttrValue AValue = theAttrV[anIndex];
266 int StricklerTypesInd = theDBFValues.indexOf( QString( AValue.myStrVal ) );
267 if ( StricklerTypesInd != -1)
268 anIt.SetStricklerType( theStricklerTypes[StricklerTypesInd] );
275 return DBFStatus_NO_DBFVALUES_CORRESPONDENCE_WARNING;
279 Export attributes to DBF File
282 void HYDROData_LandCoverMap::ExportDBF( const QString& theDBFFileName,
283 const QString& theFieldName,
284 const QStringList& theDBFValues,
285 const QStringList& theStricklerTypes) const
287 if (theDBFValues.size() != theStricklerTypes.size())
289 HYDROData_ShapeFile anExporter;
290 std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
291 Explorer anIt( *this );
292 for( ; anIt.More(); anIt.Next() )
294 QString CurST = anIt.StricklerType();
295 HYDROData_ShapeFile::DBF_AttrValue aCurAttrV;
296 aCurAttrV.myIsNull = false;
297 int StricklerTypesInd = theStricklerTypes.indexOf(CurST);
298 if (StricklerTypesInd != -1)
300 aCurAttrV.myStrVal = theDBFValues[StricklerTypesInd];
301 aCurAttrV.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String;
302 theAttrV.push_back(aCurAttrV);
305 aCurAttrV.myIsNull = true;
307 //use actual str value; not the raw value
308 anExporter.DBF_WriteFieldAndValues(theDBFFileName, theFieldName, HYDROData_ShapeFile::DBF_FieldType_String, theAttrV, false);
312 int HashCode( const gp_Pnt& thePoint, const Standard_Integer theUpper )
314 int aHashX = HashCode( thePoint.X(), theUpper );
315 int aHashY = HashCode( thePoint.Y(), theUpper );
316 return (aHashX^aHashY)%theUpper;
319 bool operator == ( const gp_Pnt& thePoint1, const gp_Pnt& thePoint2 )
321 return thePoint1.IsEqual( thePoint2, Precision::Confusion() );
324 bool EdgeDiscretization( const TopoDS_Edge& theEdge,
325 Standard_Real theDeflection,
326 NCollection_IndexedMap<gp_Pnt>& theVerticesMap,
327 QList<int>& theVerticesIds )
329 BRepAdaptor_Curve aCurve( theEdge );
330 GCPnts_QuasiUniformDeflection aDiscrete( aCurve, theDeflection );
331 if( !aDiscrete.IsDone() )
334 int n = aDiscrete.NbPoints();
335 for( int i=1; i<=n; i++ )
337 gp_Pnt aPnt = aDiscrete.Value( i );
339 if( theVerticesMap.Contains( aPnt ) )
340 anId = theVerticesMap.FindIndex( aPnt );
343 anId = theVerticesMap.Size();
344 theVerticesMap.Add( aPnt );
346 theVerticesIds.append( anId );
352 Export the land cover map for the solver (Telemac)
353 @param theFileName the name of file
354 @return if the export is successful
356 bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName,
357 double theDeflection,
358 const Handle(HYDROData_StricklerTable)& theTable ) const
360 TopoDS_Shape aLandCoverMapShape = GetShape();
361 TopTools_ListOfShape aListOfFaces;
362 Explorer anIt( *this );
363 QMap<Handle(TopoDS_TShape), QString> aTypesMap;
364 for( ; anIt.More(); anIt.Next() )
366 aListOfFaces.Append( anIt.Face() );
367 aTypesMap.insert( anIt.Face().TShape(), anIt.StricklerType() );
370 TopoDS_Shape aShape = MergeFaces( aListOfFaces, false );
372 NCollection_IndexedMap<gp_Pnt> aVerticesMap;
373 NCollection_IndexedDataMap< TopoDS_Edge, QList<int> > anEdgesMap;
374 typedef QPair< QString, QList<int> > FaceData;
375 NCollection_IndexedDataMap< TopoDS_Face, FaceData > aFacesMap;
377 // add into the map all edges existing in the shell
378 TopExp_Explorer anExp1( aShape, TopAbs_EDGE );
379 for( ; anExp1.More(); anExp1.Next() )
381 TopoDS_Edge anEdge = TopoDS::Edge( anExp1.Current() );
382 QList<int> aVerticesIdsList;
383 if( EdgeDiscretization( anEdge, theDeflection, aVerticesMap, aVerticesIdsList ) )
384 anEdgesMap.Add( anEdge, aVerticesIdsList );
387 // add into the map all faces existing in the shell and correspondence between face and edges ids
388 TopExp_Explorer anExp2( aShape, TopAbs_FACE );
389 for( ; anExp2.More(); anExp2.Next() )
391 TopoDS_Face aFace = TopoDS::Face( anExp2.Current() );
392 TopExp_Explorer anExp3( aFace, TopAbs_EDGE );
393 QList<int> anEdgesIdsList;
394 for( ; anExp3.More(); anExp3.Next() )
396 TopoDS_Edge anEdge = TopoDS::Edge( anExp3.Current() );
397 int anEdgeId = anEdgesMap.FindIndex( anEdge );
398 anEdgesIdsList.append( anEdgeId );
402 aData.first = aTypesMap[aFace.TShape()];
403 aData.second = anEdgesIdsList;
404 aFacesMap.Add( aFace, aData );
407 QFile aFile( theFileName );
408 if( !aFile.open( QFile::WriteOnly | QFile::Text ) )
411 QTextStream aStream( &aFile );
412 aStream << "# nodes\n";
413 NCollection_IndexedMap<gp_Pnt>::Iterator anIt1( aVerticesMap );
414 for( ; anIt1.More(); anIt1.Next() )
416 gp_Pnt aPnt = anIt1.Value();
417 aStream << QString::number( aPnt.X(), TELEMAC_FORMAT, TELEMAC_PRECISION );
419 aStream << QString::number( aPnt.Y(), TELEMAC_FORMAT, TELEMAC_PRECISION );
421 aStream << QString::number( aPnt.Z(), TELEMAC_FORMAT, TELEMAC_PRECISION );
426 aStream << "# edges\n";
427 NCollection_IndexedDataMap< TopoDS_Edge, QList<int> >::Iterator anIt2( anEdgesMap );
428 for( ; anIt2.More(); anIt2.Next() )
430 QList<int> aVerticesIds = anIt2.Value();
431 foreach( int anId, aVerticesIds )
432 aStream << anId << " ";
437 aStream << "# faces\n";
438 NCollection_IndexedDataMap< TopoDS_Face, FaceData >::Iterator anIt3( aFacesMap );
439 for( ; anIt3.More(); anIt3.Next() )
441 QString aType = anIt3.Value().first;
442 double aCoeff = theTable->Get( aType, 0.0 );
443 QList<int> anEdgesIds = anIt3.Value().second;
444 //aStream << "\"" << aType << "\" ";
445 aStream << QString::number( aCoeff, TELEMAC_FORMAT, TELEMAC_PRECISION ) << " ";
446 foreach( int anId, anEdgesIds )
447 aStream << anId << " ";
457 Add a new object as land cover
458 @param theObject the object to add as land cover
459 @param theType the Strickler type for the new land cover
460 @return if the addition is successful
462 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )& theObject, const QString& theType )
464 if( theObject.IsNull() )
467 TopoDS_Shape aShape = theObject->GetTopShape();
468 if( aShape.ShapeType()!=TopAbs_FACE )
471 TopoDS_Face aFace = TopoDS::Face( aShape );
472 return LocalPartition( aFace, theType );
475 bool HYDROData_LandCoverMap::Add( const TopoDS_Wire& theWire, const QString& theType )
477 if( !theWire.Closed() )
480 TopoDS_Face aFace = BRepBuilderAPI_MakeFace( theWire, Standard_True ).Face();
481 return LocalPartition( aFace, theType );
485 Add a new polyline as land cover
486 @param thePolyline the polyline to add as land cover
487 @param theType the Strickler type for the new land cover
488 @return if the addition is successful
490 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_PolylineXY )& thePolyline, const QString& theType )
492 if( thePolyline.IsNull() )
495 TopoDS_Shape aShape = thePolyline->GetShape();
496 if( aShape.ShapeType()==TopAbs_WIRE )
497 return Add( TopoDS::Wire( aShape ), theType );
499 if( aShape.ShapeType()==TopAbs_COMPOUND )
501 TopExp_Explorer anExp( aShape, TopAbs_WIRE );
502 for( ; anExp.More(); anExp.Next() )
504 TopoDS_Wire aPart = TopoDS::Wire( anExp.Current() );
505 if( !Add( aPart, theType ) )
515 Remove the given face from land cover map
516 @param theFace the face to be removed
517 @return if the removing is successful
519 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
521 TopTools_ListOfShape aList;
522 aList.Append( theFace );
523 return Remove( aList );
527 Remove the given faces from land cover map
528 @param theFacesToRemove the face list to be removed
529 @return if the removing is successful
531 bool HYDROData_LandCoverMap::Remove( const TopTools_ListOfShape& theFacesToRemove )
533 HYDROData_MapOfFaceToStricklerType aFacesToRemove, aNewFaces;
534 TopTools_ListIteratorOfListOfShape aFIt( theFacesToRemove );
535 for( ; aFIt.More(); aFIt.Next() )
537 TopoDS_Shape aShape = aFIt.Value();
538 if( aShape.ShapeType()==TopAbs_FACE )
539 aFacesToRemove.Add( TopoDS::Face( aShape ), "" );
542 Explorer anIt( *this );
543 for( ; anIt.More(); anIt.Next() )
544 if( !aFacesToRemove.Contains( anIt.Face() ) )
545 aNewFaces.Add( anIt.Face(), anIt.StricklerType() );
547 if ( aNewFaces.IsEmpty() )
550 StoreLandCovers( aNewFaces );
555 Split the land cover map by the given polyline
556 @param thePolyline the tool polyline to split the land cover map
557 @return if the removing is successful
559 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_PolylineXY )& thePolyline )
561 if( thePolyline.IsNull() )
564 TopoDS_Shape aShape = thePolyline->GetShape();
565 return Split( aShape );
570 Split the land cover map by the given polyline
571 @param theShape the tool polyline to split the land cover map
572 @return if the removing is successful
574 bool HYDROData_LandCoverMap::Split( const TopoDS_Shape& theShape )
576 int aNbCL = GetLCCount();
577 bool aResult = LocalPartition( theShape, "" );
578 return aResult && aNbCL != GetLCCount();
583 Merge the given faces in the land cover
584 @param theFaces the faces to merge in the land cover map
585 @param theType the Strickler type for the merged land cover
586 @return if the merge is successful
588 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
590 // 1. to fuse the faces into the new face
591 TopoDS_Shape aMergedFace = MergeFaces( theFaces, true );
592 if( !aMergedFace.IsNull() && aMergedFace.ShapeType()==TopAbs_FACE )
594 // 2. to remove the merged faces from the current map
597 // 3. to add the face into the map
598 return LocalPartition( TopoDS::Face( aMergedFace ), theType );
604 Merge the given faces into the shell/face
605 @param theFaces the faces to merge
606 @param IsToUnify if the common edges should be removed (fused)
607 @param theTolerance the operation's tolerance
608 @return result shape (face or shell)
610 TopoDS_Shape HYDROData_LandCoverMap::MergeFaces( const TopTools_ListOfShape& theFaces,
611 bool IsToUnify, double theTolerance )
614 TopTools_ListIteratorOfListOfShape anIt;
615 BOPCol_ListOfShape aLC;
616 anIt.Initialize(theFaces);
617 for( ; anIt.More(); anIt.Next() )
619 if (anIt.Value().ShapeType() != TopAbs_FACE)
620 return TopoDS_Shape();
621 aLC.Append( anIt.Value() );
624 BOPAlgo_PaveFiller aPF;
625 aPF.SetArguments( aLC );
626 aPF.SetRunParallel( Standard_False );
627 aPF.SetFuzzyValue( theTolerance );
630 anError = aPF.ErrorStatus();
632 return TopoDS_Shape();
634 BOPAlgo_Builder anAlgo;
635 anIt.Initialize( theFaces );
636 for( ; anIt.More(); anIt.Next() )
637 anAlgo.AddArgument( anIt.Value() );
639 anAlgo.PerformWithFiller( aPF );
640 anError = anAlgo.ErrorStatus();
642 return TopoDS_Shape();
644 const TopoDS_Shape& aMergedShape = anAlgo.Shape();
646 BRep_Builder aBuilder;
648 aBuilder.MakeShell( aShell );
649 aShell.Closed( Standard_False );
650 TopExp_Explorer anExplorer( aMergedShape, TopAbs_FACE );
651 for( ; anExplorer.More(); anExplorer.Next() )
653 const TopoDS_Face& aFace = TopoDS::Face(anExplorer.Current());
656 if( aFace.ShapeType() == TopAbs_FACE )
658 aBuilder.Add( aShell, aFace );
659 aShell.Closed( Standard_False );
663 TopoDS_Shape aResult;
666 ShapeUpgrade_UnifySameDomain aUSD;
667 aUSD.Initialize( aShell );
669 aResult = aUSD.Shape();
674 anExplorer.Init( aResult, TopAbs_FACE );
676 TopoDS_Face anOneFace;
677 for( ; anExplorer.More(); anExplorer.Next(), n++ )
678 anOneFace = TopoDS::Face( anExplorer.Current() );
682 else if (aResult.ShapeType() == TopAbs_SHELL)
684 BRepCheck_Shell aBCS(TopoDS::Shell(aResult));
685 if (aBCS.Status().First() != BRepCheck_NoError)
687 ShapeFix_Shell aFixer;
688 aFixer.FixFaceOrientation(TopoDS::Shell(aResult), 1);
689 aResult = aFixer.Shape();
697 Change Strickler type for the list of faces to the given one
698 @param theFaces the faces to change type
699 @param theType the Strickler type for the given land cover(s)
700 @return if the change type operation is successful
702 bool HYDROData_LandCoverMap::ChangeType( const TopTools_ListOfShape& theFaces, const QString& theType )
704 HYDROData_MapOfFaceToStricklerType aFacesToChangeType;
705 TopTools_ListIteratorOfListOfShape aFIt( theFaces );
706 for( ; aFIt.More(); aFIt.Next() )
708 TopoDS_Shape aShape = aFIt.Value();
709 if( aShape.ShapeType()==TopAbs_FACE )
710 aFacesToChangeType.Add( TopoDS::Face( aShape ), "" );
714 Explorer anIt( *this );
715 for( ; anIt.More(); anIt.Next() )
716 if( aFacesToChangeType.Contains( anIt.Face() ) )
718 anIt.SetStricklerType( theType );
721 if ( aNbChanges != theFaces.Extent() )
728 Get the shape of the land cover map
730 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
732 return HYDROData_Entity::GetShape( DataTag_Shape );
736 Get Strickler type of the given land cover
737 @param theLandCover the land cover to get Strickler type of
738 @return name of Strickler type
740 QString HYDROData_LandCoverMap::StricklerType( const TopoDS_Face& theLandCover ) const
744 Explorer anIt( *this );
745 for( ; anIt.More(); anIt.Next() )
746 if( anIt.Face().IsEqual( theLandCover) )
748 aType = anIt.StricklerType();
756 Set the shape of the land cover map
757 @param theShape the new shape for the land cover map
759 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
761 HYDROData_Entity::SetShape( DataTag_Shape, theShape );
765 Perform the local partition algorithm on the land cover
766 @param theNewShape the new shape to add into the land cover
767 @param theNewType the new Strickler type for the new land cover
768 @return if the local partition is successful
770 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
772 if( theNewShape.IsNull() )
775 BOPCol_ListOfShape aShapesList;
776 BOPAlgo_PaveFiller aPaveFiller;
777 HYDROData_MapOfFaceToStricklerType aNewFaces;
779 // add faces to shapes list
780 Explorer anIt( *this );
781 for( ; anIt.More(); anIt.Next() )
782 aShapesList.Append( anIt.Face() );
783 aShapesList.Append( theNewShape );
785 if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
787 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
788 StoreLandCovers( aNewFaces );
792 // prepare pave filler
793 aPaveFiller.SetArguments( aShapesList );
794 aPaveFiller.Perform();
795 Standard_Integer anError = aPaveFiller.ErrorStatus();
799 // add faces to builder
800 BOPAlgo_Builder aBuilder;
802 for( ; anIt.More(); anIt.Next() )
803 aBuilder.AddArgument( anIt.Face() );
804 aBuilder.AddArgument( theNewShape );
806 // perform the partition with the pave filler
807 aBuilder.PerformWithFiller( aPaveFiller );
808 anError = aBuilder.ErrorStatus();
812 //std::cout << "History:" << std::endl;
813 // analysis of the history
814 // a. to fill map of shapes which come from the new face
815 NCollection_IndexedMap<int> aShapesFromNewFace;
816 //std::cout << "from NEW " << theNewShape << ":" << theNewType << std::endl;
817 TopTools_ListOfShape aModified = aBuilder.Modified( theNewShape );
818 TopTools_ListIteratorOfListOfShape aMIt( aModified );
819 for( ; aMIt.More(); aMIt.Next() )
821 //std::cout << " " << aMIt.Value() << std::endl;
822 int aKey = (int)(uintptr_t)aMIt.Value().TShape().operator->();
823 aShapesFromNewFace.Add( aKey );
826 // b. to fill map of parts except parts from new face
828 for( ; anIt.More(); anIt.Next() )
830 QString aSType = anIt.StricklerType();
831 //std::cout << "from " << anIt.Face() << ": " << anIt.StricklerType() << std::endl;
832 TopTools_ListOfShape aModified = aBuilder.Modified( anIt.Face() );
833 if( aModified.Extent() == 0 )
834 aModified.Append( anIt.Face() );
836 TopTools_ListIteratorOfListOfShape aMIt( aModified );
837 for( ; aMIt.More(); aMIt.Next() )
839 TopoDS_Shape aShape = aMIt.Value();
840 bool isFace = aShape.ShapeType()==TopAbs_FACE;
841 int aKey = (int)(uintptr_t)aShape.TShape().operator->();
842 bool isAlsoFromNew = aShapesFromNewFace.Contains( aKey );
843 //std::cout << " " << aShape << " " << isAlsoFromNew << std::endl;
844 if( isFace && !isAlsoFromNew )
845 aNewFaces.Add( TopoDS::Face( aShape ), aSType );
849 // c. add the new shape if it is face with its type
850 if( theNewShape.ShapeType()==TopAbs_FACE )
851 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
853 // convert map of shape to type to compound and list of types
854 StoreLandCovers( aNewFaces );
859 Replace the set of land covers in the land cover map
860 @param theMap the map of shape (face) to Strickler type (string)
862 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
864 TopTools_ListOfShape aListOfFaces;
866 int n = theMap.Size();
868 Handle( TDataStd_ExtStringArray ) aTypes =
869 TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, n-1, Standard_True );
871 HYDROData_MapOfFaceToStricklerType::Iterator aNFIt( theMap );
872 for( int i=0; aNFIt.More(); aNFIt.Next(), i++ )
874 TopoDS_Face aFace = aNFIt.Key();
877 QString aType = aNFIt.Value();
878 aListOfFaces.Append(aFace);
879 aTypes->SetValue( i, HYDROData_Tool::toExtString( aType ) );
882 TopoDS_Shape aResult;
883 if( aListOfFaces.Extent() == 1 )
884 aResult = aListOfFaces.First();
885 else if( aListOfFaces.Extent() > 1 )
886 aResult = MergeFaces( aListOfFaces, false );
888 //remove internal edges
889 aResult = RemoveInternal(aResult);
894 Checks that object has 2D presentation. Reimlemented to retun true.
896 bool HYDROData_LandCoverMap::IsHas2dPrs() const
902 Find the land cover for the given point
903 @param thePoint the point laying in some land cover
904 @param theType the returned type
905 @return the found land cover's face
907 TopoDS_Face HYDROData_LandCoverMap::FindByPoint( const gp_Pnt2d& thePoint, QString& theType ) const
909 //TODO: some more optimal algorithm
910 Explorer anIt( *this );
911 for( ; anIt.More(); anIt.Next() )
912 if( HYDROData_Tool::ComputePointState( thePoint.XY(), anIt.Face() ) == TopAbs_IN )
914 theType = anIt.StricklerType();
919 return TopoDS_Face();
922 void Dump( const QString& theName, const QStringList& theList, QStringList& theLines )
924 theLines.append( QString( "%1 = QStringList()" ).arg( theName ) );
925 foreach( QString anItem, theList )
926 theLines.append( QString( "%1.append( u\"%2\" )" ).arg( theName ).arg( anItem ) );
931 @param theTreatedObjects the map of treated objects
933 QStringList HYDROData_LandCoverMap::DumpToPython( const QString& thePyScriptPath,
934 MapOfTreatedObjects& theTreatedObjects ) const
936 QStringList aResList = dumpObjectCreation( theTreatedObjects );
937 QString aName = GetObjPyName();
939 QString aShpFileName = thePyScriptPath;
940 aShpFileName.replace( ".py", ".shp" );
941 QString aDbfFileName = thePyScriptPath;
942 aDbfFileName.replace( ".py", ".dbf" );
944 ExportSHP( aShpFileName, true, 1 );
946 QString anAttr = "CODE_06"; //TODO: some custom choice
947 QStringList anAttrValues, aTypes;
948 HYDROData_Document::Document( myLab )->CollectQGISValues( anAttr, anAttrValues, aTypes );
949 ExportDBF( aDbfFileName, anAttr, anAttrValues, aTypes );
951 aResList << QString( "%1.ImportSHP( '%2' )" ).
952 arg( aName ).arg( aShpFileName );
954 Dump( "attr_values", anAttrValues, aResList );
955 Dump( "types", aTypes, aResList );
956 aResList << QString( "%1.ImportDBF( '%2', '%3', attr_values, types )" ).
957 arg( aName ).arg( aDbfFileName ).arg( anAttr );
962 TopoDS_Shape HYDROData_LandCoverMap::RemoveInternal(const TopoDS_Shape& InSh)
964 //Shape must be topologically correct
965 TopExp_Explorer anExp(InSh, TopAbs_EDGE);
966 TopTools_ListOfShape anEdgesToRemove;
968 for(; anExp.More(); anExp.Next() )
970 TopoDS_Edge CurEdge = TopoDS::Edge(anExp.Current());
971 if (CurEdge.Orientation() == TopAbs_INTERNAL)
972 anEdgesToRemove.Append(CurEdge);
975 Handle_ShapeBuild_ReShape aReshape = new ShapeBuild_ReShape();
976 TopoDS_Shape OutSh = aReshape->Apply(InSh);
977 TopTools_ListIteratorOfListOfShape aIt(anEdgesToRemove);
978 for (; aIt.More(); aIt.Next())
979 aReshape->Remove(aIt.Value());
980 OutSh = aReshape->Apply(InSh);
982 Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
989 void HYDROData_LandCoverMap::SetTransparency( double theTransparency )
991 Handle(TDataStd_Real) anAttr;
992 TDF_Label aLabel = myLab.FindChild( DataTag_Transparency );
993 if( !aLabel.FindAttribute( TDataStd_Real::GetID(), anAttr ) )
994 aLabel.AddAttribute( anAttr = new TDataStd_Real() );
995 anAttr->Set( theTransparency );
998 double HYDROData_LandCoverMap::GetTransparency() const
1000 Handle(TDataStd_Real) anAttr;
1001 TDF_Label aLabel = myLab.FindChild( DataTag_Transparency );
1002 if( !aLabel.FindAttribute( TDataStd_Real::GetID(), anAttr ) )
1005 return anAttr->Get();
1009 bool HYDROData_LandCoverMap::ImportSHP( const QString& theSHPFileName,
1010 const QList<int>& theIndices )
1012 HYDROData_ShapeFile anImporter;
1013 QStringList aPolyList;
1014 TopTools_SequenceOfShape aFaces;
1015 int aSHapeType = -1;
1016 int Stat = anImporter.ImportPolygons(theSHPFileName, aPolyList, aFaces, aSHapeType);
1021 HYDROData_MapOfFaceToStricklerType aMapFace2ST;
1022 int maxInd = *std::max_element(theIndices.begin(), theIndices.end());
1023 if (maxInd > aPolyList.length())
1026 if (theIndices.empty())
1029 for ( int i = 1; i <=aFaces.Length(); i++ )
1031 TopoDS_Shape aShape = aFaces(i);
1032 if ( aShape.IsNull() )
1034 aMapFace2ST.Add( TopoDS::Face( aShape ), "" );
1039 //import given indices
1040 foreach ( int Ind, theIndices )
1042 TopoDS_Shape aShape = aFaces(Ind + 1);
1043 if ( aShape.IsNull() )
1045 aMapFace2ST.Add( TopoDS::Face( aShape ), "" );
1049 StoreLandCovers(aMapFace2ST);
1053 bool HYDROData_LandCoverMap::ExportSHP( const QString& theSHPFileName, bool bUseDiscr, double theDefl) const
1055 HYDROData_ShapeFile anExporter;
1057 anExporter.Export(theSHPFileName, this, aList, bUseDiscr, theDefl );