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