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