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 const Handle_HYDROData_LandCoverMap& aLCM,
68 QStringList& aNonExpList)
71 if (!aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty())
73 hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARC );
74 for (int i = 1; i <= aPolyXYSeq.Size(); i++)
75 if (WriteObjectPolyXY(hSHPHandle, aPolyXYSeq(i)) != 1)
76 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 else if (aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty() && !aLCM->IsEmpty())
88 hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_POLYGON );
89 HYDROData_LandCoverMap::Iterator It( aLCM );
90 for( ; It.More(); It.Next() )
92 TopoDS_Face aFace = It.Face();
93 if (WriteObjectPolygon(hSHPHandle, aFace) != 1)
94 aNonExpList.append(aLCM->GetName() + "_" + QString::number(It.Index()));
96 /*for (int i = 1; i <= aLCSeq.Size(); i++)
97 if (WriteObjectLC(hSHPHandle, aLCSeq(i)) != 1)
98 aNonExpList.append(aLCSeq(i)->GetName());*/
100 if (hSHPHandle->nRecords > 0)
101 SHPClose( hSHPHandle );
104 SHPClose( hSHPHandle );
105 QString aFN = aFileName.simplified();
106 remove (aFN.toStdString().c_str());
107 remove (aFN.replace( ".shp", ".shx", Qt::CaseInsensitive).toStdString().c_str());
111 int HYDROData_ShapeFile::WriteObjectPolyXY(SHPHandle theShpHandle, Handle_HYDROData_PolylineXY thePoly )
114 std::vector<double> x, y;
115 std::vector<int> anPartStart;
117 for (int i = 0; i < thePoly->NbSections(); i++)
118 if (thePoly->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE)
121 for (int i = 0; i < thePoly->NbSections(); i++)
123 anPartStart.push_back(x.size());
124 HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPoints(i);
125 for (int j = 1; j <= aPointList.Size(); j++)
127 x.push_back( aPointList(j).X());
128 y.push_back( aPointList(j).Y());
130 if (thePoly->IsClosedSection(i))
132 x.push_back( aPointList(1).X());
133 y.push_back( aPointList(1).Y());
137 aSHPObj = SHPCreateObject( SHPT_ARC, -1, thePoly->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
138 SHPWriteObject( theShpHandle, -1, aSHPObj );
139 SHPDestroyObject( aSHPObj );
143 int HYDROData_ShapeFile::WriteObjectPoly3D(SHPHandle theShpHandle, Handle_HYDROData_Polyline3D thePoly )
146 std::vector<double> x, y, z;
147 std::vector<int> anPartStart;
149 for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)
150 if (thePoly->GetPolylineXY()->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE)
153 for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)
155 anPartStart.push_back(x.size());
156 HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPolylineXY()->GetPoints(i);
157 for (int j = 1; j <= aPointList.Size(); j++)
159 x.push_back( aPointList(j).X());
160 y.push_back( aPointList(j).Y());
161 z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(j).X(), aPointList(j).Y())));
163 if ( thePoly->GetPolylineXY()->IsClosedSection(i))
165 x.push_back( aPointList(1).X());
166 y.push_back( aPointList(1).Y());
167 z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(1).X(), aPointList(1).Y())));
172 aSHPObj = SHPCreateObject( SHPT_ARCZ, -1, thePoly->GetPolylineXY()->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], &z[0], NULL );
173 SHPWriteObject( theShpHandle, -1, aSHPObj );
174 SHPDestroyObject( aSHPObj );
178 int HYDROData_ShapeFile::WriteObjectPolygon(SHPHandle theShpHandle, const TopoDS_Shape& theInputShape )
180 if (theInputShape.IsNull())
182 TopExp_Explorer anEdgeEx(theInputShape, TopAbs_EDGE);
183 for (; anEdgeEx.More(); anEdgeEx.Next())
185 TopoDS_Edge E = TopoDS::Edge(anEdgeEx.Current());
187 Handle_Geom_Curve aCur = BRep_Tool::Curve(E, aFP, aLP);
188 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCur);
191 Handle(Geom_TrimmedCurve) aTC = Handle(Geom_TrimmedCurve)::DownCast(aCur);
194 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aTC->BasisCurve());
203 if (theInputShape.ShapeType() == TopAbs_FACE)
205 ProcessFace(TopoDS::Face(theInputShape), theShpHandle);
207 else if (theInputShape.ShapeType() == TopAbs_COMPOUND)
209 TopExp_Explorer Ex(theInputShape, TopAbs_FACE);
210 for (; Ex.More(); Ex.Next())
212 TopoDS_Face aF = TopoDS::Face(Ex.Current());
215 ProcessFace(aF, theShpHandle);
225 void HYDROData_ShapeFile::ProcessFace(TopoDS_Face theFace, SHPHandle theShpHandle)
228 std::vector<double> x, y;
229 std::vector<int> anPartStart;
230 if (theFace.ShapeType() == TopAbs_FACE)
232 TopExp_Explorer Ex(theFace, TopAbs_WIRE);
234 for (; Ex.More(); Ex.Next())
236 TopoDS_Wire aW = TopoDS::Wire(Ex.Current());
240 anPartStart.push_back(x.size());
241 TopExp_Explorer aVEx(aW, TopAbs_VERTEX);
242 NCollection_Sequence<gp_Pnt2d> aPnts;
243 for (; aVEx.More(); aVEx.Next())
245 TopoDS_Vertex aV = TopoDS::Vertex(aVEx.Current());
248 gp_Pnt P = BRep_Tool::Pnt(aV);
249 aPnts.Append(gp_Pnt2d(P.X(), P.Y()));
251 NCollection_Sequence<gp_Pnt2d> aNPnts;
252 aNPnts.Append(aPnts.First());
253 for (int j = 1; j <= aPnts.Size() - 1; j++)
255 if (!aPnts(j).IsEqual(aPnts(j + 1), Precision::Confusion()))
256 aNPnts.Append(aPnts(j + 1));
259 for (int j = 1; j <= aNPnts.Size(); j++)
261 x.push_back( aNPnts(j).X());
262 y.push_back( aNPnts(j).Y());
264 //x.push_back( aNPnts(1).X());
265 //y.push_back( aNPnts(1).Y());
268 aSHPObj = SHPCreateObject( SHPT_POLYGON, -1, NbWires, &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
269 SHPWriteObject( theShpHandle, -1, aSHPObj );
270 SHPDestroyObject( aSHPObj );
276 bool HYDROData_ShapeFile::Parse(SHPHandle theHandle, ShapeType theType, int& theShapeTypeOfFile)
279 mySHPObjects.clear();
280 SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
281 theShapeTypeOfFile = aShapeType;
282 bool ToRead = (theType == ShapeType_Polyline && (aShapeType == 3 || aShapeType == 13 || aShapeType == 23)) ||
283 (theType == ShapeType_Polygon && aShapeType == 5);
286 for (int i = 0; i < theHandle->nRecords; i++)
287 mySHPObjects.push_back(SHPReadObject(theHandle, i));
294 void HYDROData_ShapeFile::ReadSHPPolygon(SHPObject* anObj, int i, TopoDS_Face& F)
298 int nParts = anObj->nParts;
299 gp_Pln pln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
301 //Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
302 //sfs->FixFaceTool()->FixOrientationMode() = 1;
303 TopTools_SequenceOfShape aWires;
304 for ( int i = 0 ; i < nParts ; i++ )
306 BRepBuilderAPI_MakeWire aBuilder;
307 int StartIndex = anObj->panPartStart[i];
310 EndIndex = anObj->panPartStart[i + 1];
312 EndIndex = anObj->nVertices;
314 for ( int k = StartIndex; k < EndIndex - 1 ; k++ )
316 gp_Pnt P1 (anObj->padfX[k], anObj->padfY[k], 0);
317 gp_Pnt P2 (anObj->padfX[k+1], anObj->padfY[k+1], 0);
318 if (P1.Distance(P2) < Precision::Confusion())
320 BRepBuilderAPI_MakeEdge aMakeEdge(P1, P2);
321 aBuilder.Add(TopoDS::Edge(aMakeEdge.Shape()));
325 W = TopoDS::Wire(aBuilder.Shape());
326 W.Orientation(TopAbs_FORWARD);
327 BRepBuilderAPI_MakeFace aDB(pln, W);
328 TopoDS_Face aDummyFace = TopoDS::Face(aDB.Shape());
329 BRepTopAdaptor_FClass2d FClass(aDummyFace, Precision::PConfusion());
330 if ( i == 0 && FClass.PerformInfinitePoint() == TopAbs_OUT)
332 if ( i > 0 && FClass.PerformInfinitePoint() != TopAbs_IN)
338 BRepBuilderAPI_MakeFace aFBuilder(pln, TopoDS::Wire(aWires(1)));
339 for (int i = 2; i <= aWires.Length(); i++)
340 aFBuilder.Add(TopoDS::Wire(aWires(i)));
341 F = TopoDS::Face(aFBuilder.Shape());
343 BRepLib::BuildCurves3d(F);
346 int HYDROData_ShapeFile::ImportPolygons(const QString theFileName, QStringList& thePolygonsList, TopTools_SequenceOfShape& theFaces, int& theShapeTypeOfFile)
349 int Stat = TryOpenShapeFile(theFileName);
352 myHSHP = SHPOpen( theFileName.toAscii().data(), "rb" );
353 if (!Parse(myHSHP, HYDROData_ShapeFile::ShapeType_Polygon, theShapeTypeOfFile))
355 for (size_t i = 0; i < mySHPObjects.size(); i++)
356 thePolygonsList.append("polygon_" + QString::number(i + 1));
359 if (myHSHP->nShapeType == 5)
361 for (size_t i = 0; i < mySHPObjects.size(); i++)
363 ReadSHPPolygon(mySHPObjects[i], i, aF);
372 void HYDROData_ShapeFile::Free()
374 for (size_t i = 0; i < mySHPObjects.size(); i++ )
375 free (mySHPObjects[i]);
377 mySHPObjects.clear();
386 void HYDROData_ShapeFile::ReadSHPPolyXY(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName,
387 int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
390 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
392 int nParts = anObj->nParts;
393 for ( int i = 0 ; i < nParts ; i++ )
395 int StartIndex = anObj->panPartStart[i];
398 EndIndex = anObj->panPartStart[i + 1];
400 EndIndex = anObj->nVertices;
402 bool IsClosed = false;
403 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
404 if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
405 anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] )
408 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true);
411 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false);
415 for ( int k = StartIndex; k < EndIndex ; k++ )
417 HYDROData_PolylineXY::Point aSectPoint;
418 aSectPoint.SetX( anObj->padfX[k] );
419 aSectPoint.SetY( anObj->padfY[k] );
420 aPolylineXY->AddPoint( i, aSectPoint );
425 aPolylineXY->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
426 aPolylineXY->SetName( theFileName + "_PolyXY_" + QString::number(theInd) );
428 aPolylineXY->Update();
429 theEntities.Append(aPolylineXY);
433 void HYDROData_ShapeFile::ReadSHPPoly3D(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName,
434 int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
436 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
438 Handle(HYDROData_Polyline3D) aPolylineObj = Handle(HYDROData_Polyline3D)::DownCast( theDocument->CreateObject( KIND_POLYLINE ) );
440 Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( theDocument->CreateObject( KIND_BATHYMETRY ) );
441 HYDROData_Bathymetry::AltitudePoints aAPoints;
443 int nParts = anObj->nParts;
444 for ( int i = 0 ; i < nParts ; i++ )
446 //bool aSectClosure = true;
447 int StartIndex = anObj->panPartStart[i];
450 EndIndex = anObj->panPartStart[i + 1];
452 EndIndex = anObj->nVertices;
454 bool IsClosed = false;
455 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
456 if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
457 anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] &&
458 anObj->padfZ[StartIndex] == anObj->padfZ[EndIndex - 1])
461 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true );
464 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false );
468 for ( int k = StartIndex ; k < EndIndex ; k++ )
470 HYDROData_PolylineXY::Point aSectPoint;
471 aSectPoint.SetX( anObj->padfX[k] );
472 aSectPoint.SetY( anObj->padfY[k] );
473 aPolylineXY->AddPoint( i, aSectPoint );
474 aAPoints.Append(gp_XYZ (anObj->padfX[k], anObj->padfY[k], anObj->padfZ[k]));
479 QString aBathName = theFileName + "_bath_" + QString::number(theInd);
480 QString aPolyXYName = theFileName + "_polyXY_" + QString::number(theInd);
481 QString aPoly3DName = theFileName + "_poly3D_" + QString::number(theInd);
483 aPolylineXY->SetName( aPolyXYName );
484 aPolylineXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
485 aPolylineXY->Update();
487 aBath->SetAltitudePoints(aAPoints);
488 aBath->SetName( aBathName );
490 aPolylineObj->SetPolylineXY (aPolylineXY, false);
491 aPolylineObj->SetAltitudeObject(aBath);
493 aPolylineObj->SetBorderColor( aPolylineObj->DefaultBorderColor() );
494 aPolylineObj->SetName( aPoly3DName );
496 aPolylineObj->Update();
497 theEntities.Append(aPolylineXY);
498 theEntities.Append(aPolylineObj);
504 int HYDROData_ShapeFile::ImportPolylines(Handle(HYDROData_Document) theDocument, const QString& theFileName,
505 NCollection_Sequence<Handle_HYDROData_Entity>& theEntities, int& theShapeTypeOfFile)
508 int aStat = TryOpenShapeFile(theFileName);
512 HYDROData_Iterator anIter( theDocument );
514 QStringList anExistingNames;
515 std::vector<int> anAllowedIndexes;
516 for( ; anIter.More(); anIter.Next() )
517 anExistingNames.push_back(anIter.Current()->GetName());
520 aHSHP = SHPOpen( theFileName.toAscii().data(), "rb" );
522 QFileInfo aFileInfo(theFileName);
523 QString aBaseFileName = aFileInfo.baseName();
525 if (!Parse(aHSHP, HYDROData_ShapeFile::ShapeType_Polyline, theShapeTypeOfFile))
527 if (aHSHP->nShapeType == 3 || aHSHP->nShapeType == 23)
530 for (;anAllowedIndexes.size() < mySHPObjects.size();)
532 if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)))
534 anAllowedIndexes.push_back(anInd);
541 for (size_t i = 0; i < mySHPObjects.size(); i++ )
543 ReadSHPPolyXY(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
547 else if (aHSHP->nShapeType == 13)
550 for (;anAllowedIndexes.size() < mySHPObjects.size();)
552 if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)) &&
553 !anExistingNames.contains(aBaseFileName + "_Poly3D_" + QString::number(anInd)) &&
554 !anExistingNames.contains(aBaseFileName + "_Bath_" + QString::number(anInd)))
556 anAllowedIndexes.push_back(anInd);
562 for (size_t i = 0; i < mySHPObjects.size(); i++ )
563 ReadSHPPoly3D(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
571 for (size_t i = 0; i < mySHPObjects.size(); i++ )
572 free (mySHPObjects[i]);
574 mySHPObjects.clear();
579 QString HYDROData_ShapeFile::GetShapeTypeName(int theType)
586 return "point (unsupported by HYDRO)";
588 return "arc/polyline (supported by HYDRO)";
590 return "polygon (supported by HYDRO)";
592 return "multipoint (unsupported by HYDRO)";
594 return "pointZ (unsupported by HYDRO)";
596 return "arcZ/polyline (supported by HYDRO)";
598 return "polygonZ (unsupported by HYDRO)";
600 return "multipointZ (unsupported by HYDRO)";
602 return "pointM (unsupported by HYDRO)";
604 return "arcM/polyline (supported by HYDRO)";
606 return "polygonM (unsupported by HYDRO)";
608 return "multipointM (unsupported by HYDRO)";
610 return "multipatch (unsupported by HYDRO)";
616 int HYDROData_ShapeFile::TryOpenShapeFile(QString theFileName)
618 QString aSHPfile = theFileName.simplified();
619 QString aSHXfile = theFileName.simplified().replace( ".shp", ".shx", Qt::CaseInsensitive);
620 FILE* pFileSHP = NULL;
621 pFileSHP = fopen (aSHPfile.toAscii().data(), "r");
622 FILE* pFileSHX = NULL;
623 pFileSHX = fopen (aSHXfile.toAscii().data(), "r");
625 if (pFileSHP == NULL || pFileSHX == NULL)
627 if (pFileSHP == NULL)
629 if (pFileSHX == NULL)
639 bool HYDROData_ShapeFile::CheckDBFFileExisting(const QString& theSHPFilePath, QString& thePathToDBFFile)
641 QString aSHPfile = theSHPFilePath.simplified();
642 QString aDBFfile = theSHPFilePath.simplified().replace( ".shp", ".dbf", Qt::CaseInsensitive);
643 FILE* pFileDBF = NULL;
644 pFileDBF = fopen (aDBFfile.toAscii().data(), "r");
646 if (pFileDBF == NULL)
652 thePathToDBFFile = aDBFfile;
657 bool HYDROData_ShapeFile::DBF_OpenDBF(const QString& thePathToDBFFile)
659 myHDBF = DBFOpen( thePathToDBFFile.toAscii().data(), "r" );
666 int HYDROData_ShapeFile::DBF_GetNbFields()
670 return DBFGetFieldCount(myHDBF);
673 void HYDROData_ShapeFile::DBF_CloseDBF()
679 QStringList HYDROData_ShapeFile::DBF_GetFieldList()
681 QStringList FieldList;
682 int nWidth, nDecimals;
685 for( int i = 0; i < DBFGetFieldCount(myHDBF); i++ )
688 eType = DBFGetFieldInfo( myHDBF, i, chField, &nWidth, &nDecimals );
689 FieldList.append(QString(chField));
695 void HYDROData_ShapeFile::DBF_GetFieldTypeList(std::vector<DBF_FieldType>& FTVect)
697 int nWidth, nDecimals;
700 for( int i = 0; i < DBFGetFieldCount(myHDBF); i++ )
703 eType = DBFGetFieldInfo( myHDBF, i, chField, &nWidth, &nDecimals );
704 if( eType == FTString )
705 FT = DBF_FieldType_String;
706 else if( eType == FTInteger )
707 FT = DBF_FieldType_Integer;
708 else if( eType == FTDouble )
709 FT = DBF_FieldType_Double;
710 else if( eType == FTInvalid )
711 FT = DBF_FieldType_Invalid;
713 FTVect.push_back(FT);
718 int HYDROData_ShapeFile::DBF_GetNbRecords()
722 return DBFGetRecordCount(myHDBF);
725 void HYDROData_ShapeFile::DBF_GetAttributeList(int theIndexOfField, std::vector<DBF_AttrValue>& theAttrV)
727 int nWidth, nDecimals;
730 for( int i = 0; i < DBFGetRecordCount(myHDBF); i++ )
733 DBF_AttrValue anAttr;
734 eType = DBFGetFieldInfo( myHDBF, theIndexOfField, chField, &nWidth, &nDecimals );
736 if( DBFIsAttributeNULL( myHDBF, i, theIndexOfField ) )
738 anAttr.myIsNull = true;
739 DBF_FieldType FT = DBF_FieldType_None;
740 if( eType == FTString )
741 FT = DBF_FieldType_String;
742 else if( eType == FTInteger )
743 FT = DBF_FieldType_Integer;
744 else if( eType == FTDouble )
745 FT = DBF_FieldType_Double;
746 else if( eType == FTInvalid )
747 FT = DBF_FieldType_Invalid;
748 anAttr.myFieldType = FT;
756 const char* chAttr = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
757 anAttr.myIsNull = false;
758 anAttr.myFieldType = DBF_FieldType_String;
759 anAttr.myRawValue = chAttr;
760 anAttr.myStrVal = QString(chAttr);
766 int iAttr = DBFReadIntegerAttribute( myHDBF, i, theIndexOfField );
767 anAttr.myIsNull = false;
768 anAttr.myFieldType = DBF_FieldType_Integer;
769 anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
770 anAttr.myIntVal = iAttr;
776 double dAttr = DBFReadDoubleAttribute( myHDBF, i, theIndexOfField );
777 anAttr.myIsNull = false;
778 anAttr.myFieldType = DBF_FieldType_Double;
779 anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
780 anAttr.myDoubleVal = dAttr;
787 theAttrV.push_back(anAttr);
792 bool HYDROData_ShapeFile::DBF_WriteFieldAndValues(const QString& theFileName, const QString& theFieldName, DBF_FieldType theType, const std::vector<DBF_AttrValue>& theAttrV, bool bUseRawValue)
794 // Check that given field type is equal to field types of attributes values
795 for (size_t i = 0; i < theAttrV.size(); i++)
797 if (theAttrV[i].myFieldType != theType)
802 hDBF = DBFCreate( theFileName.toStdString().c_str() );
806 if (theType != DBF_FieldType_String && theType != DBF_FieldType_Integer && theType != DBF_FieldType_Double)
809 return false; //cant handle any other cases
815 case DBF_FieldType_String:
817 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTString, nWidth, 0);
821 case DBF_FieldType_Integer:
823 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTInteger, nWidth, 0);
827 case DBF_FieldType_Double:
829 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTDouble, nWidth, 0);
836 if (DBFGetFieldCount( hDBF ) != 1)
841 if (DBFGetRecordCount( hDBF ) != 0)
850 for (size_t i = 0; i < theAttrV.size(); i++)
852 if (!theAttrV[i].myIsNull)
853 stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myRawValue.c_str());
855 stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 );
866 for (size_t i = 0; i < theAttrV.size(); i++)
868 if (!theAttrV[i].myIsNull)
871 case DBF_FieldType_String:
873 stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myStrVal.toStdString().c_str());
877 case DBF_FieldType_Integer:
879 stat = DBFWriteIntegerAttribute(hDBF, (int)i, 0, theAttrV[i].myIntVal);
883 case DBF_FieldType_Double:
885 stat = DBFWriteDoubleAttribute(hDBF, (int)i, 0, theAttrV[i].myDoubleVal);
892 stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 );