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>
52 #include <OSD_Timer.hxx>
53 #include <BRepLib_MakeVertex.hxx>
54 #include <NCollection_List.hxx>
55 #include <GC_MakeSegment.hxx>
56 #include <BRep_Builder.hxx>
57 #include <BRepAdaptor_Curve.hxx>
58 #include <GCPnts_QuasiUniformDeflection.hxx>
59 #include <TopLoc_Location.hxx>
60 #include <Geom_Plane.hxx>
61 #include <NCollection_Array1.hxx>
64 #pragma warning( disable: 4996 )
67 //SHP->TFaces (Import)
70 HYDROData_ShapeFile::HYDROData_ShapeFile() : myHSHP(NULL)
74 HYDROData_ShapeFile::~HYDROData_ShapeFile()
79 void HYDROData_ShapeFile::Export(const QString& aFileName,
80 NCollection_Sequence<Handle_HYDROData_PolylineXY> aPolyXYSeq,
81 NCollection_Sequence<Handle_HYDROData_Polyline3D> aPoly3DSeq,
82 QStringList& aNonExpList)
85 if (!aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty())
87 hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARC );
88 for (int i = 1; i <= aPolyXYSeq.Size(); i++)
89 if (WriteObjectPolyXY(hSHPHandle, aPolyXYSeq(i)) != 1)
90 aNonExpList.append(aPolyXYSeq(i)->GetName());
92 else if (aPolyXYSeq.IsEmpty() && !aPoly3DSeq.IsEmpty())
94 hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARCZ );
95 for (int i = 1; i <= aPoly3DSeq.Size(); i++)
96 if (WriteObjectPoly3D(hSHPHandle, aPoly3DSeq(i)) != 1)
97 aNonExpList.append(aPoly3DSeq(i)->GetName());
99 if (hSHPHandle->nRecords > 0)
100 SHPClose( hSHPHandle );
103 SHPClose( hSHPHandle );
104 QString aFN = aFileName.simplified();
105 remove (aFN.toStdString().c_str());
106 remove ((aFN.simplified().replace( aFN.simplified().size() - 4, 4, ".shx")).toStdString().c_str());
110 void HYDROData_ShapeFile::Export(const QString& aFileName, const Handle_HYDROData_LandCoverMap& aLCM,
111 QStringList& aNonExpList, bool bCheckLinear, bool bUseDiscr, double theDefl)
113 if (bCheckLinear && !aLCM->CheckLinear())
116 SHPHandle hSHPHandle;
117 if ( !aLCM.IsNull() && !aLCM->IsEmpty())
119 hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_POLYGON );
120 HYDROData_LandCoverMap::Explorer It( aLCM );
121 for( ; It.More(); It.Next())
123 TopoDS_Face aFace = It.Face();
124 if (WriteObjectPolygon(hSHPHandle, aFace, bUseDiscr, theDefl) != 1)
125 aNonExpList.append(aLCM->GetName() + "_" + QString::number(It.Index()));
128 if (hSHPHandle->nRecords > 0)
129 SHPClose( hSHPHandle );
132 SHPClose( hSHPHandle );
133 QString aFN = aFileName.simplified();
134 remove (aFN.toStdString().c_str());
135 remove ((aFN.simplified().replace( aFN.simplified().size() - 4, 4, ".shx")).toStdString().c_str());
140 int HYDROData_ShapeFile::WriteObjectPolyXY(SHPHandle theShpHandle, Handle_HYDROData_PolylineXY thePoly )
143 std::vector<double> x, y;
144 std::vector<int> anPartStart;
146 for (int i = 0; i < thePoly->NbSections(); i++)
147 if (thePoly->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE)
150 for (int i = 0; i < thePoly->NbSections(); i++)
152 anPartStart.push_back(x.size());
153 HYDROData_PolylineXY::PointsList aPointList = thePoly->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());
159 if (thePoly->IsClosedSection(i))
161 x.push_back( aPointList(1).X());
162 y.push_back( aPointList(1).Y());
166 aSHPObj = SHPCreateObject( SHPT_ARC, -1, thePoly->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
167 SHPWriteObject( theShpHandle, -1, aSHPObj );
168 SHPDestroyObject( aSHPObj );
172 int HYDROData_ShapeFile::WriteObjectPoly3D(SHPHandle theShpHandle, Handle_HYDROData_Polyline3D thePoly )
175 std::vector<double> x, y, z;
176 std::vector<int> anPartStart;
178 for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)
179 if (thePoly->GetPolylineXY()->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE)
182 for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)
184 anPartStart.push_back(x.size());
185 HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPolylineXY()->GetPoints(i);
186 for (int j = 1; j <= aPointList.Size(); j++)
188 x.push_back( aPointList(j).X());
189 y.push_back( aPointList(j).Y());
190 z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(j).X(), aPointList(j).Y())));
192 if ( thePoly->GetPolylineXY()->IsClosedSection(i))
194 x.push_back( aPointList(1).X());
195 y.push_back( aPointList(1).Y());
196 z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(1).X(), aPointList(1).Y())));
201 aSHPObj = SHPCreateObject( SHPT_ARCZ, -1, thePoly->GetPolylineXY()->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], &z[0], NULL );
202 SHPWriteObject( theShpHandle, -1, aSHPObj );
203 SHPDestroyObject( aSHPObj );
207 int HYDROData_ShapeFile::WriteObjectPolygon(SHPHandle theShpHandle, const TopoDS_Shape& theInputShape,
208 bool bUseDiscr, double theDefl)
210 if (theInputShape.IsNull())
213 if (theInputShape.ShapeType() == TopAbs_FACE)
215 ProcessFace(TopoDS::Face(theInputShape), theShpHandle, bUseDiscr, theDefl);
217 else if (theInputShape.ShapeType() == TopAbs_COMPOUND)
219 TopExp_Explorer Ex(theInputShape, TopAbs_FACE);
220 for (; Ex.More(); Ex.Next())
222 TopoDS_Face aF = TopoDS::Face(Ex.Current());
225 ProcessFace(aF, theShpHandle, bUseDiscr, theDefl);
235 void HYDROData_ShapeFile::ProcessFace(const TopoDS_Face& theFace, SHPHandle theShpHandle,
236 bool bUseDiscr, double theDefl )
238 if (theFace.ShapeType() != TopAbs_FACE)
241 std::vector<double> x, y;
242 std::vector<int> anPartStart;
243 TopoDS_Wire OuterW = BRepTools::OuterWire(theFace);
244 NCollection_Sequence<TopoDS_Wire> aWires;
246 //write an outer wire first
247 aWires.Append(OuterW);
248 TopExp_Explorer Ex(theFace, TopAbs_WIRE);
249 for (; Ex.More(); Ex.Next())
251 TopoDS_Wire aW = TopoDS::Wire(Ex.Current());
252 if (aW.IsEqual(OuterW))
258 for (int k = 1; k <= aWires.Length(); k++)
260 TopoDS_Wire aW = aWires(k);
264 if (aW.Orientation() == TopAbs_INTERNAL)
265 //cant write internal wires/edges
267 // Try to reorder edges
268 Handle(ShapeFix_Wire) aSFW = new ShapeFix_Wire( aW, theFace, Precision::Confusion() );
269 aSFW->ModifyTopologyMode() = Standard_False;
270 aSFW->ModifyGeometryMode() = Standard_False;
272 Handle(ShapeExtend_WireData) aSEWD = aSFW->WireData();
273 Standard_Integer nbE = aSEWD->NbEdges();
277 anPartStart.push_back(x.size());
278 NCollection_Sequence<gp_Pnt2d> aPnts;
279 for (Standard_Integer i = 1; i <= nbE; i++)
281 TopoDS_Edge E = aSEWD->Edge(i);
284 TopoDS_Vertex aV = TopExp::LastVertex(E, 1);
287 gp_Pnt P = BRep_Tool::Pnt(aV);
288 aPnts.Append(gp_Pnt2d(P.X(), P.Y()));
292 BRepAdaptor_Curve Cur( E );
293 GCPnts_QuasiUniformDeflection Discr( Cur, theDefl );
294 if( !Discr.IsDone() )
295 continue; //skip edge?
296 double NewDefl = theDefl/2.0;
297 while (Discr.NbPoints() < 2)
299 Discr.Initialize(Cur, NewDefl);
300 NewDefl = NewDefl/2.0;
303 if (E.Orientation() == TopAbs_FORWARD)
305 for( int i = 1; i <= Discr.NbPoints(); i++ )
307 gp_Pnt P = Discr.Value( i );
308 aPnts.Append(gp_Pnt2d(P.X(), P.Y()));
313 for( int i = Discr.NbPoints(); i > 0; i-- )
315 gp_Pnt P = Discr.Value( i );
316 aPnts.Append(gp_Pnt2d(P.X(), P.Y()));
321 NCollection_Sequence<gp_Pnt2d> aNPnts;
322 aNPnts.Append(aPnts.First());
323 for (int j = 1; j <= aPnts.Size() - 1; j++)
325 if (!aPnts(j).IsEqual(aPnts(j + 1), Precision::Confusion()))
326 aNPnts.Append(aPnts(j + 1));
329 //assume that the orientation of external wire & internal wires is correct
330 //so just write all points "as-is"
331 //External wire will be written in clockwise direction
332 //any other wires (holes) - in anticlockwise direction
333 for (int j = aNPnts.Size(); j >= 1; j--)
335 x.push_back( aNPnts(j).X());
336 y.push_back( aNPnts(j).Y());
338 //first point is same as the last one => closed polygon
339 x.push_back( aNPnts.Last().X());
340 y.push_back( aNPnts.Last().Y());
344 aSHPObj = SHPCreateObject( SHPT_POLYGON, -1, NbWires, &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
345 SHPWriteObject( theShpHandle, -1, aSHPObj );
346 SHPDestroyObject( aSHPObj );
350 bool HYDROData_ShapeFile::Parse(SHPHandle theHandle, ShapeType theType, int& theShapeTypeOfFile)
353 mySHPObjects.clear();
354 SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
355 theShapeTypeOfFile = aShapeType;
356 bool ToRead = (theType == ShapeType_Polyline && (aShapeType == 3 || aShapeType == 13 || aShapeType == 23)) ||
357 (theType == ShapeType_Polygon && aShapeType == 5);
360 for (int i = 0; i < theHandle->nRecords; i++)
361 mySHPObjects.push_back(SHPReadObject(theHandle, i));
368 void HYDROData_ShapeFile::ReadSHPPolygon(SHPObject* anObj, int i, TopoDS_Face& F)
373 int nParts = anObj->nParts;
374 Handle_Geom_Plane aPlaneSur = new Geom_Plane(gp_Pnt(0,0,0), gp_Dir(0,0,1));
379 TopTools_SequenceOfShape aWires;
380 for ( int i = 0 ; i < nParts ; i++ )
382 BRepBuilderAPI_MakeWire aBuilder;
383 int StartIndex = anObj->panPartStart[i];
386 EndIndex = anObj->panPartStart[i + 1];
388 EndIndex = anObj->nVertices;
393 //First point is same as the last point
394 int NbPnts = EndIndex - StartIndex - 1;
395 NCollection_Array1<TopoDS_Vertex> VPoints(0, NbPnts);
397 for ( int k = StartIndex; k < EndIndex; k++ )
399 gp_Pnt P (anObj->padfX[k], anObj->padfY[k], 0);
400 VPoints.ChangeValue(j) = BRepLib_MakeVertex(P).Vertex();
404 for ( int k = 0; k < VPoints.Size() - 1; k++ )
406 gp_Pnt P1 = BRep_Tool::Pnt(VPoints(k));
407 gp_Pnt P2 = BRep_Tool::Pnt(VPoints(k + 1));
408 if (P1.Distance(P2) < Precision::Confusion())
410 Handle_Geom_TrimmedCurve aTC = GC_MakeSegment(P1, P2).Value();
412 if ( k != VPoints.Size() - 2)
413 E = BRepLib_MakeEdge(aTC, VPoints(k), VPoints(k + 1)).Edge();
415 //the last edge => use first and last vertices
416 E = BRepLib_MakeEdge(aTC, VPoints.First(), VPoints.Value(VPoints.Upper() - 1)).Edge();
418 //If SHP file is correct then the outer wire and the holes will have the correct orientations
421 //Wire must be closed anyway
422 W.Closed (BRep_Tool::IsClosed (W));
423 W.Orientation(TopAbs_FORWARD);
427 //Add surface to the face
428 BB.UpdateFace(F, aPlaneSur, TopLoc_Location(), Precision::Confusion());
429 F.Closed(Standard_True);
432 int HYDROData_ShapeFile::ImportPolygons(const QString theFileName, QStringList& thePolygonsList, TopTools_SequenceOfShape& theFaces, int& theShapeTypeOfFile)
435 int Stat = TryOpenShapeFile(theFileName);
438 myHSHP = SHPOpen( theFileName.toAscii().data(), "rb" );
439 if (!Parse(myHSHP, HYDROData_ShapeFile::ShapeType_Polygon, theShapeTypeOfFile))
441 for (size_t i = 0; i < mySHPObjects.size(); i++)
442 thePolygonsList.append("polygon_" + QString::number(i + 1));
445 if (myHSHP->nShapeType == 5)
451 for (size_t i = 0; i < mySHPObjects.size(); i++)
453 ReadSHPPolygon(mySHPObjects[i], i, aF);
466 void HYDROData_ShapeFile::Free()
468 for (size_t i = 0; i < mySHPObjects.size(); i++ )
469 free (mySHPObjects[i]);
471 mySHPObjects.clear();
480 void HYDROData_ShapeFile::ReadSHPPolyXY(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName,
481 int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
484 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
486 int nParts = anObj->nParts;
487 for ( int i = 0 ; i < nParts ; i++ )
489 int StartIndex = anObj->panPartStart[i];
492 EndIndex = anObj->panPartStart[i + 1];
494 EndIndex = anObj->nVertices;
496 bool IsClosed = false;
497 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
498 if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
499 anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] )
502 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true);
505 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false);
509 for ( int k = StartIndex; k < EndIndex ; k++ )
511 HYDROData_PolylineXY::Point aSectPoint;
512 aSectPoint.SetX( anObj->padfX[k] );
513 aSectPoint.SetY( anObj->padfY[k] );
514 aPolylineXY->AddPoint( i, aSectPoint );
519 aPolylineXY->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
520 aPolylineXY->SetName( theFileName + "_PolyXY_" + QString::number(theInd) );
522 aPolylineXY->Update();
523 theEntities.Append(aPolylineXY);
527 void HYDROData_ShapeFile::ReadSHPPoly3D(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName,
528 int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
530 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
532 Handle(HYDROData_Polyline3D) aPolylineObj = Handle(HYDROData_Polyline3D)::DownCast( theDocument->CreateObject( KIND_POLYLINE ) );
534 Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( theDocument->CreateObject( KIND_BATHYMETRY ) );
535 HYDROData_Bathymetry::AltitudePoints aAPoints;
537 int nParts = anObj->nParts;
538 for ( int i = 0 ; i < nParts ; i++ )
540 //bool aSectClosure = true;
541 int StartIndex = anObj->panPartStart[i];
544 EndIndex = anObj->panPartStart[i + 1];
546 EndIndex = anObj->nVertices;
548 bool IsClosed = false;
549 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
550 if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
551 anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] &&
552 anObj->padfZ[StartIndex] == anObj->padfZ[EndIndex - 1])
555 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true );
558 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false );
562 for ( int k = StartIndex ; k < EndIndex ; k++ )
564 HYDROData_PolylineXY::Point aSectPoint;
565 aSectPoint.SetX( anObj->padfX[k] );
566 aSectPoint.SetY( anObj->padfY[k] );
567 aPolylineXY->AddPoint( i, aSectPoint );
568 aAPoints.Append(gp_XYZ (anObj->padfX[k], anObj->padfY[k], anObj->padfZ[k]));
573 QString aBathName = theFileName + "_bath_" + QString::number(theInd);
574 QString aPolyXYName = theFileName + "_polyXY_" + QString::number(theInd);
575 QString aPoly3DName = theFileName + "_poly3D_" + QString::number(theInd);
577 aPolylineXY->SetName( aPolyXYName );
578 aPolylineXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
579 aPolylineXY->Update();
581 aBath->SetAltitudePoints(aAPoints);
582 aBath->SetName( aBathName );
584 aPolylineObj->SetPolylineXY (aPolylineXY, false);
585 aPolylineObj->SetAltitudeObject(aBath);
587 aPolylineObj->SetBorderColor( aPolylineObj->DefaultBorderColor() );
588 aPolylineObj->SetName( aPoly3DName );
590 aPolylineObj->Update();
591 theEntities.Append(aPolylineXY);
592 theEntities.Append(aPolylineObj);
598 int HYDROData_ShapeFile::ImportPolylines(Handle(HYDROData_Document) theDocument, const QString& theFileName,
599 NCollection_Sequence<Handle_HYDROData_Entity>& theEntities, int& theShapeTypeOfFile)
602 int aStat = TryOpenShapeFile(theFileName);
606 HYDROData_Iterator anIter( theDocument );
608 QStringList anExistingNames;
609 std::vector<int> anAllowedIndexes;
610 for( ; anIter.More(); anIter.Next() )
611 anExistingNames.push_back(anIter.Current()->GetName());
614 aHSHP = SHPOpen( theFileName.toAscii().data(), "rb" );
616 QFileInfo aFileInfo(theFileName);
617 QString aBaseFileName = aFileInfo.baseName();
619 if (!Parse(aHSHP, HYDROData_ShapeFile::ShapeType_Polyline, theShapeTypeOfFile))
621 if (aHSHP->nShapeType == 3 || aHSHP->nShapeType == 23)
624 for (;anAllowedIndexes.size() < mySHPObjects.size();)
626 if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)))
628 anAllowedIndexes.push_back(anInd);
635 for (size_t i = 0; i < mySHPObjects.size(); i++ )
637 ReadSHPPolyXY(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
641 else if (aHSHP->nShapeType == 13)
644 for (;anAllowedIndexes.size() < mySHPObjects.size();)
646 if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)) &&
647 !anExistingNames.contains(aBaseFileName + "_Poly3D_" + QString::number(anInd)) &&
648 !anExistingNames.contains(aBaseFileName + "_Bath_" + QString::number(anInd)))
650 anAllowedIndexes.push_back(anInd);
656 for (size_t i = 0; i < mySHPObjects.size(); i++ )
657 ReadSHPPoly3D(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
665 for (size_t i = 0; i < mySHPObjects.size(); i++ )
666 free (mySHPObjects[i]);
668 mySHPObjects.clear();
673 QString HYDROData_ShapeFile::GetShapeTypeName(int theType)
680 return "point (unsupported by HYDRO)";
682 return "arc/polyline (supported by HYDRO)";
684 return "polygon (supported by HYDRO)";
686 return "multipoint (unsupported by HYDRO)";
688 return "pointZ (unsupported by HYDRO)";
690 return "arcZ/polyline (supported by HYDRO)";
692 return "polygonZ (unsupported by HYDRO)";
694 return "multipointZ (unsupported by HYDRO)";
696 return "pointM (unsupported by HYDRO)";
698 return "arcM/polyline (supported by HYDRO)";
700 return "polygonM (unsupported by HYDRO)";
702 return "multipointM (unsupported by HYDRO)";
704 return "multipatch (unsupported by HYDRO)";
710 int HYDROData_ShapeFile::TryOpenShapeFile(QString theFileName)
712 QString aSHPfile = theFileName.simplified();
713 QString aSHXfile = theFileName.simplified().replace( theFileName.simplified().size() - 4, 4, ".shx");
715 QString anExt = theFileName.split('.', QString::SkipEmptyParts).back();
716 if (anExt.toLower() != "shp")
719 FILE* pFileSHP = NULL;
720 pFileSHP = fopen (aSHPfile.toAscii().data(), "r");
721 FILE* pFileSHX = NULL;
722 pFileSHX = fopen (aSHXfile.toAscii().data(), "r");
724 if (pFileSHP == NULL || pFileSHX == NULL)
726 if (pFileSHP == NULL)
728 if (pFileSHX == NULL)
738 bool HYDROData_ShapeFile::CheckDBFFileExisting(const QString& theSHPFilePath, QString& thePathToDBFFile)
740 QString aSHPfile = theSHPFilePath.simplified();
741 QString aDBFfile = theSHPFilePath.simplified().replace( theSHPFilePath.simplified().size() - 4, 4, ".dbf");
742 FILE* pFileDBF = NULL;
743 pFileDBF = fopen (aDBFfile.toAscii().data(), "r");
745 if (pFileDBF == NULL)
751 thePathToDBFFile = aDBFfile;
756 bool HYDROData_ShapeFile::DBF_OpenDBF(const QString& thePathToDBFFile)
758 myHDBF = DBFOpen( thePathToDBFFile.toAscii().data(), "r" );
765 int HYDROData_ShapeFile::DBF_GetNbFields()
769 return DBFGetFieldCount(myHDBF);
772 void HYDROData_ShapeFile::DBF_CloseDBF()
778 QStringList HYDROData_ShapeFile::DBF_GetFieldList()
780 QStringList FieldList;
781 int nWidth, nDecimals;
784 for( int i = 0; i < DBFGetFieldCount(myHDBF); i++ )
787 eType = DBFGetFieldInfo( myHDBF, i, chField, &nWidth, &nDecimals );
788 FieldList.append(QString(chField));
794 void HYDROData_ShapeFile::DBF_GetFieldTypeList(std::vector<DBF_FieldType>& FTVect)
796 int nWidth, nDecimals;
799 for( int i = 0; i < DBFGetFieldCount(myHDBF); i++ )
802 eType = DBFGetFieldInfo( myHDBF, i, chField, &nWidth, &nDecimals );
803 if( eType == FTString )
804 FT = DBF_FieldType_String;
805 else if( eType == FTInteger )
806 FT = DBF_FieldType_Integer;
807 else if( eType == FTDouble )
808 FT = DBF_FieldType_Double;
809 else if( eType == FTInvalid )
810 FT = DBF_FieldType_Invalid;
812 FTVect.push_back(FT);
817 int HYDROData_ShapeFile::DBF_GetNbRecords()
821 return DBFGetRecordCount(myHDBF);
824 void HYDROData_ShapeFile::DBF_GetAttributeList(int theIndexOfField, std::vector<DBF_AttrValue>& theAttrV)
826 int nWidth, nDecimals;
829 for( int i = 0; i < DBFGetRecordCount(myHDBF); i++ )
832 DBF_AttrValue anAttr;
833 eType = DBFGetFieldInfo( myHDBF, theIndexOfField, chField, &nWidth, &nDecimals );
835 if( DBFIsAttributeNULL( myHDBF, i, theIndexOfField ) )
837 anAttr.myIsNull = true;
838 DBF_FieldType FT = DBF_FieldType_None;
839 if( eType == FTString )
840 FT = DBF_FieldType_String;
841 else if( eType == FTInteger )
842 FT = DBF_FieldType_Integer;
843 else if( eType == FTDouble )
844 FT = DBF_FieldType_Double;
845 else if( eType == FTInvalid )
846 FT = DBF_FieldType_Invalid;
847 anAttr.myFieldType = FT;
855 const char* chAttr = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
856 anAttr.myIsNull = false;
857 anAttr.myFieldType = DBF_FieldType_String;
858 anAttr.myRawValue = chAttr;
859 anAttr.myStrVal = QString(chAttr);
865 int iAttr = DBFReadIntegerAttribute( myHDBF, i, theIndexOfField );
866 anAttr.myIsNull = false;
867 anAttr.myFieldType = DBF_FieldType_Integer;
868 anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
869 anAttr.myIntVal = iAttr;
875 double dAttr = DBFReadDoubleAttribute( myHDBF, i, theIndexOfField );
876 anAttr.myIsNull = false;
877 anAttr.myFieldType = DBF_FieldType_Double;
878 anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
879 anAttr.myDoubleVal = dAttr;
886 theAttrV.push_back(anAttr);
891 bool HYDROData_ShapeFile::DBF_WriteFieldAndValues(const QString& theFileName, const QString& theFieldName, DBF_FieldType theType, const std::vector<DBF_AttrValue>& theAttrV, bool bUseRawValue)
893 // Check that given field type is equal to field types of attributes values
894 for (size_t i = 0; i < theAttrV.size(); i++)
896 if (theAttrV[i].myFieldType != theType)
901 hDBF = DBFCreate( theFileName.toStdString().c_str() );
905 if (theType != DBF_FieldType_String && theType != DBF_FieldType_Integer && theType != DBF_FieldType_Double)
908 return false; //cant handle any other cases
914 case DBF_FieldType_String:
916 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTString, nWidth, 0);
920 case DBF_FieldType_Integer:
922 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTInteger, nWidth, 0);
926 case DBF_FieldType_Double:
928 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTDouble, nWidth, 0);
935 if (DBFGetFieldCount( hDBF ) != 1)
940 if (DBFGetRecordCount( hDBF ) != 0)
949 for (size_t i = 0; i < theAttrV.size(); i++)
951 if (!theAttrV[i].myIsNull)
952 stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myRawValue.c_str());
954 stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 );
965 for (size_t i = 0; i < theAttrV.size(); i++)
967 if (!theAttrV[i].myIsNull)
970 case DBF_FieldType_String:
972 stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myStrVal.toStdString().c_str());
976 case DBF_FieldType_Integer:
978 stat = DBFWriteIntegerAttribute(hDBF, (int)i, 0, theAttrV[i].myIntVal);
982 case DBF_FieldType_Double:
984 stat = DBFWriteDoubleAttribute(hDBF, (int)i, 0, theAttrV[i].myDoubleVal);
991 stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 );