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 <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>
45 #include <BRepLib.hxx>
46 #include <ShapeFix_Shape.hxx>
47 #include <TopTools_SequenceOfShape.hxx>
49 #include <BRepTopAdaptor_FClass2d.hxx>
52 #pragma warning( disable: 4996 )
55 HYDROData_ShapeFile::HYDROData_ShapeFile() : myHSHP(NULL)
59 HYDROData_ShapeFile::~HYDROData_ShapeFile()
64 void HYDROData_ShapeFile::Export(const QString& aFileName,
65 NCollection_Sequence<Handle_HYDROData_PolylineXY> aPolyXYSeq,
66 NCollection_Sequence<Handle_HYDROData_Polyline3D> aPoly3DSeq,
67 QStringList& aNonExpList)
70 if (!aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty())
72 hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARC );
73 for (int i = 1; i <= aPolyXYSeq.Size(); i++)
74 if (WriteObjectPolyXY(hSHPHandle, aPolyXYSeq(i)) != 1)
75 aNonExpList.append(aPolyXYSeq(i)->GetName());
77 else if (aPolyXYSeq.IsEmpty() && !aPoly3DSeq.IsEmpty())
79 hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARCZ );
80 for (int i = 1; i <= aPoly3DSeq.Size(); i++)
81 if (WriteObjectPoly3D(hSHPHandle, aPoly3DSeq(i)) != 1)
82 aNonExpList.append(aPoly3DSeq(i)->GetName());
84 if (hSHPHandle->nRecords > 0)
85 SHPClose( hSHPHandle );
88 SHPClose( hSHPHandle );
89 QString aFN = aFileName.simplified();
90 remove (aFN.toStdString().c_str());
91 remove (aFN.replace( ".shp", ".shx", Qt::CaseInsensitive).toStdString().c_str());
95 void HYDROData_ShapeFile::Export(const QString& aFileName,
96 const Handle_HYDROData_LandCoverMap& aLCM, QStringList& aNonExpList)
99 if ( !aLCM.IsNull() && !aLCM->IsEmpty())
101 hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_POLYGON );
102 HYDROData_LandCoverMap::Iterator It( aLCM );
103 for( ; It.More(); It.Next() )
105 TopoDS_Face aFace = It.Face();
106 if (WriteObjectPolygon(hSHPHandle, aFace) != 1)
107 aNonExpList.append(aLCM->GetName() + "_" + QString::number(It.Index()));
110 if (hSHPHandle->nRecords > 0)
111 SHPClose( hSHPHandle );
114 SHPClose( hSHPHandle );
115 QString aFN = aFileName.simplified();
116 remove (aFN.toStdString().c_str());
117 remove (aFN.replace( ".shp", ".shx", Qt::CaseInsensitive).toStdString().c_str());
122 int HYDROData_ShapeFile::WriteObjectPolyXY(SHPHandle theShpHandle, Handle_HYDROData_PolylineXY thePoly )
125 std::vector<double> x, y;
126 std::vector<int> anPartStart;
128 for (int i = 0; i < thePoly->NbSections(); i++)
129 if (thePoly->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE)
132 for (int i = 0; i < thePoly->NbSections(); i++)
134 anPartStart.push_back(x.size());
135 HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPoints(i);
136 for (int j = 1; j <= aPointList.Size(); j++)
138 x.push_back( aPointList(j).X());
139 y.push_back( aPointList(j).Y());
141 if (thePoly->IsClosedSection(i))
143 x.push_back( aPointList(1).X());
144 y.push_back( aPointList(1).Y());
148 aSHPObj = SHPCreateObject( SHPT_ARC, -1, thePoly->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
149 SHPWriteObject( theShpHandle, -1, aSHPObj );
150 SHPDestroyObject( aSHPObj );
154 int HYDROData_ShapeFile::WriteObjectPoly3D(SHPHandle theShpHandle, Handle_HYDROData_Polyline3D thePoly )
157 std::vector<double> x, y, z;
158 std::vector<int> anPartStart;
160 for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)
161 if (thePoly->GetPolylineXY()->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE)
164 for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)
166 anPartStart.push_back(x.size());
167 HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPolylineXY()->GetPoints(i);
168 for (int j = 1; j <= aPointList.Size(); j++)
170 x.push_back( aPointList(j).X());
171 y.push_back( aPointList(j).Y());
172 z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(j).X(), aPointList(j).Y())));
174 if ( thePoly->GetPolylineXY()->IsClosedSection(i))
176 x.push_back( aPointList(1).X());
177 y.push_back( aPointList(1).Y());
178 z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(1).X(), aPointList(1).Y())));
183 aSHPObj = SHPCreateObject( SHPT_ARCZ, -1, thePoly->GetPolylineXY()->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], &z[0], NULL );
184 SHPWriteObject( theShpHandle, -1, aSHPObj );
185 SHPDestroyObject( aSHPObj );
189 int HYDROData_ShapeFile::WriteObjectPolygon(SHPHandle theShpHandle, const TopoDS_Shape& theInputShape )
191 if (theInputShape.IsNull())
193 TopExp_Explorer anEdgeEx(theInputShape, TopAbs_EDGE);
194 for (; anEdgeEx.More(); anEdgeEx.Next())
196 TopoDS_Edge E = TopoDS::Edge(anEdgeEx.Current());
198 Handle_Geom_Curve aCur = BRep_Tool::Curve(E, aFP, aLP);
199 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCur);
202 Handle(Geom_TrimmedCurve) aTC = Handle(Geom_TrimmedCurve)::DownCast(aCur);
205 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aTC->BasisCurve());
214 if (theInputShape.ShapeType() == TopAbs_FACE)
216 ProcessFace(TopoDS::Face(theInputShape), theShpHandle);
218 else if (theInputShape.ShapeType() == TopAbs_COMPOUND)
220 TopExp_Explorer Ex(theInputShape, TopAbs_FACE);
221 for (; Ex.More(); Ex.Next())
223 TopoDS_Face aF = TopoDS::Face(Ex.Current());
226 ProcessFace(aF, theShpHandle);
236 void HYDROData_ShapeFile::ProcessFace(TopoDS_Face theFace, SHPHandle theShpHandle)
239 std::vector<double> x, y;
240 std::vector<int> anPartStart;
241 if (theFace.ShapeType() == TopAbs_FACE)
243 TopExp_Explorer Ex(theFace, TopAbs_WIRE);
245 for (; Ex.More(); Ex.Next())
247 TopoDS_Wire aW = TopoDS::Wire(Ex.Current());
251 anPartStart.push_back(x.size());
252 TopExp_Explorer aVEx(aW, TopAbs_VERTEX);
253 NCollection_Sequence<gp_Pnt2d> aPnts;
254 for (; aVEx.More(); aVEx.Next())
256 TopoDS_Vertex aV = TopoDS::Vertex(aVEx.Current());
259 gp_Pnt P = BRep_Tool::Pnt(aV);
260 aPnts.Append(gp_Pnt2d(P.X(), P.Y()));
262 NCollection_Sequence<gp_Pnt2d> aNPnts;
263 aNPnts.Append(aPnts.First());
264 for (int j = 1; j <= aPnts.Size() - 1; j++)
266 if (!aPnts(j).IsEqual(aPnts(j + 1), Precision::Confusion()))
267 aNPnts.Append(aPnts(j + 1));
270 for (int j = 1; j <= aNPnts.Size(); j++)
272 x.push_back( aNPnts(j).X());
273 y.push_back( aNPnts(j).Y());
275 //x.push_back( aNPnts(1).X());
276 //y.push_back( aNPnts(1).Y());
279 aSHPObj = SHPCreateObject( SHPT_POLYGON, -1, NbWires, &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
280 SHPWriteObject( theShpHandle, -1, aSHPObj );
281 SHPDestroyObject( aSHPObj );
287 bool HYDROData_ShapeFile::Parse(SHPHandle theHandle, ShapeType theType, int& theShapeTypeOfFile)
290 mySHPObjects.clear();
291 SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
292 theShapeTypeOfFile = aShapeType;
293 bool ToRead = (theType == ShapeType_Polyline && (aShapeType == 3 || aShapeType == 13 || aShapeType == 23)) ||
294 (theType == ShapeType_Polygon && aShapeType == 5);
297 for (int i = 0; i < theHandle->nRecords; i++)
298 mySHPObjects.push_back(SHPReadObject(theHandle, i));
305 void HYDROData_ShapeFile::ReadSHPPolygon(SHPObject* anObj, int i, TopoDS_Face& F)
309 int nParts = anObj->nParts;
310 gp_Pln pln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
312 //Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
313 //sfs->FixFaceTool()->FixOrientationMode() = 1;
314 TopTools_SequenceOfShape aWires;
315 for ( int i = 0 ; i < nParts ; i++ )
317 BRepBuilderAPI_MakeWire aBuilder;
318 int StartIndex = anObj->panPartStart[i];
321 EndIndex = anObj->panPartStart[i + 1];
323 EndIndex = anObj->nVertices;
325 for ( int k = StartIndex; k < EndIndex - 1 ; k++ )
327 gp_Pnt P1 (anObj->padfX[k], anObj->padfY[k], 0);
328 gp_Pnt P2 (anObj->padfX[k+1], anObj->padfY[k+1], 0);
329 if (P1.Distance(P2) < Precision::Confusion())
331 BRepBuilderAPI_MakeEdge aMakeEdge(P1, P2);
332 aBuilder.Add(TopoDS::Edge(aMakeEdge.Shape()));
336 W = TopoDS::Wire(aBuilder.Shape());
337 W.Orientation(TopAbs_FORWARD);
338 BRepBuilderAPI_MakeFace aDB(pln, W);
339 TopoDS_Face aDummyFace = TopoDS::Face(aDB.Shape());
340 BRepTopAdaptor_FClass2d FClass(aDummyFace, Precision::PConfusion());
341 if ( i == 0 && FClass.PerformInfinitePoint() == TopAbs_OUT)
343 if ( i > 0 && FClass.PerformInfinitePoint() != TopAbs_IN)
349 BRepBuilderAPI_MakeFace aFBuilder(pln, TopoDS::Wire(aWires(1)));
350 for (int i = 2; i <= aWires.Length(); i++)
351 aFBuilder.Add(TopoDS::Wire(aWires(i)));
352 F = TopoDS::Face(aFBuilder.Shape());
354 BRepLib::BuildCurves3d(F);
357 int HYDROData_ShapeFile::ImportPolygons(const QString theFileName, QStringList& thePolygonsList, TopTools_SequenceOfShape& theFaces, int& theShapeTypeOfFile)
360 int Stat = TryOpenShapeFile(theFileName);
363 myHSHP = SHPOpen( theFileName.toAscii().data(), "rb" );
364 if (!Parse(myHSHP, HYDROData_ShapeFile::ShapeType_Polygon, theShapeTypeOfFile))
366 for (size_t i = 0; i < mySHPObjects.size(); i++)
367 thePolygonsList.append("polygon_" + QString::number(i + 1));
370 if (myHSHP->nShapeType == 5)
372 for (size_t i = 0; i < mySHPObjects.size(); i++)
374 ReadSHPPolygon(mySHPObjects[i], i, aF);
383 void HYDROData_ShapeFile::Free()
385 for (size_t i = 0; i < mySHPObjects.size(); i++ )
386 free (mySHPObjects[i]);
388 mySHPObjects.clear();
397 void HYDROData_ShapeFile::ReadSHPPolyXY(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName,
398 int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
401 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
403 int nParts = anObj->nParts;
404 for ( int i = 0 ; i < nParts ; i++ )
406 int StartIndex = anObj->panPartStart[i];
409 EndIndex = anObj->panPartStart[i + 1];
411 EndIndex = anObj->nVertices;
413 bool IsClosed = false;
414 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
415 if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
416 anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] )
419 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true);
422 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false);
426 for ( int k = StartIndex; k < EndIndex ; k++ )
428 HYDROData_PolylineXY::Point aSectPoint;
429 aSectPoint.SetX( anObj->padfX[k] );
430 aSectPoint.SetY( anObj->padfY[k] );
431 aPolylineXY->AddPoint( i, aSectPoint );
436 aPolylineXY->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
437 aPolylineXY->SetName( theFileName + "_PolyXY_" + QString::number(theInd) );
439 aPolylineXY->Update();
440 theEntities.Append(aPolylineXY);
444 void HYDROData_ShapeFile::ReadSHPPoly3D(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName,
445 int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
447 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
449 Handle(HYDROData_Polyline3D) aPolylineObj = Handle(HYDROData_Polyline3D)::DownCast( theDocument->CreateObject( KIND_POLYLINE ) );
451 Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( theDocument->CreateObject( KIND_BATHYMETRY ) );
452 HYDROData_Bathymetry::AltitudePoints aAPoints;
454 int nParts = anObj->nParts;
455 for ( int i = 0 ; i < nParts ; i++ )
457 //bool aSectClosure = true;
458 int StartIndex = anObj->panPartStart[i];
461 EndIndex = anObj->panPartStart[i + 1];
463 EndIndex = anObj->nVertices;
465 bool IsClosed = false;
466 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
467 if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
468 anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] &&
469 anObj->padfZ[StartIndex] == anObj->padfZ[EndIndex - 1])
472 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true );
475 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false );
479 for ( int k = StartIndex ; k < EndIndex ; k++ )
481 HYDROData_PolylineXY::Point aSectPoint;
482 aSectPoint.SetX( anObj->padfX[k] );
483 aSectPoint.SetY( anObj->padfY[k] );
484 aPolylineXY->AddPoint( i, aSectPoint );
485 aAPoints.Append(gp_XYZ (anObj->padfX[k], anObj->padfY[k], anObj->padfZ[k]));
490 QString aBathName = theFileName + "_bath_" + QString::number(theInd);
491 QString aPolyXYName = theFileName + "_polyXY_" + QString::number(theInd);
492 QString aPoly3DName = theFileName + "_poly3D_" + QString::number(theInd);
494 aPolylineXY->SetName( aPolyXYName );
495 aPolylineXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
496 aPolylineXY->Update();
498 aBath->SetAltitudePoints(aAPoints);
499 aBath->SetName( aBathName );
501 aPolylineObj->SetPolylineXY (aPolylineXY, false);
502 aPolylineObj->SetAltitudeObject(aBath);
504 aPolylineObj->SetBorderColor( aPolylineObj->DefaultBorderColor() );
505 aPolylineObj->SetName( aPoly3DName );
507 aPolylineObj->Update();
508 theEntities.Append(aPolylineXY);
509 theEntities.Append(aPolylineObj);
515 int HYDROData_ShapeFile::ImportPolylines(Handle(HYDROData_Document) theDocument, const QString& theFileName,
516 NCollection_Sequence<Handle_HYDROData_Entity>& theEntities, int& theShapeTypeOfFile)
519 int aStat = TryOpenShapeFile(theFileName);
523 HYDROData_Iterator anIter( theDocument );
525 QStringList anExistingNames;
526 std::vector<int> anAllowedIndexes;
527 for( ; anIter.More(); anIter.Next() )
528 anExistingNames.push_back(anIter.Current()->GetName());
531 aHSHP = SHPOpen( theFileName.toAscii().data(), "rb" );
533 QFileInfo aFileInfo(theFileName);
534 QString aBaseFileName = aFileInfo.baseName();
536 if (!Parse(aHSHP, HYDROData_ShapeFile::ShapeType_Polyline, theShapeTypeOfFile))
538 if (aHSHP->nShapeType == 3 || aHSHP->nShapeType == 23)
541 for (;anAllowedIndexes.size() < mySHPObjects.size();)
543 if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)))
545 anAllowedIndexes.push_back(anInd);
552 for (size_t i = 0; i < mySHPObjects.size(); i++ )
554 ReadSHPPolyXY(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
558 else if (aHSHP->nShapeType == 13)
561 for (;anAllowedIndexes.size() < mySHPObjects.size();)
563 if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)) &&
564 !anExistingNames.contains(aBaseFileName + "_Poly3D_" + QString::number(anInd)) &&
565 !anExistingNames.contains(aBaseFileName + "_Bath_" + QString::number(anInd)))
567 anAllowedIndexes.push_back(anInd);
573 for (size_t i = 0; i < mySHPObjects.size(); i++ )
574 ReadSHPPoly3D(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
582 for (size_t i = 0; i < mySHPObjects.size(); i++ )
583 free (mySHPObjects[i]);
585 mySHPObjects.clear();
590 QString HYDROData_ShapeFile::GetShapeTypeName(int theType)
597 return "point (unsupported by HYDRO)";
599 return "arc/polyline (supported by HYDRO)";
601 return "polygon (supported by HYDRO)";
603 return "multipoint (unsupported by HYDRO)";
605 return "pointZ (unsupported by HYDRO)";
607 return "arcZ/polyline (supported by HYDRO)";
609 return "polygonZ (unsupported by HYDRO)";
611 return "multipointZ (unsupported by HYDRO)";
613 return "pointM (unsupported by HYDRO)";
615 return "arcM/polyline (supported by HYDRO)";
617 return "polygonM (unsupported by HYDRO)";
619 return "multipointM (unsupported by HYDRO)";
621 return "multipatch (unsupported by HYDRO)";
627 int HYDROData_ShapeFile::TryOpenShapeFile(QString theFileName)
629 QString aSHPfile = theFileName.simplified();
630 QString aSHXfile = theFileName.simplified().replace( ".shp", ".shx", Qt::CaseInsensitive);
631 FILE* pFileSHP = NULL;
632 pFileSHP = fopen (aSHPfile.toAscii().data(), "r");
633 FILE* pFileSHX = NULL;
634 pFileSHX = fopen (aSHXfile.toAscii().data(), "r");
636 if (pFileSHP == NULL || pFileSHX == NULL)
638 if (pFileSHP == NULL)
640 if (pFileSHX == NULL)
650 bool HYDROData_ShapeFile::CheckDBFFileExisting(const QString& theSHPFilePath, QString& thePathToDBFFile)
652 QString aSHPfile = theSHPFilePath.simplified();
653 QString aDBFfile = theSHPFilePath.simplified().replace( ".shp", ".dbf", Qt::CaseInsensitive);
654 FILE* pFileDBF = NULL;
655 pFileDBF = fopen (aDBFfile.toAscii().data(), "r");
657 if (pFileDBF == NULL)
663 thePathToDBFFile = aDBFfile;
668 bool HYDROData_ShapeFile::DBF_OpenDBF(const QString& thePathToDBFFile)
670 myHDBF = DBFOpen( thePathToDBFFile.toAscii().data(), "r" );
677 int HYDROData_ShapeFile::DBF_GetNbFields()
681 return DBFGetFieldCount(myHDBF);
684 void HYDROData_ShapeFile::DBF_CloseDBF()
690 QStringList HYDROData_ShapeFile::DBF_GetFieldList()
692 QStringList FieldList;
693 int nWidth, nDecimals;
696 for( int i = 0; i < DBFGetFieldCount(myHDBF); i++ )
699 eType = DBFGetFieldInfo( myHDBF, i, chField, &nWidth, &nDecimals );
700 FieldList.append(QString(chField));
706 void HYDROData_ShapeFile::DBF_GetFieldTypeList(std::vector<DBF_FieldType>& FTVect)
708 int nWidth, nDecimals;
711 for( int i = 0; i < DBFGetFieldCount(myHDBF); i++ )
714 eType = DBFGetFieldInfo( myHDBF, i, chField, &nWidth, &nDecimals );
715 if( eType == FTString )
716 FT = DBF_FieldType_String;
717 else if( eType == FTInteger )
718 FT = DBF_FieldType_Integer;
719 else if( eType == FTDouble )
720 FT = DBF_FieldType_Double;
721 else if( eType == FTInvalid )
722 FT = DBF_FieldType_Invalid;
724 FTVect.push_back(FT);
729 int HYDROData_ShapeFile::DBF_GetNbRecords()
733 return DBFGetRecordCount(myHDBF);
736 void HYDROData_ShapeFile::DBF_GetAttributeList(int theIndexOfField, std::vector<DBF_AttrValue>& theAttrV)
738 int nWidth, nDecimals;
741 for( int i = 0; i < DBFGetRecordCount(myHDBF); i++ )
744 DBF_AttrValue anAttr;
745 eType = DBFGetFieldInfo( myHDBF, theIndexOfField, chField, &nWidth, &nDecimals );
747 if( DBFIsAttributeNULL( myHDBF, i, theIndexOfField ) )
749 anAttr.myIsNull = true;
750 DBF_FieldType FT = DBF_FieldType_None;
751 if( eType == FTString )
752 FT = DBF_FieldType_String;
753 else if( eType == FTInteger )
754 FT = DBF_FieldType_Integer;
755 else if( eType == FTDouble )
756 FT = DBF_FieldType_Double;
757 else if( eType == FTInvalid )
758 FT = DBF_FieldType_Invalid;
759 anAttr.myFieldType = FT;
767 const char* chAttr = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
768 anAttr.myIsNull = false;
769 anAttr.myFieldType = DBF_FieldType_String;
770 anAttr.myRawValue = chAttr;
771 anAttr.myStrVal = QString(chAttr);
777 int iAttr = DBFReadIntegerAttribute( myHDBF, i, theIndexOfField );
778 anAttr.myIsNull = false;
779 anAttr.myFieldType = DBF_FieldType_Integer;
780 anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
781 anAttr.myIntVal = iAttr;
787 double dAttr = DBFReadDoubleAttribute( myHDBF, i, theIndexOfField );
788 anAttr.myIsNull = false;
789 anAttr.myFieldType = DBF_FieldType_Double;
790 anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
791 anAttr.myDoubleVal = dAttr;
798 theAttrV.push_back(anAttr);
803 bool HYDROData_ShapeFile::DBF_WriteFieldAndValues(const QString& theFileName, const QString& theFieldName, DBF_FieldType theType, const std::vector<DBF_AttrValue>& theAttrV, bool bUseRawValue)
805 // Check that given field type is equal to field types of attributes values
806 for (size_t i = 0; i < theAttrV.size(); i++)
808 if (theAttrV[i].myFieldType != theType)
813 hDBF = DBFCreate( theFileName.toStdString().c_str() );
817 if (theType != DBF_FieldType_String && theType != DBF_FieldType_Integer && theType != DBF_FieldType_Double)
820 return false; //cant handle any other cases
826 case DBF_FieldType_String:
828 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTString, nWidth, 0);
832 case DBF_FieldType_Integer:
834 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTInteger, nWidth, 0);
838 case DBF_FieldType_Double:
840 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTDouble, nWidth, 0);
847 if (DBFGetFieldCount( hDBF ) != 1)
852 if (DBFGetRecordCount( hDBF ) != 0)
861 for (size_t i = 0; i < theAttrV.size(); i++)
863 if (!theAttrV[i].myIsNull)
864 stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myRawValue.c_str());
866 stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 );
877 for (size_t i = 0; i < theAttrV.size(); i++)
879 if (!theAttrV[i].myIsNull)
882 case DBF_FieldType_String:
884 stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myStrVal.toStdString().c_str());
888 case DBF_FieldType_Integer:
890 stat = DBFWriteIntegerAttribute(hDBF, (int)i, 0, theAttrV[i].myIntVal);
894 case DBF_FieldType_Double:
896 stat = DBFWriteDoubleAttribute(hDBF, (int)i, 0, theAttrV[i].myDoubleVal);
903 stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 );