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 return LocalPartition( theShape, "" );
581 Merge the given faces in the land cover
582 @param theFaces the faces to merge in the land cover map
583 @param theType the Strickler type for the merged land cover
584 @return if the merge is successful
586 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
588 // 1. to fuse the faces into the new face
589 TopoDS_Shape aMergedFace = MergeFaces( theFaces, true );
590 if( !aMergedFace.IsNull() && aMergedFace.ShapeType()==TopAbs_FACE )
592 // 2. to remove the merged faces from the current map
595 // 3. to add the face into the map
596 return LocalPartition( TopoDS::Face( aMergedFace ), theType );
602 Merge the given faces into the shell/face
603 @param theFaces the faces to merge
604 @param IsToUnify if the common edges should be removed (fused)
605 @param theTolerance the operation's tolerance
606 @return result shape (face or shell)
608 TopoDS_Shape HYDROData_LandCoverMap::MergeFaces( const TopTools_ListOfShape& theFaces,
609 bool IsToUnify, double theTolerance )
612 TopTools_ListIteratorOfListOfShape anIt;
613 BOPCol_ListOfShape aLC;
614 anIt.Initialize(theFaces);
615 for( ; anIt.More(); anIt.Next() )
617 if (anIt.Value().ShapeType() != TopAbs_FACE)
618 return TopoDS_Shape();
619 aLC.Append( anIt.Value() );
622 BOPAlgo_PaveFiller aPF;
623 aPF.SetArguments( aLC );
624 aPF.SetRunParallel( Standard_False );
625 aPF.SetFuzzyValue( theTolerance );
628 anError = aPF.ErrorStatus();
630 return TopoDS_Shape();
632 BOPAlgo_Builder anAlgo;
633 anIt.Initialize( theFaces );
634 for( ; anIt.More(); anIt.Next() )
635 anAlgo.AddArgument( anIt.Value() );
637 anAlgo.PerformWithFiller( aPF );
638 anError = anAlgo.ErrorStatus();
640 return TopoDS_Shape();
642 const TopoDS_Shape& aMergedShape = anAlgo.Shape();
644 BRep_Builder aBuilder;
646 aBuilder.MakeShell( aShell );
647 aShell.Closed( Standard_False );
648 TopExp_Explorer anExplorer( aMergedShape, TopAbs_FACE );
649 for( ; anExplorer.More(); anExplorer.Next() )
651 const TopoDS_Face& aFace = TopoDS::Face(anExplorer.Current());
654 if( aFace.ShapeType() == TopAbs_FACE )
656 aBuilder.Add( aShell, aFace );
657 aShell.Closed( Standard_False );
661 TopoDS_Shape aResult;
664 ShapeUpgrade_UnifySameDomain aUSD;
665 aUSD.Initialize( aShell );
667 aResult = aUSD.Shape();
672 anExplorer.Init( aResult, TopAbs_FACE );
674 TopoDS_Face anOneFace;
675 for( ; anExplorer.More(); anExplorer.Next(), n++ )
676 anOneFace = TopoDS::Face( anExplorer.Current() );
680 else if (aResult.ShapeType() == TopAbs_SHELL)
682 BRepCheck_Shell aBCS(TopoDS::Shell(aResult));
683 if (aBCS.Status().First() != BRepCheck_NoError)
685 ShapeFix_Shell aFixer;
686 aFixer.FixFaceOrientation(TopoDS::Shell(aResult), 1);
687 aResult = aFixer.Shape();
695 Change Strickler type for the list of faces to the given one
696 @param theFaces the faces to change type
697 @param theType the Strickler type for the given land cover(s)
698 @return if the change type operation is successful
700 bool HYDROData_LandCoverMap::ChangeType( const TopTools_ListOfShape& theFaces, const QString& theType )
702 HYDROData_MapOfFaceToStricklerType aFacesToChangeType;
703 TopTools_ListIteratorOfListOfShape aFIt( theFaces );
704 for( ; aFIt.More(); aFIt.Next() )
706 TopoDS_Shape aShape = aFIt.Value();
707 if( aShape.ShapeType()==TopAbs_FACE )
708 aFacesToChangeType.Add( TopoDS::Face( aShape ), "" );
712 Explorer anIt( *this );
713 for( ; anIt.More(); anIt.Next() )
714 if( aFacesToChangeType.Contains( anIt.Face() ) )
716 anIt.SetStricklerType( theType );
719 if ( aNbChanges != theFaces.Extent() )
726 Get the shape of the land cover map
728 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
730 return HYDROData_Entity::GetShape( DataTag_Shape );
734 Get Strickler type of the given land cover
735 @param theLandCover the land cover to get Strickler type of
736 @return name of Strickler type
738 QString HYDROData_LandCoverMap::StricklerType( const TopoDS_Face& theLandCover ) const
742 Explorer anIt( *this );
743 for( ; anIt.More(); anIt.Next() )
744 if( anIt.Face().IsEqual( theLandCover) )
746 aType = anIt.StricklerType();
754 Set the shape of the land cover map
755 @param theShape the new shape for the land cover map
757 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
759 HYDROData_Entity::SetShape( DataTag_Shape, theShape );
763 Perform the local partition algorithm on the land cover
764 @param theNewShape the new shape to add into the land cover
765 @param theNewType the new Strickler type for the new land cover
766 @return if the local partition is successful
768 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
770 if( theNewShape.IsNull() )
773 BOPCol_ListOfShape aShapesList;
774 BOPAlgo_PaveFiller aPaveFiller;
775 HYDROData_MapOfFaceToStricklerType aNewFaces;
777 // add faces to shapes list
778 Explorer anIt( *this );
779 for( ; anIt.More(); anIt.Next() )
780 aShapesList.Append( anIt.Face() );
781 aShapesList.Append( theNewShape );
783 if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
785 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
786 StoreLandCovers( aNewFaces );
790 // prepare pave filler
791 aPaveFiller.SetArguments( aShapesList );
792 aPaveFiller.Perform();
793 Standard_Integer anError = aPaveFiller.ErrorStatus();
797 // add faces to builder
798 BOPAlgo_Builder aBuilder;
800 for( ; anIt.More(); anIt.Next() )
801 aBuilder.AddArgument( anIt.Face() );
802 aBuilder.AddArgument( theNewShape );
804 // perform the partition with the pave filler
805 aBuilder.PerformWithFiller( aPaveFiller );
806 anError = aBuilder.ErrorStatus();
810 //std::cout << "History:" << std::endl;
811 // analysis of the history
812 // a. to fill map of shapes which come from the new face
813 NCollection_IndexedMap<int> aShapesFromNewFace;
814 //std::cout << "from NEW " << theNewShape << ":" << theNewType << std::endl;
815 TopTools_ListOfShape aModified = aBuilder.Modified( theNewShape );
816 TopTools_ListIteratorOfListOfShape aMIt( aModified );
817 for( ; aMIt.More(); aMIt.Next() )
819 //std::cout << " " << aMIt.Value() << std::endl;
820 int aKey = (int)(uintptr_t)aMIt.Value().TShape().operator->();
821 aShapesFromNewFace.Add( aKey );
824 // b. to fill map of parts except parts from new face
826 for( ; anIt.More(); anIt.Next() )
828 QString aSType = anIt.StricklerType();
829 //std::cout << "from " << anIt.Face() << ": " << anIt.StricklerType() << std::endl;
830 TopTools_ListOfShape aModified = aBuilder.Modified( anIt.Face() );
831 if( aModified.Extent() == 0 )
832 aModified.Append( anIt.Face() );
834 TopTools_ListIteratorOfListOfShape aMIt( aModified );
835 for( ; aMIt.More(); aMIt.Next() )
837 TopoDS_Shape aShape = aMIt.Value();
838 bool isFace = aShape.ShapeType()==TopAbs_FACE;
839 int aKey = (int)(uintptr_t)aShape.TShape().operator->();
840 bool isAlsoFromNew = aShapesFromNewFace.Contains( aKey );
841 //std::cout << " " << aShape << " " << isAlsoFromNew << std::endl;
842 if( isFace && !isAlsoFromNew )
843 aNewFaces.Add( TopoDS::Face( aShape ), aSType );
847 // c. add the new shape if it is face with its type
848 if( theNewShape.ShapeType()==TopAbs_FACE )
849 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
851 // convert map of shape to type to compound and list of types
852 StoreLandCovers( aNewFaces );
857 Replace the set of land covers in the land cover map
858 @param theMap the map of shape (face) to Strickler type (string)
860 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
862 TopTools_ListOfShape aListOfFaces;
864 int n = theMap.Size();
866 Handle( TDataStd_ExtStringArray ) aTypes =
867 TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, n-1, Standard_True );
869 HYDROData_MapOfFaceToStricklerType::Iterator aNFIt( theMap );
870 for( int i=0; aNFIt.More(); aNFIt.Next(), i++ )
872 TopoDS_Face aFace = aNFIt.Key();
875 QString aType = aNFIt.Value();
876 aListOfFaces.Append(aFace);
877 aTypes->SetValue( i, HYDROData_Tool::toExtString( aType ) );
880 TopoDS_Shape aResult;
881 if( aListOfFaces.Extent() == 1 )
882 aResult = aListOfFaces.First();
883 else if( aListOfFaces.Extent() > 1 )
884 aResult = MergeFaces( aListOfFaces, false );
886 //remove internal edges
887 aResult = RemoveInternal(aResult);
892 Checks that object has 2D presentation. Reimlemented to retun true.
894 bool HYDROData_LandCoverMap::IsHas2dPrs() const
900 Find the land cover for the given point
901 @param thePoint the point laying in some land cover
902 @param theType the returned type
903 @return the found land cover's face
905 TopoDS_Face HYDROData_LandCoverMap::FindByPoint( const gp_Pnt2d& thePoint, QString& theType ) const
907 //TODO: some more optimal algorithm
908 Explorer anIt( *this );
909 for( ; anIt.More(); anIt.Next() )
910 if( HYDROData_Tool::ComputePointState( thePoint.XY(), anIt.Face() ) == TopAbs_IN )
912 theType = anIt.StricklerType();
917 return TopoDS_Face();
920 void Dump( const QString& theName, const QStringList& theList, QStringList& theLines )
922 theLines.append( QString( "%1 = QStringList()" ).arg( theName ) );
923 foreach( QString anItem, theList )
924 theLines.append( QString( "%1.append( u\"%2\" )" ).arg( theName ).arg( anItem ) );
929 @param theTreatedObjects the map of treated objects
931 QStringList HYDROData_LandCoverMap::DumpToPython( const QString& thePyScriptPath,
932 MapOfTreatedObjects& theTreatedObjects ) const
934 QStringList aResList = dumpObjectCreation( theTreatedObjects );
935 QString aName = GetObjPyName();
937 QString aShpFileName = thePyScriptPath;
938 aShpFileName.replace( ".py", ".shp" );
939 QString aDbfFileName = thePyScriptPath;
940 aDbfFileName.replace( ".py", ".dbf" );
942 ExportSHP( aShpFileName, true, 1 );
944 QString anAttr = "CODE_06"; //TODO: some custom choice
945 QStringList anAttrValues, aTypes;
946 HYDROData_Document::Document( myLab )->CollectQGISValues( anAttr, anAttrValues, aTypes );
947 ExportDBF( aDbfFileName, anAttr, anAttrValues, aTypes );
949 aResList << QString( "%1.ImportSHP( '%2' )" ).
950 arg( aName ).arg( aShpFileName );
952 Dump( "attr_values", anAttrValues, aResList );
953 Dump( "types", aTypes, aResList );
954 aResList << QString( "%1.ImportDBF( '%2', '%3', attr_values, types )" ).
955 arg( aName ).arg( aDbfFileName ).arg( anAttr );
960 TopoDS_Shape HYDROData_LandCoverMap::RemoveInternal(const TopoDS_Shape& InSh)
962 //Shape must be topologically correct
963 TopExp_Explorer anExp(InSh, TopAbs_EDGE);
964 TopTools_ListOfShape anEdgesToRemove;
966 for(; anExp.More(); anExp.Next() )
968 TopoDS_Edge CurEdge = TopoDS::Edge(anExp.Current());
969 if (CurEdge.Orientation() == TopAbs_INTERNAL)
970 anEdgesToRemove.Append(CurEdge);
973 Handle_ShapeBuild_ReShape aReshape = new ShapeBuild_ReShape();
974 TopoDS_Shape OutSh = aReshape->Apply(InSh);
975 TopTools_ListIteratorOfListOfShape aIt(anEdgesToRemove);
976 for (; aIt.More(); aIt.Next())
977 aReshape->Remove(aIt.Value());
978 OutSh = aReshape->Apply(InSh);
980 Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
987 void HYDROData_LandCoverMap::SetTransparency( double theTransparency )
989 Handle(TDataStd_Real) anAttr;
990 TDF_Label aLabel = myLab.FindChild( DataTag_Transparency );
991 if( !aLabel.FindAttribute( TDataStd_Real::GetID(), anAttr ) )
992 aLabel.AddAttribute( anAttr = new TDataStd_Real() );
993 anAttr->Set( theTransparency );
996 double HYDROData_LandCoverMap::GetTransparency() const
998 Handle(TDataStd_Real) anAttr;
999 TDF_Label aLabel = myLab.FindChild( DataTag_Transparency );
1000 if( !aLabel.FindAttribute( TDataStd_Real::GetID(), anAttr ) )
1003 return anAttr->Get();
1007 bool HYDROData_LandCoverMap::ImportSHP( const QString& theSHPFileName,
1008 const QList<int>& theIndices )
1010 HYDROData_ShapeFile anImporter;
1011 QStringList aPolyList;
1012 TopTools_SequenceOfShape aFaces;
1013 int aSHapeType = -1;
1014 int Stat = anImporter.ImportPolygons(theSHPFileName, aPolyList, aFaces, aSHapeType);
1019 HYDROData_MapOfFaceToStricklerType aMapFace2ST;
1020 int maxInd = *std::max_element(theIndices.begin(), theIndices.end());
1021 if (maxInd > aPolyList.length())
1024 if (theIndices.empty())
1027 for ( int i = 1; i <=aFaces.Length(); i++ )
1029 TopoDS_Shape aShape = aFaces(i);
1030 if ( aShape.IsNull() )
1032 aMapFace2ST.Add( TopoDS::Face( aShape ), "" );
1037 //import given indices
1038 foreach ( int Ind, theIndices )
1040 TopoDS_Shape aShape = aFaces(Ind + 1);
1041 if ( aShape.IsNull() )
1043 aMapFace2ST.Add( TopoDS::Face( aShape ), "" );
1047 StoreLandCovers(aMapFace2ST);
1051 bool HYDROData_LandCoverMap::ExportSHP( const QString& theSHPFileName, bool bUseDiscr, double theDefl) const
1053 HYDROData_ShapeFile anExporter;
1055 anExporter.Export(theSHPFileName, this, aList, bUseDiscr, theDefl );