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 // Try to reorder edges
268 Handle(ShapeFix_Wire) aSFW = new ShapeFix_Wire( aW, theFace, Precision::Confusion() );
270 Handle(ShapeExtend_WireData) aSEWD = aSFW->WireData();
271 Standard_Integer nbE = aSEWD->NbEdges();
273 anPartStart.push_back(x.size());
274 NCollection_Sequence<gp_Pnt2d> aPnts;
275 for (Standard_Integer i = 1; i <= nbE; i++)
277 TopoDS_Edge E = aSEWD->Edge(i);
278 TopoDS_Vertex aV = TopExp::LastVertex(E, 1);
281 gp_Pnt P = BRep_Tool::Pnt(aV);
282 aPnts.Append(gp_Pnt2d(P.X(), P.Y()));
284 NCollection_Sequence<gp_Pnt2d> aNPnts;
285 aNPnts.Append(aPnts.First());
286 for (int j = 1; j <= aPnts.Size() - 1; j++)
288 if (!aPnts(j).IsEqual(aPnts(j + 1), Precision::Confusion()))
289 aNPnts.Append(aPnts(j + 1));
295 //clockwise direction
296 for (int j = 1; j <= aNPnts.Size(); j++)
298 x.push_back( aNPnts(j).X());
299 y.push_back( aNPnts(j).Y());
301 x.push_back( aNPnts(1).X());
302 y.push_back( aNPnts(1).Y());
307 //counter-clockwise direction
308 for (int j = aNPnts.Size(); j > 0; j--)
310 x.push_back( aNPnts(j).X());
311 y.push_back( aNPnts(j).Y());
313 x.push_back( aNPnts(aNPnts.Size()).X());
314 y.push_back( aNPnts(aNPnts.Size()).Y());
318 aSHPObj = SHPCreateObject( SHPT_POLYGON, -1, NbWires, &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
319 SHPWriteObject( theShpHandle, -1, aSHPObj );
320 SHPDestroyObject( aSHPObj );
324 bool HYDROData_ShapeFile::Parse(SHPHandle theHandle, ShapeType theType, int& theShapeTypeOfFile)
327 mySHPObjects.clear();
328 SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
329 theShapeTypeOfFile = aShapeType;
330 bool ToRead = (theType == ShapeType_Polyline && (aShapeType == 3 || aShapeType == 13 || aShapeType == 23)) ||
331 (theType == ShapeType_Polygon && aShapeType == 5);
334 for (int i = 0; i < theHandle->nRecords; i++)
335 mySHPObjects.push_back(SHPReadObject(theHandle, i));
342 void HYDROData_ShapeFile::ReadSHPPolygon(SHPObject* anObj, int i, TopoDS_Face& F)
346 int nParts = anObj->nParts;
347 gp_Pln pln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
349 //Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
350 //sfs->FixFaceTool()->FixOrientationMode() = 1;
351 TopTools_SequenceOfShape aWires;
352 for ( int i = 0 ; i < nParts ; i++ )
354 BRepBuilderAPI_MakeWire aBuilder;
355 int StartIndex = anObj->panPartStart[i];
358 EndIndex = anObj->panPartStart[i + 1];
360 EndIndex = anObj->nVertices;
362 for ( int k = StartIndex; k < EndIndex - 1 ; k++ )
364 gp_Pnt P1 (anObj->padfX[k], anObj->padfY[k], 0);
365 gp_Pnt P2 (anObj->padfX[k+1], anObj->padfY[k+1], 0);
366 if (P1.Distance(P2) < Precision::Confusion())
368 BRepBuilderAPI_MakeEdge aMakeEdge(P1, P2);
369 aBuilder.Add(TopoDS::Edge(aMakeEdge.Shape()));
373 W = TopoDS::Wire(aBuilder.Shape());
374 W.Orientation(TopAbs_FORWARD);
375 BRepBuilderAPI_MakeFace aDB(pln, W);
376 TopoDS_Face aDummyFace = TopoDS::Face(aDB.Shape());
377 BRepTopAdaptor_FClass2d FClass(aDummyFace, Precision::PConfusion());
378 if ( i == 0 && FClass.PerformInfinitePoint() == TopAbs_OUT)
380 if ( i > 0 && FClass.PerformInfinitePoint() != TopAbs_IN)
386 BRepBuilderAPI_MakeFace aFBuilder(pln, TopoDS::Wire(aWires(1)));
387 for (int i = 2; i <= aWires.Length(); i++)
388 aFBuilder.Add(TopoDS::Wire(aWires(i)));
389 F = TopoDS::Face(aFBuilder.Shape());
391 BRepLib::BuildCurves3d(F);
394 int HYDROData_ShapeFile::ImportPolygons(const QString theFileName, QStringList& thePolygonsList, TopTools_SequenceOfShape& theFaces, int& theShapeTypeOfFile)
397 int Stat = TryOpenShapeFile(theFileName);
400 myHSHP = SHPOpen( theFileName.toAscii().data(), "rb" );
401 if (!Parse(myHSHP, HYDROData_ShapeFile::ShapeType_Polygon, theShapeTypeOfFile))
403 for (size_t i = 0; i < mySHPObjects.size(); i++)
404 thePolygonsList.append("polygon_" + QString::number(i + 1));
407 if (myHSHP->nShapeType == 5)
409 for (size_t i = 0; i < mySHPObjects.size(); i++)
411 ReadSHPPolygon(mySHPObjects[i], i, aF);
420 void HYDROData_ShapeFile::Free()
422 for (size_t i = 0; i < mySHPObjects.size(); i++ )
423 free (mySHPObjects[i]);
425 mySHPObjects.clear();
434 void HYDROData_ShapeFile::ReadSHPPolyXY(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName,
435 int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
438 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
440 int nParts = anObj->nParts;
441 for ( int i = 0 ; i < nParts ; i++ )
443 int StartIndex = anObj->panPartStart[i];
446 EndIndex = anObj->panPartStart[i + 1];
448 EndIndex = anObj->nVertices;
450 bool IsClosed = false;
451 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
452 if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
453 anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] )
456 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true);
459 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false);
463 for ( int k = StartIndex; k < EndIndex ; k++ )
465 HYDROData_PolylineXY::Point aSectPoint;
466 aSectPoint.SetX( anObj->padfX[k] );
467 aSectPoint.SetY( anObj->padfY[k] );
468 aPolylineXY->AddPoint( i, aSectPoint );
473 aPolylineXY->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
474 aPolylineXY->SetName( theFileName + "_PolyXY_" + QString::number(theInd) );
476 aPolylineXY->Update();
477 theEntities.Append(aPolylineXY);
481 void HYDROData_ShapeFile::ReadSHPPoly3D(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName,
482 int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
484 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
486 Handle(HYDROData_Polyline3D) aPolylineObj = Handle(HYDROData_Polyline3D)::DownCast( theDocument->CreateObject( KIND_POLYLINE ) );
488 Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( theDocument->CreateObject( KIND_BATHYMETRY ) );
489 HYDROData_Bathymetry::AltitudePoints aAPoints;
491 int nParts = anObj->nParts;
492 for ( int i = 0 ; i < nParts ; i++ )
494 //bool aSectClosure = true;
495 int StartIndex = anObj->panPartStart[i];
498 EndIndex = anObj->panPartStart[i + 1];
500 EndIndex = anObj->nVertices;
502 bool IsClosed = false;
503 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
504 if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
505 anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] &&
506 anObj->padfZ[StartIndex] == anObj->padfZ[EndIndex - 1])
509 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true );
512 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false );
516 for ( int k = StartIndex ; k < EndIndex ; k++ )
518 HYDROData_PolylineXY::Point aSectPoint;
519 aSectPoint.SetX( anObj->padfX[k] );
520 aSectPoint.SetY( anObj->padfY[k] );
521 aPolylineXY->AddPoint( i, aSectPoint );
522 aAPoints.Append(gp_XYZ (anObj->padfX[k], anObj->padfY[k], anObj->padfZ[k]));
527 QString aBathName = theFileName + "_bath_" + QString::number(theInd);
528 QString aPolyXYName = theFileName + "_polyXY_" + QString::number(theInd);
529 QString aPoly3DName = theFileName + "_poly3D_" + QString::number(theInd);
531 aPolylineXY->SetName( aPolyXYName );
532 aPolylineXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
533 aPolylineXY->Update();
535 aBath->SetAltitudePoints(aAPoints);
536 aBath->SetName( aBathName );
538 aPolylineObj->SetPolylineXY (aPolylineXY, false);
539 aPolylineObj->SetAltitudeObject(aBath);
541 aPolylineObj->SetBorderColor( aPolylineObj->DefaultBorderColor() );
542 aPolylineObj->SetName( aPoly3DName );
544 aPolylineObj->Update();
545 theEntities.Append(aPolylineXY);
546 theEntities.Append(aPolylineObj);
552 int HYDROData_ShapeFile::ImportPolylines(Handle(HYDROData_Document) theDocument, const QString& theFileName,
553 NCollection_Sequence<Handle_HYDROData_Entity>& theEntities, int& theShapeTypeOfFile)
556 int aStat = TryOpenShapeFile(theFileName);
560 HYDROData_Iterator anIter( theDocument );
562 QStringList anExistingNames;
563 std::vector<int> anAllowedIndexes;
564 for( ; anIter.More(); anIter.Next() )
565 anExistingNames.push_back(anIter.Current()->GetName());
568 aHSHP = SHPOpen( theFileName.toAscii().data(), "rb" );
570 QFileInfo aFileInfo(theFileName);
571 QString aBaseFileName = aFileInfo.baseName();
573 if (!Parse(aHSHP, HYDROData_ShapeFile::ShapeType_Polyline, theShapeTypeOfFile))
575 if (aHSHP->nShapeType == 3 || aHSHP->nShapeType == 23)
578 for (;anAllowedIndexes.size() < mySHPObjects.size();)
580 if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)))
582 anAllowedIndexes.push_back(anInd);
589 for (size_t i = 0; i < mySHPObjects.size(); i++ )
591 ReadSHPPolyXY(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
595 else if (aHSHP->nShapeType == 13)
598 for (;anAllowedIndexes.size() < mySHPObjects.size();)
600 if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)) &&
601 !anExistingNames.contains(aBaseFileName + "_Poly3D_" + QString::number(anInd)) &&
602 !anExistingNames.contains(aBaseFileName + "_Bath_" + QString::number(anInd)))
604 anAllowedIndexes.push_back(anInd);
610 for (size_t i = 0; i < mySHPObjects.size(); i++ )
611 ReadSHPPoly3D(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
619 for (size_t i = 0; i < mySHPObjects.size(); i++ )
620 free (mySHPObjects[i]);
622 mySHPObjects.clear();
627 QString HYDROData_ShapeFile::GetShapeTypeName(int theType)
634 return "point (unsupported by HYDRO)";
636 return "arc/polyline (supported by HYDRO)";
638 return "polygon (supported by HYDRO)";
640 return "multipoint (unsupported by HYDRO)";
642 return "pointZ (unsupported by HYDRO)";
644 return "arcZ/polyline (supported by HYDRO)";
646 return "polygonZ (unsupported by HYDRO)";
648 return "multipointZ (unsupported by HYDRO)";
650 return "pointM (unsupported by HYDRO)";
652 return "arcM/polyline (supported by HYDRO)";
654 return "polygonM (unsupported by HYDRO)";
656 return "multipointM (unsupported by HYDRO)";
658 return "multipatch (unsupported by HYDRO)";
664 int HYDROData_ShapeFile::TryOpenShapeFile(QString theFileName)
666 QString aSHPfile = theFileName.simplified();
667 QString aSHXfile = theFileName.simplified().replace( ".shp", ".shx", Qt::CaseInsensitive);
668 FILE* pFileSHP = NULL;
669 pFileSHP = fopen (aSHPfile.toAscii().data(), "r");
670 FILE* pFileSHX = NULL;
671 pFileSHX = fopen (aSHXfile.toAscii().data(), "r");
673 if (pFileSHP == NULL || pFileSHX == NULL)
675 if (pFileSHP == NULL)
677 if (pFileSHX == NULL)
687 bool HYDROData_ShapeFile::CheckDBFFileExisting(const QString& theSHPFilePath, QString& thePathToDBFFile)
689 QString aSHPfile = theSHPFilePath.simplified();
690 QString aDBFfile = theSHPFilePath.simplified().replace( ".shp", ".dbf", Qt::CaseInsensitive);
691 FILE* pFileDBF = NULL;
692 pFileDBF = fopen (aDBFfile.toAscii().data(), "r");
694 if (pFileDBF == NULL)
700 thePathToDBFFile = aDBFfile;
705 bool HYDROData_ShapeFile::DBF_OpenDBF(const QString& thePathToDBFFile)
707 myHDBF = DBFOpen( thePathToDBFFile.toAscii().data(), "r" );
714 int HYDROData_ShapeFile::DBF_GetNbFields()
718 return DBFGetFieldCount(myHDBF);
721 void HYDROData_ShapeFile::DBF_CloseDBF()
727 QStringList HYDROData_ShapeFile::DBF_GetFieldList()
729 QStringList FieldList;
730 int nWidth, nDecimals;
733 for( int i = 0; i < DBFGetFieldCount(myHDBF); i++ )
736 eType = DBFGetFieldInfo( myHDBF, i, chField, &nWidth, &nDecimals );
737 FieldList.append(QString(chField));
743 void HYDROData_ShapeFile::DBF_GetFieldTypeList(std::vector<DBF_FieldType>& FTVect)
745 int nWidth, nDecimals;
748 for( int i = 0; i < DBFGetFieldCount(myHDBF); i++ )
751 eType = DBFGetFieldInfo( myHDBF, i, chField, &nWidth, &nDecimals );
752 if( eType == FTString )
753 FT = DBF_FieldType_String;
754 else if( eType == FTInteger )
755 FT = DBF_FieldType_Integer;
756 else if( eType == FTDouble )
757 FT = DBF_FieldType_Double;
758 else if( eType == FTInvalid )
759 FT = DBF_FieldType_Invalid;
761 FTVect.push_back(FT);
766 int HYDROData_ShapeFile::DBF_GetNbRecords()
770 return DBFGetRecordCount(myHDBF);
773 void HYDROData_ShapeFile::DBF_GetAttributeList(int theIndexOfField, std::vector<DBF_AttrValue>& theAttrV)
775 int nWidth, nDecimals;
778 for( int i = 0; i < DBFGetRecordCount(myHDBF); i++ )
781 DBF_AttrValue anAttr;
782 eType = DBFGetFieldInfo( myHDBF, theIndexOfField, chField, &nWidth, &nDecimals );
784 if( DBFIsAttributeNULL( myHDBF, i, theIndexOfField ) )
786 anAttr.myIsNull = true;
787 DBF_FieldType FT = DBF_FieldType_None;
788 if( eType == FTString )
789 FT = DBF_FieldType_String;
790 else if( eType == FTInteger )
791 FT = DBF_FieldType_Integer;
792 else if( eType == FTDouble )
793 FT = DBF_FieldType_Double;
794 else if( eType == FTInvalid )
795 FT = DBF_FieldType_Invalid;
796 anAttr.myFieldType = FT;
804 const char* chAttr = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
805 anAttr.myIsNull = false;
806 anAttr.myFieldType = DBF_FieldType_String;
807 anAttr.myRawValue = chAttr;
808 anAttr.myStrVal = QString(chAttr);
814 int iAttr = DBFReadIntegerAttribute( myHDBF, i, theIndexOfField );
815 anAttr.myIsNull = false;
816 anAttr.myFieldType = DBF_FieldType_Integer;
817 anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
818 anAttr.myIntVal = iAttr;
824 double dAttr = DBFReadDoubleAttribute( myHDBF, i, theIndexOfField );
825 anAttr.myIsNull = false;
826 anAttr.myFieldType = DBF_FieldType_Double;
827 anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
828 anAttr.myDoubleVal = dAttr;
835 theAttrV.push_back(anAttr);
840 bool HYDROData_ShapeFile::DBF_WriteFieldAndValues(const QString& theFileName, const QString& theFieldName, DBF_FieldType theType, const std::vector<DBF_AttrValue>& theAttrV, bool bUseRawValue)
842 // Check that given field type is equal to field types of attributes values
843 for (size_t i = 0; i < theAttrV.size(); i++)
845 if (theAttrV[i].myFieldType != theType)
850 hDBF = DBFCreate( theFileName.toStdString().c_str() );
854 if (theType != DBF_FieldType_String && theType != DBF_FieldType_Integer && theType != DBF_FieldType_Double)
857 return false; //cant handle any other cases
863 case DBF_FieldType_String:
865 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTString, nWidth, 0);
869 case DBF_FieldType_Integer:
871 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTInteger, nWidth, 0);
875 case DBF_FieldType_Double:
877 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTDouble, nWidth, 0);
884 if (DBFGetFieldCount( hDBF ) != 1)
889 if (DBFGetRecordCount( hDBF ) != 0)
898 for (size_t i = 0; i < theAttrV.size(); i++)
900 if (!theAttrV[i].myIsNull)
901 stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myRawValue.c_str());
903 stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 );
914 for (size_t i = 0; i < theAttrV.size(); i++)
916 if (!theAttrV[i].myIsNull)
919 case DBF_FieldType_String:
921 stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myStrVal.toStdString().c_str());
925 case DBF_FieldType_Integer:
927 stat = DBFWriteIntegerAttribute(hDBF, (int)i, 0, theAttrV[i].myIntVal);
931 case DBF_FieldType_Double:
933 stat = DBFWriteDoubleAttribute(hDBF, (int)i, 0, theAttrV[i].myDoubleVal);
940 stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 );