1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include <HYDROData_ShapeFile.h>
24 #include <HYDROData_PolylineXY.h>
25 #include <HYDROData_Polyline3D.h>
26 #include <HYDROData_Bathymetry.h>
27 #include <HYDROData_Profile.h>
28 #include <HYDROData_Iterator.h>
33 #include <TopExp_Explorer.hxx>
34 #include <TopoDS_Wire.hxx>
35 #include <TopoDS_Vertex.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <TopoDS_Face.hxx>
38 #include <BRep_Tool.hxx>
39 #include <Precision.hxx>
40 #include <Handle_Geom_Curve.hxx>
41 #include <Handle_Geom_Line.hxx>
42 #include <Handle_Geom_TrimmedCurve.hxx>
43 #include <Geom_TrimmedCurve.hxx>
44 #include <BRepBuilderAPI_MakeEdge.hxx>
45 #include <BRepBuilderAPI_MakeWire.hxx>
46 #include <BRepBuilderAPI_MakeFace.hxx>
48 #include <BRepLib.hxx>
49 #include <ShapeFix_Shape.hxx>
50 #include <TopTools_SequenceOfShape.hxx>
52 #include <BRepTopAdaptor_FClass2d.hxx>
55 #pragma warning( disable: 4996 )
58 HYDROData_ShapeFile::HYDROData_ShapeFile() : myHSHP(NULL)
62 HYDROData_ShapeFile::~HYDROData_ShapeFile()
67 void HYDROData_ShapeFile::Export(const QString& aFileName,
68 NCollection_Sequence<Handle_HYDROData_PolylineXY> aPolyXYSeq,
69 NCollection_Sequence<Handle_HYDROData_Polyline3D> aPoly3DSeq,
70 const Handle_HYDROData_LandCoverMap& aLCSeq,
71 QStringList& aNonExpList)
75 if (!aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty())
77 hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARC );
78 for (int i = 1; i <= aPolyXYSeq.Size(); i++)
79 if (WriteObjectPolyXY(hSHPHandle, aPolyXYSeq(i)) != 1)
80 aNonExpList.append(aPolyXYSeq(i)->GetName());
83 else if (aPolyXYSeq.IsEmpty() && !aPoly3DSeq.IsEmpty())
85 hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARCZ );
86 for (int i = 1; i <= aPoly3DSeq.Size(); i++)
87 if (WriteObjectPoly3D(hSHPHandle, aPoly3DSeq(i)) != 1)
88 aNonExpList.append(aPoly3DSeq(i)->GetName());
90 else if (aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty() && !aLCSeq.IsEmpty())
92 hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_POLYGON );
93 for (int i = 1; i <= aLCSeq.Size(); i++)
94 if (WriteObjectLC(hSHPHandle, aLCSeq(i)) != 1)
95 aNonExpList.append(aLCSeq(i)->GetName());
97 if (hSHPHandle->nRecords > 0)
98 SHPClose( hSHPHandle );
101 SHPClose( hSHPHandle );
102 QString aFN = aFileName.simplified();
103 remove (aFN.toStdString().c_str());
104 remove (aFN.replace( ".shp", ".shx", Qt::CaseInsensitive).toStdString().c_str());
108 int HYDROData_ShapeFile::WriteObjectPolyXY(SHPHandle theShpHandle, Handle_HYDROData_PolylineXY thePoly )
111 std::vector<double> x, y;
112 std::vector<int> anPartStart;
114 for (int i = 0; i < thePoly->NbSections(); i++)
115 if (thePoly->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE)
118 for (int i = 0; i < thePoly->NbSections(); i++)
120 anPartStart.push_back(x.size());
121 HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPoints(i);
122 for (int j = 1; j <= aPointList.Size(); j++)
124 x.push_back( aPointList(j).X());
125 y.push_back( aPointList(j).Y());
127 if (thePoly->IsClosedSection(i))
129 x.push_back( aPointList(1).X());
130 y.push_back( aPointList(1).Y());
134 aSHPObj = SHPCreateObject( SHPT_ARC, -1, thePoly->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
135 SHPWriteObject( theShpHandle, -1, aSHPObj );
136 SHPDestroyObject( aSHPObj );
140 int HYDROData_ShapeFile::WriteObjectPoly3D(SHPHandle theShpHandle, Handle_HYDROData_Polyline3D thePoly )
143 std::vector<double> x, y, z;
144 std::vector<int> anPartStart;
146 for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)
147 if (thePoly->GetPolylineXY()->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE)
150 for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)
152 anPartStart.push_back(x.size());
153 HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPolylineXY()->GetPoints(i);
154 for (int j = 1; j <= aPointList.Size(); j++)
156 x.push_back( aPointList(j).X());
157 y.push_back( aPointList(j).Y());
158 z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(j).X(), aPointList(j).Y())));
160 if ( thePoly->GetPolylineXY()->IsClosedSection(i))
162 x.push_back( aPointList(1).X());
163 y.push_back( aPointList(1).Y());
164 z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(1).X(), aPointList(1).Y())));
169 aSHPObj = SHPCreateObject( SHPT_ARCZ, -1, thePoly->GetPolylineXY()->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], &z[0], NULL );
170 SHPWriteObject( theShpHandle, -1, aSHPObj );
171 SHPDestroyObject( aSHPObj );
175 /*TODO:int HYDROData_ShapeFile::WriteObjectLC(SHPHandle theShpHandle, Handle_HYDROData_LandCover theLC )
177 TopoDS_Shape aSh = theLC->GetShape();
180 TopExp_Explorer anEdgeEx(aSh, TopAbs_EDGE);
181 for (; anEdgeEx.More(); anEdgeEx.Next())
183 TopoDS_Edge E = TopoDS::Edge(anEdgeEx.Current());
185 Handle_Geom_Curve aCur = BRep_Tool::Curve(E, aFP, aLP);
186 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCur);
189 Handle(Geom_TrimmedCurve) aTC = Handle(Geom_TrimmedCurve)::DownCast(aCur);
192 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aTC->BasisCurve());
201 if (aSh.ShapeType() == TopAbs_FACE)
203 ProcessFace(TopoDS::Face(aSh), theShpHandle);
205 else if (aSh.ShapeType() == TopAbs_COMPOUND)
207 TopExp_Explorer Ex(aSh, TopAbs_FACE);
208 for (; Ex.More(); Ex.Next())
210 TopoDS_Face aF = TopoDS::Face(Ex.Current());
213 ProcessFace(aF, theShpHandle);
223 void HYDROData_ShapeFile::ProcessFace(TopoDS_Face theFace, SHPHandle theShpHandle)
226 std::vector<double> x, y;
227 std::vector<int> anPartStart;
228 if (theFace.ShapeType() == TopAbs_FACE)
230 TopExp_Explorer Ex(theFace, TopAbs_WIRE);
232 for (; Ex.More(); Ex.Next())
234 TopoDS_Wire aW = TopoDS::Wire(Ex.Current());
238 anPartStart.push_back(x.size());
239 TopExp_Explorer aVEx(aW, TopAbs_VERTEX);
240 NCollection_Sequence<gp_Pnt2d> aPnts;
241 for (; aVEx.More(); aVEx.Next())
243 TopoDS_Vertex aV = TopoDS::Vertex(aVEx.Current());
246 gp_Pnt P = BRep_Tool::Pnt(aV);
247 aPnts.Append(gp_Pnt2d(P.X(), P.Y()));
249 NCollection_Sequence<gp_Pnt2d> aNPnts;
250 aNPnts.Append(aPnts.First());
251 for (int j = 1; j <= aPnts.Size() - 1; j++)
253 if (!aPnts(j).IsEqual(aPnts(j + 1), Precision::Confusion()))
254 aNPnts.Append(aPnts(j + 1));
257 for (int j = 1; j <= aNPnts.Size(); j++)
259 x.push_back( aNPnts(j).X());
260 y.push_back( aNPnts(j).Y());
262 //x.push_back( aNPnts(1).X());
263 //y.push_back( aNPnts(1).Y());
267 aSHPObj = SHPCreateObject( SHPT_POLYGON, -1, NbWires, &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
268 SHPWriteObject( theShpHandle, -1, aSHPObj );
269 SHPDestroyObject( aSHPObj );
275 bool HYDROData_ShapeFile::Parse(SHPHandle theHandle, ShapeType theType, int& theShapeTypeOfFile)
278 mySHPObjects.clear();
279 SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
280 theShapeTypeOfFile = aShapeType;
281 bool ToRead = (theType == ShapeType_Polyline && (aShapeType == 3 || aShapeType == 13 || aShapeType == 23)) ||
282 (theType == ShapeType_Polygon && aShapeType == 5);
285 for (int i = 0; i < theHandle->nRecords; i++)
286 mySHPObjects.push_back(SHPReadObject(theHandle, i));
293 void HYDROData_ShapeFile::ReadSHPPolygon(SHPObject* anObj, int i, TopoDS_Face& F)
297 int nParts = anObj->nParts;
298 gp_Pln pln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
300 //Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
301 //sfs->FixFaceTool()->FixOrientationMode() = 1;
302 TopTools_SequenceOfShape aWires;
303 for ( int i = 0 ; i < nParts ; i++ )
305 BRepBuilderAPI_MakeWire aBuilder;
306 int StartIndex = anObj->panPartStart[i];
309 EndIndex = anObj->panPartStart[i + 1];
311 EndIndex = anObj->nVertices;
313 for ( int k = StartIndex; k < EndIndex - 1 ; k++ )
315 gp_Pnt P1 (anObj->padfX[k], anObj->padfY[k], 0);
316 gp_Pnt P2 (anObj->padfX[k+1], anObj->padfY[k+1], 0);
317 if (P1.Distance(P2) < Precision::Confusion())
319 BRepBuilderAPI_MakeEdge aMakeEdge(P1, P2);
320 aBuilder.Add(TopoDS::Edge(aMakeEdge.Shape()));
324 W = TopoDS::Wire(aBuilder.Shape());
325 W.Orientation(TopAbs_FORWARD);
326 BRepBuilderAPI_MakeFace aDB(pln, W);
327 TopoDS_Face aDummyFace = TopoDS::Face(aDB.Shape());
328 BRepTopAdaptor_FClass2d FClass(aDummyFace, Precision::PConfusion());
329 if ( i == 0 && FClass.PerformInfinitePoint() == TopAbs_OUT)
331 if ( i > 0 && FClass.PerformInfinitePoint() != TopAbs_IN)
337 BRepBuilderAPI_MakeFace aFBuilder(pln, TopoDS::Wire(aWires(1)));
338 for (int i = 2; i <= aWires.Length(); i++)
339 aFBuilder.Add(TopoDS::Wire(aWires(i)));
340 TopoDS_Face DF = TopoDS::Face(aFBuilder.Shape());
342 BRepLib::BuildCurves3d(DF);
347 F = DF; //TopoDS::Face(sfs->Shape());
351 int HYDROData_ShapeFile::ImportLandCovers(const QString theFileName, QStringList& thePolygonsList, TopTools_SequenceOfShape& theFaces, int& theShapeTypeOfFile)
354 int Stat = TryOpenShapeFile(theFileName);
357 myHSHP = SHPOpen( theFileName.toAscii().data(), "rb" );
358 if (!Parse(myHSHP, HYDROData_ShapeFile::ShapeType_Polygon, theShapeTypeOfFile))
360 for (size_t i = 0; i < mySHPObjects.size(); i++)
361 thePolygonsList.append("polygon_" + QString::number(i + 1));
364 if (myHSHP->nShapeType == 5)
366 for (size_t i = 0; i < mySHPObjects.size(); i++)
368 ReadSHPPolygon(mySHPObjects[i], i, aF);
377 void HYDROData_ShapeFile::Free()
379 for (size_t i = 0; i < mySHPObjects.size(); i++ )
380 free (mySHPObjects[i]);
382 mySHPObjects.clear();
391 void HYDROData_ShapeFile::ReadSHPPolyXY(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName,
392 int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
395 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
397 int nParts = anObj->nParts;
398 for ( int i = 0 ; i < nParts ; i++ )
400 int StartIndex = anObj->panPartStart[i];
403 EndIndex = anObj->panPartStart[i + 1];
405 EndIndex = anObj->nVertices;
407 bool IsClosed = false;
408 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
409 if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
410 anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] )
413 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true);
416 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false);
420 for ( int k = StartIndex; k < EndIndex ; k++ )
422 HYDROData_PolylineXY::Point aSectPoint;
423 aSectPoint.SetX( anObj->padfX[k] );
424 aSectPoint.SetY( anObj->padfY[k] );
425 aPolylineXY->AddPoint( i, aSectPoint );
430 aPolylineXY->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
431 aPolylineXY->SetName( theFileName + "_PolyXY_" + QString::number(theInd) );
433 aPolylineXY->Update();
434 theEntities.Append(aPolylineXY);
438 void HYDROData_ShapeFile::ReadSHPPoly3D(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName,
439 int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
441 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
443 Handle(HYDROData_Polyline3D) aPolylineObj = Handle(HYDROData_Polyline3D)::DownCast( theDocument->CreateObject( KIND_POLYLINE ) );
445 Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( theDocument->CreateObject( KIND_BATHYMETRY ) );
446 HYDROData_Bathymetry::AltitudePoints aAPoints;
448 int nParts = anObj->nParts;
449 for ( int i = 0 ; i < nParts ; i++ )
451 //bool aSectClosure = true;
452 int StartIndex = anObj->panPartStart[i];
455 EndIndex = anObj->panPartStart[i + 1];
457 EndIndex = anObj->nVertices;
459 bool IsClosed = false;
460 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
461 if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
462 anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] &&
463 anObj->padfZ[StartIndex] == anObj->padfZ[EndIndex - 1])
466 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true );
469 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false );
473 for ( int k = StartIndex ; k < EndIndex ; k++ )
475 HYDROData_PolylineXY::Point aSectPoint;
476 aSectPoint.SetX( anObj->padfX[k] );
477 aSectPoint.SetY( anObj->padfY[k] );
478 aPolylineXY->AddPoint( i, aSectPoint );
479 aAPoints.Append(gp_XYZ (anObj->padfX[k], anObj->padfY[k], anObj->padfZ[k]));
484 QString aBathName = theFileName + "_bath_" + QString::number(theInd);
485 QString aPolyXYName = theFileName + "_polyXY_" + QString::number(theInd);
486 QString aPoly3DName = theFileName + "_poly3D_" + QString::number(theInd);
488 aPolylineXY->SetName( aPolyXYName );
489 aPolylineXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
490 aPolylineXY->Update();
492 aBath->SetAltitudePoints(aAPoints);
493 aBath->SetName( aBathName );
495 aPolylineObj->SetPolylineXY (aPolylineXY, false);
496 aPolylineObj->SetAltitudeObject(aBath);
498 aPolylineObj->SetBorderColor( aPolylineObj->DefaultBorderColor() );
499 aPolylineObj->SetName( aPoly3DName );
501 aPolylineObj->Update();
502 theEntities.Append(aPolylineXY);
503 theEntities.Append(aPolylineObj);
509 int HYDROData_ShapeFile::ImportPolylines(Handle(HYDROData_Document) theDocument, const QString& theFileName,
510 NCollection_Sequence<Handle_HYDROData_Entity>& theEntities, int& theShapeTypeOfFile)
513 int aStat = TryOpenShapeFile(theFileName);
517 HYDROData_Iterator anIter( theDocument );
519 QStringList anExistingNames;
520 std::vector<int> anAllowedIndexes;
521 for( ; anIter.More(); anIter.Next() )
522 anExistingNames.push_back(anIter.Current()->GetName());
525 aHSHP = SHPOpen( theFileName.toAscii().data(), "rb" );
527 QFileInfo aFileInfo(theFileName);
528 QString aBaseFileName = aFileInfo.baseName();
530 if (!Parse(aHSHP, HYDROData_ShapeFile::ShapeType_Polyline, theShapeTypeOfFile))
532 if (aHSHP->nShapeType == 3 || aHSHP->nShapeType == 23)
535 for (;anAllowedIndexes.size() < mySHPObjects.size();)
537 if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)))
539 anAllowedIndexes.push_back(anInd);
546 for (size_t i = 0; i < mySHPObjects.size(); i++ )
548 ReadSHPPolyXY(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
552 else if (aHSHP->nShapeType == 13)
555 for (;anAllowedIndexes.size() < mySHPObjects.size();)
557 if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)) &&
558 !anExistingNames.contains(aBaseFileName + "_Poly3D_" + QString::number(anInd)) &&
559 !anExistingNames.contains(aBaseFileName + "_Bath_" + QString::number(anInd)))
561 anAllowedIndexes.push_back(anInd);
567 for (size_t i = 0; i < mySHPObjects.size(); i++ )
568 ReadSHPPoly3D(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
576 for (size_t i = 0; i < mySHPObjects.size(); i++ )
577 free (mySHPObjects[i]);
579 mySHPObjects.clear();
584 QString HYDROData_ShapeFile::GetShapeTypeName(int theType)
591 return "point (unsupported by HYDRO)";
593 return "arc/polyline (supported by HYDRO)";
595 return "polygon (supported by HYDRO)";
597 return "multipoint (unsupported by HYDRO)";
599 return "pointZ (unsupported by HYDRO)";
601 return "arcZ/polyline (supported by HYDRO)";
603 return "polygonZ (unsupported by HYDRO)";
605 return "multipointZ (unsupported by HYDRO)";
607 return "pointM (unsupported by HYDRO)";
609 return "arcM/polyline (supported by HYDRO)";
611 return "polygonM (unsupported by HYDRO)";
613 return "multipointM (unsupported by HYDRO)";
615 return "multipatch (unsupported by HYDRO)";
621 int HYDROData_ShapeFile::TryOpenShapeFile(QString theFileName)
623 QString aSHPfile = theFileName.simplified();
624 QString aSHXfile = theFileName.simplified().replace( ".shp", ".shx", Qt::CaseInsensitive);
625 FILE* pFileSHP = NULL;
626 pFileSHP = fopen (aSHPfile.toAscii().data(), "r");
627 FILE* pFileSHX = NULL;
628 pFileSHX = fopen (aSHXfile.toAscii().data(), "r");
630 if (pFileSHP == NULL || pFileSHX == NULL)
632 if (pFileSHP == NULL)
634 if (pFileSHX == NULL)
644 bool HYDROData_ShapeFile::CheckDBFFileExisting(const QString& theSHPFilePath, QString& thePathToDBFFile)
646 QString aSHPfile = theSHPFilePath.simplified();
647 QString aDBFfile = theSHPFilePath.simplified().replace( ".shp", ".dbf", Qt::CaseInsensitive);
648 FILE* pFileDBF = NULL;
649 pFileDBF = fopen (aDBFfile.toAscii().data(), "r");
651 if (pFileDBF == NULL)
657 thePathToDBFFile = aDBFfile;
662 bool HYDROData_ShapeFile::DBF_OpenDBF(const QString& thePathToDBFFile)
664 myHDBF = DBFOpen( thePathToDBFFile.toAscii().data(), "r" );
671 int HYDROData_ShapeFile::DBF_GetNbFields()
675 return DBFGetFieldCount(myHDBF);
678 void HYDROData_ShapeFile::DBF_CloseDBF()
684 QStringList HYDROData_ShapeFile::DBF_GetFieldList()
686 QStringList FieldList;
687 int nWidth, nDecimals;
690 for( int i = 0; i < DBFGetFieldCount(myHDBF); i++ )
693 eType = DBFGetFieldInfo( myHDBF, i, chField, &nWidth, &nDecimals );
694 FieldList.append(QString(chField));
700 void HYDROData_ShapeFile::DBF_GetFieldTypeList(std::vector<DBF_FieldType>& FTVect)
702 int nWidth, nDecimals;
705 for( int i = 0; i < DBFGetFieldCount(myHDBF); i++ )
708 eType = DBFGetFieldInfo( myHDBF, i, chField, &nWidth, &nDecimals );
709 if( eType == FTString )
710 FT = DBF_FieldType_String;
711 else if( eType == FTInteger )
712 FT = DBF_FieldType_Integer;
713 else if( eType == FTDouble )
714 FT = DBF_FieldType_Double;
715 else if( eType == FTInvalid )
716 FT = DBF_FieldType_Invalid;
718 FTVect.push_back(FT);
723 int HYDROData_ShapeFile::DBF_GetNbRecords()
727 return DBFGetRecordCount(myHDBF);
730 void HYDROData_ShapeFile::DBF_GetAttributeList(int theIndexOfField, std::vector<DBF_AttrValue>& theAttrV)
732 int nWidth, nDecimals;
735 for( int i = 0; i < DBFGetRecordCount(myHDBF); i++ )
738 DBF_AttrValue anAttr;
739 eType = DBFGetFieldInfo( myHDBF, theIndexOfField, chField, &nWidth, &nDecimals );
741 if( DBFIsAttributeNULL( myHDBF, i, theIndexOfField ) )
743 anAttr.myIsNull = true;
744 DBF_FieldType FT = DBF_FieldType_None;
745 if( eType == FTString )
746 FT = DBF_FieldType_String;
747 else if( eType == FTInteger )
748 FT = DBF_FieldType_Integer;
749 else if( eType == FTDouble )
750 FT = DBF_FieldType_Double;
751 else if( eType == FTInvalid )
752 FT = DBF_FieldType_Invalid;
753 anAttr.myFieldType = FT;
761 const char* chAttr = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
762 anAttr.myIsNull = false;
763 anAttr.myFieldType = DBF_FieldType_String;
764 anAttr.myRawValue = chAttr;
765 anAttr.myStrVal = QString(chAttr);
771 int iAttr = DBFReadIntegerAttribute( myHDBF, i, theIndexOfField );
772 anAttr.myIsNull = false;
773 anAttr.myFieldType = DBF_FieldType_Integer;
774 anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
775 anAttr.myIntVal = iAttr;
781 double dAttr = DBFReadDoubleAttribute( myHDBF, i, theIndexOfField );
782 anAttr.myIsNull = false;
783 anAttr.myFieldType = DBF_FieldType_Double;
784 anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
785 anAttr.myDoubleVal = dAttr;
792 theAttrV.push_back(anAttr);
797 bool HYDROData_ShapeFile::DBF_WriteFieldAndValues(const QString& theFileName, const QString& theFieldName, DBF_FieldType theType, const std::vector<DBF_AttrValue>& theAttrV, bool bUseRawValue)
799 // Check that given field type is equal to field types of attributes values
800 for (size_t i = 0; i < theAttrV.size(); i++)
802 if (theAttrV[i].myFieldType != theType)
807 hDBF = DBFCreate( theFileName.toStdString().c_str() );
811 if (theType != DBF_FieldType_String && theType != DBF_FieldType_Integer && theType != DBF_FieldType_Double)
814 return false; //cant handle any other cases
820 case DBF_FieldType_String:
822 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTString, nWidth, 0);
826 case DBF_FieldType_Integer:
828 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTInteger, nWidth, 0);
832 case DBF_FieldType_Double:
834 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTDouble, nWidth, 0);
841 if (DBFGetFieldCount( hDBF ) != 1)
846 if (DBFGetRecordCount( hDBF ) != 0)
855 for (size_t i = 0; i < theAttrV.size(); i++)
857 if (!theAttrV[i].myIsNull)
858 stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myRawValue.c_str());
860 stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 );
871 for (size_t i = 0; i < theAttrV.size(); i++)
873 if (!theAttrV[i].myIsNull)
876 case DBF_FieldType_String:
878 stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myStrVal.toStdString().c_str());
882 case DBF_FieldType_Integer:
884 stat = DBFWriteIntegerAttribute(hDBF, (int)i, 0, theAttrV[i].myIntVal);
888 case DBF_FieldType_Double:
890 stat = DBFWriteDoubleAttribute(hDBF, (int)i, 0, theAttrV[i].myDoubleVal);
897 stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 );