]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_LandCoverMap.cxx
Salome HOME
9df53033988a5f9b7406934b9ef22bd7db23971c
[modules/hydro.git] / src / HYDROData / HYDROData_LandCoverMap.cxx
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.
6 //
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.
11 //
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
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
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
25 #include <BOPAlgo_BOP.hxx>
26 #include <BOPAlgo_Builder.hxx>
27 #include <BOPAlgo_PaveFiller.hxx>
28 #include <BOPCol_ListOfShape.hxx>
29 #include <BRep_Builder.hxx>
30 #include <BRepAdaptor_Curve.hxx>
31 #include <BRepAlgoAPI_Fuse.hxx>
32 #include <BRepBuilderAPI_MakeFace.hxx>
33 #include <GCPnts_QuasiUniformDeflection.hxx>
34 #include <TopoDS.hxx>
35 #include <TopoDS_Compound.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <TopoDS_Face.hxx>
38 #include <TopoDS_Iterator.hxx>
39 #include <TopoDS_Shell.hxx>
40 #include <TopExp_Explorer.hxx>
41 #include <TopTools_ListIteratorOfListOfShape.hxx>
42 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
43 #include <BOPAlgo_PaveFiller.hxx>
44 #include <BRepTools.hxx>
45 #include <TopExp_Explorer.hxx>
46 #include <ShapeUpgrade_UnifySameDomain.hxx>
47 #include <TopExp.hxx>
48 #include <TDataStd_Real.hxx>
49 #include <TopTools_IndexedMapOfShape.hxx>
50 #include <ShapeBuild_ReShape.hxx>
51 #include <ShapeFix_Shape.hxx>
52
53
54 #include <QFile>
55 #include <QString>
56 #include <QTextStream>
57
58 const char TELEMAC_FORMAT = 'f';
59 const int TELEMAC_PRECISION = 3;
60
61
62 IMPLEMENT_STANDARD_HANDLE(HYDROData_LandCoverMap, HYDROData_Entity)
63 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_LandCoverMap, HYDROData_Entity)
64
65 /**
66   Constructor
67   @param theMap the land cover map to iterate through
68 */
69 HYDROData_LandCoverMap::Iterator::Iterator( const HYDROData_LandCoverMap& theMap )
70 {
71   Init( theMap );
72 }
73
74 HYDROData_LandCoverMap::Iterator::Iterator( const Handle( HYDROData_LandCoverMap )& theMap )
75 {
76   if( theMap.IsNull() )
77   {
78     myIterator = 0;
79     myIndex = -1;
80   }
81   else
82     Init( *theMap );
83 }
84
85 /**
86   Initialize the iterator
87   @param theMap the land cover map to iterate through
88 */
89 void HYDROData_LandCoverMap::Iterator::Init( const HYDROData_LandCoverMap& theMap )
90 {
91   TopoDS_Shape aShape = theMap.GetShape();
92   if( aShape.IsNull() )
93     myIterator = 0;
94   else
95     myIterator = new TopoDS_Iterator( aShape );
96   
97   theMap.myLab.FindChild( DataTag_Types ).FindAttribute( TDataStd_ExtStringArray::GetID(), myArray );
98   if( myArray.IsNull() )
99     myIndex = -1;
100   else
101     myIndex = myArray->Lower();
102 }
103
104 /**
105   Destructor
106 */
107 HYDROData_LandCoverMap::Iterator::~Iterator()
108 {
109   delete myIterator;
110 }
111
112 /**
113   Return the current 0-based index of the iterator
114   @return the current index
115 */
116 int HYDROData_LandCoverMap::Iterator::Index() const
117 {
118   if( myArray.IsNull() )
119     return -1;
120   else
121     return myIndex - myArray->Lower();
122 }
123
124 /**
125   Return if the iterator has more elements
126   @return if the iterator has more elements
127 */
128 bool HYDROData_LandCoverMap::Iterator::More() const
129 {
130   return !myArray.IsNull() && myIterator && myIterator->More();
131 }
132
133 /**
134   Move iterator to the next element
135 */
136 void HYDROData_LandCoverMap::Iterator::Next()
137 {
138   if( myIterator )
139   {
140     myIterator->Next();
141     myIndex++;
142   }
143 }
144
145 /**
146   Get the current land cover (face)
147   @return the land cover's face
148 */
149 TopoDS_Face HYDROData_LandCoverMap::Iterator::Face() const
150 {
151   if( myIterator )
152     return TopoDS::Face( myIterator->Value() );
153   else
154     return TopoDS_Face();
155 }
156
157 /**
158   Get the current land cover's Strickler type 
159   @return the land cover's Strickler type 
160 */
161 QString HYDROData_LandCoverMap::Iterator::StricklerType() const
162 {
163   if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
164     return "";
165   else
166     return HYDROData_Tool::toQString( myArray->Value( myIndex ) );
167 }
168
169 /**
170   Set the Strickler type for the current land cover
171   @param theType the Strickler type
172 */
173 void HYDROData_LandCoverMap::Iterator::SetStricklerType( const QString& theType )
174 {
175   if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
176     return;
177   else
178     myArray->SetValue( myIndex, HYDROData_Tool::toExtString( theType ) );
179 }
180
181 /**
182   Constructor
183 */
184 HYDROData_LandCoverMap::HYDROData_LandCoverMap()
185   : HYDROData_Entity( Geom_No )
186 {
187 }
188
189 /**
190   Destructor
191 */
192 HYDROData_LandCoverMap::~HYDROData_LandCoverMap()
193 {
194 }
195
196 /**
197   Get object's kind
198   @return object's kind
199 */
200 const ObjectKind HYDROData_LandCoverMap::GetKind() const
201 {
202   return KIND_LAND_COVER_MAP;
203 }
204
205 int HYDROData_LandCoverMap::GetLCCount() const
206 {
207   Iterator anIt( *this );
208   int i = 0;
209   for( ; anIt.More(); anIt.Next() )
210     i++;
211   return i;
212 }
213
214 bool HYDROData_LandCoverMap::IsEmpty() const
215 {
216   Iterator anIt( *this );
217   if ( !anIt.More() )
218     return true;
219   else
220     return false;
221 }
222
223 /**
224   Load attributes from DBF File
225 ///
226 */
227 HYDROData_LandCoverMap::DBFStatus HYDROData_LandCoverMap::ImportDBF( const QString& theDBFFileName, 
228                                                                      const QString& theFieldName, 
229                                                                      const QStringList& theDBFValues,
230                                                                      const QStringList& theStricklerTypes,
231                                                                      const QList<int>& theIndices )
232 {
233   if (theDBFValues.size() != theStricklerTypes.size())
234     return DBFStatus_DIFF_SIZE_ERROR;
235   HYDROData_ShapeFile aDBFImporter;
236   if (!aDBFImporter.DBF_OpenDBF(theDBFFileName))
237     return DBFStatus_OPEN_FILE_ERROR; //cant open file
238
239   QStringList FieldList = aDBFImporter.DBF_GetFieldList();
240   int FieldNameIndex = FieldList.indexOf(theFieldName);
241   if (FieldNameIndex == -1)
242     return DBFStatus_NO_SUCH_FIELD_ERROR; //no such field
243
244   std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
245   aDBFImporter.DBF_GetAttributeList(FieldNameIndex, theAttrV ); 
246
247   bool allOK = true;
248   Iterator anIt( *this );
249   for( ; anIt.More(); anIt.Next() )
250   {
251     int CurIndex = anIt.Index();
252     HYDROData_ShapeFile::DBF_AttrValue AValue = theAttrV[theIndices[CurIndex]];
253     int StricklerTypesInd = theDBFValues.indexOf(QString(AValue.myStrVal));
254     if ( StricklerTypesInd != -1)
255       anIt.SetStricklerType(theStricklerTypes[StricklerTypesInd]);
256     else
257       allOK = false;
258   }
259   if (allOK)
260     return DBFStatus_OK;
261   else
262     return DBFStatus_NO_DBFVALUES_CORRESPONDENCE_WARNING;
263 }
264
265 /**
266   Export attributes to DBF File
267 ///
268 */
269 void HYDROData_LandCoverMap::ExportDBF( const QString& theDBFFileName, 
270                                         const QString& theFieldName, 
271                                         const QStringList& theDBFValues,
272                                         const QStringList& theStricklerTypes) const
273 {
274   if (theDBFValues.size() != theStricklerTypes.size())
275     return;
276   HYDROData_ShapeFile anExporter; 
277   std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
278   Iterator anIt( *this );
279   for( ; anIt.More(); anIt.Next() )
280   {
281     QString CurST = anIt.StricklerType();
282     HYDROData_ShapeFile::DBF_AttrValue aCurAttrV;
283     aCurAttrV.myIsNull = false;
284     int StricklerTypesInd = theStricklerTypes.indexOf(CurST);
285     if (StricklerTypesInd != -1)
286     {
287       aCurAttrV.myStrVal = theDBFValues[StricklerTypesInd];
288       aCurAttrV.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String;
289       theAttrV.push_back(aCurAttrV);
290     }
291     else
292       aCurAttrV.myIsNull = true;
293   }
294   //use actual str value; not the raw value
295   anExporter.DBF_WriteFieldAndValues(theDBFFileName, theFieldName, HYDROData_ShapeFile::DBF_FieldType_String, theAttrV, false);
296
297 }
298
299 int HashCode( const gp_Pnt& thePoint, const Standard_Integer theUpper )
300 {
301   int aHashX = HashCode( thePoint.X(), theUpper );
302   int aHashY = HashCode( thePoint.Y(), theUpper );
303   return (aHashX^aHashY)%theUpper;
304 }
305
306 bool operator == ( const gp_Pnt& thePoint1, const gp_Pnt& thePoint2 )
307 {
308   return thePoint1.IsEqual( thePoint2, Precision::Confusion() );
309 }
310
311 bool EdgeDiscretization( const TopoDS_Edge& theEdge, 
312                          Standard_Real theDeflection,
313                          NCollection_IndexedMap<gp_Pnt>& theVerticesMap,
314                          QList<int>& theVerticesIds )
315 {
316   BRepAdaptor_Curve aCurve( theEdge );
317   GCPnts_QuasiUniformDeflection aDiscrete( aCurve, theDeflection );
318   if( !aDiscrete.IsDone() )
319     return false;
320
321   int n = aDiscrete.NbPoints();
322   for( int i=1; i<=n; i++ )
323   {
324     gp_Pnt aPnt = aDiscrete.Value( i );
325     int anId;
326     if( theVerticesMap.Contains( aPnt ) )
327       anId = theVerticesMap.FindIndex( aPnt );
328     else
329     {
330       anId = theVerticesMap.Size();
331       theVerticesMap.Add( aPnt );
332     }
333     theVerticesIds.append( anId );
334   }
335   return true;
336 }
337
338 /**
339   Export the land cover map for the solver (Telemac)
340   @param theFileName the name of file
341   @return if the export is successful
342 */
343 bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName, Standard_Real theDeflection ) const
344 {
345   TopoDS_Shape aLandCoverMapShape = GetShape();
346   TopTools_ListOfShape aListOfFaces;
347   TopExp_Explorer anExp( aLandCoverMapShape, TopAbs_FACE );
348   for( ; anExp.More(); anExp.Next() )
349     aListOfFaces.Append( anExp.Current() );
350
351   TopoDS_Shape aShape = MergeFaces( aListOfFaces, false );
352
353   NCollection_IndexedMap<gp_Pnt> aVerticesMap;
354   NCollection_IndexedDataMap< TopoDS_Edge, QList<int> > anEdgesMap;
355   NCollection_IndexedDataMap< TopoDS_Face, QList<int> > aFacesMap;
356
357   // add into the map all edges existing in the shell
358   TopExp_Explorer anExp1( aShape, TopAbs_EDGE );
359   for( ; anExp1.More(); anExp1.Next() )
360   {
361     TopoDS_Edge anEdge = TopoDS::Edge( anExp1.Current() );
362     QList<int> aVerticesIdsList;
363     if( EdgeDiscretization( anEdge, theDeflection, aVerticesMap, aVerticesIdsList ) )
364       anEdgesMap.Add( anEdge, aVerticesIdsList );
365   }
366
367   // add into the map all faces existing in the shell and correspondence between face and edges ids
368   TopExp_Explorer anExp2( aShape, TopAbs_FACE );
369   for( ; anExp2.More(); anExp2.Next() )
370   {
371     TopoDS_Face aFace = TopoDS::Face( anExp2.Current() );
372     TopExp_Explorer anExp3( aFace, TopAbs_EDGE );
373     QList<int> anEdgesIdsList;
374     for( ; anExp3.More(); anExp3.Next() )
375     {
376       TopoDS_Edge anEdge = TopoDS::Edge( anExp3.Current() );
377       int anEdgeId = anEdgesMap.FindIndex( anEdge );
378       anEdgesIdsList.append( anEdgeId );
379     }
380     aFacesMap.Add( aFace, anEdgesIdsList );
381   }
382
383   QFile aFile( theFileName );
384   if( !aFile.open( QFile::WriteOnly | QFile::Text ) )
385     return false;
386
387   QTextStream aStream( &aFile );
388   aStream << "# nodes\n";
389   NCollection_IndexedMap<gp_Pnt>::Iterator anIt1( aVerticesMap );
390   for( ; anIt1.More(); anIt1.Next() )
391   {
392     gp_Pnt aPnt = anIt1.Value();
393     aStream << QString::number( aPnt.X(), TELEMAC_FORMAT, TELEMAC_PRECISION );
394     aStream << " ";
395     aStream << QString::number( aPnt.Y(), TELEMAC_FORMAT, TELEMAC_PRECISION );
396     aStream << " ";
397     aStream << QString::number( aPnt.Z(), TELEMAC_FORMAT, TELEMAC_PRECISION );
398     aStream << "\n";
399   }
400   aStream << "\n";
401
402   aStream << "# edges\n";
403   NCollection_IndexedDataMap< TopoDS_Edge, QList<int> >::Iterator anIt2( anEdgesMap );
404   for( ; anIt2.More(); anIt2.Next() )
405   {
406     QList<int> aVerticesIds = anIt2.Value();
407     foreach( int anId, aVerticesIds )
408       aStream << anId << " ";
409     aStream << "\n";
410   }
411   aStream << "\n";
412
413   aStream << "# faces\n";
414   NCollection_IndexedDataMap< TopoDS_Face, QList<int> >::Iterator anIt3( aFacesMap );
415   for( ; anIt3.More(); anIt3.Next() )
416   {
417     QList<int> anEdgesIds = anIt3.Value();
418     foreach( int anId, anEdgesIds )
419       aStream << anId << " ";
420     aStream << "\n";
421   }
422   aStream << "\n";
423
424   aFile.close();
425   return true;
426 }
427
428 /**
429   Add a new object as land cover
430   @param theObject the object to add as land cover
431   @param theType the Strickler type for the new land cover
432   @return if the addition is successful
433 */
434 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )& theObject, const QString& theType )
435 {
436   if( theObject.IsNull() )
437     return false;
438
439   TopoDS_Shape aShape = theObject->GetTopShape();
440   if( aShape.ShapeType()!=TopAbs_FACE )
441     return false;
442
443   TopoDS_Face aFace = TopoDS::Face( aShape );
444   return LocalPartition( aFace, theType );
445 }
446
447 bool HYDROData_LandCoverMap::Add( const TopoDS_Wire& theWire, const QString& theType )
448 {
449   if( !theWire.Closed() )
450     return false;
451
452   TopoDS_Face aFace = BRepBuilderAPI_MakeFace( theWire, Standard_True ).Face();
453   return LocalPartition( aFace, theType );
454 }
455
456 /**
457   Add a new polyline as land cover
458   @param thePolyline the polyline to add as land cover
459   @param theType the Strickler type for the new land cover
460   @return if the addition is successful
461 */
462 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_PolylineXY )& thePolyline, const QString& theType )
463 {
464   if( thePolyline.IsNull() )
465     return false;
466
467   TopoDS_Shape aShape = thePolyline->GetShape();
468   if( aShape.ShapeType()==TopAbs_WIRE )
469     return Add( TopoDS::Wire( aShape ), theType );
470
471   if( aShape.ShapeType()==TopAbs_COMPOUND )
472   {
473     TopExp_Explorer anExp( aShape, TopAbs_WIRE );
474     for( ; anExp.More(); anExp.Next() )
475     {
476       TopoDS_Wire aPart = TopoDS::Wire( anExp.Current() );
477       if( !Add( aPart, theType ) )
478         return false;
479     }
480     return true;
481   }
482
483   return false;
484 }
485
486 /**
487   Remove the given face from land cover map
488   @param theFace the face to be removed
489   @return if the removing is successful
490 */
491 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
492 {
493   TopTools_ListOfShape aList;
494   aList.Append( theFace );
495   return Remove( aList );
496 }
497
498 /**
499   Remove the given faces from land cover map
500   @param theFacesToRemove the face list to be removed
501   @return if the removing is successful
502 */
503 bool HYDROData_LandCoverMap::Remove( const TopTools_ListOfShape& theFacesToRemove )
504 {
505   HYDROData_MapOfFaceToStricklerType aFacesToRemove, aNewFaces;
506   TopTools_ListIteratorOfListOfShape aFIt( theFacesToRemove );
507   for( ; aFIt.More(); aFIt.Next() )
508   {
509     TopoDS_Shape aShape = aFIt.Value();
510     if( aShape.ShapeType()==TopAbs_FACE )
511       aFacesToRemove.Add( TopoDS::Face( aShape ), "" );
512   }
513
514   Iterator anIt( *this );
515   for( ; anIt.More(); anIt.Next() )
516     if( !aFacesToRemove.Contains( anIt.Face() ) )
517       aNewFaces.Add( anIt.Face(), anIt.StricklerType() );
518
519   if ( aNewFaces.IsEmpty() )
520     return false;
521
522   StoreLandCovers( aNewFaces );
523   return true;
524 }
525
526 /**
527   Split the land cover map by the given polyline
528   @param thePolyline the tool polyline to split the land cover map
529   @return if the removing is successful
530 */
531 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_PolylineXY )& thePolyline )
532 {
533   if( thePolyline.IsNull() )
534     return false;
535
536   TopoDS_Shape aShape = thePolyline->GetShape();
537   return Split( aShape );
538 }
539
540
541 /**
542   Split the land cover map by the given polyline
543   @param theShape the tool polyline to split the land cover map
544   @return if the removing is successful
545 */
546 bool HYDROData_LandCoverMap::Split( const TopoDS_Shape& theShape )
547 {
548   return LocalPartition( theShape, "" );
549 }
550
551
552 /**
553   Merge the given faces in the land cover
554   @param theFaces the faces to merge in the land cover map
555   @param theType the Strickler type for the merged land cover
556   @return if the merge is successful
557 */
558 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
559 {
560   // 1. to fuse the faces into the new face
561   TopoDS_Shape aMergedFace = MergeFaces( theFaces, true );
562   if( !aMergedFace.IsNull() && aMergedFace.ShapeType()==TopAbs_FACE )
563   {
564     // 2. to remove the merged faces from the current map
565     Remove( theFaces );
566
567     // 3. to add the face into the map
568     return LocalPartition( TopoDS::Face( aMergedFace ), theType );
569   }
570   return false;
571 }
572
573 /**
574   Merge the given faces into the shell/face
575   @param theFaces the faces to merge
576   @param IsToUnify if the common edges should be removed (fused)
577   @param theTolerance the operation's tolerance
578   @return result shape (face or shell)
579 */
580 TopoDS_Shape HYDROData_LandCoverMap::MergeFaces( const TopTools_ListOfShape& theFaces,
581                                                  bool IsToUnify, double theTolerance )
582 {
583   int anError;
584   TopTools_ListIteratorOfListOfShape anIt;
585   BOPCol_ListOfShape aLC;
586   anIt.Initialize(theFaces);
587   for( ; anIt.More(); anIt.Next() )
588   {
589     if (anIt.Value().ShapeType() != TopAbs_FACE)
590       return TopoDS_Shape();
591     aLC.Append( anIt.Value() );
592   }
593
594   BOPAlgo_PaveFiller aPF;
595   aPF.SetArguments( aLC );
596   aPF.SetRunParallel( Standard_False );
597   aPF.SetFuzzyValue( theTolerance );
598
599   aPF.Perform();
600   anError = aPF.ErrorStatus();
601   if( anError )
602     return TopoDS_Shape();
603
604   BOPAlgo_Builder anAlgo;
605   anIt.Initialize( theFaces );
606   for( ; anIt.More(); anIt.Next() )
607     anAlgo.AddArgument( anIt.Value() );
608
609   anAlgo.PerformWithFiller( aPF ); 
610   anError = anAlgo.ErrorStatus();
611   if( anError )
612     return TopoDS_Shape();
613
614   const TopoDS_Shape& aMergedShape = anAlgo.Shape();
615
616   BRep_Builder aBuilder;
617   TopoDS_Shell aShell; 
618   aBuilder.MakeShell( aShell ); 
619   aShell.Closed( Standard_False );
620   TopExp_Explorer anExplorer( aMergedShape, TopAbs_FACE );
621   for( ; anExplorer.More(); anExplorer.Next() ) 
622   {
623     const TopoDS_Face& aFace = TopoDS::Face(anExplorer.Current());
624     if( aFace.IsNull() ) 
625       continue;
626     if( aFace.ShapeType() == TopAbs_FACE )
627     {
628       aBuilder.Add( aShell, aFace );
629       aShell.Closed( Standard_False );
630     }
631   }
632
633   TopoDS_Shape aResult;
634   if( IsToUnify )
635   {
636     ShapeUpgrade_UnifySameDomain aUSD;
637     aUSD.Initialize( aShell );
638     aUSD.Build();
639     aResult = aUSD.Shape();
640   }
641   else
642     aResult = aShell;
643
644   anExplorer.Init( aResult, TopAbs_FACE );
645   int n = 0;
646   TopoDS_Face anOneFace;
647   for( ; anExplorer.More(); anExplorer.Next(), n++ ) 
648     anOneFace = TopoDS::Face( anExplorer.Current() );
649
650   if( n == 1 )
651     aResult = anOneFace;
652
653   return aResult;
654 }
655
656 /**
657   Change Strickler type for the list of faces to the given one
658   @param theFaces the faces to change type
659   @param theType the Strickler type for the given land cover(s)
660   @return if the change type operation is successful
661 */
662 bool HYDROData_LandCoverMap::ChangeType( const TopTools_ListOfShape& theFaces, const QString& theType )
663 {
664   HYDROData_MapOfFaceToStricklerType aFacesToChangeType;
665   TopTools_ListIteratorOfListOfShape aFIt( theFaces );
666   for( ; aFIt.More(); aFIt.Next() )
667   {
668     TopoDS_Shape aShape = aFIt.Value();
669     if( aShape.ShapeType()==TopAbs_FACE )
670       aFacesToChangeType.Add( TopoDS::Face( aShape ), "" );
671   }
672
673   int aNbChanges = 0;
674   Iterator anIt( *this );
675   for( ; anIt.More(); anIt.Next() )
676     if( aFacesToChangeType.Contains( anIt.Face() ) )
677     {
678       anIt.SetStricklerType( theType );
679       aNbChanges++;
680     }
681   if ( aNbChanges != theFaces.Extent() )
682     return false;
683
684   return true;
685 }
686
687 /**
688   Get the shape of the land cover map
689 */
690 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
691 {
692   return HYDROData_Entity::GetShape( DataTag_Shape );
693 }
694
695 /**
696   Get Strickler type of the given land cover
697   @param theLandCover the land cover to get Strickler type of
698   @return name of Strickler type
699 */
700 QString HYDROData_LandCoverMap::StricklerType( const TopoDS_Face& theLandCover ) const
701 {
702   QString aType = "";
703
704   Iterator anIt( *this );
705   for( ; anIt.More(); anIt.Next() )
706     if( anIt.Face().IsEqual( theLandCover) )
707     {
708       aType = anIt.StricklerType();
709       break;
710     }
711
712   return aType;
713 }
714
715 /**
716   Set the shape of the land cover map
717   @param theShape the new shape for the land cover map
718 */
719 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
720 {
721   HYDROData_Entity::SetShape( DataTag_Shape, theShape );
722 }
723
724 /**
725   Perform the local partition algorithm on the land cover
726   @param theNewShape the new shape to add into the land cover
727   @param theNewType the new Strickler type for the new land cover
728   @return if the local partition is successful
729 */
730 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
731 {
732   if( theNewShape.IsNull() )
733     return false;
734
735   BOPCol_ListOfShape aShapesList;
736   BOPAlgo_PaveFiller aPaveFiller;
737   HYDROData_MapOfFaceToStricklerType aNewFaces;
738
739   // add faces to shapes list
740   Iterator anIt( *this );
741   for( ; anIt.More(); anIt.Next() )
742     aShapesList.Append( anIt.Face() );
743   aShapesList.Append( theNewShape );
744
745   if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
746   {
747     aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
748     StoreLandCovers( aNewFaces );
749     return true;
750   }
751
752   // prepare pave filler
753   aPaveFiller.SetArguments( aShapesList );
754   aPaveFiller.Perform();
755   Standard_Integer anError = aPaveFiller.ErrorStatus();
756   if( anError )
757     return false;
758
759   // add faces to builder
760   BOPAlgo_Builder aBuilder;
761   anIt.Init( *this );
762   for( ; anIt.More(); anIt.Next() )
763     aBuilder.AddArgument( anIt.Face() );
764   aBuilder.AddArgument( theNewShape );
765
766   // perform the partition with the pave filler
767   aBuilder.PerformWithFiller( aPaveFiller );
768   anError = aBuilder.ErrorStatus();
769   if( anError )
770     return false;
771
772   //std::cout << "History:" << std::endl;
773   // analysis of the history
774   //     a. to fill map of shapes which come from the new face
775   NCollection_IndexedMap<int> aShapesFromNewFace;
776   //std::cout << "from NEW " << theNewShape << ":" << theNewType << std::endl;
777   TopTools_ListOfShape aModified = aBuilder.Modified( theNewShape );
778   TopTools_ListIteratorOfListOfShape aMIt( aModified );
779   for( ; aMIt.More(); aMIt.Next() )
780   {
781     //std::cout << "   " << aMIt.Value() << std::endl;
782     int aKey = (int)(uintptr_t)aMIt.Value().TShape().operator->();
783     aShapesFromNewFace.Add( aKey );
784   }
785
786   //     b. to fill map of parts except parts from new face
787   anIt.Init( *this );
788   for( ; anIt.More(); anIt.Next() )
789   {
790     QString aSType = anIt.StricklerType();
791     //std::cout << "from " << anIt.Face() << ": " << anIt.StricklerType() << std::endl;
792     TopTools_ListOfShape aModified = aBuilder.Modified( anIt.Face() );
793     if( aModified.Extent() == 0 )
794       aModified.Append( anIt.Face() );
795
796     TopTools_ListIteratorOfListOfShape aMIt( aModified );
797     for( ; aMIt.More(); aMIt.Next() )
798     {
799       TopoDS_Shape aShape = aMIt.Value();
800       bool isFace = aShape.ShapeType()==TopAbs_FACE;
801       int aKey = (int)(uintptr_t)aShape.TShape().operator->();
802       bool isAlsoFromNew = aShapesFromNewFace.Contains( aKey );
803       //std::cout << "   " << aShape << " " << isAlsoFromNew << std::endl;
804       if( isFace && !isAlsoFromNew )
805         aNewFaces.Add( TopoDS::Face( aShape ), aSType );
806     }
807   }
808
809   //     c. add the new shape if it is face with its type
810   if( theNewShape.ShapeType()==TopAbs_FACE )
811     aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
812   
813   // convert map of shape to type to compound and list of types
814   StoreLandCovers( aNewFaces );
815   return true;
816 }
817
818 /**
819   Replace the set of land covers in the land cover map
820   @param theMap the map of shape (face) to Strickler type (string)
821 */
822 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
823 {
824   TopTools_ListOfShape aListOfFaces;
825
826   int n = theMap.Size();
827
828   Handle( TDataStd_ExtStringArray ) aTypes = 
829     TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, n-1, Standard_True );
830
831   HYDROData_MapOfFaceToStricklerType::Iterator aNFIt( theMap );
832   for( int i=0; aNFIt.More(); aNFIt.Next(), i++ )
833   {
834     TopoDS_Face aFace = aNFIt.Key();
835     if( aFace.IsNull() )
836       continue;
837     QString aType = aNFIt.Value();
838     aListOfFaces.Append(aFace);
839     aTypes->SetValue( i, HYDROData_Tool::toExtString( aType ) );
840   }
841
842   TopoDS_Shape aResult;
843   if( aListOfFaces.Extent() == 1 )
844   {
845     TopoDS_Shell aShell;
846     BRep_Builder aShellBuilder;
847     aShellBuilder.MakeShell( aShell );
848     aShell.Closed( Standard_False );
849     aShellBuilder.Add( aShell, aListOfFaces.First() );
850     aResult = aShell;
851   }
852   else if( aListOfFaces.Extent() > 1 )
853     aResult = MergeFaces( aListOfFaces, false );
854
855   //remove internal edges
856   aResult = RemoveInternal(aResult);
857   SetShape( aResult );
858 }
859
860 /**
861   Find the land cover for the given point
862   @param thePoint the point laying in some land cover
863   @param theType the returned type
864   @return the found land cover's face
865 */
866 TopoDS_Face HYDROData_LandCoverMap::FindByPoint( const gp_Pnt2d& thePoint, QString& theType ) const
867 {
868   //TODO: some more optimal algorithm
869   Iterator anIt( *this );
870   for( ; anIt.More(); anIt.Next() )
871     if( HYDROData_Tool::ComputePointState( thePoint.XY(), anIt.Face() ) == TopAbs_IN )
872     {
873       theType = anIt.StricklerType();
874       return anIt.Face();
875     }
876
877   theType = "";
878   return TopoDS_Face();
879 }
880
881 /**
882   Dump to Python
883   @param theTreatedObjects the map of treated objects
884 */
885 QStringList HYDROData_LandCoverMap::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
886 {
887   QStringList aResList = dumpObjectCreation( theTreatedObjects );
888   QString aName = GetObjPyName();
889
890   //Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
891   //setPythonReferenceObject( theTreatedObjects, aResList, aHydAxis, "SetHydraulicAxis" );
892
893   //HYDROData_SequenceOfObjects aSeqOfProfiles = GetProfiles();
894   //for ( int i = 1, aNb = aSeqOfProfiles.Size(); i <= aNb; ++i )
895   //{
896     //const Handle(HYDROData_Entity) aProfile = aSeqOfProfiles.Value( i );
897     //setPythonReferenceObject( theTreatedObjects, aResList, aProfile, "AddProfile" );
898   //}
899
900   //TODO
901
902   return aResList;
903 }
904
905 TopoDS_Shape HYDROData_LandCoverMap::RemoveInternal(const TopoDS_Shape& InSh)
906 {
907   //Shape must be topologically correct
908   TopExp_Explorer anExp(InSh, TopAbs_EDGE);
909   TopTools_ListOfShape anEdgesToRemove;
910
911   for(; anExp.More(); anExp.Next() )
912   {
913     TopoDS_Edge CurEdge = TopoDS::Edge(anExp.Current());
914     if (CurEdge.Orientation() == TopAbs_INTERNAL)
915       anEdgesToRemove.Append(CurEdge);
916   }
917  
918   Handle_ShapeBuild_ReShape aReshape = new ShapeBuild_ReShape();
919   TopoDS_Shape OutSh = aReshape->Apply(InSh);
920   TopTools_ListIteratorOfListOfShape aIt(anEdgesToRemove);
921   for (; aIt.More(); aIt.Next()) 
922     aReshape->Remove(aIt.Value());
923   OutSh = aReshape->Apply(InSh);
924
925   Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
926   sfs->Init(OutSh);
927   sfs->Perform();
928   return sfs->Shape();
929
930 }
931
932 void HYDROData_LandCoverMap::SetTransparency( double theTransparency )
933 {
934   Handle(TDataStd_Real) anAttr;
935   TDF_Label aLabel = myLab.FindChild( DataTag_Transparency );
936   if( !aLabel.FindAttribute( TDataStd_Real::GetID(), anAttr ) )
937     aLabel.AddAttribute( anAttr = new TDataStd_Real() );
938   anAttr->Set( theTransparency );
939 }
940
941 double HYDROData_LandCoverMap::GetTransparency() const
942 {
943   Handle(TDataStd_Real) anAttr;
944   TDF_Label aLabel = myLab.FindChild( DataTag_Transparency );
945   if( !aLabel.FindAttribute( TDataStd_Real::GetID(), anAttr ) )
946     return 0.5;
947
948   return anAttr->Get();
949   
950 }