Salome HOME
76dfe69af1dda49f443db23c6bbae9588d9fbc32
[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 #include <HYDROData_Document.h>
25 #include <HYDROData_StricklerTable.h>
26 #include <HYDROData_ShapesTool.h>
27
28 #include <BOPAlgo_BOP.hxx>
29 #include <BOPAlgo_Builder.hxx>
30 #include <BOPAlgo_PaveFiller.hxx>
31 #include <TopTools_ListOfShape.hxx>
32 #include <BRep_Builder.hxx>
33 #include <BRepAdaptor_Curve.hxx>
34 #include <BRepAlgoAPI_Fuse.hxx>
35 #include <BRepBuilderAPI_MakeFace.hxx>
36 #include <GCPnts_QuasiUniformDeflection.hxx>
37 #include <TopoDS.hxx>
38 #include <TopoDS_Compound.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Face.hxx>
41 #include <TopoDS_Iterator.hxx>
42 #include <TopoDS_Shell.hxx>
43 #include <TopExp_Explorer.hxx>
44 #include <TopTools_ListIteratorOfListOfShape.hxx>
45 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
46 #include <TopTools_SequenceOfShape.hxx>
47 #include <BOPAlgo_PaveFiller.hxx>
48 #include <BRepTools.hxx>
49 #include <TopExp_Explorer.hxx>
50 #include <ShapeUpgrade_UnifySameDomain.hxx>
51 #include <TopExp.hxx>
52 #include <TopTools_IndexedMapOfShape.hxx>
53 #include <ShapeBuild_ReShape.hxx>
54 #include <ShapeFix_Shape.hxx>
55 #include <BRepCheck_Shell.hxx>
56 #include <BRepCheck_ListOfStatus.hxx>
57 #include <TopTools_SequenceOfShape.hxx>
58 #include <Geom_Curve.hxx>
59 #include <Geom_Line.hxx>
60 #include <Geom_TrimmedCurve.hxx>
61 #include <Geom_TrimmedCurve.hxx>
62 #include <TopTools_DataMapOfShapeListOfShape.hxx>
63 #include <NCollection_DoubleMap.hxx>
64 #include <Message_ProgressSentry.hxx>
65 #include <HYDROData_LCM_FaceClassifier.h>
66 #include <QDir>
67
68 #include <stdexcept>
69
70 #include <QFile>
71 #include <QString>
72 #include <QTextStream>
73 #include <QFileInfo>
74
75 //#define _DEVDEBUG_
76 #include "HYDRO_trace.hxx"
77
78 const char TELEMAC_FORMAT = 'f';
79 const int TELEMAC_PRECISION = 3;
80
81
82 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_LandCoverMap, HYDROData_Entity)
83
84 /**
85   Constructor
86   @param theMap the land cover map to iterate through
87 */
88 HYDROData_LandCoverMap::Explorer::Explorer( const HYDROData_LandCoverMap& theMap )
89 {
90   Init( theMap );
91 }
92
93 HYDROData_LandCoverMap::Explorer::Explorer( const Handle( HYDROData_LandCoverMap )& theMap )
94 {
95   if( theMap.IsNull() )
96   {
97     myExplorer = 0;
98     myIndex = -1;
99   }
100   else
101     Init( *theMap );
102 }
103
104 /**
105   Initialize the iterator
106   @param theMap the land cover map to iterate through
107 */
108 void HYDROData_LandCoverMap::Explorer::Init( const HYDROData_LandCoverMap& theMap )
109 {
110   TopoDS_Shape aShape = theMap.GetShape();
111   if( aShape.IsNull() )
112     myExplorer = 0;
113   else
114   {
115     myExplorer = new TopExp_Explorer();
116     myExplorer->Init( aShape, TopAbs_FACE );
117   }
118   
119   theMap.myLab.FindChild( DataTag_Types ).FindAttribute( TDataStd_ExtStringArray::GetID(), myArray );
120   if( myArray.IsNull() )
121     myIndex = -1;
122   else
123     myIndex = myArray->Lower();
124 }
125
126 /**
127   Destructor
128 */
129 HYDROData_LandCoverMap::Explorer::~Explorer()
130 {
131   delete myExplorer;
132 }
133
134 /**
135   Return the current 0-based index of the iterator
136   @return the current index
137 */
138 int HYDROData_LandCoverMap::Explorer::Index() const
139 {
140   if( myArray.IsNull() )
141     return -1;
142   else
143     return myIndex - myArray->Lower();
144 }
145
146 /**
147   Return if the iterator has more elements
148   @return if the iterator has more elements
149 */
150 bool HYDROData_LandCoverMap::Explorer::More() const
151 {
152   return !myArray.IsNull() && myExplorer && myExplorer->More();
153 }
154
155 /**
156   Move iterator to the next element
157 */
158 void HYDROData_LandCoverMap::Explorer::Next()
159 {
160   if( myExplorer )
161   {
162     myExplorer->Next();
163     myIndex++;
164   }
165 }
166
167 /**
168   Get the current land cover (face)
169   @return the land cover's face
170 */
171 TopoDS_Face HYDROData_LandCoverMap::Explorer::Face() const
172 {
173   if( myExplorer )
174     return TopoDS::Face( myExplorer->Current() );
175   else
176     return TopoDS_Face();
177 }
178
179 /**
180   Get the current land cover's Strickler type 
181   @return the land cover's Strickler type 
182 */
183 QString HYDROData_LandCoverMap::Explorer::StricklerType() const
184 {
185   if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
186     return "";
187   else
188     return HYDROData_Tool::toQString( myArray->Value( myIndex ) );
189 }
190
191 /**
192   Set the Strickler type for the current land cover
193   @param theType the Strickler type
194 */
195 void HYDROData_LandCoverMap::Explorer::SetStricklerType( const QString& theType )
196 {
197   if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
198     return;
199   else
200     myArray->SetValue( myIndex, HYDROData_Tool::toExtString( theType ) );
201 }
202
203 /**
204   Constructor
205 */
206 HYDROData_LandCoverMap::HYDROData_LandCoverMap()
207   : HYDROData_Entity( Geom_No )
208 {
209 }
210
211 /**
212   Destructor
213 */
214 HYDROData_LandCoverMap::~HYDROData_LandCoverMap()
215 {
216 }
217
218 /**
219   Get object's kind
220   @return object's kind
221 */
222 const ObjectKind HYDROData_LandCoverMap::GetKind() const
223 {
224   return KIND_LAND_COVER_MAP;
225 }
226
227 int HYDROData_LandCoverMap::GetLCCount() const
228 {
229   Explorer anIt( *this );
230   int i = 0;
231   for( ; anIt.More(); anIt.Next() )
232     i++;
233   return i;
234 }
235
236 bool HYDROData_LandCoverMap::IsEmpty() const
237 {
238   Explorer anIt( *this );
239   if ( !anIt.More() )
240     return true;
241   else
242     return false;
243 }
244
245 /**
246   Load attributes from DBF File
247 ///
248 */
249 HYDROData_LandCoverMap::DBFStatus HYDROData_LandCoverMap::ImportDBF( const QString& theDBFFileName, 
250                                                                      const QString& theFieldName, 
251                                                                      const QStringList& theDBFValues,
252                                                                      const QStringList& theStricklerTypes,
253                                                                      const QList<int>& theIndices )
254 {
255   if (theDBFValues.size() != theStricklerTypes.size())
256     return DBFStatus_DIFF_SIZE_ERROR;
257   HYDROData_ShapeFile aDBFImporter;
258   if (!aDBFImporter.DBF_OpenDBF(theDBFFileName))
259     return DBFStatus_OPEN_FILE_ERROR; //cant open file
260
261   QStringList FieldList = aDBFImporter.DBF_GetFieldList();
262   int FieldNameIndex = FieldList.indexOf(theFieldName);
263   if (FieldNameIndex == -1)
264     return DBFStatus_NO_SUCH_FIELD_ERROR; //no such field
265
266   std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
267   aDBFImporter.DBF_GetAttributeList(FieldNameIndex, theAttrV ); 
268
269   bool allOK = true;
270   Explorer anIt( *this );
271   for( ; anIt.More(); anIt.Next() )
272   {
273     int CurIndex = anIt.Index();
274     int anIndex = CurIndex;
275     if( !theIndices.isEmpty() )
276       anIndex = theIndices[CurIndex];
277
278     HYDROData_ShapeFile::DBF_AttrValue AValue = theAttrV[anIndex];
279     int StricklerTypesInd = theDBFValues.indexOf( QString( AValue.myStrVal ) );
280     if ( StricklerTypesInd != -1)
281       anIt.SetStricklerType( theStricklerTypes[StricklerTypesInd] );
282     else
283       allOK = false;
284   }
285   if (allOK)
286     return DBFStatus_OK;
287   else
288     return DBFStatus_NO_DBFVALUES_CORRESPONDENCE_WARNING;
289 }
290
291 /**
292   Export attributes to DBF File
293 ///
294 */
295 void HYDROData_LandCoverMap::ExportDBF( const QString& theDBFFileName, 
296                                         const QString& theFieldName, 
297                                         const QStringList& theDBFValues,
298                                         const QStringList& theStricklerTypes) const
299 {
300   if (theDBFValues.size() != theStricklerTypes.size())
301     return;
302   HYDROData_ShapeFile anExporter; 
303   std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
304   Explorer anIt( *this );
305   for( ; anIt.More(); anIt.Next() )
306   {
307     QString CurST = anIt.StricklerType();
308     HYDROData_ShapeFile::DBF_AttrValue aCurAttrV;
309     aCurAttrV.myIsNull = false;
310     int StricklerTypesInd = theStricklerTypes.indexOf(CurST);
311     if (StricklerTypesInd != -1)
312     {
313       aCurAttrV.myStrVal = theDBFValues[StricklerTypesInd];
314       aCurAttrV.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String;
315       theAttrV.push_back(aCurAttrV);
316     }
317     else
318       aCurAttrV.myIsNull = true;
319   }
320   //use actual str value; not the raw value
321   anExporter.DBF_WriteFieldAndValues(theDBFFileName, theFieldName, HYDROData_ShapeFile::DBF_FieldType_String, theAttrV, false);
322
323 }
324
325 int HashCode( const gp_Pnt& thePoint, const Standard_Integer theUpper )
326 {
327   int aHashX = HashCode( thePoint.X(), theUpper );
328   int aHashY = HashCode( thePoint.Y(), theUpper );
329   return (aHashX^aHashY)%theUpper;
330 }
331
332 bool operator == ( const gp_Pnt& thePoint1, const gp_Pnt& thePoint2 )
333 {
334   return thePoint1.IsEqual( thePoint2, Precision::Confusion() );
335 }
336
337 bool EdgeDiscretization( const TopoDS_Edge& theEdge, 
338                          Standard_Real theDeflection,
339                          NCollection_IndexedMap<gp_Pnt>& theVerticesMap,
340                          QList<int>& theVerticesIds )
341 {
342   BRepAdaptor_Curve aCurve( theEdge );
343   GCPnts_QuasiUniformDeflection aDiscrete( aCurve, theDeflection );
344   if( !aDiscrete.IsDone() )
345     return false;
346
347   int n = aDiscrete.NbPoints();
348   for( int i=1; i<=n; i++ )
349   {
350     gp_Pnt aPnt = aDiscrete.Value( i );
351     int anId;
352     if( theVerticesMap.Contains( aPnt ) )
353       anId = theVerticesMap.FindIndex( aPnt );
354     else
355     {
356       anId = theVerticesMap.Size();
357       theVerticesMap.Add( aPnt );
358     }
359     theVerticesIds.append( anId );
360   }
361   return true;
362 }
363
364 /**
365   Export the land cover map for the solver (Telemac)
366   @param theFileName the name of file
367   @return if the export is successful
368 */
369 bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName,
370                                             double theDeflection,
371                                             const Handle(HYDROData_StricklerTable)& theTable,
372                                             QString& statMessage) const
373 {
374   TopoDS_Shape aLandCoverMapShape = GetShape();
375   TopTools_ListOfShape aListOfFaces;
376   Explorer anIt( *this );
377   QMap<Handle(TopoDS_TShape), QString> aTypesMap;
378   for( ; anIt.More(); anIt.Next() )
379   {
380     aListOfFaces.Append( anIt.Face() );
381     aTypesMap.insert( anIt.Face().TShape(), anIt.StricklerType() );
382   }
383
384   TopoDS_Shape aShape = MergeFaces( aListOfFaces, false, NULL );
385
386   NCollection_IndexedMap<gp_Pnt> aVerticesMap;
387   NCollection_IndexedDataMap< TopoDS_Edge, QList<int> > anEdgesMap;
388   typedef QPair< QString, QList<int> > FaceData;
389   NCollection_IndexedDataMap< TopoDS_Face, FaceData > aFacesMap;
390
391   // add into the map all edges existing in the shell
392   TopExp_Explorer anExp1( aShape, TopAbs_EDGE );
393   for( ; anExp1.More(); anExp1.Next() )
394   {
395     TopoDS_Edge anEdge = TopoDS::Edge( anExp1.Current() );
396     QList<int> aVerticesIdsList;
397     if( EdgeDiscretization( anEdge, theDeflection, aVerticesMap, aVerticesIdsList ) )
398       anEdgesMap.Add( anEdge, aVerticesIdsList );
399   }
400
401   // add into the map all faces existing in the shell and correspondence between face and edges ids
402   TopExp_Explorer anExp2( aShape, TopAbs_FACE );
403   for( ; anExp2.More(); anExp2.Next() )
404   {
405     TopoDS_Face aFace = TopoDS::Face( anExp2.Current() );
406     TopExp_Explorer anExp3( aFace, TopAbs_EDGE );
407     QList<int> anEdgesIdsList;
408     for( ; anExp3.More(); anExp3.Next() )
409     {
410       TopoDS_Edge anEdge = TopoDS::Edge( anExp3.Current() );
411       int anEdgeId = anEdgesMap.FindIndex( anEdge );
412       anEdgesIdsList.append( anEdgeId );
413     }
414
415     FaceData aData;
416     aData.first = aTypesMap[aFace.TShape()];
417     aData.second = anEdgesIdsList;
418     aFacesMap.Add( aFace, aData );
419   }
420
421   QFile aFile( theFileName );
422   if( !aFile.open( QFile::WriteOnly | QFile::Text ) )
423   {
424     QString homeFilePath = QDir::home().absoluteFilePath( theFileName );
425     aFile.setFileName(homeFilePath);
426     if (aFile.open( QFile::WriteOnly | QFile::Text ) )
427       statMessage = "Telemac file have been exported to the home directory: " + homeFilePath;
428     else
429       return false;
430   }
431   else
432   {
433     QString absFilePath = QDir::current().absoluteFilePath( theFileName );
434     statMessage = "Telemac file have been exported to the current directory: " + absFilePath;
435   }
436
437   QTextStream aStream( &aFile );
438   aStream << "# nodes\n";
439   NCollection_IndexedMap<gp_Pnt>::Iterator anIt1( aVerticesMap );
440   for( ; anIt1.More(); anIt1.Next() )
441   {
442     gp_Pnt aPnt = anIt1.Value();
443     aStream << QString::number( aPnt.X(), TELEMAC_FORMAT, TELEMAC_PRECISION );
444     aStream << " ";
445     aStream << QString::number( aPnt.Y(), TELEMAC_FORMAT, TELEMAC_PRECISION );
446     aStream << " ";
447     aStream << QString::number( aPnt.Z(), TELEMAC_FORMAT, TELEMAC_PRECISION );
448     aStream << "\n";
449   }
450   aStream << "\n";
451
452   aStream << "# edges\n";
453   NCollection_IndexedDataMap< TopoDS_Edge, QList<int> >::Iterator anIt2( anEdgesMap );
454   for( ; anIt2.More(); anIt2.Next() )
455   {
456     QList<int> aVerticesIds = anIt2.Value();
457     foreach( int anId, aVerticesIds )
458       aStream << anId << " ";
459     aStream << "\n";
460   }
461   aStream << "\n";
462
463   aStream << "# faces\n";
464   NCollection_IndexedDataMap< TopoDS_Face, FaceData >::Iterator anIt3( aFacesMap );
465   for( ; anIt3.More(); anIt3.Next() )
466   {
467     QString aType = anIt3.Value().first;
468     double aCoeff = theTable->Get( aType, 0.0 );
469     QList<int> anEdgesIds = anIt3.Value().second;
470     //aStream << "\"" << aType << "\" ";
471     aStream << QString::number( aCoeff, TELEMAC_FORMAT, TELEMAC_PRECISION ) << " ";
472     foreach( int anId, anEdgesIds )
473       aStream << anId << " ";
474     aStream << "\n";
475   }
476   aStream << "\n";
477
478   aFile.close();
479   return true;
480 }
481
482 /**
483   Add a new object as land cover
484   @param theObject the object to add as land cover
485   @param theType the Strickler type for the new land cover
486   @return if the addition is successful
487 */
488 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )& theObject, const QString& theType )
489 {
490   if( theObject.IsNull() )
491     return false;
492
493   TopoDS_Shape aShape = theObject->GetTopShape();
494   TopoDS_Face aFace;
495
496   if( aShape.ShapeType() ==TopAbs_FACE )
497   {
498     aFace = TopoDS::Face(aShape);
499   }
500   else if ( aShape.ShapeType() ==TopAbs_COMPOUND )
501   {
502     TopoDS_Iterator It(aShape);
503     for (; It.More(); It.Next())
504       if (It.Value().ShapeType() == TopAbs_FACE)
505       {
506         aFace = TopoDS::Face(It.Value());
507         break;
508       }
509   }
510
511   if (aFace.IsNull())
512     return false;
513   return LocalPartition( aFace, theType );
514 }
515
516 bool HYDROData_LandCoverMap::Add( const TopoDS_Wire& theWire, const QString& theType )
517 {
518   if( !theWire.Closed() )
519     return false;
520
521   TopoDS_Face aFace = BRepBuilderAPI_MakeFace( theWire, Standard_True ).Face();
522   return LocalPartition( aFace, theType );
523 }
524
525 /**
526   Add a new polyline as land cover
527   @param thePolyline the polyline to add as land cover
528   @param theType the Strickler type for the new land cover
529   @return if the addition is successful
530 */
531 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_PolylineXY )& thePolyline, const QString& theType )
532 {
533   if( thePolyline.IsNull() )
534     return false;
535
536   TopoDS_Shape aShape = thePolyline->GetShape();
537   if( aShape.ShapeType()==TopAbs_WIRE )
538     return Add( TopoDS::Wire( aShape ), theType );
539
540   if( aShape.ShapeType()==TopAbs_COMPOUND )
541   {
542     TopExp_Explorer anExp( aShape, TopAbs_WIRE );
543     for( ; anExp.More(); anExp.Next() )
544     {
545       TopoDS_Wire aPart = TopoDS::Wire( anExp.Current() );
546       if( !Add( aPart, theType ) )
547         return false;
548     }
549     return true;
550   }
551
552   return false;
553 }
554
555 /**
556   Remove the given face from land cover map
557   @param theFace the face to be removed
558   @return if the removing is successful
559 */
560 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
561 {
562   TopTools_ListOfShape aList;
563   aList.Append( theFace );
564   return Remove( aList );
565 }
566
567 /**
568   Remove the given faces from land cover map
569   @param theFacesToRemove the face list to be removed
570   @return if the removing is successful
571 */
572 bool HYDROData_LandCoverMap::Remove( const TopTools_ListOfShape& theFacesToRemove )
573 {
574   HYDROData_MapOfFaceToStricklerType aFacesToRemove, aNewFaces;
575   TopTools_ListIteratorOfListOfShape aFIt( theFacesToRemove );
576   for( ; aFIt.More(); aFIt.Next() )
577   {
578     TopoDS_Shape aShape = aFIt.Value();
579     if( aShape.ShapeType()==TopAbs_FACE )
580       aFacesToRemove.Add( TopoDS::Face( aShape ), "" );
581   }
582
583   Explorer anIt( *this );
584   for( ; anIt.More(); anIt.Next() )
585     if( !aFacesToRemove.Contains( anIt.Face() ) )
586       aNewFaces.Add( anIt.Face(), anIt.StricklerType() );
587
588   if ( aNewFaces.IsEmpty() )
589     return false;
590
591   StoreLandCovers( aNewFaces );
592   return true;
593 }
594
595 /**
596   Split the land cover map by the given polyline
597   @param thePolyline the tool polyline to split the land cover map
598   @return if the removing is successful
599 */
600 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_PolylineXY )& thePolyline )
601 {
602   if( thePolyline.IsNull() )
603     return false;
604
605   TopoDS_Shape aShape = thePolyline->GetShape();
606   return Split( aShape );
607 }
608
609
610 /**
611   Split the land cover map by the given polyline
612   @param theShape the tool polyline to split the land cover map
613   @return if the removing is successful
614 */
615 bool HYDROData_LandCoverMap::Split( const TopoDS_Shape& theShape )
616 {
617   int aNbCL = GetLCCount();
618   bool aResult = LocalPartition( theShape, "" );
619   return aResult && aNbCL != GetLCCount();
620 }
621
622
623 /**
624   Merge the given faces in the land cover
625   @param theFaces the faces to merge in the land cover map
626   @param theType the Strickler type for the merged land cover
627   @return if the merge is successful
628 */
629 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
630 {
631   // 1. to fuse the faces into the new face
632   TopoDS_Shape aMergedFace = MergeFaces( theFaces, true, NULL );  
633   bool aStat = true;
634   if( !aMergedFace.IsNull() )
635   { 
636     // 2. to remove the merged faces from the current map
637     Remove( theFaces );
638     TopExp_Explorer Exp(aMergedFace, TopAbs_FACE);
639     for( ; Exp.More(); Exp.Next() )
640     {
641       const TopoDS_Face& aCF = TopoDS::Face(Exp.Current());
642       // 3. to add the face into the map
643       aStat = aStat && LocalPartition( aCF, theType );
644     }
645   }
646   else
647     aStat = false;
648   return aStat;
649 }
650
651 /**
652   Merge the given faces into the shell/face
653   @param theFaces the faces to merge
654   @param IsToUnify if the common edges should be removed (fused)
655   @param theTolerance the operation's tolerance
656   @return result shape (face or shell)
657 */
658  
659 TopoDS_Shape HYDROData_LandCoverMap::MergeFaces( const TopTools_ListOfShape& theFaces,
660                                                  bool IsToUnify, 
661                                                  TopTools_IndexedDataMapOfShapeListOfShape* theShHistory, 
662                                                  double theTolerance)
663 {
664   //DEBTRACE("MergeFaces");
665   int anError;
666   TopTools_ListIteratorOfListOfShape anIt;
667   TopTools_ListOfShape aLC;
668   anIt.Initialize(theFaces);
669   for( ; anIt.More(); anIt.Next() )
670   {
671     if (anIt.Value().ShapeType() != TopAbs_FACE)
672       return TopoDS_Shape();
673     aLC.Append( anIt.Value() );
674   }
675
676   BOPAlgo_PaveFiller aPF;
677   aPF.SetArguments( aLC );
678   aPF.SetRunParallel( Standard_False );
679   aPF.SetFuzzyValue( theTolerance );
680
681   aPF.Perform();
682   if( aPF.HasErrors())
683     return TopoDS_Shape();
684
685   BOPAlgo_Builder anAlgo;
686   anIt.Initialize( theFaces );
687   for( ; anIt.More(); anIt.Next() )
688     anAlgo.AddArgument( anIt.Value() );
689
690   anAlgo.PerformWithFiller( aPF ); 
691   if( anAlgo.HasErrors())
692     return TopoDS_Shape();
693
694   const TopoDS_Shape& aMergedShape = anAlgo.Shape();
695
696   // retrieve history of modifications
697   if (theShHistory)
698   {
699     theShHistory->Clear();
700     anIt.Initialize(theFaces);
701     for( ; anIt.More(); anIt.Next() )
702     {
703       const TopTools_ListOfShape aMLS = anAlgo.Modified( anIt.Value() );
704       theShHistory->Add(anIt.Value(), aMLS);
705     }
706   }
707   //
708
709   BRep_Builder aBuilder;
710   TopoDS_Shell aShell; 
711   aBuilder.MakeShell( aShell ); 
712   aShell.Closed( Standard_False );
713   TopExp_Explorer anExplorer( aMergedShape, TopAbs_FACE );
714   for( ; anExplorer.More(); anExplorer.Next() ) 
715   {
716     const TopoDS_Face& aFace = TopoDS::Face(anExplorer.Current());
717     if( aFace.IsNull() ) 
718       continue;
719     if( aFace.ShapeType() == TopAbs_FACE )
720     {
721       aBuilder.Add( aShell, aFace );
722       aShell.Closed( Standard_False );
723     }
724   }
725
726   TopoDS_Shape aResult;
727   if( IsToUnify )
728   {
729     ShapeUpgrade_UnifySameDomain aUSD;
730     aUSD.Initialize( aShell );
731     aUSD.Build();
732     aResult = aUSD.Shape();
733   }
734   else
735     aResult = aShell;
736
737   anExplorer.Init( aResult, TopAbs_FACE );
738   int n = 0;
739   TopoDS_Face anOneFace;
740   for( ; anExplorer.More(); anExplorer.Next(), n++ ) 
741     anOneFace = TopoDS::Face( anExplorer.Current() );
742
743   if (n == 1)
744     aResult = anOneFace;
745   else if (aResult.ShapeType() == TopAbs_SHELL)
746   {
747     BRepCheck_Shell aBCS(TopoDS::Shell(aResult));
748     if (aBCS.Status().First() != BRepCheck_NoError)
749     {
750       ShapeFix_Shell aFixer;
751       aFixer.FixFaceOrientation(TopoDS::Shell(aResult), 1);
752       aResult = aFixer.Shape();
753     }
754   }
755
756   return aResult;
757 }
758 /**
759   Change Strickler type for the list of faces to the given one
760   @param theFaces the faces to change type
761   @param theType the Strickler type for the given land cover(s)
762   @return if the change type operation is successful
763 */
764 bool HYDROData_LandCoverMap::ChangeType( const TopTools_ListOfShape& theFaces, const QString& theType )
765 {
766   HYDROData_MapOfFaceToStricklerType aFacesToChangeType;
767   TopTools_ListIteratorOfListOfShape aFIt( theFaces );
768   for( ; aFIt.More(); aFIt.Next() )
769   {
770     TopoDS_Shape aShape = aFIt.Value();
771     if( aShape.ShapeType()==TopAbs_FACE )
772       aFacesToChangeType.Add( TopoDS::Face( aShape ), "" );
773   }
774
775   int aNbChanges = 0;
776   Explorer anIt( *this );
777   for( ; anIt.More(); anIt.Next() )
778     if( aFacesToChangeType.Contains( anIt.Face() ) )
779     {
780       anIt.SetStricklerType( theType );
781       aNbChanges++;
782     }
783   if ( aNbChanges != theFaces.Extent() )
784     return false;
785
786   return true;
787 }
788
789 /**
790   Get the shape of the land cover map
791 */
792 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
793 {
794   return HYDROData_Entity::GetShape( DataTag_Shape );
795 }
796
797 /**
798   Get Strickler type of the given land cover
799   @param theLandCover the land cover to get Strickler type of
800   @return name of Strickler type
801 */
802 QString HYDROData_LandCoverMap::StricklerType( const TopoDS_Face& theLandCover ) const
803 {
804   QString aType = "";
805
806   Explorer anIt( *this );
807   for( ; anIt.More(); anIt.Next() )
808     if( anIt.Face().IsEqual( theLandCover) )
809     {
810       aType = anIt.StricklerType();
811       break;
812     }
813
814   return aType;
815 }
816
817 /**
818   Set the shape of the land cover map
819   @param theShape the new shape for the land cover map
820 */
821 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
822 {
823   HYDROData_Entity::SetShape( DataTag_Shape, theShape );
824 }
825
826 /**
827   Perform the local partition algorithm on the land cover
828   @param theNewShape the new shape to add into the land cover
829   @param theNewType the new Strickler type for the new land cover
830   @return if the local partition is successful
831 */
832 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
833 {
834   if( theNewShape.IsNull() )
835     return false;
836
837   TopTools_ListOfShape aShapesList;
838   BOPAlgo_PaveFiller aPaveFiller;
839   HYDROData_MapOfFaceToStricklerType aNewFaces;
840
841   // add faces to shapes list
842   Explorer anIt( *this );
843   for( ; anIt.More(); anIt.Next() )
844     aShapesList.Append( anIt.Face() );
845   aShapesList.Append( theNewShape );
846
847   //DEBTRACE("theNewType " << theNewType);
848   if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
849   {
850     aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
851     StoreLandCovers( aNewFaces );
852     return true;
853   }
854
855   // prepare pave filler
856   aPaveFiller.SetArguments( aShapesList );
857   aPaveFiller.Perform();
858   if( aPaveFiller.HasErrors())
859     return false;
860
861   // add faces to builder
862   BOPAlgo_Builder aBuilder;
863   anIt.Init( *this );
864   for( ; anIt.More(); anIt.Next() )
865     aBuilder.AddArgument( anIt.Face() );
866   aBuilder.AddArgument( theNewShape );
867
868   // perform the partition with the pave filler
869   aBuilder.PerformWithFiller( aPaveFiller );
870   if( aBuilder.HasErrors())
871     return false;
872
873   //std::cout << "History:" << std::endl;
874   // analysis of the history
875   //     a. to fill map of shapes which come from the new face
876   NCollection_IndexedMap<int> aShapesFromNewFace;
877   //std::cout << "from NEW " << theNewShape << ":" << theNewType << std::endl;
878   TopTools_ListOfShape aModified = aBuilder.Modified( theNewShape );
879   TopTools_ListIteratorOfListOfShape aMIt( aModified );
880   for( ; aMIt.More(); aMIt.Next() )
881   {
882     //std::cout << "   " << aMIt.Value() << std::endl;
883     //DEBTRACE(aMIt.Value());
884     int aKey = (int)(uintptr_t)aMIt.Value().TShape().operator->();
885     aShapesFromNewFace.Add( aKey );
886   }
887
888   //     b. to fill map of parts except parts from new face
889   anIt.Init( *this );
890   for( ; anIt.More(); anIt.Next() )
891   {
892     QString aSType = anIt.StricklerType();
893     //DEBTRACE(anIt.StricklerType() << " " << anIt.Face());
894     //std::cout << "from " << anIt.Face() << ": " << anIt.StricklerType() << std::endl;
895     TopTools_ListOfShape aModified = aBuilder.Modified( anIt.Face() );
896     //
897     if( aModified.Extent() == 0 )
898       aModified.Append( anIt.Face() );
899     //DEBTRACE(anIt.StricklerType() << " " << anIt.Face());
900
901     TopTools_ListIteratorOfListOfShape aMIt( aModified );
902     for( ; aMIt.More(); aMIt.Next() )
903     {
904       TopoDS_Shape aShape = aMIt.Value();
905       bool isFace = aShape.ShapeType()==TopAbs_FACE;
906       int aKey = (int)(uintptr_t)aShape.TShape().operator->();
907       bool isAlsoFromNew = aShapesFromNewFace.Contains( aKey );
908       //std::cout << "   " << aShape << " " << isAlsoFromNew << std::endl;
909       if( isFace && !isAlsoFromNew )
910         aNewFaces.Add( TopoDS::Face( aShape ), aSType );
911     }
912     //DEBTRACE(anIt.StricklerType() << " " << anIt.Face());
913   }
914   //     c. add the new shape if it is face with its type
915   if( theNewShape.ShapeType()==TopAbs_FACE )
916     aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
917   //DEBTRACE(theNewShape << " " << theNewType);
918   
919   // convert map of shape to type to compound and list of types
920   StoreLandCovers( aNewFaces );
921
922 //  anIt.Init( *this );
923 //  for( ; anIt.More(); anIt.Next() )
924 //  {
925 //     DEBTRACE(anIt.StricklerType() << " " << anIt.Face());
926 //  }
927   return true;
928 }
929
930 /**
931   Replace the set of land covers in the land cover map
932   @param theMap the map of shape (face) to Strickler type (string)
933 */
934 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
935 {
936   TopTools_ListOfShape aListOfFaces;
937
938   //DEBTRACE("theMap.Extent() " << theMap.Extent());
939   for( int i = 1; i <= theMap.Extent(); i++ )
940   {
941     TopoDS_Face aFace = theMap.FindKey(i);
942     if( aFace.IsNull() )
943       continue;
944     aListOfFaces.Append(aFace);
945   }
946
947   TopTools_IndexedDataMapOfShapeListOfShape ShHistory;
948   ShHistory.Clear();
949
950   TopoDS_Shape aResult;
951   if( aListOfFaces.Extent() == 1 )
952     aResult = aListOfFaces.First();
953   else if( aListOfFaces.Extent() > 1 )
954     aResult = MergeFaces( aListOfFaces, false, &ShHistory );
955
956   //remove internal edges
957   //if nothing changes => the result shape should be the same
958   //hence the map will be empty
959
960   NCollection_IndexedDataMap<TopoDS_Face, TopoDS_Face> ShF2FHistory;
961   RemoveInternal(aResult, &ShF2FHistory);
962
963   //one face => mark as unchanged
964   if( aListOfFaces.Extent() == 1 )
965     ShHistory.Add(aResult, TopTools_ListOfShape());
966
967   NCollection_IndexedDataMap<TopoDS_Face, QString> aChF2ST;
968   QStringList aSTypes;
969   //
970   for( int i = 1; i <= theMap.Extent(); i++ )
971   {
972     TopoDS_Face aFF = theMap.FindKey(i);
973     //DEBTRACE("  --- " << aFF);
974     if( aFF.IsNull() )
975       continue;
976     //DEBTRACE(ShHistory.IsEmpty());
977     //DEBTRACE(aFF.Checked());
978     TopTools_ListOfShape aLS;
979     try
980     {
981       aLS = ShHistory.FindFromKey(aFF); //TODO: bug to fix. Observed on an incomplete split of a face
982     }
983     catch (...)
984     {
985       DEBTRACE("TODO: bug to fix. Observed on an incomplete split of a face");
986       //continue; // No, keep aLS empty and propagate the type of the original face
987     }
988     if (aLS.IsEmpty())
989     {
990       //DEBTRACE("--- aLS.IsEmpty()");
991       QString aSType = theMap.FindFromKey(aFF);
992       //DEBTRACE("  --- " << aSType.toStdString());
993       if (ShF2FHistory.Contains(aFF))
994         {
995           //DEBTRACE("ShF2FHistory.FindFromKey(aFF) " << ShF2FHistory.FindFromKey(aFF));
996           aChF2ST.Add(ShF2FHistory.FindFromKey(aFF), aSType);
997         }
998       else
999         {
1000           //DEBTRACE("aFF " << aFF);
1001           aChF2ST.Add(aFF, aSType);
1002         }
1003     }
1004     else
1005     {
1006       //DEBTRACE("--- !aLS.IsEmpty()");
1007       TopTools_ListIteratorOfListOfShape anIt(aLS);
1008       for (; anIt.More(); anIt.Next())
1009       {
1010         QString aSType = theMap.FindFromKey(aFF);
1011         //DEBTRACE("  --- " << aSType.toStdString());
1012         const TopoDS_Face& aMF = TopoDS::Face(anIt.Value()); 
1013         //if (ShF2FHistory.Contains(aFF))
1014         if (ShF2FHistory.Contains(aMF))
1015           {
1016             //DEBTRACE("ShF2FHistory.FindFromKey(aMF) " << ShF2FHistory.FindFromKey(aFF));
1017             aChF2ST.Add(ShF2FHistory.FindFromKey(aMF), aSType);
1018           }
1019         else
1020           {
1021             //DEBTRACE("aMF " << aMF);
1022             aChF2ST.Add(aMF, aSType);
1023           }
1024       }
1025     }
1026   }
1027   //
1028   SetShape( aResult );
1029   //
1030   //Explorer Exp(*this);
1031   TopExp_Explorer FExp(aResult, TopAbs_FACE);
1032   for( ; FExp.More(); FExp.Next() )
1033   {
1034     TopoDS_Face aFace = TopoDS::Face(FExp.Current());
1035     QString aST = ""; 
1036     if (aChF2ST.Contains(aFace))
1037       aST = aChF2ST.FindFromKey(aFace);
1038     //DEBTRACE("aFace "  << aFace << " aST " << aST.toStdString());
1039     aSTypes << aST;
1040   }
1041   
1042   Handle( TDataStd_ExtStringArray ) aTypes = TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, aSTypes.size() - 1, Standard_True );
1043   aTypes->SetID(TDataStd_ExtStringArray::GetID());
1044   int k = 0;
1045   foreach (QString aST, aSTypes)
1046   {
1047     //DEBTRACE("aST " << aST.toStdString());
1048     aTypes->SetValue( k, HYDROData_Tool::toExtString( aST ) );
1049     k++;
1050   }
1051
1052 }
1053
1054 /**
1055    Checks that object has 2D presentation. Reimlemented to retun true.
1056 */
1057 bool HYDROData_LandCoverMap::IsHas2dPrs() const
1058 {
1059   return true;
1060 }
1061
1062 /**
1063   Find the land cover for the given point
1064   @param thePoint the point laying in some land cover
1065   @param theType the returned type
1066   @return the found land cover's face
1067 */
1068 TopoDS_Face HYDROData_LandCoverMap::FindByPoint( const gp_Pnt2d& thePoint, QString& theType ) const
1069 {
1070   //TODO: some more optimal algorithm
1071   Explorer anIt( *this );
1072   for( ; anIt.More(); anIt.Next() )
1073     if( HYDROData_Tool::ComputePointState( thePoint.XY(), anIt.Face() ) == TopAbs_IN )
1074     {
1075       theType = anIt.StricklerType();
1076       return anIt.Face();
1077     }
1078
1079   theType = "";
1080   return TopoDS_Face();
1081 }
1082
1083 void Dump( const QString& theName, const QStringList& theList, QStringList& theLines )
1084 {
1085   theLines.append( QString( "%1 = []" ).arg( theName ) );
1086   foreach( QString anItem, theList )
1087     theLines.append( QString( "%1.append( u\"%2\" )" ).arg( theName ).arg( anItem ) );
1088 }
1089
1090 /**
1091   Dump to Python
1092   @param theTreatedObjects the map of treated objects
1093 */
1094 QStringList HYDROData_LandCoverMap::DumpToPython( const QString&       thePyScriptPath,
1095                                                   MapOfTreatedObjects& theTreatedObjects ) const
1096 {
1097   QStringList aResList = dumpObjectCreation( theTreatedObjects );
1098   QString aName = GetObjPyName();
1099
1100   QString aShpFileName = thePyScriptPath;
1101   aShpFileName.replace( ".py", ".shp" );
1102   QString aDbfFileName = thePyScriptPath;
1103   aDbfFileName.replace( ".py", ".dbf" );
1104
1105   ExportSHP( aShpFileName, true, 0.1 );
1106
1107   QString anAttr = "CODE_06"; //TODO: some custom choice
1108   QStringList anAttrValues, aTypes;
1109   HYDROData_Document::Document( myLab )->CollectQGISValues( anAttr, anAttrValues, aTypes );
1110   ExportDBF( aDbfFileName, anAttr, anAttrValues, aTypes );
1111
1112   aResList << QString( "if not(%1.ImportSHP( '%2' )):" ).
1113     arg( aName ).arg( QFileInfo( aShpFileName ).fileName() );
1114   aResList << QString( "  raise ValueError('problem while loading LandCoverMap shape')" );
1115
1116   Dump( "attr_values", anAttrValues, aResList );
1117   Dump( "types", aTypes, aResList );
1118   aResList << QString( "if (%1.ImportDBF( '%2', '%3', attr_values, types ) != %1.DBFStatus_OK):" ).
1119     arg( aName ).arg( QFileInfo( aDbfFileName ).fileName() ).arg( anAttr );
1120   aResList << QString( "  raise ValueError('problem while loading LandCoverMap data base')" );
1121
1122   return aResList;
1123 }
1124
1125 void HYDROData_LandCoverMap::RemoveInternal(TopoDS_Shape& ShToRebuild, NCollection_IndexedDataMap<TopoDS_Face, TopoDS_Face>* aF2FReplace)
1126 {
1127   //DEBTRACE("RemoveInternal");
1128   //Shape must be topologically correct
1129   TopExp_Explorer anExpF(ShToRebuild, TopAbs_FACE);
1130   //
1131   for(; anExpF.More(); anExpF.Next() )
1132   {
1133     TopoDS_Face CurFace = TopoDS::Face(anExpF.Current());
1134     //
1135     TopExp_Explorer anExp(CurFace, TopAbs_EDGE);
1136     TopTools_ListOfShape anEdgesToRemove;
1137     //
1138     for(; anExp.More(); anExp.Next() )
1139     {
1140       TopoDS_Edge CurEdge = TopoDS::Edge(anExp.Current());
1141       if (CurEdge.Orientation() == TopAbs_INTERNAL)
1142         anEdgesToRemove.Append(CurEdge);
1143     }
1144     //
1145     if (!anEdgesToRemove.IsEmpty())
1146     {
1147       Handle(ShapeBuild_ReShape) aReshape = new ShapeBuild_ReShape();
1148       TopoDS_Shape OutF = aReshape->Apply(CurFace);
1149       TopTools_ListIteratorOfListOfShape aIt(anEdgesToRemove);
1150       for (; aIt.More(); aIt.Next()) 
1151         aReshape->Remove(aIt.Value());
1152       OutF = aReshape->Apply(CurFace);
1153
1154       Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
1155       sfs->Init(OutF);
1156       sfs->Perform();
1157       OutF = sfs->Shape();
1158       aF2FReplace->Add(CurFace, TopoDS::Face(OutF));
1159     }
1160   }
1161   //
1162   Handle(ShapeBuild_ReShape) anExtReshape = new ShapeBuild_ReShape();
1163   for (int i = 1; i <= aF2FReplace->Extent(); i++)
1164   {
1165     TopoDS_Face aFK = aF2FReplace->FindKey(i);
1166     TopoDS_Face aFV = aF2FReplace->FindFromIndex(i);
1167     anExtReshape->Replace(aFK, aFV);
1168     ShToRebuild = anExtReshape->Apply(ShToRebuild);
1169   }
1170 //  for (int i = 1; i <= aF2FReplace->Extent(); i++)
1171 //    {
1172 //      DEBTRACE("aF2FReplace key,value " << aF2FReplace->FindKey(i) << " " << aF2FReplace->FindFromIndex(i));
1173 //    }
1174
1175 }
1176
1177 void HYDROData_LandCoverMap::SetTransparency( double theTransparency )
1178 {
1179   SetDouble( DataTag_Transparency, theTransparency );
1180 }
1181
1182 double HYDROData_LandCoverMap::GetTransparency() const
1183 {
1184   return GetDouble( DataTag_Transparency, 0.25 );
1185 }
1186
1187 bool HYDROData_LandCoverMap::ImportSHP( const QString& theSHPFileName,
1188                                         const QList<int>& theIndices )
1189 {
1190   HYDROData_ShapeFile anImporter;
1191   QStringList aPolyList; 
1192   TopTools_SequenceOfShape aFaces;
1193   //int aSHapeType = -1;
1194   int Stat = anImporter.ImportPolygons(HYDROData_Document::Document(), theSHPFileName, aPolyList, aFaces);
1195   //
1196   if (Stat != 1)
1197     return false;
1198   //
1199   HYDROData_MapOfFaceToStricklerType aMapFace2ST;
1200   int maxInd = *std::max_element(theIndices.begin(), theIndices.end());
1201   if (maxInd > aPolyList.length())
1202     return false;
1203   //
1204   if (theIndices.empty())
1205   {
1206     //import all shapes
1207     for ( int i = 1; i <=aFaces.Length(); i++ )
1208     {
1209       TopoDS_Shape aShape = aFaces(i);
1210       if ( aShape.IsNull() ) 
1211         continue;
1212       aMapFace2ST.Add( TopoDS::Face( aShape ), "" );
1213     }
1214   }
1215   else
1216   {
1217     //import given indices
1218     foreach ( int Ind, theIndices )
1219     {
1220       TopoDS_Shape aShape = aFaces(Ind + 1);
1221       if ( aShape.IsNull() ) 
1222         continue;
1223       aMapFace2ST.Add( TopoDS::Face( aShape ), "" );
1224     }
1225   }
1226   //
1227   StoreLandCovers(aMapFace2ST);
1228   return true;
1229 }
1230
1231 bool HYDROData_LandCoverMap::ExportSHP( const QString& theSHPFileName, bool bUseDiscr, double theDefl) const
1232 {
1233   HYDROData_ShapeFile anExporter;
1234   QStringList aList;
1235   anExporter.Export(HYDROData_Document::Document(), theSHPFileName, this, aList, bUseDiscr, theDefl );
1236   if (aList.empty())
1237     return true;
1238   else 
1239     return false;
1240 }
1241
1242 bool HYDROData_LandCoverMap::CheckLinear()
1243 {
1244   TopoDS_Shape InpShape = GetShape();
1245   TopExp_Explorer anEdgeEx(InpShape, TopAbs_EDGE);
1246   for (; anEdgeEx.More(); anEdgeEx.Next()) 
1247   {
1248     TopoDS_Edge E = TopoDS::Edge(anEdgeEx.Current());
1249     double aFP, aLP;
1250     Handle(Geom_Curve) aCur = BRep_Tool::Curve(E, aFP, aLP);
1251     Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCur);
1252     if (aLine.IsNull())
1253     {
1254       Handle(Geom_TrimmedCurve) aTC = Handle(Geom_TrimmedCurve)::DownCast(aCur);
1255       if (!aTC.IsNull())
1256       {
1257         Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aTC->BasisCurve());
1258         if (aLine.IsNull())
1259           return false;
1260       }
1261       else
1262         return false;
1263     }
1264   }
1265   return true;
1266 }
1267
1268 void HYDROData_LandCoverMap::UpdateLocalCS( double theDx, double theDy )
1269 {
1270   TopoDS_Shape aShape = GetShape();
1271   TopoDS_Shape aLocatedShape = HYDROData_ShapesTool::Translated( aShape, theDx, theDy, 0 );
1272   SetShape( aLocatedShape );
1273 }
1274
1275 void HYDROData_LandCoverMap::ClassifyPoints( const std::vector<gp_XY>& thePoints,
1276                                              std::vector<std::set <QString> >& theTypes ) const
1277 {
1278   HYDROData_LCM_FaceClassifier FC(this);
1279   FC.Classify(thePoints, theTypes, NULL);
1280 }
1281
1282 void HYDROData_LandCoverMap::ClassifyPoints( const std::vector<gp_XY>& thePoints,
1283                                              const Handle(HYDROData_StricklerTable)& theTable,
1284                                              std::vector<int>& theTypes ) const
1285 {
1286   std::vector<std::set <QString> > types;
1287   HYDROData_LCM_FaceClassifier FC(this);
1288   FC.Classify(thePoints, types, NULL);
1289
1290   size_t n = types.size();
1291   theTypes.resize( n );
1292   for( size_t i=0; i<n; i++ )
1293   {
1294     const std::set<QString>& sub_types = types[i];
1295     int aType = 0;
1296     if( !sub_types.empty() )
1297     {
1298       const QString& aSType = *sub_types.begin();
1299       aType = theTable->GetAttrValue( aSType ).toInt();
1300     }
1301     theTypes[i] = aType;
1302   }
1303 }
1304
1305 void HYDROData_LandCoverMap::ClassifyPoints( const std::vector<gp_XY>& thePoints,  
1306   const Handle(HYDROData_StricklerTable)& theTable, 
1307   std::vector<double>& theCoeffs, double DefValue, bool UseMax ) const
1308 {
1309   Message_ProgressSentry aPSentryStages(HYDROData_Tool::GetSIProgress(), "ClassifyPoints", 0, 2, 1);
1310
1311   std::vector<std::set <QString> > Types;
1312   HYDROData_LCM_FaceClassifier FC(this);
1313   FC.Classify(thePoints, Types, NULL);
1314
1315   aPSentryStages.Next();
1316
1317   theCoeffs.reserve(thePoints.size());
1318
1319   Message_ProgressSentry aPSentryCoeff(HYDROData_Tool::GetSIProgress(), "Assign coefficients", 0, Types.size() - 1, 1);
1320
1321   for (size_t i = 0; i < Types.size() && aPSentryCoeff.More(); i++, aPSentryCoeff.Next())
1322   {
1323     const std::set<QString>& SStr = Types[i];
1324     if (SStr.empty())
1325       theCoeffs.push_back( DefValue );
1326     else
1327     {
1328       std::set<QString>::const_iterator it;
1329       std::vector<double> C1;
1330       for (it = SStr.begin(); it != SStr.end(); ++it)
1331         C1.push_back(theTable->Get( *it, DefValue ));
1332       double Val;
1333       if (UseMax)
1334         Val = *(std::max_element( C1.begin(), C1.end() ) );
1335       else
1336         Val = *(std::min_element( C1.begin(), C1.end() ) );
1337       theCoeffs.push_back( Val );
1338     }
1339   }
1340
1341   aPSentryStages.Next();
1342 }