Salome HOME
449c443e57a404c5e0db173570de386dc22f87ef
[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   HYDROData_ShapeFile aDBFImporter;
214   if (aDBFImporter.DBF_OpenDBF(theDBFFileName))
215     return DBFStatus_OPEN_FILE_ERROR; //cant open file
216
217   QStringList FieldList = aDBFImporter.DBF_GetFieldList();
218   int FieldNameIndex = FieldList.indexOf(theFieldName);
219   if (FieldNameIndex == -1)
220     return DBFStatus_NO_SUCH_FIELD_ERROR; //no such field
221
222   std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
223   aDBFImporter.DBF_GetAttributeList(FieldNameIndex, theAttrV ); 
224
225   bool allOK = true;
226   Iterator anIt( *this );
227   for( ; anIt.More(); anIt.Next() )
228   {
229     int CurIndex = anIt.Index();
230     HYDROData_ShapeFile::DBF_AttrValue AValue = theAttrV[theIndices[CurIndex]];
231     int StricklerTypesInd = theDBFValues.indexOf(QString(AValue.myStrVal));
232     if ( StricklerTypesInd != -1)
233       anIt.SetStricklerType(theDBFValues[StricklerTypesInd]);
234     else
235       allOK = false;
236   }
237   if (allOK)
238     return DBFStatus_OK;
239   else
240     return DBFStatus_NO_DBFVALUES_CORRESPONDENCE_WARNING;
241 }
242
243 /**
244   Export attributes to DBF File
245 ///
246 */
247 void HYDROData_LandCoverMap::ExportDBF( const QString& theDBFFileName, 
248                                         const QString& theFieldName, 
249                                         const QStringList& theDBFValues,
250                                         const QStringList& theStricklerTypes) const
251 {
252   HYDROData_ShapeFile anExporter; 
253   std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
254   Iterator anIt( *this );
255   for( ; anIt.More(); anIt.Next() )
256   {
257     QString CurST = anIt.StricklerType();
258     HYDROData_ShapeFile::DBF_AttrValue aCurAttrV;
259     aCurAttrV.myIsNull = false;
260     int StricklerTypesInd = theStricklerTypes.indexOf(CurST);
261     if (StricklerTypesInd != -1)
262     {
263       aCurAttrV.myStrVal = theDBFValues[StricklerTypesInd];
264       aCurAttrV.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String;
265       theAttrV.push_back(aCurAttrV);
266     }
267     else
268       aCurAttrV.myIsNull = true;
269   }
270
271   anExporter.DBF_WriteFieldAndValues(theDBFFileName, theFieldName, HYDROData_ShapeFile::DBF_FieldType_String, theAttrV, true);
272
273 }
274
275 int HashCode( const gp_Pnt& thePoint, const Standard_Integer theUpper )
276 {
277   int aHashX = HashCode( thePoint.X(), theUpper );
278   int aHashY = HashCode( thePoint.Y(), theUpper );
279   return (aHashX^aHashY)%theUpper;
280 }
281
282 bool operator == ( const gp_Pnt& thePoint1, const gp_Pnt& thePoint2 )
283 {
284   return thePoint1.IsEqual( thePoint2, Precision::Confusion() );
285 }
286
287 bool EdgeDiscretization( const TopoDS_Edge& theEdge, 
288                          Standard_Real theDeflection,
289                          NCollection_IndexedMap<gp_Pnt>& theVerticesMap,
290                          QList<int>& theVerticesIds )
291 {
292   BRepAdaptor_Curve aCurve( theEdge );
293   GCPnts_QuasiUniformDeflection aDiscrete( aCurve, theDeflection );
294   if( !aDiscrete.IsDone() )
295     return false;
296
297   int n = aDiscrete.NbPoints();
298   for( int i=1; i<=n; i++ )
299   {
300     gp_Pnt aPnt = aDiscrete.Value( i );
301     int anId;
302     if( theVerticesMap.Contains( aPnt ) )
303       anId = theVerticesMap.FindIndex( aPnt );
304     else
305     {
306       anId = theVerticesMap.Size();
307       theVerticesMap.Add( aPnt );
308     }
309     theVerticesIds.append( anId );
310   }
311   return true;
312 }
313
314 /**
315   Export the land cover map for the solver (Telemac)
316   @param theFileName the name of file
317   @return if the export is successful
318 */
319 bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName, Standard_Real theDeflection ) const
320 {
321   TopoDS_Shape aLandCoverMapShape = GetShape();
322   TopTools_ListOfShape aListOfFaces;
323   TopExp_Explorer anExp( aLandCoverMapShape, TopAbs_FACE );
324   for( ; anExp.More(); anExp.Next() )
325     aListOfFaces.Append( anExp.Current() );
326
327   TopoDS_Shape aShape = MergeFaces( aListOfFaces, false );
328
329   NCollection_IndexedMap<gp_Pnt> aVerticesMap;
330   NCollection_IndexedDataMap< TopoDS_Edge, QList<int> > anEdgesMap;
331   NCollection_IndexedDataMap< TopoDS_Face, QList<int> > aFacesMap;
332
333   // add into the map all edges existing in the shell
334   TopExp_Explorer anExp1( aShape, TopAbs_EDGE );
335   for( ; anExp1.More(); anExp1.Next() )
336   {
337     TopoDS_Edge anEdge = TopoDS::Edge( anExp1.Current() );
338     QList<int> aVerticesIdsList;
339     if( EdgeDiscretization( anEdge, theDeflection, aVerticesMap, aVerticesIdsList ) )
340       anEdgesMap.Add( anEdge, aVerticesIdsList );
341   }
342
343   // add into the map all faces existing in the shell and correspondence between face and edges ids
344   TopExp_Explorer anExp2( aShape, TopAbs_FACE );
345   for( ; anExp2.More(); anExp2.Next() )
346   {
347     TopoDS_Face aFace = TopoDS::Face( anExp2.Current() );
348     TopExp_Explorer anExp3( aFace, TopAbs_EDGE );
349     QList<int> anEdgesIdsList;
350     for( ; anExp3.More(); anExp3.Next() )
351     {
352       TopoDS_Edge anEdge = TopoDS::Edge( anExp3.Current() );
353       int anEdgeId = anEdgesMap.FindIndex( anEdge );
354       anEdgesIdsList.append( anEdgeId );
355     }
356     aFacesMap.Add( aFace, anEdgesIdsList );
357   }
358
359   QFile aFile( theFileName );
360   if( !aFile.open( QFile::WriteOnly | QFile::Text ) )
361     return false;
362
363   QTextStream aStream( &aFile );
364   aStream << "# nodes\n";
365   NCollection_IndexedMap<gp_Pnt>::Iterator anIt1( aVerticesMap );
366   for( ; anIt1.More(); anIt1.Next() )
367   {
368     gp_Pnt aPnt = anIt1.Value();
369     aStream << QString::number( aPnt.X(), TELEMAC_FORMAT, TELEMAC_PRECISION );
370     aStream << " ";
371     aStream << QString::number( aPnt.Y(), TELEMAC_FORMAT, TELEMAC_PRECISION );
372     aStream << " ";
373     aStream << QString::number( aPnt.Z(), TELEMAC_FORMAT, TELEMAC_PRECISION );
374     aStream << "\n";
375   }
376   aStream << "\n";
377
378   aStream << "# edges\n";
379   NCollection_IndexedDataMap< TopoDS_Edge, QList<int> >::Iterator anIt2( anEdgesMap );
380   for( ; anIt2.More(); anIt2.Next() )
381   {
382     QList<int> aVerticesIds = anIt2.Value();
383     foreach( int anId, aVerticesIds )
384       aStream << anId << " ";
385     aStream << "\n";
386   }
387   aStream << "\n";
388
389   aStream << "# faces\n";
390   NCollection_IndexedDataMap< TopoDS_Face, QList<int> >::Iterator anIt3( aFacesMap );
391   for( ; anIt3.More(); anIt3.Next() )
392   {
393     QList<int> anEdgesIds = anIt3.Value();
394     foreach( int anId, anEdgesIds )
395       aStream << anId << " ";
396     aStream << "\n";
397   }
398   aStream << "\n";
399
400   aFile.close();
401   return true;
402 }
403
404 /**
405   Add a new object as land cover
406   @param theObject the object to add as land cover
407   @param theType the Strickler type for the new land cover
408   @return if the addition is successful
409 */
410 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )& theObject, const QString& theType )
411 {
412   if( theObject.IsNull() )
413     return false;
414
415   TopoDS_Shape aShape = theObject->GetTopShape();
416   if( aShape.ShapeType()!=TopAbs_FACE )
417     return false;
418
419   TopoDS_Face aFace = TopoDS::Face( aShape );
420   return LocalPartition( aFace, theType );
421 }
422
423 /**
424   Add a new polyline as land cover
425   @param thePolyline the polyline to add as land cover
426   @param theType the Strickler type for the new land cover
427   @return if the addition is successful
428 */
429 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_PolylineXY )& thePolyline, const QString& theType )
430 {
431   if( thePolyline.IsNull() )
432     return false;
433
434   TopoDS_Shape aShape = thePolyline->GetShape();
435   if( aShape.ShapeType()!=TopAbs_WIRE )
436     return false;
437
438   TopoDS_Wire aWire = TopoDS::Wire( aShape );
439   if( !aWire.Closed() )
440     return false;
441
442   TopoDS_Face aFace = BRepBuilderAPI_MakeFace( aWire, Standard_True ).Face();
443   return LocalPartition( aFace, theType );
444 }
445
446 /**
447   Remove the given face from land cover map
448   @param theFace the face to be removed
449   @return if the removing is successful
450 */
451 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
452 {
453   TopTools_ListOfShape aList;
454   aList.Append( theFace );
455   return Remove( aList );
456 }
457
458 /**
459   Remove the given faces from land cover map
460   @param theFacesToRemove the face list to be removed
461   @return if the removing is successful
462 */
463 bool HYDROData_LandCoverMap::Remove( const TopTools_ListOfShape& theFacesToRemove )
464 {
465   HYDROData_MapOfFaceToStricklerType aFacesToRemove, aNewFaces;
466   TopTools_ListIteratorOfListOfShape aFIt( theFacesToRemove );
467   for( ; aFIt.More(); aFIt.Next() )
468   {
469     TopoDS_Shape aShape = aFIt.Value();
470     if( aShape.ShapeType()==TopAbs_FACE )
471       aFacesToRemove.Add( TopoDS::Face( aShape ), "" );
472   }
473
474   Iterator anIt( *this );
475   for( ; anIt.More(); anIt.Next() )
476     if( !aFacesToRemove.Contains( anIt.Face() ) )
477       aNewFaces.Add( anIt.Face(), anIt.StricklerType() );
478
479   StoreLandCovers( aNewFaces );
480   return true;
481 }
482
483 /**
484   Split the land cover map by the given polyline
485   @param thePolyline the tool polyline to split the land cover map
486   @return if the removing is successful
487 */
488 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_PolylineXY )& thePolyline )
489 {
490   if( thePolyline.IsNull() )
491     return false;
492
493   TopoDS_Shape aShape = thePolyline->GetShape();
494   return LocalPartition( aShape, "" );
495 }
496
497 /**
498   Merge the given faces in the land cover
499   @param theFaces the faces to merge in the land cover map
500   @param theType the Strickler type for the merged land cover
501   @return if the merge is successful
502 */
503 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
504 {
505   // 1. to fuse the faces into the new face
506   TopoDS_Shape aMergedFace = MergeFaces( theFaces, true );
507   if( aMergedFace.ShapeType()==TopAbs_FACE )
508   {
509     // 2. to remove the merged faces from the current map
510     Remove( theFaces );
511
512     // 3. to add the face into the map
513     return LocalPartition( TopoDS::Face( aMergedFace ), theType );
514   }
515   return false;
516 }
517
518 TopoDS_Shape HYDROData_LandCoverMap::MergeFaces( const TopTools_ListOfShape& theFaces,
519                                                  bool IsToUnify, double theTolerance )
520 {
521   int anError;
522   TopTools_ListIteratorOfListOfShape anIt;
523   BOPCol_ListOfShape aLC;
524   anIt.Initialize(theFaces);
525   for( ; anIt.More(); anIt.Next() )
526     aLC.Append( anIt.Value() );
527
528   BOPAlgo_PaveFiller aPF;
529   aPF.SetArguments( aLC );
530   aPF.SetRunParallel( Standard_False );
531   aPF.SetFuzzyValue( theTolerance );
532
533   aPF.Perform();
534   anError = aPF.ErrorStatus();
535   if( anError )
536     return TopoDS_Shape();
537
538   BOPAlgo_Builder anAlgo;
539   anIt.Initialize( theFaces );
540   for( ; anIt.More(); anIt.Next() )
541     anAlgo.AddArgument( anIt.Value() );
542
543   anAlgo.PerformWithFiller( aPF ); 
544   anError = anAlgo.ErrorStatus();
545   if( anError )
546     return TopoDS_Shape();
547
548   const TopoDS_Shape& aMergedShape = anAlgo.Shape();
549
550   BRep_Builder aBuilder;
551   TopoDS_Shell aShell; 
552   aBuilder.MakeShell( aShell ); 
553   aShell.Closed( Standard_False );
554   TopExp_Explorer anExplorer( aMergedShape, TopAbs_FACE );
555   for( ; anExplorer.More(); anExplorer.Next() ) 
556   {
557     const TopoDS_Face& aFace = TopoDS::Face(anExplorer.Current());
558     if( aFace.IsNull() ) 
559       continue;
560     if( aFace.ShapeType() == TopAbs_FACE )
561     {
562       aBuilder.Add( aShell, aFace );
563       aShell.Closed( Standard_False );
564     }
565   }
566
567   TopoDS_Shape aResult;
568   if( IsToUnify )
569   {
570     ShapeUpgrade_UnifySameDomain aUSD;
571     aUSD.Initialize( aShell );
572     aUSD.Build();
573     aResult = aUSD.Shape();
574   }
575   else
576     aResult = aShell;
577
578   anExplorer.Init( aResult, TopAbs_FACE );
579   int i = 0;
580   TopoDS_Face anOneFace;
581   for( ; anExplorer.More(); anExplorer.Next(), i++ ) 
582     anOneFace = TopoDS::Face( anExplorer.Current() );
583
584   if( i == 1 )
585     aResult = anOneFace;
586
587   return aResult;
588 }
589
590 /**
591   Get the shape of the land cover map
592 */
593 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
594 {
595   return HYDROData_Entity::GetShape( DataTag_Shape );
596 }
597
598 /**
599   Set the shape of the land cover map
600   @param theShape the new shape for the land cover map
601 */
602 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
603 {
604   HYDROData_Entity::SetShape( DataTag_Shape, theShape );
605 }
606
607 /**
608   Perform the local partition algorithm on the land cover
609   @param theNewShape the new shape to add into the land cover
610   @param theNewType the new Strickler type for the new land cover
611   @return if the local partition is successful
612 */
613 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
614 {
615   if( theNewShape.IsNull() )
616     return false;
617
618   BOPCol_ListOfShape aShapesList;
619   BOPAlgo_PaveFiller aPaveFiller;
620   HYDROData_MapOfFaceToStricklerType aNewFaces;
621
622   // add faces to shapes list
623   Iterator anIt( *this );
624   for( ; anIt.More(); anIt.Next() )
625     aShapesList.Append( anIt.Face() );
626   aShapesList.Append( theNewShape );
627
628   if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
629   {
630     aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
631     StoreLandCovers( aNewFaces );
632     return true;
633   }
634
635   // prepare pave filler
636   aPaveFiller.SetArguments( aShapesList );
637   aPaveFiller.Perform();
638   Standard_Integer anError = aPaveFiller.ErrorStatus();
639   if( anError )
640     return false;
641
642   // add faces to builder
643   BOPAlgo_Builder aBuilder;
644   anIt.Init( *this );
645   for( ; anIt.More(); anIt.Next() )
646     aBuilder.AddArgument( anIt.Face() );
647   aBuilder.AddArgument( theNewShape );
648
649   // perform the partition with the pave filler
650   aBuilder.PerformWithFiller( aPaveFiller );
651   anError = aBuilder.ErrorStatus();
652   if( anError )
653     return false;
654
655   // analysis of the history
656   //     a. to fill map of shapes which come from the new face
657   NCollection_IndexedMap<TopoDS_Shape> aShapesFromNewFace;
658   //std::cout << "new: " << theNewShape << " " << theNewType << std::endl;
659   TopTools_ListOfShape aModified = aBuilder.Modified( theNewShape );
660   TopTools_ListIteratorOfListOfShape aMIt( aModified );
661   for( ; aMIt.More(); aMIt.Next() )
662   {
663     //std::cout << "   " << aMIt.Value() << std::endl;
664     aShapesFromNewFace.Add( aMIt.Value() );
665   }
666
667   //     b. to fill map of parts except parts from new face
668   anIt.Init( *this );
669   for( ; anIt.More(); anIt.Next() )
670   {
671     QString aSType = anIt.StricklerType();
672     //std::cout << anIt.Face() << " " << anIt.StricklerType() << std::endl;
673     TopTools_ListOfShape aModified = aBuilder.Modified( anIt.Face() );
674     TopTools_ListIteratorOfListOfShape aMIt( aModified );
675     for( ; aMIt.More(); aMIt.Next() )
676     {
677       TopoDS_Shape aShape = aMIt.Value();
678       bool isFace = aShape.ShapeType()==TopAbs_FACE;
679       bool isAlsoFromNew = aShapesFromNewFace.Contains( aShape );
680       //std::cout << "   " << aShape << " " << isAlsoFromNew << std::endl;
681       if( isFace && !isAlsoFromNew )
682         aNewFaces.Add( TopoDS::Face( aShape ), aSType );
683     }
684   }
685
686   //     c. add the new shape if it is face with its type
687   if( theNewShape.ShapeType()==TopAbs_FACE )
688     aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
689   
690   // convert map of shape to type to compound and list of types
691   StoreLandCovers( aNewFaces );
692   return true;
693 }
694
695 /**
696   Replace the set of land covers in the land cover map
697   @param theMap the map of shape (face) to Strickler type (string)
698 */
699 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
700 {
701   TopoDS_Compound aCompound;
702   BRep_Builder aCompoundBuilder;
703   aCompoundBuilder.MakeCompound( aCompound );
704
705   int n = theMap.Size();
706   Handle( TDataStd_ExtStringArray ) aTypes = 
707     TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, n-1, Standard_True );
708   HYDROData_MapOfFaceToStricklerType::Iterator aNFIt( theMap );
709   for( int i=0; aNFIt.More(); aNFIt.Next(), i++ )
710   {
711     TopoDS_Face aFace = aNFIt.Key();
712     QString aType = aNFIt.Value();
713     aCompoundBuilder.Add( aCompound, aFace );
714     aTypes->SetValue( i, HYDROData_Tool::toExtString( aType ) );
715   }
716
717   SetShape( aCompound );
718 }
719
720 /**
721   Find the land cover for the given point
722   @param thePoint the point laying in some land cover
723   @param theType the returned type
724   @return the found land cover's face
725 */
726 TopoDS_Face HYDROData_LandCoverMap::FindByPoint( const gp_Pnt2d& thePoint, QString& theType ) const
727 {
728   //TODO: some more optimal algorithm
729   Iterator anIt( *this );
730   for( ; anIt.More(); anIt.Next() )
731     if( HYDROData_Tool::ComputePointState( thePoint.XY(), anIt.Face() ) == TopAbs_IN )
732     {
733       theType = anIt.StricklerType();
734       return anIt.Face();
735     }
736
737   theType = "";
738   return TopoDS_Face();
739 }