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::Explorer 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() );
272 aSFW->ModifyTopologyMode() = Standard_False;
273 aSFW->ModifyGeometryMode() = Standard_False;
275 Handle(ShapeExtend_WireData) aSEWD = aSFW->WireData();
276 Standard_Integer nbE = aSEWD->NbEdges();
280 anPartStart.push_back(x.size());
281 NCollection_Sequence<gp_Pnt2d> aPnts;
282 for (Standard_Integer i = 1; i <= nbE; i++)
284 TopoDS_Edge E = aSEWD->Edge(i);
285 TopoDS_Vertex aV = TopExp::LastVertex(E, 1);
288 gp_Pnt P = BRep_Tool::Pnt(aV);
289 aPnts.Append(gp_Pnt2d(P.X(), P.Y()));
291 NCollection_Sequence<gp_Pnt2d> aNPnts;
292 aNPnts.Append(aPnts.First());
293 for (int j = 1; j <= aPnts.Size() - 1; j++)
295 if (!aPnts(j).IsEqual(aPnts(j + 1), Precision::Confusion()))
296 aNPnts.Append(aPnts(j + 1));
299 //assume that the orientation of extrenal wire & internal wires is correct
300 //so just write all points "as-is"
301 //External wire will be written in clockwise direction
302 //any other wires (holes) - in anticlockwise direction
303 for (int j = 1; j <= aNPnts.Size(); j++)
305 x.push_back( aNPnts(j).X());
306 y.push_back( aNPnts(j).Y());
308 x.push_back( aNPnts(1).X());
309 y.push_back( aNPnts(1).Y());
312 aSHPObj = SHPCreateObject( SHPT_POLYGON, -1, NbWires, &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
313 SHPWriteObject( theShpHandle, -1, aSHPObj );
314 SHPDestroyObject( aSHPObj );
318 bool HYDROData_ShapeFile::Parse(SHPHandle theHandle, ShapeType theType, int& theShapeTypeOfFile)
321 mySHPObjects.clear();
322 SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
323 theShapeTypeOfFile = aShapeType;
324 bool ToRead = (theType == ShapeType_Polyline && (aShapeType == 3 || aShapeType == 13 || aShapeType == 23)) ||
325 (theType == ShapeType_Polygon && aShapeType == 5);
328 for (int i = 0; i < theHandle->nRecords; i++)
329 mySHPObjects.push_back(SHPReadObject(theHandle, i));
336 void HYDROData_ShapeFile::ReadSHPPolygon(SHPObject* anObj, int i, TopoDS_Face& F)
342 int nParts = anObj->nParts;
343 gp_Pln pln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
345 //Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
346 //sfs->FixFaceTool()->FixOrientationMode() = 1;
347 TopTools_SequenceOfShape aWires;
348 for ( int i = 0 ; i < nParts ; i++ )
350 BRepBuilderAPI_MakeWire aBuilder;
351 int StartIndex = anObj->panPartStart[i];
354 EndIndex = anObj->panPartStart[i + 1];
356 EndIndex = anObj->nVertices;
358 for ( int k = StartIndex; k < EndIndex - 1 ; k++ )
360 gp_Pnt P1 (anObj->padfX[k], anObj->padfY[k], 0);
361 gp_Pnt P2 (anObj->padfX[k+1], anObj->padfY[k+1], 0);
362 if (P1.Distance(P2) < Precision::Confusion())
364 BRepBuilderAPI_MakeEdge aMakeEdge(P1, P2);
365 aBuilder.Add(TopoDS::Edge(aMakeEdge.Shape()));
369 W = TopoDS::Wire(aBuilder.Shape());
370 W.Orientation(TopAbs_FORWARD);
371 BRepBuilderAPI_MakeFace aDB(pln, W);
372 TopoDS_Face aDummyFace = TopoDS::Face(aDB.Shape());
373 BRepTopAdaptor_FClass2d FClass(aDummyFace, Precision::PConfusion());
374 if ( i == 0 && FClass.PerformInfinitePoint() == TopAbs_OUT)
376 if ( i > 0 && FClass.PerformInfinitePoint() != TopAbs_IN)
382 BRepBuilderAPI_MakeFace aFBuilder(pln, TopoDS::Wire(aWires(1)));
383 for (int i = 2; i <= aWires.Length(); i++)
384 aFBuilder.Add(TopoDS::Wire(aWires(i)));
385 F = TopoDS::Face(aFBuilder.Shape());
387 BRepLib::BuildCurves3d(F);
390 int HYDROData_ShapeFile::ImportPolygons(const QString theFileName, QStringList& thePolygonsList, TopTools_SequenceOfShape& theFaces, int& theShapeTypeOfFile)
393 int Stat = TryOpenShapeFile(theFileName);
396 myHSHP = SHPOpen( theFileName.toAscii().data(), "rb" );
397 if (!Parse(myHSHP, HYDROData_ShapeFile::ShapeType_Polygon, theShapeTypeOfFile))
399 for (size_t i = 0; i < mySHPObjects.size(); i++)
400 thePolygonsList.append("polygon_" + QString::number(i + 1));
403 if (myHSHP->nShapeType == 5)
405 for (size_t i = 0; i < mySHPObjects.size(); i++)
407 ReadSHPPolygon(mySHPObjects[i], i, aF);
416 void HYDROData_ShapeFile::Free()
418 for (size_t i = 0; i < mySHPObjects.size(); i++ )
419 free (mySHPObjects[i]);
421 mySHPObjects.clear();
430 void HYDROData_ShapeFile::ReadSHPPolyXY(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName,
431 int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
434 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
436 int nParts = anObj->nParts;
437 for ( int i = 0 ; i < nParts ; i++ )
439 int StartIndex = anObj->panPartStart[i];
442 EndIndex = anObj->panPartStart[i + 1];
444 EndIndex = anObj->nVertices;
446 bool IsClosed = false;
447 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
448 if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
449 anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] )
452 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true);
455 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false);
459 for ( int k = StartIndex; k < EndIndex ; k++ )
461 HYDROData_PolylineXY::Point aSectPoint;
462 aSectPoint.SetX( anObj->padfX[k] );
463 aSectPoint.SetY( anObj->padfY[k] );
464 aPolylineXY->AddPoint( i, aSectPoint );
469 aPolylineXY->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
470 aPolylineXY->SetName( theFileName + "_PolyXY_" + QString::number(theInd) );
472 aPolylineXY->Update();
473 theEntities.Append(aPolylineXY);
477 void HYDROData_ShapeFile::ReadSHPPoly3D(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName,
478 int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
480 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
482 Handle(HYDROData_Polyline3D) aPolylineObj = Handle(HYDROData_Polyline3D)::DownCast( theDocument->CreateObject( KIND_POLYLINE ) );
484 Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( theDocument->CreateObject( KIND_BATHYMETRY ) );
485 HYDROData_Bathymetry::AltitudePoints aAPoints;
487 int nParts = anObj->nParts;
488 for ( int i = 0 ; i < nParts ; i++ )
490 //bool aSectClosure = true;
491 int StartIndex = anObj->panPartStart[i];
494 EndIndex = anObj->panPartStart[i + 1];
496 EndIndex = anObj->nVertices;
498 bool IsClosed = false;
499 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
500 if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
501 anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] &&
502 anObj->padfZ[StartIndex] == anObj->padfZ[EndIndex - 1])
505 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true );
508 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false );
512 for ( int k = StartIndex ; k < EndIndex ; k++ )
514 HYDROData_PolylineXY::Point aSectPoint;
515 aSectPoint.SetX( anObj->padfX[k] );
516 aSectPoint.SetY( anObj->padfY[k] );
517 aPolylineXY->AddPoint( i, aSectPoint );
518 aAPoints.Append(gp_XYZ (anObj->padfX[k], anObj->padfY[k], anObj->padfZ[k]));
523 QString aBathName = theFileName + "_bath_" + QString::number(theInd);
524 QString aPolyXYName = theFileName + "_polyXY_" + QString::number(theInd);
525 QString aPoly3DName = theFileName + "_poly3D_" + QString::number(theInd);
527 aPolylineXY->SetName( aPolyXYName );
528 aPolylineXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
529 aPolylineXY->Update();
531 aBath->SetAltitudePoints(aAPoints);
532 aBath->SetName( aBathName );
534 aPolylineObj->SetPolylineXY (aPolylineXY, false);
535 aPolylineObj->SetAltitudeObject(aBath);
537 aPolylineObj->SetBorderColor( aPolylineObj->DefaultBorderColor() );
538 aPolylineObj->SetName( aPoly3DName );
540 aPolylineObj->Update();
541 theEntities.Append(aPolylineXY);
542 theEntities.Append(aPolylineObj);
548 int HYDROData_ShapeFile::ImportPolylines(Handle(HYDROData_Document) theDocument, const QString& theFileName,
549 NCollection_Sequence<Handle_HYDROData_Entity>& theEntities, int& theShapeTypeOfFile)
552 int aStat = TryOpenShapeFile(theFileName);
556 HYDROData_Iterator anIter( theDocument );
558 QStringList anExistingNames;
559 std::vector<int> anAllowedIndexes;
560 for( ; anIter.More(); anIter.Next() )
561 anExistingNames.push_back(anIter.Current()->GetName());
564 aHSHP = SHPOpen( theFileName.toAscii().data(), "rb" );
566 QFileInfo aFileInfo(theFileName);
567 QString aBaseFileName = aFileInfo.baseName();
569 if (!Parse(aHSHP, HYDROData_ShapeFile::ShapeType_Polyline, theShapeTypeOfFile))
571 if (aHSHP->nShapeType == 3 || aHSHP->nShapeType == 23)
574 for (;anAllowedIndexes.size() < mySHPObjects.size();)
576 if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)))
578 anAllowedIndexes.push_back(anInd);
585 for (size_t i = 0; i < mySHPObjects.size(); i++ )
587 ReadSHPPolyXY(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
591 else if (aHSHP->nShapeType == 13)
594 for (;anAllowedIndexes.size() < mySHPObjects.size();)
596 if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)) &&
597 !anExistingNames.contains(aBaseFileName + "_Poly3D_" + QString::number(anInd)) &&
598 !anExistingNames.contains(aBaseFileName + "_Bath_" + QString::number(anInd)))
600 anAllowedIndexes.push_back(anInd);
606 for (size_t i = 0; i < mySHPObjects.size(); i++ )
607 ReadSHPPoly3D(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
615 for (size_t i = 0; i < mySHPObjects.size(); i++ )
616 free (mySHPObjects[i]);
618 mySHPObjects.clear();
623 QString HYDROData_ShapeFile::GetShapeTypeName(int theType)
630 return "point (unsupported by HYDRO)";
632 return "arc/polyline (supported by HYDRO)";
634 return "polygon (supported by HYDRO)";
636 return "multipoint (unsupported by HYDRO)";
638 return "pointZ (unsupported by HYDRO)";
640 return "arcZ/polyline (supported by HYDRO)";
642 return "polygonZ (unsupported by HYDRO)";
644 return "multipointZ (unsupported by HYDRO)";
646 return "pointM (unsupported by HYDRO)";
648 return "arcM/polyline (supported by HYDRO)";
650 return "polygonM (unsupported by HYDRO)";
652 return "multipointM (unsupported by HYDRO)";
654 return "multipatch (unsupported by HYDRO)";
660 int HYDROData_ShapeFile::TryOpenShapeFile(QString theFileName)
662 QString aSHPfile = theFileName.simplified();
663 QString aSHXfile = theFileName.simplified().replace( ".shp", ".shx", Qt::CaseInsensitive);
664 FILE* pFileSHP = NULL;
665 pFileSHP = fopen (aSHPfile.toAscii().data(), "r");
666 FILE* pFileSHX = NULL;
667 pFileSHX = fopen (aSHXfile.toAscii().data(), "r");
669 if (pFileSHP == NULL || pFileSHX == NULL)
671 if (pFileSHP == NULL)
673 if (pFileSHX == NULL)
683 bool HYDROData_ShapeFile::CheckDBFFileExisting(const QString& theSHPFilePath, QString& thePathToDBFFile)
685 QString aSHPfile = theSHPFilePath.simplified();
686 QString aDBFfile = theSHPFilePath.simplified().replace( ".shp", ".dbf", Qt::CaseInsensitive);
687 FILE* pFileDBF = NULL;
688 pFileDBF = fopen (aDBFfile.toAscii().data(), "r");
690 if (pFileDBF == NULL)
696 thePathToDBFFile = aDBFfile;
701 bool HYDROData_ShapeFile::DBF_OpenDBF(const QString& thePathToDBFFile)
703 myHDBF = DBFOpen( thePathToDBFFile.toAscii().data(), "r" );
710 int HYDROData_ShapeFile::DBF_GetNbFields()
714 return DBFGetFieldCount(myHDBF);
717 void HYDROData_ShapeFile::DBF_CloseDBF()
723 QStringList HYDROData_ShapeFile::DBF_GetFieldList()
725 QStringList FieldList;
726 int nWidth, nDecimals;
729 for( int i = 0; i < DBFGetFieldCount(myHDBF); i++ )
732 eType = DBFGetFieldInfo( myHDBF, i, chField, &nWidth, &nDecimals );
733 FieldList.append(QString(chField));
739 void HYDROData_ShapeFile::DBF_GetFieldTypeList(std::vector<DBF_FieldType>& FTVect)
741 int nWidth, nDecimals;
744 for( int i = 0; i < DBFGetFieldCount(myHDBF); i++ )
747 eType = DBFGetFieldInfo( myHDBF, i, chField, &nWidth, &nDecimals );
748 if( eType == FTString )
749 FT = DBF_FieldType_String;
750 else if( eType == FTInteger )
751 FT = DBF_FieldType_Integer;
752 else if( eType == FTDouble )
753 FT = DBF_FieldType_Double;
754 else if( eType == FTInvalid )
755 FT = DBF_FieldType_Invalid;
757 FTVect.push_back(FT);
762 int HYDROData_ShapeFile::DBF_GetNbRecords()
766 return DBFGetRecordCount(myHDBF);
769 void HYDROData_ShapeFile::DBF_GetAttributeList(int theIndexOfField, std::vector<DBF_AttrValue>& theAttrV)
771 int nWidth, nDecimals;
774 for( int i = 0; i < DBFGetRecordCount(myHDBF); i++ )
777 DBF_AttrValue anAttr;
778 eType = DBFGetFieldInfo( myHDBF, theIndexOfField, chField, &nWidth, &nDecimals );
780 if( DBFIsAttributeNULL( myHDBF, i, theIndexOfField ) )
782 anAttr.myIsNull = true;
783 DBF_FieldType FT = DBF_FieldType_None;
784 if( eType == FTString )
785 FT = DBF_FieldType_String;
786 else if( eType == FTInteger )
787 FT = DBF_FieldType_Integer;
788 else if( eType == FTDouble )
789 FT = DBF_FieldType_Double;
790 else if( eType == FTInvalid )
791 FT = DBF_FieldType_Invalid;
792 anAttr.myFieldType = FT;
800 const char* chAttr = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
801 anAttr.myIsNull = false;
802 anAttr.myFieldType = DBF_FieldType_String;
803 anAttr.myRawValue = chAttr;
804 anAttr.myStrVal = QString(chAttr);
810 int iAttr = DBFReadIntegerAttribute( myHDBF, i, theIndexOfField );
811 anAttr.myIsNull = false;
812 anAttr.myFieldType = DBF_FieldType_Integer;
813 anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
814 anAttr.myIntVal = iAttr;
820 double dAttr = DBFReadDoubleAttribute( myHDBF, i, theIndexOfField );
821 anAttr.myIsNull = false;
822 anAttr.myFieldType = DBF_FieldType_Double;
823 anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
824 anAttr.myDoubleVal = dAttr;
831 theAttrV.push_back(anAttr);
836 bool HYDROData_ShapeFile::DBF_WriteFieldAndValues(const QString& theFileName, const QString& theFieldName, DBF_FieldType theType, const std::vector<DBF_AttrValue>& theAttrV, bool bUseRawValue)
838 // Check that given field type is equal to field types of attributes values
839 for (size_t i = 0; i < theAttrV.size(); i++)
841 if (theAttrV[i].myFieldType != theType)
846 hDBF = DBFCreate( theFileName.toStdString().c_str() );
850 if (theType != DBF_FieldType_String && theType != DBF_FieldType_Integer && theType != DBF_FieldType_Double)
853 return false; //cant handle any other cases
859 case DBF_FieldType_String:
861 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTString, nWidth, 0);
865 case DBF_FieldType_Integer:
867 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTInteger, nWidth, 0);
871 case DBF_FieldType_Double:
873 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTDouble, nWidth, 0);
880 if (DBFGetFieldCount( hDBF ) != 1)
885 if (DBFGetRecordCount( hDBF ) != 0)
894 for (size_t i = 0; i < theAttrV.size(); i++)
896 if (!theAttrV[i].myIsNull)
897 stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myRawValue.c_str());
899 stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 );
910 for (size_t i = 0; i < theAttrV.size(); i++)
912 if (!theAttrV[i].myIsNull)
915 case DBF_FieldType_String:
917 stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myStrVal.toStdString().c_str());
921 case DBF_FieldType_Integer:
923 stat = DBFWriteIntegerAttribute(hDBF, (int)i, 0, theAttrV[i].myIntVal);
927 case DBF_FieldType_Double:
929 stat = DBFWriteDoubleAttribute(hDBF, (int)i, 0, theAttrV[i].myDoubleVal);
936 stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 );