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