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