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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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>
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 <BRepTools.hxx>
37 #include <Precision.hxx>
38 #include <Handle_Geom_Curve.hxx>
39 #include <Handle_Geom_Line.hxx>
40 #include <Handle_Geom_TrimmedCurve.hxx>
41 #include <Geom_TrimmedCurve.hxx>
42 #include <BRepBuilderAPI_MakeEdge.hxx>
43 #include <BRepBuilderAPI_MakeWire.hxx>
44 #include <BRepBuilderAPI_MakeFace.hxx>
46 #include <BRepLib.hxx>
47 #include <ShapeFix_Shape.hxx>
48 #include <TopTools_SequenceOfShape.hxx>
50 #include <BRepTopAdaptor_FClass2d.hxx>
54 #pragma warning( disable: 4996 )
57 HYDROData_ShapeFile::HYDROData_ShapeFile() : myHSHP(NULL)
61 HYDROData_ShapeFile::~HYDROData_ShapeFile()
66 void HYDROData_ShapeFile::Export(const QString& aFileName,
67 NCollection_Sequence<Handle_HYDROData_PolylineXY> aPolyXYSeq,
68 NCollection_Sequence<Handle_HYDROData_Polyline3D> aPoly3DSeq,
69 QStringList& aNonExpList)
72 if (!aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty())
74 hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARC );
75 for (int i = 1; i <= aPolyXYSeq.Size(); i++)
76 if (WriteObjectPolyXY(hSHPHandle, aPolyXYSeq(i)) != 1)
77 aNonExpList.append(aPolyXYSeq(i)->GetName());
79 else if (aPolyXYSeq.IsEmpty() && !aPoly3DSeq.IsEmpty())
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());
86 if (hSHPHandle->nRecords > 0)
87 SHPClose( hSHPHandle );
90 SHPClose( hSHPHandle );
91 QString aFN = aFileName.simplified();
92 remove (aFN.toStdString().c_str());
93 remove (aFN.replace( ".shp", ".shx", Qt::CaseInsensitive).toStdString().c_str());
97 void HYDROData_ShapeFile::Export(const QString& aFileName,
98 const Handle_HYDROData_LandCoverMap& aLCM, QStringList& aNonExpList)
100 SHPHandle hSHPHandle;
101 if ( !aLCM.IsNull() && !aLCM->IsEmpty())
103 hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_POLYGON );
104 HYDROData_LandCoverMap::Iterator It( aLCM );
105 for( ; It.More(); It.Next() )
107 TopoDS_Face aFace = It.Face();
108 if (WriteObjectPolygon(hSHPHandle, aFace) != 1)
109 aNonExpList.append(aLCM->GetName() + "_" + QString::number(It.Index()));
112 if (hSHPHandle->nRecords > 0)
113 SHPClose( hSHPHandle );
116 SHPClose( hSHPHandle );
117 QString aFN = aFileName.simplified();
118 remove (aFN.toStdString().c_str());
119 remove (aFN.replace( ".shp", ".shx", Qt::CaseInsensitive).toStdString().c_str());
124 int HYDROData_ShapeFile::WriteObjectPolyXY(SHPHandle theShpHandle, Handle_HYDROData_PolylineXY thePoly )
127 std::vector<double> x, y;
128 std::vector<int> anPartStart;
130 for (int i = 0; i < thePoly->NbSections(); i++)
131 if (thePoly->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE)
134 for (int i = 0; i < thePoly->NbSections(); i++)
136 anPartStart.push_back(x.size());
137 HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPoints(i);
138 for (int j = 1; j <= aPointList.Size(); j++)
140 x.push_back( aPointList(j).X());
141 y.push_back( aPointList(j).Y());
143 if (thePoly->IsClosedSection(i))
145 x.push_back( aPointList(1).X());
146 y.push_back( aPointList(1).Y());
150 aSHPObj = SHPCreateObject( SHPT_ARC, -1, thePoly->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
151 SHPWriteObject( theShpHandle, -1, aSHPObj );
152 SHPDestroyObject( aSHPObj );
156 int HYDROData_ShapeFile::WriteObjectPoly3D(SHPHandle theShpHandle, Handle_HYDROData_Polyline3D thePoly )
159 std::vector<double> x, y, z;
160 std::vector<int> anPartStart;
162 for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)
163 if (thePoly->GetPolylineXY()->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE)
166 for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)
168 anPartStart.push_back(x.size());
169 HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPolylineXY()->GetPoints(i);
170 for (int j = 1; j <= aPointList.Size(); j++)
172 x.push_back( aPointList(j).X());
173 y.push_back( aPointList(j).Y());
174 z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(j).X(), aPointList(j).Y())));
176 if ( thePoly->GetPolylineXY()->IsClosedSection(i))
178 x.push_back( aPointList(1).X());
179 y.push_back( aPointList(1).Y());
180 z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(1).X(), aPointList(1).Y())));
185 aSHPObj = SHPCreateObject( SHPT_ARCZ, -1, thePoly->GetPolylineXY()->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], &z[0], NULL );
186 SHPWriteObject( theShpHandle, -1, aSHPObj );
187 SHPDestroyObject( aSHPObj );
191 int HYDROData_ShapeFile::WriteObjectPolygon(SHPHandle theShpHandle, const TopoDS_Shape& theInputShape )
193 if (theInputShape.IsNull())
195 TopExp_Explorer anEdgeEx(theInputShape, TopAbs_EDGE);
196 for (; anEdgeEx.More(); anEdgeEx.Next())
198 TopoDS_Edge E = TopoDS::Edge(anEdgeEx.Current());
200 Handle_Geom_Curve aCur = BRep_Tool::Curve(E, aFP, aLP);
201 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCur);
204 Handle(Geom_TrimmedCurve) aTC = Handle(Geom_TrimmedCurve)::DownCast(aCur);
207 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aTC->BasisCurve());
216 if (theInputShape.ShapeType() == TopAbs_FACE)
218 ProcessFace(TopoDS::Face(theInputShape), theShpHandle);
220 else if (theInputShape.ShapeType() == TopAbs_COMPOUND)
222 TopExp_Explorer Ex(theInputShape, TopAbs_FACE);
223 for (; Ex.More(); Ex.Next())
225 TopoDS_Face aF = TopoDS::Face(Ex.Current());
228 ProcessFace(aF, theShpHandle);
238 void HYDROData_ShapeFile::ProcessFace(TopoDS_Face theFace, SHPHandle theShpHandle)
240 if (theFace.ShapeType() != TopAbs_FACE)
243 std::vector<double> x, y;
244 std::vector<int> anPartStart;
245 TopoDS_Wire OuterW = BRepTools::OuterWire(theFace);
246 NCollection_Sequence<TopoDS_Wire> aWires;
248 //write an outer wire first
249 aWires.Append(OuterW);
250 TopExp_Explorer Ex(theFace, TopAbs_WIRE);
251 for (; Ex.More(); Ex.Next())
253 TopoDS_Wire aW = TopoDS::Wire(Ex.Current());
254 if (aW.IsEqual(OuterW))
259 //TopExp_Explorer Ex(theFace, TopAbs_WIRE);
261 for (int k = 1; k <= aWires.Length(); k++)
263 TopoDS_Wire aW = aWires(k);
267 if (aW.Orientation() == TopAbs_INTERNAL)
268 //cant write internal wires/edges
270 // Try to reorder edges
271 Handle(ShapeFix_Wire) aSFW = new ShapeFix_Wire( aW, theFace, Precision::Confusion() );
273 Handle(ShapeExtend_WireData) aSEWD = aSFW->WireData();
274 Standard_Integer nbE = aSEWD->NbEdges();
278 anPartStart.push_back(x.size());
279 NCollection_Sequence<gp_Pnt2d> aPnts;
280 for (Standard_Integer i = 1; i <= nbE; i++)
282 TopoDS_Edge E = aSEWD->Edge(i);
283 TopoDS_Vertex aV = TopExp::LastVertex(E, 1);
286 gp_Pnt P = BRep_Tool::Pnt(aV);
287 aPnts.Append(gp_Pnt2d(P.X(), P.Y()));
289 NCollection_Sequence<gp_Pnt2d> aNPnts;
290 aNPnts.Append(aPnts.First());
291 for (int j = 1; j <= aPnts.Size() - 1; j++)
293 if (!aPnts(j).IsEqual(aPnts(j + 1), Precision::Confusion()))
294 aNPnts.Append(aPnts(j + 1));
300 //clockwise direction
301 for (int j = 1; j <= aNPnts.Size(); j++)
303 x.push_back( aNPnts(j).X());
304 y.push_back( aNPnts(j).Y());
306 x.push_back( aNPnts(1).X());
307 y.push_back( aNPnts(1).Y());
312 //counter-clockwise direction
313 for (int j = aNPnts.Size(); j > 0; j--)
315 x.push_back( aNPnts(j).X());
316 y.push_back( aNPnts(j).Y());
318 x.push_back( aNPnts(aNPnts.Size()).X());
319 y.push_back( aNPnts(aNPnts.Size()).Y());
323 aSHPObj = SHPCreateObject( SHPT_POLYGON, -1, NbWires, &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
324 SHPWriteObject( theShpHandle, -1, aSHPObj );
325 SHPDestroyObject( aSHPObj );
329 bool HYDROData_ShapeFile::Parse(SHPHandle theHandle, ShapeType theType, int& theShapeTypeOfFile)
332 mySHPObjects.clear();
333 SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
334 theShapeTypeOfFile = aShapeType;
335 bool ToRead = (theType == ShapeType_Polyline && (aShapeType == 3 || aShapeType == 13 || aShapeType == 23)) ||
336 (theType == ShapeType_Polygon && aShapeType == 5);
339 for (int i = 0; i < theHandle->nRecords; i++)
340 mySHPObjects.push_back(SHPReadObject(theHandle, i));
347 void HYDROData_ShapeFile::ReadSHPPolygon(SHPObject* anObj, int i, TopoDS_Face& F)
353 int nParts = anObj->nParts;
354 gp_Pln pln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
356 //Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
357 //sfs->FixFaceTool()->FixOrientationMode() = 1;
358 TopTools_SequenceOfShape aWires;
359 for ( int i = 0 ; i < nParts ; i++ )
361 BRepBuilderAPI_MakeWire aBuilder;
362 int StartIndex = anObj->panPartStart[i];
365 EndIndex = anObj->panPartStart[i + 1];
367 EndIndex = anObj->nVertices;
369 for ( int k = StartIndex; k < EndIndex - 1 ; k++ )
371 gp_Pnt P1 (anObj->padfX[k], anObj->padfY[k], 0);
372 gp_Pnt P2 (anObj->padfX[k+1], anObj->padfY[k+1], 0);
373 if (P1.Distance(P2) < Precision::Confusion())
375 BRepBuilderAPI_MakeEdge aMakeEdge(P1, P2);
376 aBuilder.Add(TopoDS::Edge(aMakeEdge.Shape()));
380 W = TopoDS::Wire(aBuilder.Shape());
381 W.Orientation(TopAbs_FORWARD);
382 BRepBuilderAPI_MakeFace aDB(pln, W);
383 TopoDS_Face aDummyFace = TopoDS::Face(aDB.Shape());
384 BRepTopAdaptor_FClass2d FClass(aDummyFace, Precision::PConfusion());
385 if ( i == 0 && FClass.PerformInfinitePoint() == TopAbs_OUT)
387 if ( i > 0 && FClass.PerformInfinitePoint() != TopAbs_IN)
393 BRepBuilderAPI_MakeFace aFBuilder(pln, TopoDS::Wire(aWires(1)));
394 for (int i = 2; i <= aWires.Length(); i++)
395 aFBuilder.Add(TopoDS::Wire(aWires(i)));
396 F = TopoDS::Face(aFBuilder.Shape());
398 BRepLib::BuildCurves3d(F);
401 int HYDROData_ShapeFile::ImportPolygons(const QString theFileName, QStringList& thePolygonsList, TopTools_SequenceOfShape& theFaces, int& theShapeTypeOfFile)
404 int Stat = TryOpenShapeFile(theFileName);
407 myHSHP = SHPOpen( theFileName.toAscii().data(), "rb" );
408 if (!Parse(myHSHP, HYDROData_ShapeFile::ShapeType_Polygon, theShapeTypeOfFile))
410 for (size_t i = 0; i < mySHPObjects.size(); i++)
411 thePolygonsList.append("polygon_" + QString::number(i + 1));
414 if (myHSHP->nShapeType == 5)
416 for (size_t i = 0; i < mySHPObjects.size(); i++)
418 ReadSHPPolygon(mySHPObjects[i], i, aF);
427 void HYDROData_ShapeFile::Free()
429 for (size_t i = 0; i < mySHPObjects.size(); i++ )
430 free (mySHPObjects[i]);
432 mySHPObjects.clear();
441 void HYDROData_ShapeFile::ReadSHPPolyXY(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName,
442 int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
445 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
447 int nParts = anObj->nParts;
448 for ( int i = 0 ; i < nParts ; i++ )
450 int StartIndex = anObj->panPartStart[i];
453 EndIndex = anObj->panPartStart[i + 1];
455 EndIndex = anObj->nVertices;
457 bool IsClosed = false;
458 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
459 if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
460 anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] )
463 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true);
466 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false);
470 for ( int k = StartIndex; k < EndIndex ; k++ )
472 HYDROData_PolylineXY::Point aSectPoint;
473 aSectPoint.SetX( anObj->padfX[k] );
474 aSectPoint.SetY( anObj->padfY[k] );
475 aPolylineXY->AddPoint( i, aSectPoint );
480 aPolylineXY->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
481 aPolylineXY->SetName( theFileName + "_PolyXY_" + QString::number(theInd) );
483 aPolylineXY->Update();
484 theEntities.Append(aPolylineXY);
488 void HYDROData_ShapeFile::ReadSHPPoly3D(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName,
489 int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
491 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
493 Handle(HYDROData_Polyline3D) aPolylineObj = Handle(HYDROData_Polyline3D)::DownCast( theDocument->CreateObject( KIND_POLYLINE ) );
495 Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( theDocument->CreateObject( KIND_BATHYMETRY ) );
496 HYDROData_Bathymetry::AltitudePoints aAPoints;
498 int nParts = anObj->nParts;
499 for ( int i = 0 ; i < nParts ; i++ )
501 //bool aSectClosure = true;
502 int StartIndex = anObj->panPartStart[i];
505 EndIndex = anObj->panPartStart[i + 1];
507 EndIndex = anObj->nVertices;
509 bool IsClosed = false;
510 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
511 if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
512 anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] &&
513 anObj->padfZ[StartIndex] == anObj->padfZ[EndIndex - 1])
516 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true );
519 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false );
523 for ( int k = StartIndex ; k < EndIndex ; k++ )
525 HYDROData_PolylineXY::Point aSectPoint;
526 aSectPoint.SetX( anObj->padfX[k] );
527 aSectPoint.SetY( anObj->padfY[k] );
528 aPolylineXY->AddPoint( i, aSectPoint );
529 aAPoints.Append(gp_XYZ (anObj->padfX[k], anObj->padfY[k], anObj->padfZ[k]));
534 QString aBathName = theFileName + "_bath_" + QString::number(theInd);
535 QString aPolyXYName = theFileName + "_polyXY_" + QString::number(theInd);
536 QString aPoly3DName = theFileName + "_poly3D_" + QString::number(theInd);
538 aPolylineXY->SetName( aPolyXYName );
539 aPolylineXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
540 aPolylineXY->Update();
542 aBath->SetAltitudePoints(aAPoints);
543 aBath->SetName( aBathName );
545 aPolylineObj->SetPolylineXY (aPolylineXY, false);
546 aPolylineObj->SetAltitudeObject(aBath);
548 aPolylineObj->SetBorderColor( aPolylineObj->DefaultBorderColor() );
549 aPolylineObj->SetName( aPoly3DName );
551 aPolylineObj->Update();
552 theEntities.Append(aPolylineXY);
553 theEntities.Append(aPolylineObj);
559 int HYDROData_ShapeFile::ImportPolylines(Handle(HYDROData_Document) theDocument, const QString& theFileName,
560 NCollection_Sequence<Handle_HYDROData_Entity>& theEntities, int& theShapeTypeOfFile)
563 int aStat = TryOpenShapeFile(theFileName);
567 HYDROData_Iterator anIter( theDocument );
569 QStringList anExistingNames;
570 std::vector<int> anAllowedIndexes;
571 for( ; anIter.More(); anIter.Next() )
572 anExistingNames.push_back(anIter.Current()->GetName());
575 aHSHP = SHPOpen( theFileName.toAscii().data(), "rb" );
577 QFileInfo aFileInfo(theFileName);
578 QString aBaseFileName = aFileInfo.baseName();
580 if (!Parse(aHSHP, HYDROData_ShapeFile::ShapeType_Polyline, theShapeTypeOfFile))
582 if (aHSHP->nShapeType == 3 || aHSHP->nShapeType == 23)
585 for (;anAllowedIndexes.size() < mySHPObjects.size();)
587 if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)))
589 anAllowedIndexes.push_back(anInd);
596 for (size_t i = 0; i < mySHPObjects.size(); i++ )
598 ReadSHPPolyXY(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
602 else if (aHSHP->nShapeType == 13)
605 for (;anAllowedIndexes.size() < mySHPObjects.size();)
607 if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)) &&
608 !anExistingNames.contains(aBaseFileName + "_Poly3D_" + QString::number(anInd)) &&
609 !anExistingNames.contains(aBaseFileName + "_Bath_" + QString::number(anInd)))
611 anAllowedIndexes.push_back(anInd);
617 for (size_t i = 0; i < mySHPObjects.size(); i++ )
618 ReadSHPPoly3D(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
626 for (size_t i = 0; i < mySHPObjects.size(); i++ )
627 free (mySHPObjects[i]);
629 mySHPObjects.clear();
634 QString HYDROData_ShapeFile::GetShapeTypeName(int theType)
641 return "point (unsupported by HYDRO)";
643 return "arc/polyline (supported by HYDRO)";
645 return "polygon (supported by HYDRO)";
647 return "multipoint (unsupported by HYDRO)";
649 return "pointZ (unsupported by HYDRO)";
651 return "arcZ/polyline (supported by HYDRO)";
653 return "polygonZ (unsupported by HYDRO)";
655 return "multipointZ (unsupported by HYDRO)";
657 return "pointM (unsupported by HYDRO)";
659 return "arcM/polyline (supported by HYDRO)";
661 return "polygonM (unsupported by HYDRO)";
663 return "multipointM (unsupported by HYDRO)";
665 return "multipatch (unsupported by HYDRO)";
671 int HYDROData_ShapeFile::TryOpenShapeFile(QString theFileName)
673 QString aSHPfile = theFileName.simplified();
674 QString aSHXfile = theFileName.simplified().replace( ".shp", ".shx", Qt::CaseInsensitive);
675 FILE* pFileSHP = NULL;
676 pFileSHP = fopen (aSHPfile.toAscii().data(), "r");
677 FILE* pFileSHX = NULL;
678 pFileSHX = fopen (aSHXfile.toAscii().data(), "r");
680 if (pFileSHP == NULL || pFileSHX == NULL)
682 if (pFileSHP == NULL)
684 if (pFileSHX == NULL)
694 bool HYDROData_ShapeFile::CheckDBFFileExisting(const QString& theSHPFilePath, QString& thePathToDBFFile)
696 QString aSHPfile = theSHPFilePath.simplified();
697 QString aDBFfile = theSHPFilePath.simplified().replace( ".shp", ".dbf", Qt::CaseInsensitive);
698 FILE* pFileDBF = NULL;
699 pFileDBF = fopen (aDBFfile.toAscii().data(), "r");
701 if (pFileDBF == NULL)
707 thePathToDBFFile = aDBFfile;
712 bool HYDROData_ShapeFile::DBF_OpenDBF(const QString& thePathToDBFFile)
714 myHDBF = DBFOpen( thePathToDBFFile.toAscii().data(), "r" );
721 int HYDROData_ShapeFile::DBF_GetNbFields()
725 return DBFGetFieldCount(myHDBF);
728 void HYDROData_ShapeFile::DBF_CloseDBF()
734 QStringList HYDROData_ShapeFile::DBF_GetFieldList()
736 QStringList FieldList;
737 int nWidth, nDecimals;
740 for( int i = 0; i < DBFGetFieldCount(myHDBF); i++ )
743 eType = DBFGetFieldInfo( myHDBF, i, chField, &nWidth, &nDecimals );
744 FieldList.append(QString(chField));
750 void HYDROData_ShapeFile::DBF_GetFieldTypeList(std::vector<DBF_FieldType>& FTVect)
752 int nWidth, nDecimals;
755 for( int i = 0; i < DBFGetFieldCount(myHDBF); i++ )
758 eType = DBFGetFieldInfo( myHDBF, i, chField, &nWidth, &nDecimals );
759 if( eType == FTString )
760 FT = DBF_FieldType_String;
761 else if( eType == FTInteger )
762 FT = DBF_FieldType_Integer;
763 else if( eType == FTDouble )
764 FT = DBF_FieldType_Double;
765 else if( eType == FTInvalid )
766 FT = DBF_FieldType_Invalid;
768 FTVect.push_back(FT);
773 int HYDROData_ShapeFile::DBF_GetNbRecords()
777 return DBFGetRecordCount(myHDBF);
780 void HYDROData_ShapeFile::DBF_GetAttributeList(int theIndexOfField, std::vector<DBF_AttrValue>& theAttrV)
782 int nWidth, nDecimals;
785 for( int i = 0; i < DBFGetRecordCount(myHDBF); i++ )
788 DBF_AttrValue anAttr;
789 eType = DBFGetFieldInfo( myHDBF, theIndexOfField, chField, &nWidth, &nDecimals );
791 if( DBFIsAttributeNULL( myHDBF, i, theIndexOfField ) )
793 anAttr.myIsNull = true;
794 DBF_FieldType FT = DBF_FieldType_None;
795 if( eType == FTString )
796 FT = DBF_FieldType_String;
797 else if( eType == FTInteger )
798 FT = DBF_FieldType_Integer;
799 else if( eType == FTDouble )
800 FT = DBF_FieldType_Double;
801 else if( eType == FTInvalid )
802 FT = DBF_FieldType_Invalid;
803 anAttr.myFieldType = FT;
811 const char* chAttr = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
812 anAttr.myIsNull = false;
813 anAttr.myFieldType = DBF_FieldType_String;
814 anAttr.myRawValue = chAttr;
815 anAttr.myStrVal = QString(chAttr);
821 int iAttr = DBFReadIntegerAttribute( myHDBF, i, theIndexOfField );
822 anAttr.myIsNull = false;
823 anAttr.myFieldType = DBF_FieldType_Integer;
824 anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
825 anAttr.myIntVal = iAttr;
831 double dAttr = DBFReadDoubleAttribute( myHDBF, i, theIndexOfField );
832 anAttr.myIsNull = false;
833 anAttr.myFieldType = DBF_FieldType_Double;
834 anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
835 anAttr.myDoubleVal = dAttr;
842 theAttrV.push_back(anAttr);
847 bool HYDROData_ShapeFile::DBF_WriteFieldAndValues(const QString& theFileName, const QString& theFieldName, DBF_FieldType theType, const std::vector<DBF_AttrValue>& theAttrV, bool bUseRawValue)
849 // Check that given field type is equal to field types of attributes values
850 for (size_t i = 0; i < theAttrV.size(); i++)
852 if (theAttrV[i].myFieldType != theType)
857 hDBF = DBFCreate( theFileName.toStdString().c_str() );
861 if (theType != DBF_FieldType_String && theType != DBF_FieldType_Integer && theType != DBF_FieldType_Double)
864 return false; //cant handle any other cases
870 case DBF_FieldType_String:
872 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTString, nWidth, 0);
876 case DBF_FieldType_Integer:
878 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTInteger, nWidth, 0);
882 case DBF_FieldType_Double:
884 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTDouble, nWidth, 0);
891 if (DBFGetFieldCount( hDBF ) != 1)
896 if (DBFGetRecordCount( hDBF ) != 0)
905 for (size_t i = 0; i < theAttrV.size(); i++)
907 if (!theAttrV[i].myIsNull)
908 stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myRawValue.c_str());
910 stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 );
921 for (size_t i = 0; i < theAttrV.size(); i++)
923 if (!theAttrV[i].myIsNull)
926 case DBF_FieldType_String:
928 stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myStrVal.toStdString().c_str());
932 case DBF_FieldType_Integer:
934 stat = DBFWriteIntegerAttribute(hDBF, (int)i, 0, theAttrV[i].myIntVal);
938 case DBF_FieldType_Double:
940 stat = DBFWriteDoubleAttribute(hDBF, (int)i, 0, theAttrV[i].myDoubleVal);
947 stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 );