Salome HOME
Merge branch 'BR_LAND_COVER_MAP' of ssh://git.salome-platform.org/modules/hydro into...
[modules/hydro.git] / src / HYDROData / HYDROData_ShapeFile.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <HYDROData_ShapeFile.h>
24 #include <HYDROData_PolylineXY.h>
25 #include <HYDROData_Polyline3D.h>
26 #include <HYDROData_Bathymetry.h>
27 #include <HYDROData_Profile.h>
28 #include <HYDROData_Iterator.h>
29
30 #include <QFile>
31 #include <QFileInfo>
32 #include <TopoDS.hxx>
33 #include <TopExp_Explorer.hxx>
34 #include <TopoDS_Wire.hxx>
35 #include <TopoDS_Vertex.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <TopoDS_Face.hxx>
38 #include <BRep_Tool.hxx>
39 #include <Precision.hxx>
40 #include <Handle_Geom_Curve.hxx>
41 #include <Handle_Geom_Line.hxx>
42 #include <Handle_Geom_TrimmedCurve.hxx>
43 #include <Geom_TrimmedCurve.hxx>
44 #include <BRepBuilderAPI_MakeEdge.hxx>
45 #include <BRepBuilderAPI_MakeWire.hxx>
46 #include <BRepBuilderAPI_MakeFace.hxx>
47 #include <gp_Pln.hxx>
48 #include <BRepLib.hxx>
49 #include <ShapeFix_Shape.hxx>
50 #include <TopTools_SequenceOfShape.hxx>
51 #include <QColor>
52 #include <BRepTopAdaptor_FClass2d.hxx>
53
54 #ifdef WIN32
55   #pragma warning( disable: 4996 )
56 #endif
57
58 HYDROData_ShapeFile::HYDROData_ShapeFile() : myHSHP(NULL)
59
60 }
61
62 HYDROData_ShapeFile::~HYDROData_ShapeFile()
63 {
64   Free();
65 }
66
67 void HYDROData_ShapeFile::Export(const QString& aFileName, 
68   NCollection_Sequence<Handle_HYDROData_PolylineXY> aPolyXYSeq,
69   NCollection_Sequence<Handle_HYDROData_Polyline3D> aPoly3DSeq,
70   const Handle_HYDROData_LandCoverMap& aLCSeq,
71   QStringList& aNonExpList)
72 {
73   /*TODO
74   SHPHandle hSHPHandle;
75   if (!aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty())
76   {
77     hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARC );        
78     for (int i = 1; i <= aPolyXYSeq.Size(); i++)
79       if (WriteObjectPolyXY(hSHPHandle, aPolyXYSeq(i)) != 1)
80         aNonExpList.append(aPolyXYSeq(i)->GetName());
81
82   }
83   else if (aPolyXYSeq.IsEmpty() && !aPoly3DSeq.IsEmpty())
84   {
85     hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARCZ );
86     for (int i = 1; i <= aPoly3DSeq.Size(); i++)
87       if (WriteObjectPoly3D(hSHPHandle, aPoly3DSeq(i)) != 1)
88         aNonExpList.append(aPoly3DSeq(i)->GetName());
89   }
90   else if (aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty() && !aLCSeq.IsEmpty())
91   {
92     hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_POLYGON );
93     for (int i = 1; i <= aLCSeq.Size(); i++)
94       if (WriteObjectLC(hSHPHandle, aLCSeq(i)) != 1)
95         aNonExpList.append(aLCSeq(i)->GetName());
96   }
97   if (hSHPHandle->nRecords > 0)
98     SHPClose( hSHPHandle );
99   else
100   {
101     SHPClose( hSHPHandle );
102     QString aFN = aFileName.simplified();
103     remove (aFN.toStdString().c_str());
104     remove (aFN.replace( ".shp", ".shx", Qt::CaseInsensitive).toStdString().c_str());
105   }*/
106 }
107
108 int HYDROData_ShapeFile::WriteObjectPolyXY(SHPHandle theShpHandle, Handle_HYDROData_PolylineXY thePoly )
109 {
110   SHPObject     *aSHPObj;
111   std::vector<double> x, y;
112   std::vector<int> anPartStart;
113   
114   for (int i = 0; i < thePoly->NbSections(); i++)    
115     if (thePoly->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE)
116       return -1;
117
118   for (int i = 0; i < thePoly->NbSections(); i++)
119   {
120     anPartStart.push_back(x.size());
121     HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPoints(i);
122     for (int j = 1; j <= aPointList.Size(); j++)
123     {
124       x.push_back( aPointList(j).X());
125       y.push_back( aPointList(j).Y()); 
126     }
127     if (thePoly->IsClosedSection(i))
128     {
129       x.push_back( aPointList(1).X());
130       y.push_back( aPointList(1).Y()); 
131     }
132   }
133     
134   aSHPObj = SHPCreateObject( SHPT_ARC, -1, thePoly->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
135   SHPWriteObject( theShpHandle, -1, aSHPObj );
136   SHPDestroyObject( aSHPObj );
137   return 1;
138 }
139
140 int HYDROData_ShapeFile::WriteObjectPoly3D(SHPHandle theShpHandle, Handle_HYDROData_Polyline3D thePoly )
141 {
142   SHPObject     *aSHPObj;
143   std::vector<double> x, y, z;
144   std::vector<int> anPartStart;
145
146   for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)    
147     if (thePoly->GetPolylineXY()->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE)
148       return -1;
149   
150   for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)
151   {
152     anPartStart.push_back(x.size());
153     HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPolylineXY()->GetPoints(i);
154     for (int j = 1; j <= aPointList.Size(); j++)
155     {
156       x.push_back( aPointList(j).X());
157       y.push_back( aPointList(j).Y()); 
158       z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(j).X(), aPointList(j).Y())));
159     }
160     if ( thePoly->GetPolylineXY()->IsClosedSection(i))
161     {
162       x.push_back( aPointList(1).X());
163       y.push_back( aPointList(1).Y()); 
164       z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(1).X(), aPointList(1).Y())));
165
166     }
167   }
168   
169   aSHPObj = SHPCreateObject( SHPT_ARCZ, -1, thePoly->GetPolylineXY()->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], &z[0], NULL );
170   SHPWriteObject( theShpHandle, -1, aSHPObj );
171   SHPDestroyObject( aSHPObj );
172   return 1;
173 }
174
175 /*TODO:int HYDROData_ShapeFile::WriteObjectLC(SHPHandle theShpHandle, Handle_HYDROData_LandCover theLC )
176 {
177   TopoDS_Shape aSh = theLC->GetShape();
178   if (aSh.IsNull())
179     return 0;
180   TopExp_Explorer anEdgeEx(aSh, TopAbs_EDGE);
181   for (; anEdgeEx.More(); anEdgeEx.Next()) 
182   {
183     TopoDS_Edge E = TopoDS::Edge(anEdgeEx.Current());
184     double aFP, aLP;
185     Handle_Geom_Curve aCur = BRep_Tool::Curve(E, aFP, aLP);
186     Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCur);
187     if (aLine.IsNull())
188     {
189       Handle(Geom_TrimmedCurve) aTC = Handle(Geom_TrimmedCurve)::DownCast(aCur);
190       if (!aTC.IsNull())
191       {
192         Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aTC->BasisCurve());
193         if (aLine.IsNull())
194           return -1;
195       }
196       else
197         return -1;
198     }
199
200   }
201   if (aSh.ShapeType() == TopAbs_FACE)
202   {
203     ProcessFace(TopoDS::Face(aSh), theShpHandle);
204   }
205   else if (aSh.ShapeType() == TopAbs_COMPOUND)
206   {
207     TopExp_Explorer Ex(aSh, TopAbs_FACE);
208     for (; Ex.More(); Ex.Next()) 
209     {
210       TopoDS_Face aF = TopoDS::Face(Ex.Current());   
211       if (aF.IsNull())
212         continue;
213       ProcessFace(aF, theShpHandle);
214     }
215   }
216   else
217     return 0;
218
219   return 1;
220  
221 }
222 */
223 void HYDROData_ShapeFile::ProcessFace(TopoDS_Face theFace, SHPHandle theShpHandle)
224 {
225   SHPObject     *aSHPObj;
226   std::vector<double> x, y;
227   std::vector<int> anPartStart;
228   if (theFace.ShapeType() == TopAbs_FACE)
229   {
230     TopExp_Explorer Ex(theFace, TopAbs_WIRE);
231     int NbWires = 0;
232     for (; Ex.More(); Ex.Next()) 
233     {
234       TopoDS_Wire aW = TopoDS::Wire(Ex.Current());   
235       if (aW.IsNull())
236         continue;
237       NbWires++;
238       anPartStart.push_back(x.size());
239       TopExp_Explorer aVEx(aW, TopAbs_VERTEX);
240       NCollection_Sequence<gp_Pnt2d> aPnts;
241       for (; aVEx.More(); aVEx.Next())
242       {
243         TopoDS_Vertex aV = TopoDS::Vertex(aVEx.Current()); 
244         if (aV.IsNull())
245           continue;
246         gp_Pnt P = BRep_Tool::Pnt(aV);
247         aPnts.Append(gp_Pnt2d(P.X(), P.Y()));
248       }
249       NCollection_Sequence<gp_Pnt2d> aNPnts;
250       aNPnts.Append(aPnts.First());
251       for (int j = 1; j <= aPnts.Size() - 1; j++)
252       {
253         if (!aPnts(j).IsEqual(aPnts(j + 1), Precision::Confusion())) 
254           aNPnts.Append(aPnts(j + 1));
255       }
256
257       for (int j = 1; j <= aNPnts.Size(); j++)
258       { 
259         x.push_back( aNPnts(j).X());
260         y.push_back( aNPnts(j).Y()); 
261       }
262       //x.push_back( aNPnts(1).X());
263       //y.push_back( aNPnts(1).Y()); 
264
265     }
266     
267     aSHPObj = SHPCreateObject( SHPT_POLYGON, -1, NbWires, &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
268     SHPWriteObject( theShpHandle, -1, aSHPObj );
269     SHPDestroyObject( aSHPObj );
270   }
271   else
272     return;
273 }
274
275 bool HYDROData_ShapeFile::Parse(SHPHandle theHandle, ShapeType theType, int& theShapeTypeOfFile)
276 {
277   int aShapeType;
278   mySHPObjects.clear();
279   SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
280   theShapeTypeOfFile = aShapeType;
281   bool ToRead = (theType == ShapeType_Polyline && (aShapeType == 3 || aShapeType == 13 || aShapeType == 23)) ||
282    (theType == ShapeType_Polygon && aShapeType == 5);
283   if (ToRead) 
284   {
285     for (int i = 0; i < theHandle->nRecords; i++) 
286       mySHPObjects.push_back(SHPReadObject(theHandle, i));
287     return true;
288   }
289   else
290     return false;
291 }
292
293 void HYDROData_ShapeFile::ReadSHPPolygon(SHPObject* anObj, int i, TopoDS_Face& F)
294 {
295   TopoDS_Wire W;
296   TopoDS_Edge E; 
297   int nParts = anObj->nParts;
298   gp_Pln pln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
299
300   //Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
301   //sfs->FixFaceTool()->FixOrientationMode() = 1;
302   TopTools_SequenceOfShape aWires;
303   for ( int i = 0 ; i < nParts ; i++ )
304   { 
305     BRepBuilderAPI_MakeWire aBuilder;
306     int StartIndex = anObj->panPartStart[i];
307     int EndIndex;
308     if (i != nParts - 1)
309       EndIndex = anObj->panPartStart[i + 1];
310     else
311       EndIndex = anObj->nVertices;
312
313     for ( int k = StartIndex; k < EndIndex - 1  ; k++ )
314     {
315       gp_Pnt P1 (anObj->padfX[k], anObj->padfY[k], 0);
316       gp_Pnt P2 (anObj->padfX[k+1], anObj->padfY[k+1], 0);
317       if (P1.Distance(P2) < Precision::Confusion())
318         continue;
319       BRepBuilderAPI_MakeEdge aMakeEdge(P1, P2);
320       aBuilder.Add(TopoDS::Edge(aMakeEdge.Shape()));
321     }
322     
323     aBuilder.Build();
324     W = TopoDS::Wire(aBuilder.Shape());
325     W.Orientation(TopAbs_FORWARD);
326     BRepBuilderAPI_MakeFace aDB(pln, W);
327     TopoDS_Face aDummyFace = TopoDS::Face(aDB.Shape());
328     BRepTopAdaptor_FClass2d FClass(aDummyFace, Precision::PConfusion());
329     if ( i == 0 && FClass.PerformInfinitePoint() == TopAbs_OUT) 
330       W.Reverse();
331     if ( i > 0 && FClass.PerformInfinitePoint() != TopAbs_IN) 
332       W.Reverse();
333    
334     aWires.Append(W);
335   }
336   
337   BRepBuilderAPI_MakeFace aFBuilder(pln, TopoDS::Wire(aWires(1)));
338   for (int i = 2; i <= aWires.Length(); i++)
339     aFBuilder.Add(TopoDS::Wire(aWires(i)));
340   TopoDS_Face DF = TopoDS::Face(aFBuilder.Shape());
341
342   BRepLib::BuildCurves3d(DF);  
343   if(!DF.IsNull()) 
344   {
345     //sfs->Init ( DF );
346     //sfs->Perform();
347     F = DF; //TopoDS::Face(sfs->Shape());
348   }
349 }
350
351 int HYDROData_ShapeFile::ImportLandCovers(const QString theFileName, QStringList& thePolygonsList, TopTools_SequenceOfShape& theFaces, int& theShapeTypeOfFile)
352 {
353   Free();
354   int Stat = TryOpenShapeFile(theFileName);
355   if (Stat != 0)
356     return Stat;
357   myHSHP = SHPOpen( theFileName.toAscii().data(), "rb" );
358   if (!Parse(myHSHP, HYDROData_ShapeFile::ShapeType_Polygon, theShapeTypeOfFile))
359     return 0;
360   for (size_t i = 0; i < mySHPObjects.size(); i++)
361     thePolygonsList.append("polygon_" + QString::number(i + 1));
362    
363   TopoDS_Face aF;
364   if (myHSHP->nShapeType == 5)
365   {
366     for (size_t i = 0; i < mySHPObjects.size(); i++) 
367     {
368        ReadSHPPolygon(mySHPObjects[i], i, aF);
369        theFaces.Append(aF);
370     }
371     return 1;
372   }
373   else
374     return 0;
375 }
376
377 void HYDROData_ShapeFile::Free()
378 {  
379   for (size_t i = 0; i < mySHPObjects.size(); i++ )
380     free (mySHPObjects[i]);
381
382   mySHPObjects.clear();
383   if (myHSHP != NULL)
384   {
385     SHPClose(myHSHP);
386     myHSHP = NULL; 
387   }
388 }
389
390
391 void HYDROData_ShapeFile::ReadSHPPolyXY(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName, 
392   int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
393 {
394
395   Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
396   
397   int nParts = anObj->nParts;
398   for ( int i = 0 ; i < nParts ; i++ )
399   {
400     int StartIndex = anObj->panPartStart[i];
401     int EndIndex;
402     if (i != nParts - 1)
403       EndIndex = anObj->panPartStart[i + 1];
404     else
405       EndIndex = anObj->nVertices;
406
407     bool IsClosed = false;
408     HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE; 
409     if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
410         anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] )
411     {
412       IsClosed = true;
413       aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true);
414     }
415     else
416       aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false);
417     
418     if (IsClosed)
419       EndIndex--;
420     for ( int k = StartIndex; k < EndIndex ; k++ )
421     {
422       HYDROData_PolylineXY::Point aSectPoint;
423       aSectPoint.SetX( anObj->padfX[k] );
424       aSectPoint.SetY( anObj->padfY[k] );
425       aPolylineXY->AddPoint( i, aSectPoint );
426     }
427
428   }
429   
430   aPolylineXY->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
431   aPolylineXY->SetName( theFileName + "_PolyXY_" + QString::number(theInd) );
432   
433   aPolylineXY->Update();
434   theEntities.Append(aPolylineXY);
435
436 }
437
438 void HYDROData_ShapeFile::ReadSHPPoly3D(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName, 
439   int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
440 {
441   Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
442
443   Handle(HYDROData_Polyline3D) aPolylineObj = Handle(HYDROData_Polyline3D)::DownCast( theDocument->CreateObject( KIND_POLYLINE ) );
444
445   Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( theDocument->CreateObject( KIND_BATHYMETRY ) );
446   HYDROData_Bathymetry::AltitudePoints aAPoints;
447
448   int nParts = anObj->nParts;
449   for ( int i = 0 ; i < nParts ; i++ )
450   {
451     //bool aSectClosure = true;
452     int StartIndex = anObj->panPartStart[i];
453     int EndIndex;
454     if (i != nParts - 1)
455       EndIndex = anObj->panPartStart[i + 1];
456     else
457       EndIndex = anObj->nVertices;
458
459     bool IsClosed = false;
460     HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE; 
461     if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
462         anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] &&
463         anObj->padfZ[StartIndex] == anObj->padfZ[EndIndex - 1])
464     {
465       IsClosed = true;
466       aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true );
467     }
468     else
469       aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false  );
470     
471     if (IsClosed)
472       EndIndex--;
473     for ( int k = StartIndex ; k < EndIndex ; k++ )
474     {
475       HYDROData_PolylineXY::Point aSectPoint;
476       aSectPoint.SetX( anObj->padfX[k] );
477       aSectPoint.SetY( anObj->padfY[k] );
478       aPolylineXY->AddPoint( i, aSectPoint );
479       aAPoints.Append(gp_XYZ (anObj->padfX[k], anObj->padfY[k], anObj->padfZ[k]));
480     }
481   }
482
483
484   QString aBathName = theFileName + "_bath_" + QString::number(theInd);
485   QString aPolyXYName = theFileName + "_polyXY_" + QString::number(theInd);
486   QString aPoly3DName = theFileName + "_poly3D_" + QString::number(theInd);
487
488   aPolylineXY->SetName( aPolyXYName );
489   aPolylineXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
490   aPolylineXY->Update();
491   
492   aBath->SetAltitudePoints(aAPoints);
493   aBath->SetName( aBathName );
494
495   aPolylineObj->SetPolylineXY (aPolylineXY, false);
496   aPolylineObj->SetAltitudeObject(aBath);
497
498   aPolylineObj->SetBorderColor( aPolylineObj->DefaultBorderColor() );
499   aPolylineObj->SetName( aPoly3DName );
500   
501   aPolylineObj->Update();
502   theEntities.Append(aPolylineXY);
503   theEntities.Append(aPolylineObj);
504
505 }
506
507
508
509 int HYDROData_ShapeFile::ImportPolylines(Handle(HYDROData_Document) theDocument, const QString& theFileName, 
510   NCollection_Sequence<Handle_HYDROData_Entity>& theEntities, int& theShapeTypeOfFile)
511 {
512   //Free();
513   int aStat = TryOpenShapeFile(theFileName);
514   if (aStat != 0)
515     return aStat;
516
517   HYDROData_Iterator anIter( theDocument );
518   int anInd = 0;
519   QStringList anExistingNames;
520   std::vector<int> anAllowedIndexes;
521   for( ; anIter.More(); anIter.Next() )
522     anExistingNames.push_back(anIter.Current()->GetName());
523
524   SHPHandle aHSHP;
525   aHSHP = SHPOpen( theFileName.toAscii().data(), "rb" );
526   
527   QFileInfo aFileInfo(theFileName);
528   QString aBaseFileName = aFileInfo.baseName();
529
530   if (!Parse(aHSHP, HYDROData_ShapeFile::ShapeType_Polyline, theShapeTypeOfFile))
531     return 0;
532   if (aHSHP->nShapeType == 3 || aHSHP->nShapeType == 23)
533   {
534     anInd = 0;
535     for (;anAllowedIndexes.size() < mySHPObjects.size();)
536     {
537       if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)))
538       {
539         anAllowedIndexes.push_back(anInd);
540         anInd++;
541       }
542       else
543         anInd++;
544     }
545     
546     for (size_t i = 0; i < mySHPObjects.size(); i++ )
547     {
548       ReadSHPPolyXY(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
549     }
550     aStat = 1;
551   }
552   else if (aHSHP->nShapeType == 13)
553   {
554     anInd = 0;
555     for (;anAllowedIndexes.size() < mySHPObjects.size();)
556     {
557       if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)) &&
558           !anExistingNames.contains(aBaseFileName + "_Poly3D_" + QString::number(anInd)) &&
559           !anExistingNames.contains(aBaseFileName + "_Bath_" + QString::number(anInd)))
560       {
561         anAllowedIndexes.push_back(anInd);
562         anInd++;
563       }
564       else
565         anInd++;
566     }
567     for (size_t i = 0; i < mySHPObjects.size(); i++ )
568       ReadSHPPoly3D(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
569     aStat = 1;
570   }
571   else  
572   {
573     aStat = 0;
574   }
575   
576   for (size_t i = 0; i < mySHPObjects.size(); i++ )
577     free (mySHPObjects[i]);
578
579   mySHPObjects.clear();
580   SHPClose(aHSHP);
581   return aStat;
582 }
583
584 QString HYDROData_ShapeFile::GetShapeTypeName(int theType)
585 {
586   switch (theType)
587   {
588     case 0:
589       return "null shape";
590     case 1:
591       return "point (unsupported by HYDRO)";
592     case 3:
593       return "arc/polyline (supported by HYDRO)";
594     case 5:
595       return "polygon (supported by HYDRO)";
596     case 8:
597       return "multipoint (unsupported by HYDRO)";
598     case 11:
599       return "pointZ (unsupported by HYDRO)";
600     case 13:
601       return "arcZ/polyline (supported by HYDRO)";
602     case 15:
603       return "polygonZ (unsupported by HYDRO)";
604     case 18:
605       return "multipointZ (unsupported by HYDRO)";
606     case 21:
607       return "pointM (unsupported by HYDRO)";
608     case 23:
609       return "arcM/polyline (supported by HYDRO)";
610     case 25:
611       return "polygonM (unsupported by HYDRO)";
612     case 28:
613       return "multipointM (unsupported by HYDRO)";
614     case 31:
615       return "multipatch (unsupported by HYDRO)";
616     default:
617       return "unknown";
618   }
619 }
620
621 int HYDROData_ShapeFile::TryOpenShapeFile(QString theFileName)
622 {
623   QString aSHPfile = theFileName.simplified();
624   QString aSHXfile = theFileName.simplified().replace( ".shp", ".shx", Qt::CaseInsensitive);
625   FILE* pFileSHP = NULL;
626   pFileSHP = fopen (aSHPfile.toAscii().data(), "r");
627   FILE* pFileSHX = NULL;
628   pFileSHX = fopen (aSHXfile.toAscii().data(), "r");
629
630   if (pFileSHP == NULL || pFileSHX == NULL)
631   {
632     if (pFileSHP == NULL)
633       return -1;
634     if (pFileSHX == NULL)
635       return -2;
636   }
637   
638   fclose (pFileSHP);
639   fclose (pFileSHX);
640   return 0;
641 }
642
643
644 bool HYDROData_ShapeFile::CheckDBFFileExisting(const QString& theSHPFilePath, QString& thePathToDBFFile)
645 {
646   QString aSHPfile = theSHPFilePath.simplified();
647   QString aDBFfile = theSHPFilePath.simplified().replace( ".shp", ".dbf", Qt::CaseInsensitive);
648   FILE* pFileDBF = NULL;
649   pFileDBF = fopen (aDBFfile.toAscii().data(), "r");
650
651   if (pFileDBF == NULL)
652   {
653     return false;
654   }
655   
656   fclose (pFileDBF);
657   thePathToDBFFile = aDBFfile;
658   return true;
659 }
660
661
662 bool HYDROData_ShapeFile::DBF_OpenDBF(const QString& thePathToDBFFile)
663 {
664   myHDBF = DBFOpen( thePathToDBFFile.toAscii().data(), "r" );
665   if(myHDBF != NULL)
666     return true;
667   else
668     return false;
669 }
670
671 int HYDROData_ShapeFile::DBF_GetNbFields()
672 {
673   if(myHDBF == NULL)
674     return 0;
675   return DBFGetFieldCount(myHDBF);
676 }
677
678 void HYDROData_ShapeFile::DBF_CloseDBF()
679 {
680   if(myHDBF != NULL)
681      DBFClose( myHDBF );
682 }
683
684 QStringList HYDROData_ShapeFile::DBF_GetFieldList()
685 {
686   QStringList FieldList;
687   int   nWidth, nDecimals;
688   char chField[12];
689
690   for( int i = 0; i < DBFGetFieldCount(myHDBF); i++ )
691   {
692       DBFFieldType eType;
693       eType = DBFGetFieldInfo( myHDBF, i, chField, &nWidth, &nDecimals );
694       FieldList.append(QString(chField));
695   }
696
697   return FieldList;
698 }
699
700 void HYDROData_ShapeFile::DBF_GetFieldTypeList(std::vector<DBF_FieldType>& FTVect)
701 {
702   int   nWidth, nDecimals;
703   char chField[12];
704   DBF_FieldType FT;
705   for( int i = 0; i < DBFGetFieldCount(myHDBF); i++ )
706   {
707     DBFFieldType eType;
708     eType = DBFGetFieldInfo( myHDBF, i, chField, &nWidth, &nDecimals );
709     if( eType == FTString )
710       FT = DBF_FieldType_String;
711     else if( eType == FTInteger )
712       FT = DBF_FieldType_Integer;
713     else if( eType == FTDouble )
714       FT = DBF_FieldType_Double;
715     else if( eType == FTInvalid )
716       FT = DBF_FieldType_Invalid;
717
718     FTVect.push_back(FT);
719   }
720
721 }
722
723 int HYDROData_ShapeFile::DBF_GetNbRecords()
724 {
725   if(myHDBF == NULL)
726     return 0;
727   return DBFGetRecordCount(myHDBF);
728 }
729
730 void HYDROData_ShapeFile::DBF_GetAttributeList(int theIndexOfField, std::vector<DBF_AttrValue>& theAttrV)
731 {
732   int nWidth, nDecimals;
733   char chField[12];
734   
735   for( int i = 0; i < DBFGetRecordCount(myHDBF); i++ )
736   {      
737     DBFFieldType eType;
738     DBF_AttrValue anAttr;
739     eType = DBFGetFieldInfo( myHDBF, theIndexOfField, chField, &nWidth, &nDecimals );
740
741     if( DBFIsAttributeNULL( myHDBF, i, theIndexOfField ) )
742     {
743       anAttr.myIsNull = true;
744       DBF_FieldType FT = DBF_FieldType_None;
745       if( eType == FTString )
746         FT = DBF_FieldType_String;
747       else if( eType == FTInteger )
748         FT = DBF_FieldType_Integer;
749       else if( eType == FTDouble )
750         FT = DBF_FieldType_Double;
751       else if( eType == FTInvalid )
752         FT = DBF_FieldType_Invalid;
753       anAttr.myFieldType = FT;
754     }
755     else
756     {
757       switch( eType )
758       {
759         case FTString:
760         {
761           const char* chAttr = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
762           anAttr.myIsNull = false;
763           anAttr.myFieldType = DBF_FieldType_String;
764           anAttr.myRawValue = chAttr;
765           anAttr.myStrVal = QString(chAttr);
766           break;
767         }
768        
769         case FTInteger:
770         {
771           int iAttr = DBFReadIntegerAttribute( myHDBF, i, theIndexOfField );
772           anAttr.myIsNull = false;
773           anAttr.myFieldType = DBF_FieldType_Integer;
774           anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
775           anAttr.myIntVal = iAttr;
776           break;
777         }
778           
779         case FTDouble:
780         {
781           double dAttr = DBFReadDoubleAttribute( myHDBF, i, theIndexOfField );
782           anAttr.myIsNull = false;
783           anAttr.myFieldType = DBF_FieldType_Double;
784           anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
785           anAttr.myDoubleVal = dAttr;
786           break;
787         }
788         default:
789           break;
790       }
791     }
792     theAttrV.push_back(anAttr);
793   }
794
795 }
796
797 bool HYDROData_ShapeFile::DBF_WriteFieldAndValues(const QString& theFileName, const QString& theFieldName, DBF_FieldType theType, const std::vector<DBF_AttrValue>& theAttrV, bool bUseRawValue)
798 {
799   // Check that given field type is equal to field types of attributes values
800   for (size_t i = 0; i < theAttrV.size(); i++)
801   {
802     if (theAttrV[i].myFieldType != theType)
803       return false;
804   }
805
806   DBFHandle     hDBF;
807   hDBF = DBFCreate( theFileName.toStdString().c_str() );
808   if( hDBF == NULL )
809           return false;
810   
811   if (theType != DBF_FieldType_String && theType != DBF_FieldType_Integer && theType != DBF_FieldType_Double) 
812   {
813     DBFClose( hDBF );
814     return false; //cant handle any other cases
815   }
816
817   int nWidth = 20;
818   switch( theType )
819   {
820     case DBF_FieldType_String:
821     {
822       DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTString, nWidth, 0);
823       break;
824     }
825    
826     case DBF_FieldType_Integer:
827     {
828       DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTInteger, nWidth, 0);
829       break;
830     }
831       
832     case DBF_FieldType_Double:
833     {
834       DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTDouble, nWidth, 0);
835       break;
836     }
837     default:
838       break;
839   }
840
841   if (DBFGetFieldCount( hDBF ) != 1) 
842   {
843     DBFClose( hDBF );
844     return false;
845   }
846   if (DBFGetRecordCount( hDBF ) != 0)
847   {
848     DBFClose( hDBF );
849     return false;
850   }
851   int stat = -1;
852
853   if (bUseRawValue)
854   {
855     for (size_t i = 0; i < theAttrV.size(); i++)
856     {
857       if (!theAttrV[i].myIsNull)
858         stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myRawValue.c_str());
859       else
860         stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 );
861
862       if (stat != 1)
863       {
864         DBFClose( hDBF );
865         return false;
866       }
867     }
868   }
869   else
870   {
871     for (size_t i = 0; i < theAttrV.size(); i++)
872     {
873       if (!theAttrV[i].myIsNull)
874         switch( theType )
875         {
876           case DBF_FieldType_String:
877           {
878             stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myStrVal.toStdString().c_str());
879             break;
880           }
881          
882           case DBF_FieldType_Integer:
883           {
884             stat = DBFWriteIntegerAttribute(hDBF, (int)i, 0, theAttrV[i].myIntVal);
885             break;
886           }
887             
888           case DBF_FieldType_Double:
889           {
890             stat = DBFWriteDoubleAttribute(hDBF, (int)i, 0, theAttrV[i].myDoubleVal);
891             break;
892           }
893           default:
894             break;
895         }
896       else
897         stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 );
898
899       if (stat != 1)
900       {
901         DBFClose( hDBF );
902         return false;
903       }
904     }
905   }
906
907   DBFClose( hDBF );
908   return true;
909
910 }