]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_LandCoverMap.cxx
Salome HOME
lots 3,8
[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 <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   TopTools_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   if( aPF.HasErrors())
682     return TopoDS_Shape();
683
684   BOPAlgo_Builder anAlgo;
685   anIt.Initialize( theFaces );
686   for( ; anIt.More(); anIt.Next() )
687     anAlgo.AddArgument( anIt.Value() );
688
689   anAlgo.PerformWithFiller( aPF ); 
690   if( anAlgo.HasErrors())
691     return TopoDS_Shape();
692
693   const TopoDS_Shape& aMergedShape = anAlgo.Shape();
694
695   // retrieve history of modifications
696   if (theShHistory)
697   {
698     theShHistory->Clear();
699     anIt.Initialize(theFaces);
700     for( ; anIt.More(); anIt.Next() )
701     {
702       const TopTools_ListOfShape aMLS = anAlgo.Modified( anIt.Value() );
703       theShHistory->Add(anIt.Value(), aMLS);
704     }
705   }
706   //
707
708   BRep_Builder aBuilder;
709   TopoDS_Shell aShell; 
710   aBuilder.MakeShell( aShell ); 
711   aShell.Closed( Standard_False );
712   TopExp_Explorer anExplorer( aMergedShape, TopAbs_FACE );
713   for( ; anExplorer.More(); anExplorer.Next() ) 
714   {
715     const TopoDS_Face& aFace = TopoDS::Face(anExplorer.Current());
716     if( aFace.IsNull() ) 
717       continue;
718     if( aFace.ShapeType() == TopAbs_FACE )
719     {
720       aBuilder.Add( aShell, aFace );
721       aShell.Closed( Standard_False );
722     }
723   }
724
725   TopoDS_Shape aResult;
726   if( IsToUnify )
727   {
728     ShapeUpgrade_UnifySameDomain aUSD;
729     aUSD.Initialize( aShell );
730     aUSD.Build();
731     aResult = aUSD.Shape();
732   }
733   else
734     aResult = aShell;
735
736   anExplorer.Init( aResult, TopAbs_FACE );
737   int n = 0;
738   TopoDS_Face anOneFace;
739   for( ; anExplorer.More(); anExplorer.Next(), n++ ) 
740     anOneFace = TopoDS::Face( anExplorer.Current() );
741
742   if (n == 1)
743     aResult = anOneFace;
744   else if (aResult.ShapeType() == TopAbs_SHELL)
745   {
746     BRepCheck_Shell aBCS(TopoDS::Shell(aResult));
747     if (aBCS.Status().First() != BRepCheck_NoError)
748     {
749       ShapeFix_Shell aFixer;
750       aFixer.FixFaceOrientation(TopoDS::Shell(aResult), 1);
751       aResult = aFixer.Shape();
752     }
753   }
754
755   return aResult;
756 }
757 /**
758   Change Strickler type for the list of faces to the given one
759   @param theFaces the faces to change type
760   @param theType the Strickler type for the given land cover(s)
761   @return if the change type operation is successful
762 */
763 bool HYDROData_LandCoverMap::ChangeType( const TopTools_ListOfShape& theFaces, const QString& theType )
764 {
765   HYDROData_MapOfFaceToStricklerType aFacesToChangeType;
766   TopTools_ListIteratorOfListOfShape aFIt( theFaces );
767   for( ; aFIt.More(); aFIt.Next() )
768   {
769     TopoDS_Shape aShape = aFIt.Value();
770     if( aShape.ShapeType()==TopAbs_FACE )
771       aFacesToChangeType.Add( TopoDS::Face( aShape ), "" );
772   }
773
774   int aNbChanges = 0;
775   Explorer anIt( *this );
776   for( ; anIt.More(); anIt.Next() )
777     if( aFacesToChangeType.Contains( anIt.Face() ) )
778     {
779       anIt.SetStricklerType( theType );
780       aNbChanges++;
781     }
782   if ( aNbChanges != theFaces.Extent() )
783     return false;
784
785   return true;
786 }
787
788 /**
789   Get the shape of the land cover map
790 */
791 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
792 {
793   return HYDROData_Entity::GetShape( DataTag_Shape );
794 }
795
796 /**
797   Get Strickler type of the given land cover
798   @param theLandCover the land cover to get Strickler type of
799   @return name of Strickler type
800 */
801 QString HYDROData_LandCoverMap::StricklerType( const TopoDS_Face& theLandCover ) const
802 {
803   QString aType = "";
804
805   Explorer anIt( *this );
806   for( ; anIt.More(); anIt.Next() )
807     if( anIt.Face().IsEqual( theLandCover) )
808     {
809       aType = anIt.StricklerType();
810       break;
811     }
812
813   return aType;
814 }
815
816 /**
817   Set the shape of the land cover map
818   @param theShape the new shape for the land cover map
819 */
820 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
821 {
822   HYDROData_Entity::SetShape( DataTag_Shape, theShape );
823 }
824
825 /**
826   Perform the local partition algorithm on the land cover
827   @param theNewShape the new shape to add into the land cover
828   @param theNewType the new Strickler type for the new land cover
829   @return if the local partition is successful
830 */
831 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
832 {
833   if( theNewShape.IsNull() )
834     return false;
835
836   TopTools_ListOfShape aShapesList;
837   BOPAlgo_PaveFiller aPaveFiller;
838   HYDROData_MapOfFaceToStricklerType aNewFaces;
839
840   // add faces to shapes list
841   Explorer anIt( *this );
842   for( ; anIt.More(); anIt.Next() )
843     aShapesList.Append( anIt.Face() );
844   aShapesList.Append( theNewShape );
845
846   //DEBTRACE("theNewType " << theNewType);
847   if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
848   {
849     aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
850     StoreLandCovers( aNewFaces );
851     return true;
852   }
853
854   // prepare pave filler
855   aPaveFiller.SetArguments( aShapesList );
856   aPaveFiller.Perform();
857   if( aPaveFiller.HasErrors())
858     return false;
859
860   // add faces to builder
861   BOPAlgo_Builder aBuilder;
862   anIt.Init( *this );
863   for( ; anIt.More(); anIt.Next() )
864     aBuilder.AddArgument( anIt.Face() );
865   aBuilder.AddArgument( theNewShape );
866
867   // perform the partition with the pave filler
868   aBuilder.PerformWithFiller( aPaveFiller );
869   if( aBuilder.HasErrors())
870     return false;
871
872   //std::cout << "History:" << std::endl;
873   // analysis of the history
874   //     a. to fill map of shapes which come from the new face
875   NCollection_IndexedMap<int> aShapesFromNewFace;
876   //std::cout << "from NEW " << theNewShape << ":" << theNewType << std::endl;
877   TopTools_ListOfShape aModified = aBuilder.Modified( theNewShape );
878   TopTools_ListIteratorOfListOfShape aMIt( aModified );
879   for( ; aMIt.More(); aMIt.Next() )
880   {
881     //std::cout << "   " << aMIt.Value() << std::endl;
882     //DEBTRACE(aMIt.Value());
883     int aKey = (int)(uintptr_t)aMIt.Value().TShape().operator->();
884     aShapesFromNewFace.Add( aKey );
885   }
886
887   //     b. to fill map of parts except parts from new face
888   anIt.Init( *this );
889   for( ; anIt.More(); anIt.Next() )
890   {
891     QString aSType = anIt.StricklerType();
892     //DEBTRACE(anIt.StricklerType() << " " << anIt.Face());
893     //std::cout << "from " << anIt.Face() << ": " << anIt.StricklerType() << std::endl;
894     TopTools_ListOfShape aModified = aBuilder.Modified( anIt.Face() );
895     //
896     if( aModified.Extent() == 0 )
897       aModified.Append( anIt.Face() );
898     //DEBTRACE(anIt.StricklerType() << " " << anIt.Face());
899
900     TopTools_ListIteratorOfListOfShape aMIt( aModified );
901     for( ; aMIt.More(); aMIt.Next() )
902     {
903       TopoDS_Shape aShape = aMIt.Value();
904       bool isFace = aShape.ShapeType()==TopAbs_FACE;
905       int aKey = (int)(uintptr_t)aShape.TShape().operator->();
906       bool isAlsoFromNew = aShapesFromNewFace.Contains( aKey );
907       //std::cout << "   " << aShape << " " << isAlsoFromNew << std::endl;
908       if( isFace && !isAlsoFromNew )
909         aNewFaces.Add( TopoDS::Face( aShape ), aSType );
910     }
911     //DEBTRACE(anIt.StricklerType() << " " << anIt.Face());
912   }
913   //     c. add the new shape if it is face with its type
914   if( theNewShape.ShapeType()==TopAbs_FACE )
915     aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
916   //DEBTRACE(theNewShape << " " << theNewType);
917   
918   // convert map of shape to type to compound and list of types
919   StoreLandCovers( aNewFaces );
920
921 //  anIt.Init( *this );
922 //  for( ; anIt.More(); anIt.Next() )
923 //  {
924 //     DEBTRACE(anIt.StricklerType() << " " << anIt.Face());
925 //  }
926   return true;
927 }
928
929 /**
930   Replace the set of land covers in the land cover map
931   @param theMap the map of shape (face) to Strickler type (string)
932 */
933 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
934 {
935   TopTools_ListOfShape aListOfFaces;
936
937   //DEBTRACE("theMap.Extent() " << theMap.Extent());
938   for( int i = 1; i <= theMap.Extent(); i++ )
939   {
940     TopoDS_Face aFace = theMap.FindKey(i);
941     if( aFace.IsNull() )
942       continue;
943     aListOfFaces.Append(aFace);
944   }
945
946   TopTools_IndexedDataMapOfShapeListOfShape ShHistory;
947   ShHistory.Clear();
948
949   TopoDS_Shape aResult;
950   if( aListOfFaces.Extent() == 1 )
951     aResult = aListOfFaces.First();
952   else if( aListOfFaces.Extent() > 1 )
953     aResult = MergeFaces( aListOfFaces, false, &ShHistory );
954
955   //remove internal edges
956   //if nothing changes => the result shape should be the same
957   //hence the map will be empty
958
959   NCollection_IndexedDataMap<TopoDS_Face, TopoDS_Face> ShF2FHistory;
960   RemoveInternal(aResult, &ShF2FHistory);
961
962   //one face => mark as unchanged
963   if( aListOfFaces.Extent() == 1 )
964     ShHistory.Add(aResult, TopTools_ListOfShape());
965
966   NCollection_IndexedDataMap<TopoDS_Face, QString> aChF2ST;
967   QStringList aSTypes;
968   //
969   for( int i = 1; i <= theMap.Extent(); i++ )
970   {
971     TopoDS_Face aFF = theMap.FindKey(i);
972     //DEBTRACE("  --- " << aFF);
973     if( aFF.IsNull() )
974       continue;
975     //DEBTRACE(ShHistory.IsEmpty());
976     //DEBTRACE(aFF.Checked());
977     TopTools_ListOfShape aLS;
978     try
979     {
980       aLS = ShHistory.FindFromKey(aFF); //TODO: bug to fix. Observed on an incomplete split of a face
981     }
982     catch (...)
983     {
984       DEBTRACE("TODO: bug to fix. Observed on an incomplete split of a face");
985       //continue; // No, keep aLS empty and propagate the type of the original face
986     }
987     if (aLS.IsEmpty())
988     {
989       //DEBTRACE("--- aLS.IsEmpty()");
990       QString aSType = theMap.FindFromKey(aFF);
991       //DEBTRACE("  --- " << aSType.toStdString());
992       if (ShF2FHistory.Contains(aFF))
993         {
994           //DEBTRACE("ShF2FHistory.FindFromKey(aFF) " << ShF2FHistory.FindFromKey(aFF));
995           aChF2ST.Add(ShF2FHistory.FindFromKey(aFF), aSType);
996         }
997       else
998         {
999           //DEBTRACE("aFF " << aFF);
1000           aChF2ST.Add(aFF, aSType);
1001         }
1002     }
1003     else
1004     {
1005       //DEBTRACE("--- !aLS.IsEmpty()");
1006       TopTools_ListIteratorOfListOfShape anIt(aLS);
1007       for (; anIt.More(); anIt.Next())
1008       {
1009         QString aSType = theMap.FindFromKey(aFF);
1010         //DEBTRACE("  --- " << aSType.toStdString());
1011         const TopoDS_Face& aMF = TopoDS::Face(anIt.Value()); 
1012         //if (ShF2FHistory.Contains(aFF))
1013         if (ShF2FHistory.Contains(aMF))
1014           {
1015             //DEBTRACE("ShF2FHistory.FindFromKey(aMF) " << ShF2FHistory.FindFromKey(aFF));
1016             aChF2ST.Add(ShF2FHistory.FindFromKey(aMF), aSType);
1017           }
1018         else
1019           {
1020             //DEBTRACE("aMF " << aMF);
1021             aChF2ST.Add(aMF, aSType);
1022           }
1023       }
1024     }
1025   }
1026   //
1027   SetShape( aResult );
1028   //
1029   //Explorer Exp(*this);
1030   TopExp_Explorer FExp(aResult, TopAbs_FACE);
1031   for( ; FExp.More(); FExp.Next() )
1032   {
1033     TopoDS_Face aFace = TopoDS::Face(FExp.Current());
1034     QString aST = ""; 
1035     if (aChF2ST.Contains(aFace))
1036       aST = aChF2ST.FindFromKey(aFace);
1037     //DEBTRACE("aFace "  << aFace << " aST " << aST.toStdString());
1038     aSTypes << aST;
1039   }
1040   
1041   Handle( TDataStd_ExtStringArray ) aTypes = TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, aSTypes.size() - 1, Standard_True );
1042   aTypes->SetID(TDataStd_ExtStringArray::GetID());
1043   int k = 0;
1044   foreach (QString aST, aSTypes)
1045   {
1046     //DEBTRACE("aST " << aST.toStdString());
1047     aTypes->SetValue( k, HYDROData_Tool::toExtString( aST ) );
1048     k++;
1049   }
1050
1051 }
1052
1053 /**
1054    Checks that object has 2D presentation. Reimlemented to retun true.
1055 */
1056 bool HYDROData_LandCoverMap::IsHas2dPrs() const
1057 {
1058   return true;
1059 }
1060
1061 /**
1062   Find the land cover for the given point
1063   @param thePoint the point laying in some land cover
1064   @param theType the returned type
1065   @return the found land cover's face
1066 */
1067 TopoDS_Face HYDROData_LandCoverMap::FindByPoint( const gp_Pnt2d& thePoint, QString& theType ) const
1068 {
1069   //TODO: some more optimal algorithm
1070   Explorer anIt( *this );
1071   for( ; anIt.More(); anIt.Next() )
1072     if( HYDROData_Tool::ComputePointState( thePoint.XY(), anIt.Face() ) == TopAbs_IN )
1073     {
1074       theType = anIt.StricklerType();
1075       return anIt.Face();
1076     }
1077
1078   theType = "";
1079   return TopoDS_Face();
1080 }
1081
1082 void Dump( const QString& theName, const QStringList& theList, QStringList& theLines )
1083 {
1084   theLines.append( QString( "%1 = []" ).arg( theName ) );
1085   foreach( QString anItem, theList )
1086     theLines.append( QString( "%1.append( u\"%2\" )" ).arg( theName ).arg( anItem ) );
1087 }
1088
1089 /**
1090   Dump to Python
1091   @param theTreatedObjects the map of treated objects
1092 */
1093 QStringList HYDROData_LandCoverMap::DumpToPython( const QString&       thePyScriptPath,
1094                                                   MapOfTreatedObjects& theTreatedObjects ) const
1095 {
1096   QStringList aResList = dumpObjectCreation( theTreatedObjects );
1097   QString aName = GetObjPyName();
1098
1099   QString aShpFileName = thePyScriptPath;
1100   aShpFileName.replace( ".py", ".shp" );
1101   QString aDbfFileName = thePyScriptPath;
1102   aDbfFileName.replace( ".py", ".dbf" );
1103
1104   ExportSHP( aShpFileName, true, 0.1 );
1105
1106   QString anAttr = "CODE_06"; //TODO: some custom choice
1107   QStringList anAttrValues, aTypes;
1108   HYDROData_Document::Document( myLab )->CollectQGISValues( anAttr, anAttrValues, aTypes );
1109   ExportDBF( aDbfFileName, anAttr, anAttrValues, aTypes );
1110
1111   aResList << QString( "if not(%1.ImportSHP( '%2' )):" ).
1112     arg( aName ).arg( QFileInfo( aShpFileName ).fileName() );
1113   aResList << QString( "  raise ValueError('problem while loading LandCoverMap shape')" );
1114
1115   Dump( "attr_values", anAttrValues, aResList );
1116   Dump( "types", aTypes, aResList );
1117   aResList << QString( "if (%1.ImportDBF( '%2', '%3', attr_values, types ) != %1.DBFStatus_OK):" ).
1118     arg( aName ).arg( QFileInfo( aDbfFileName ).fileName() ).arg( anAttr );
1119   aResList << QString( "  raise ValueError('problem while loading LandCoverMap data base')" );
1120
1121   return aResList;
1122 }
1123
1124 void HYDROData_LandCoverMap::RemoveInternal(TopoDS_Shape& ShToRebuild, NCollection_IndexedDataMap<TopoDS_Face, TopoDS_Face>* aF2FReplace)
1125 {
1126   //DEBTRACE("RemoveInternal");
1127   //Shape must be topologically correct
1128   TopExp_Explorer anExpF(ShToRebuild, TopAbs_FACE);
1129   //
1130   for(; anExpF.More(); anExpF.Next() )
1131   {
1132     TopoDS_Face CurFace = TopoDS::Face(anExpF.Current());
1133     //
1134     TopExp_Explorer anExp(CurFace, TopAbs_EDGE);
1135     TopTools_ListOfShape anEdgesToRemove;
1136     //
1137     for(; anExp.More(); anExp.Next() )
1138     {
1139       TopoDS_Edge CurEdge = TopoDS::Edge(anExp.Current());
1140       if (CurEdge.Orientation() == TopAbs_INTERNAL)
1141         anEdgesToRemove.Append(CurEdge);
1142     }
1143     //
1144     if (!anEdgesToRemove.IsEmpty())
1145     {
1146       Handle(ShapeBuild_ReShape) aReshape = new ShapeBuild_ReShape();
1147       TopoDS_Shape OutF = aReshape->Apply(CurFace);
1148       TopTools_ListIteratorOfListOfShape aIt(anEdgesToRemove);
1149       for (; aIt.More(); aIt.Next()) 
1150         aReshape->Remove(aIt.Value());
1151       OutF = aReshape->Apply(CurFace);
1152
1153       Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
1154       sfs->Init(OutF);
1155       sfs->Perform();
1156       OutF = sfs->Shape();
1157       aF2FReplace->Add(CurFace, TopoDS::Face(OutF));
1158     }
1159   }
1160   //
1161   Handle(ShapeBuild_ReShape) anExtReshape = new ShapeBuild_ReShape();
1162   for (int i = 1; i <= aF2FReplace->Extent(); i++)
1163   {
1164     TopoDS_Face aFK = aF2FReplace->FindKey(i);
1165     TopoDS_Face aFV = aF2FReplace->FindFromIndex(i);
1166     anExtReshape->Replace(aFK, aFV);
1167     ShToRebuild = anExtReshape->Apply(ShToRebuild);
1168   }
1169 //  for (int i = 1; i <= aF2FReplace->Extent(); i++)
1170 //    {
1171 //      DEBTRACE("aF2FReplace key,value " << aF2FReplace->FindKey(i) << " " << aF2FReplace->FindFromIndex(i));
1172 //    }
1173
1174 }
1175
1176 void HYDROData_LandCoverMap::SetTransparency( double theTransparency )
1177 {
1178   SetDouble( DataTag_Transparency, theTransparency );
1179 }
1180
1181 double HYDROData_LandCoverMap::GetTransparency() const
1182 {
1183   return GetDouble( DataTag_Transparency, 0.25 );
1184 }
1185
1186 bool HYDROData_LandCoverMap::ImportSHP( const QString& theSHPFileName,
1187                                         const QList<int>& theIndices )
1188 {
1189   HYDROData_ShapeFile anImporter;
1190   QStringList aPolyList; 
1191   TopTools_SequenceOfShape aFaces;
1192   int aSHapeType = -1;
1193   int Stat = anImporter.ImportPolygons(HYDROData_Document::Document(1), theSHPFileName, aPolyList, aFaces, aSHapeType);
1194   //
1195   if (Stat != 1)
1196     return false;
1197   //
1198   HYDROData_MapOfFaceToStricklerType aMapFace2ST;
1199   int maxInd = *std::max_element(theIndices.begin(), theIndices.end());
1200   if (maxInd > aPolyList.length())
1201     return false;
1202   //
1203   if (theIndices.empty())
1204   {
1205     //import all shapes
1206     for ( int i = 1; i <=aFaces.Length(); i++ )
1207     {
1208       TopoDS_Shape aShape = aFaces(i);
1209       if ( aShape.IsNull() ) 
1210         continue;
1211       aMapFace2ST.Add( TopoDS::Face( aShape ), "" );
1212     }
1213   }
1214   else
1215   {
1216     //import given indices
1217     foreach ( int Ind, theIndices )
1218     {
1219       TopoDS_Shape aShape = aFaces(Ind + 1);
1220       if ( aShape.IsNull() ) 
1221         continue;
1222       aMapFace2ST.Add( TopoDS::Face( aShape ), "" );
1223     }
1224   }
1225   //
1226   StoreLandCovers(aMapFace2ST);
1227   return true;
1228 }
1229
1230 bool HYDROData_LandCoverMap::ExportSHP( const QString& theSHPFileName, bool bUseDiscr, double theDefl) const
1231 {
1232   HYDROData_ShapeFile anExporter;
1233   QStringList aList;
1234   anExporter.Export(HYDROData_Document::Document(1), theSHPFileName, this, aList, bUseDiscr, theDefl );
1235   if (aList.empty())
1236     return true;
1237   else 
1238     return false;
1239 }
1240
1241 bool HYDROData_LandCoverMap::CheckLinear()
1242 {
1243   TopoDS_Shape InpShape = GetShape();
1244   TopExp_Explorer anEdgeEx(InpShape, TopAbs_EDGE);
1245   for (; anEdgeEx.More(); anEdgeEx.Next()) 
1246   {
1247     TopoDS_Edge E = TopoDS::Edge(anEdgeEx.Current());
1248     double aFP, aLP;
1249     Handle(Geom_Curve) aCur = BRep_Tool::Curve(E, aFP, aLP);
1250     Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCur);
1251     if (aLine.IsNull())
1252     {
1253       Handle(Geom_TrimmedCurve) aTC = Handle(Geom_TrimmedCurve)::DownCast(aCur);
1254       if (!aTC.IsNull())
1255       {
1256         Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aTC->BasisCurve());
1257         if (aLine.IsNull())
1258           return false;
1259       }
1260       else
1261         return false;
1262     }
1263   }
1264   return true;
1265 }
1266
1267 void HYDROData_LandCoverMap::UpdateLocalCS( double theDx, double theDy )
1268 {
1269   TopoDS_Shape aShape = GetShape();
1270   TopoDS_Shape aLocatedShape = HYDROData_ShapesTool::Translated( aShape, theDx, theDy, 0 );
1271   SetShape( aLocatedShape );
1272 }
1273
1274 void HYDROData_LandCoverMap::ClassifyPoints( const std::vector<gp_XY>& thePoints,
1275                                              std::vector<std::set <QString> >& theTypes ) const
1276 {
1277   HYDROData_LCM_FaceClassifier FC(this);
1278   FC.Classify(thePoints, theTypes, NULL);
1279 }
1280
1281 void HYDROData_LandCoverMap::ClassifyPoints( const std::vector<gp_XY>& thePoints,
1282                                              const Handle(HYDROData_StricklerTable)& theTable,
1283                                              std::vector<int>& theTypes ) const
1284 {
1285   std::vector<std::set <QString> > types;
1286   HYDROData_LCM_FaceClassifier FC(this);
1287   FC.Classify(thePoints, types, NULL);
1288
1289   size_t n = types.size();
1290   theTypes.resize( n );
1291   for( size_t i=0; i<n; i++ )
1292   {
1293     const std::set<QString>& sub_types = types[i];
1294     int aType = 0;
1295     if( !sub_types.empty() )
1296     {
1297       const QString& aSType = *sub_types.begin();
1298       aType = theTable->GetAttrValue( aSType ).toInt();
1299     }
1300     theTypes[i] = aType;
1301   }
1302 }
1303
1304 void HYDROData_LandCoverMap::ClassifyPoints( const std::vector<gp_XY>& thePoints,  
1305   const Handle(HYDROData_StricklerTable)& theTable, 
1306   std::vector<double>& theCoeffs, double DefValue, bool UseMax ) const
1307 {
1308   std::vector<std::set <QString> > Types;
1309   HYDROData_LCM_FaceClassifier FC(this);
1310   FC.Classify(thePoints, Types, NULL);
1311   theCoeffs.resize(thePoints.size());
1312   for (size_t i = 0; i < Types.size(); i++)
1313   {
1314     const std::set<QString>& SStr = Types[i];
1315     if (SStr.empty())
1316       theCoeffs[i] = DefValue;
1317     else
1318     {
1319       std::set<QString>::const_iterator it;
1320       std::vector<double> C1;
1321       for (it = SStr.begin(); it != SStr.end(); ++it)
1322         C1.push_back(theTable->Get( *it, DefValue ));
1323       double Val;
1324       if (UseMax)
1325         Val = *(std::max_element( C1.begin(), C1.end() ) );
1326       else
1327         Val = *(std::min_element( C1.begin(), C1.end() ) );
1328       theCoeffs[i] = Val;
1329     }
1330   }
1331 }