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 <Geom_Curve.hxx>
39 #include <Geom_Line.hxx>
40 #include <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>
62 #include <BRepBndLib.hxx>
63 #include <Bnd_Box.hxx>
66 #pragma warning( disable: 4996 )
69 //SHP->TFaces (Import)
72 HYDROData_ShapeFile::HYDROData_ShapeFile() : myHSHP(NULL)
76 HYDROData_ShapeFile::~HYDROData_ShapeFile()
81 void HYDROData_ShapeFile::Export(Handle(HYDROData_Document) theDocument,
82 const QString& aFileName,
83 NCollection_Sequence<Handle(HYDROData_PolylineXY)> aPolyXYSeq,
84 NCollection_Sequence<Handle(HYDROData_Polyline3D)> aPoly3DSeq,
85 QStringList& aNonExpList)
88 if (!aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty())
90 hSHPHandle = SHPCreate( aFileName.toLatin1().data(), SHPT_ARC );
91 for (int i = 1; i <= aPolyXYSeq.Size(); i++)
92 if (WriteObjectPolyXY(theDocument, hSHPHandle, aPolyXYSeq(i)) != 1)
93 aNonExpList.append(aPolyXYSeq(i)->GetName());
95 else if (aPolyXYSeq.IsEmpty() && !aPoly3DSeq.IsEmpty())
97 hSHPHandle = SHPCreate( aFileName.toLatin1().data(), SHPT_ARCZ );
98 for (int i = 1; i <= aPoly3DSeq.Size(); i++)
99 if (WriteObjectPoly3D(theDocument, hSHPHandle, aPoly3DSeq(i)) != 1)
100 aNonExpList.append(aPoly3DSeq(i)->GetName());
102 if (hSHPHandle->nRecords > 0)
103 SHPClose( hSHPHandle );
106 SHPClose( hSHPHandle );
107 QString aFN = aFileName.simplified();
108 remove (aFN.toStdString().c_str());
109 remove ((aFN.simplified().replace( aFN.simplified().size() - 4, 4, ".shx")).toStdString().c_str());
113 void HYDROData_ShapeFile::Export(Handle(HYDROData_Document) theDocument,
114 const QString& aFileName,
115 const Handle(HYDROData_LandCoverMap)& aLCM,
116 QStringList& aNonExpList,
117 bool bCheckLinear, bool bUseDiscr, double theDefl)
119 if (bCheckLinear && !aLCM->CheckLinear())
122 SHPHandle hSHPHandle = NULL;
123 if ( !aLCM.IsNull() && !aLCM->IsEmpty())
125 hSHPHandle = SHPCreate( aFileName.toLatin1().data(), SHPT_POLYGON );
126 HYDROData_LandCoverMap::Explorer It( aLCM );
127 for( ; It.More(); It.Next())
129 TopoDS_Face aFace = It.Face();
130 if (WriteObjectPolygon(theDocument, hSHPHandle, aFace, bUseDiscr, theDefl) != 1)
131 aNonExpList.append(aLCM->GetName() + "_" + QString::number(It.Index()));
134 if (hSHPHandle && hSHPHandle->nRecords > 0)
135 SHPClose( hSHPHandle );
138 SHPClose( hSHPHandle );
139 QString aFN = aFileName.simplified();
140 remove (aFN.toStdString().c_str());
141 remove ((aFN.simplified().replace( aFN.simplified().size() - 4, 4, ".shx")).toStdString().c_str());
146 int HYDROData_ShapeFile::WriteObjectPolyXY(Handle(HYDROData_Document) theDocument,
147 SHPHandle theShpHandle,
148 Handle(HYDROData_PolylineXY) thePoly )
151 std::vector<double> x, y;
152 std::vector<int> anPartStart;
154 for (int i = 0; i < thePoly->NbSections(); i++)
155 if (thePoly->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE)
158 for (int i = 0; i < thePoly->NbSections(); i++)
160 anPartStart.push_back(x.size());
161 HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPoints(i);
162 for (int j = 1; j <= aPointList.Size(); j++)
164 gp_XY P = aPointList(j);
165 theDocument->Transform(P, false);
169 if (thePoly->IsClosedSection(i))
171 gp_XY P = aPointList(1);
172 theDocument->Transform(P, false);
178 aSHPObj = SHPCreateObject( SHPT_ARC, -1, thePoly->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
179 SHPWriteObject( theShpHandle, -1, aSHPObj );
180 SHPDestroyObject( aSHPObj );
184 int HYDROData_ShapeFile::WriteObjectPoly3D(Handle(HYDROData_Document) theDocument,
185 SHPHandle theShpHandle,
186 Handle(HYDROData_Polyline3D) thePoly )
189 std::vector<double> x, y, z;
190 std::vector<int> anPartStart;
192 for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)
193 if (thePoly->GetPolylineXY()->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE)
196 for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)
198 anPartStart.push_back(x.size());
199 HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPolylineXY()->GetPoints(i);
200 for (int j = 1; j <= aPointList.Size(); j++)
202 gp_XY P = aPointList(j);
203 theDocument->Transform(P, false);
206 z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(j).X(), aPointList(j).Y())));
208 if ( thePoly->GetPolylineXY()->IsClosedSection(i))
210 gp_XY P = aPointList(1);
211 theDocument->Transform(P, false);
214 z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(1).X(), aPointList(1).Y())));
219 aSHPObj = SHPCreateObject( SHPT_ARCZ, -1, thePoly->GetPolylineXY()->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], &z[0], NULL );
220 SHPWriteObject( theShpHandle, -1, aSHPObj );
221 SHPDestroyObject( aSHPObj );
225 int HYDROData_ShapeFile::WriteObjectPolygon(Handle(HYDROData_Document) theDocument,
226 SHPHandle theShpHandle,
227 const TopoDS_Shape& theInputShape,
228 bool bUseDiscr, double theDefl)
230 if (theInputShape.IsNull())
233 if (theInputShape.ShapeType() == TopAbs_FACE)
235 ProcessFace(theDocument, TopoDS::Face(theInputShape), theShpHandle, bUseDiscr, theDefl);
237 else if (theInputShape.ShapeType() == TopAbs_COMPOUND)
239 TopExp_Explorer Ex(theInputShape, TopAbs_FACE);
240 for (; Ex.More(); Ex.Next())
242 TopoDS_Face aF = TopoDS::Face(Ex.Current());
245 ProcessFace(theDocument, aF, theShpHandle, bUseDiscr, theDefl);
255 void HYDROData_ShapeFile::ProcessFace(Handle(HYDROData_Document) theDocument,
256 const TopoDS_Face& theFace,
257 SHPHandle theShpHandle,
258 bool bUseDiscr, double theDefl )
260 if (theFace.ShapeType() != TopAbs_FACE)
263 std::vector<double> x, y;
264 std::vector<int> anPartStart;
265 TopoDS_Wire OuterW = BRepTools::OuterWire(theFace);
266 NCollection_Sequence<TopoDS_Wire> aWires;
268 //write an outer wire first
269 aWires.Append(OuterW);
270 TopExp_Explorer Ex(theFace, TopAbs_WIRE);
271 for (; Ex.More(); Ex.Next())
273 TopoDS_Wire aW = TopoDS::Wire(Ex.Current());
274 if (aW.IsEqual(OuterW))
280 for (int k = 1; k <= aWires.Length(); k++)
282 TopoDS_Wire aW = aWires(k);
286 if (aW.Orientation() == TopAbs_INTERNAL)
287 //cant write internal wires/edges
289 // Try to reorder edges
290 Handle(ShapeFix_Wire) aSFW = new ShapeFix_Wire( aW, theFace, Precision::Confusion() );
291 aSFW->ModifyTopologyMode() = Standard_False;
292 aSFW->ModifyGeometryMode() = Standard_False;
294 Handle(ShapeExtend_WireData) aSEWD = aSFW->WireData();
295 Standard_Integer nbE = aSEWD->NbEdges();
299 anPartStart.push_back(x.size());
300 NCollection_Sequence<gp_Pnt2d> aPnts;
301 for (Standard_Integer i = 1; i <= nbE; i++)
303 TopoDS_Edge E = aSEWD->Edge(i);
306 TopoDS_Vertex aV = TopExp::LastVertex(E, 1);
309 gp_Pnt P = BRep_Tool::Pnt(aV);
310 aPnts.Append(gp_Pnt2d(P.X(), P.Y()));
314 BRepAdaptor_Curve Cur( E );
315 GCPnts_QuasiUniformDeflection Discr( Cur, theDefl );
316 if( !Discr.IsDone() )
317 continue; //skip edge?
318 double NewDefl = theDefl/2.0;
319 while (Discr.NbPoints() < 2)
321 Discr.Initialize(Cur, NewDefl);
322 NewDefl = NewDefl/2.0;
325 if (E.Orientation() == TopAbs_FORWARD)
327 for( int i = 1; i <= Discr.NbPoints(); i++ )
329 gp_Pnt P = Discr.Value( i );
330 aPnts.Append(gp_Pnt2d(P.X(), P.Y()));
335 for( int i = Discr.NbPoints(); i > 0; i-- )
337 gp_Pnt P = Discr.Value( i );
338 aPnts.Append(gp_Pnt2d(P.X(), P.Y()));
343 NCollection_Sequence<gp_Pnt2d> aNPnts;
344 aNPnts.Append(aPnts.First());
345 for (int j = 1; j <= aPnts.Size() - 1; j++)
347 if (!aPnts(j).IsEqual(aPnts(j + 1), Precision::Confusion()))
348 aNPnts.Append(aPnts(j + 1));
351 //assume that the orientation of external wire & internal wires is correct
352 //so just write all points "as-is"
353 //External wire will be written in clockwise direction
354 //any other wires (holes) - in anticlockwise direction
355 for (int j = aNPnts.Size(); j >= 1; j--)
357 gp_XY P = gp_XY(aNPnts(j).X(), aNPnts(j).Y());
358 theDocument->Transform(P, false);
362 //first point is same as the last one => closed polygon
363 gp_XY P = gp_XY(aNPnts.Last().X(), aNPnts.Last().Y());
364 theDocument->Transform(P, false);
370 aSHPObj = SHPCreateObject( SHPT_POLYGON, -1, NbWires, &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
371 SHPWriteObject( theShpHandle, -1, aSHPObj );
372 SHPDestroyObject( aSHPObj );
376 bool HYDROData_ShapeFile::Parse(SHPHandle theHandle, ShapeType theType, int& theShapeTypeOfFile)
379 mySHPObjects.clear();
380 SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
381 theShapeTypeOfFile = aShapeType;
382 bool ToRead = (theType == ShapeType_Polyline && (aShapeType == 3 || aShapeType == 13 || aShapeType == 23)) ||
383 (theType == ShapeType_Polygon && aShapeType == 5);
386 for (int i = 0; i < theHandle->nRecords; i++)
387 mySHPObjects.push_back(SHPReadObject(theHandle, i));
394 void HYDROData_ShapeFile::ReadSHPPolygon(Handle(HYDROData_Document) theDocument, SHPObject* anObj, int i, TopoDS_Face& F)
399 int nParts = anObj->nParts;
400 Handle(Geom_Plane) aPlaneSur = new Geom_Plane(gp_Pnt(0,0,0), gp_Dir(0,0,1));
405 NCollection_Sequence<TopoDS_Wire> allWires;
407 TopTools_SequenceOfShape aWires;
408 for ( int i = 0 ; i < nParts ; i++ )
410 BRepBuilderAPI_MakeWire aBuilder;
411 int StartIndex = anObj->panPartStart[i];
414 EndIndex = anObj->panPartStart[i + 1];
416 EndIndex = anObj->nVertices;
421 //First point is same as the last point
422 int NbPnts = EndIndex - StartIndex - 1;
423 NCollection_Array1<TopoDS_Vertex> VPoints(0, NbPnts);
425 for ( int k = StartIndex; k < EndIndex; k++ )
427 gp_Pnt P (anObj->padfX[k], anObj->padfY[k], 0);
428 theDocument->Transform(P, true);
429 VPoints.ChangeValue(j) = BRepLib_MakeVertex(P).Vertex();
433 for ( int k = 0; k < VPoints.Size() - 1; k++ )
435 gp_Pnt P1 = BRep_Tool::Pnt(VPoints(k));
436 gp_Pnt P2 = BRep_Tool::Pnt(VPoints(k + 1));
437 if (P1.Distance(P2) < Precision::Confusion())
439 Handle(Geom_TrimmedCurve) aTC = GC_MakeSegment(P1, P2).Value();
441 if ( k != VPoints.Size() - 2)
442 E = BRepLib_MakeEdge(aTC, VPoints(k), VPoints(k + 1)).Edge();
444 //the last edge => use first and last vertices
445 E = BRepLib_MakeEdge(aTC, VPoints.First(), VPoints.Value(VPoints.Upper() - 1)).Edge();
447 //If SHP file is correct then the outer wire and the holes will have the correct orientations
448 W.Closed (Standard_True);
449 W.Orientation(TopAbs_FORWARD);
456 if (allWires.Size() > 1)
458 NCollection_Sequence<Bnd_Box> BBs;
459 //try to find the largest bbox
460 for (int i = 1; i <= allWires.Size(); i++)
462 TopoDS_Wire W = allWires(i);
464 BRepBndLib::AddClose(W, BB);
467 for (int i = 1; i <= BBs.Size(); i++)
470 for (int j = 1; j <= BBs.Size(); j++)
474 Standard_Real iXmax, iXmin, iYmax, iYmin, z0, z1;
475 Standard_Real jXmax, jXmin, jYmax, jYmin;
476 BBs(i).Get(iXmin, iYmin, z0, iXmax, iYmax, z1);
477 BBs(j).Get(jXmin, jYmin, z0, jXmax, jYmax, z1);
478 if (!(iXmin > jXmin &&
492 OutWIndex = 1; //one wire => no need to check
494 for (int i = 1; i <= allWires.Size(); i++)
498 TopoDS_Wire W = allWires(i);
500 BB.UpdateFace(DF, aPlaneSur, TopLoc_Location(), Precision::Confusion());
502 BRepTopAdaptor_FClass2d FClass(DF, Precision::PConfusion());
503 if ( i == OutWIndex && FClass.PerformInfinitePoint() == TopAbs_IN)
505 if ( i != OutWIndex && FClass.PerformInfinitePoint() == TopAbs_OUT)
511 //Add surface to the face
512 BB.UpdateFace(F, aPlaneSur, TopLoc_Location(), Precision::Confusion());
513 F.Closed(Standard_True);
516 int HYDROData_ShapeFile::ImportPolygons(Handle(HYDROData_Document) theDocument, const QString theFileName, QStringList& thePolygonsList, TopTools_SequenceOfShape& theFaces, int& theShapeTypeOfFile)
519 int Stat = TryOpenShapeFile(theFileName);
522 myHSHP = SHPOpen( theFileName.toLatin1().data(), "rb" );
523 if (!Parse(myHSHP, HYDROData_ShapeFile::ShapeType_Polygon, theShapeTypeOfFile))
525 for (size_t i = 0; i < mySHPObjects.size(); i++)
526 thePolygonsList.append("polygon_" + QString::number(i + 1));
529 if (myHSHP->nShapeType == 5)
535 for (size_t i = 0; i < mySHPObjects.size(); i++)
537 ReadSHPPolygon(theDocument, mySHPObjects[i], i, aF);
550 void HYDROData_ShapeFile::Free()
552 for (size_t i = 0; i < mySHPObjects.size(); i++ )
553 free (mySHPObjects[i]);
555 mySHPObjects.clear();
564 void HYDROData_ShapeFile::ReadSHPPolyXY(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName,
565 int theInd, NCollection_Sequence<Handle(HYDROData_Entity)>& theEntities)
568 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
570 int nParts = anObj->nParts;
571 for ( int i = 0 ; i < nParts ; i++ )
573 int StartIndex = anObj->panPartStart[i];
576 EndIndex = anObj->panPartStart[i + 1];
578 EndIndex = anObj->nVertices;
580 bool IsClosed = false;
581 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
582 if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
583 anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] )
586 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toLatin1()), aSectType, true);
589 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toLatin1()), aSectType, false);
593 for ( int k = StartIndex; k < EndIndex ; k++ )
595 HYDROData_PolylineXY::Point aSectPoint = gp_XY(anObj->padfX[k], anObj->padfY[k]);
596 theDocument->Transform(aSectPoint, true);
597 aPolylineXY->AddPoint( i, aSectPoint );
602 aPolylineXY->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
603 aPolylineXY->SetName( theFileName + "_PolyXY_" + QString::number(theInd) );
605 aPolylineXY->Update();
606 theEntities.Append(aPolylineXY);
610 void HYDROData_ShapeFile::ReadSHPPoly3D(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName,
611 int theInd, NCollection_Sequence<Handle(HYDROData_Entity)>& theEntities)
613 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
615 Handle(HYDROData_Polyline3D) aPolylineObj = Handle(HYDROData_Polyline3D)::DownCast( theDocument->CreateObject( KIND_POLYLINE ) );
617 Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( theDocument->CreateObject( KIND_BATHYMETRY ) );
618 HYDROData_Bathymetry::AltitudePoints aAPoints;
620 int nParts = anObj->nParts;
621 for ( int i = 0 ; i < nParts ; i++ )
623 //bool aSectClosure = true;
624 int StartIndex = anObj->panPartStart[i];
627 EndIndex = anObj->panPartStart[i + 1];
629 EndIndex = anObj->nVertices;
631 bool IsClosed = false;
632 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
633 if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
634 anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] &&
635 anObj->padfZ[StartIndex] == anObj->padfZ[EndIndex - 1])
638 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toLatin1()), aSectType, true );
641 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toLatin1()), aSectType, false );
645 for ( int k = StartIndex ; k < EndIndex ; k++ )
647 HYDROData_PolylineXY::Point aSectPoint = gp_XY(anObj->padfX[k], anObj->padfY[k]);
648 theDocument->Transform(aSectPoint, true);
649 aPolylineXY->AddPoint( i, aSectPoint );
650 aAPoints.Append(gp_XYZ (aSectPoint.X(), aSectPoint.Y(), anObj->padfZ[k]));
655 QString aBathName = theFileName + "_bath_" + QString::number(theInd);
656 QString aPolyXYName = theFileName + "_polyXY_" + QString::number(theInd);
657 QString aPoly3DName = theFileName + "_poly3D_" + QString::number(theInd);
659 aPolylineXY->SetName( aPolyXYName );
660 aPolylineXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
661 aPolylineXY->Update();
663 aBath->SetAltitudePoints(aAPoints);
664 aBath->SetName( aBathName );
666 aPolylineObj->SetPolylineXY (aPolylineXY, false);
667 aPolylineObj->SetAltitudeObject(aBath);
669 aPolylineObj->SetBorderColor( aPolylineObj->DefaultBorderColor() );
670 aPolylineObj->SetName( aPoly3DName );
672 aPolylineObj->Update();
673 theEntities.Append(aPolylineXY);
674 theEntities.Append(aPolylineObj);
680 int HYDROData_ShapeFile::ImportPolylines(Handle(HYDROData_Document) theDocument, const QString& theFileName,
681 NCollection_Sequence<Handle(HYDROData_Entity)>& theEntities, int& theShapeTypeOfFile)
684 int aStat = TryOpenShapeFile(theFileName);
688 HYDROData_Iterator anIter( theDocument );
690 QStringList anExistingNames;
691 std::vector<int> anAllowedIndexes;
692 for( ; anIter.More(); anIter.Next() )
693 anExistingNames.push_back(anIter.Current()->GetName());
696 aHSHP = SHPOpen( theFileName.toLatin1().data(), "rb" );
698 QFileInfo aFileInfo(theFileName);
699 QString aBaseFileName = aFileInfo.baseName();
701 if (!Parse(aHSHP, HYDROData_ShapeFile::ShapeType_Polyline, theShapeTypeOfFile))
703 if (aHSHP->nShapeType == 3 || aHSHP->nShapeType == 23)
706 for (;anAllowedIndexes.size() < mySHPObjects.size();)
708 if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)))
710 anAllowedIndexes.push_back(anInd);
717 for (size_t i = 0; i < mySHPObjects.size(); i++ )
719 ReadSHPPolyXY(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
723 else if (aHSHP->nShapeType == 13)
726 for (;anAllowedIndexes.size() < mySHPObjects.size();)
728 if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)) &&
729 !anExistingNames.contains(aBaseFileName + "_Poly3D_" + QString::number(anInd)) &&
730 !anExistingNames.contains(aBaseFileName + "_Bath_" + QString::number(anInd)))
732 anAllowedIndexes.push_back(anInd);
738 for (size_t i = 0; i < mySHPObjects.size(); i++ )
739 ReadSHPPoly3D(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
747 for (size_t i = 0; i < mySHPObjects.size(); i++ )
748 free (mySHPObjects[i]);
750 mySHPObjects.clear();
755 QString HYDROData_ShapeFile::GetShapeTypeName(int theType)
762 return "point (unsupported by HYDRO)";
764 return "arc/polyline (supported by HYDRO)";
766 return "polygon (supported by HYDRO)";
768 return "multipoint (unsupported by HYDRO)";
770 return "pointZ (unsupported by HYDRO)";
772 return "arcZ/polyline (supported by HYDRO)";
774 return "polygonZ (unsupported by HYDRO)";
776 return "multipointZ (unsupported by HYDRO)";
778 return "pointM (unsupported by HYDRO)";
780 return "arcM/polyline (supported by HYDRO)";
782 return "polygonM (unsupported by HYDRO)";
784 return "multipointM (unsupported by HYDRO)";
786 return "multipatch (unsupported by HYDRO)";
792 int HYDROData_ShapeFile::TryOpenShapeFile(QString theFileName)
794 QString aSHPfile = theFileName.simplified();
795 QString aSHXfile = theFileName.simplified().replace( theFileName.simplified().size() - 4, 4, ".shx");
797 QString anExt = theFileName.split('.', QString::SkipEmptyParts).back();
798 if (anExt.toLower() != "shp")
801 FILE* pFileSHP = NULL;
802 pFileSHP = fopen (aSHPfile.toLatin1().data(), "r");
803 FILE* pFileSHX = NULL;
804 pFileSHX = fopen (aSHXfile.toLatin1().data(), "r");
806 if (pFileSHP == NULL || pFileSHX == NULL)
808 if (pFileSHP == NULL)
810 if (pFileSHX == NULL)
820 bool HYDROData_ShapeFile::CheckDBFFileExisting(const QString& theSHPFilePath, QString& thePathToDBFFile)
822 QString aSHPfile = theSHPFilePath.simplified();
823 QString aDBFfile = theSHPFilePath.simplified().replace( theSHPFilePath.simplified().size() - 4, 4, ".dbf");
824 FILE* pFileDBF = NULL;
825 pFileDBF = fopen (aDBFfile.toLatin1().data(), "r");
827 if (pFileDBF == NULL)
833 thePathToDBFFile = aDBFfile;
838 bool HYDROData_ShapeFile::DBF_OpenDBF(const QString& thePathToDBFFile)
840 myHDBF = DBFOpen( thePathToDBFFile.toLatin1().data(), "r" );
847 int HYDROData_ShapeFile::DBF_GetNbFields()
851 return DBFGetFieldCount(myHDBF);
854 void HYDROData_ShapeFile::DBF_CloseDBF()
860 QStringList HYDROData_ShapeFile::DBF_GetFieldList()
862 QStringList FieldList;
863 int nWidth, nDecimals;
866 for( int i = 0; i < DBFGetFieldCount(myHDBF); i++ )
869 eType = DBFGetFieldInfo( myHDBF, i, chField, &nWidth, &nDecimals );
870 FieldList.append(QString(chField));
876 void HYDROData_ShapeFile::DBF_GetFieldTypeList(std::vector<DBF_FieldType>& FTVect)
878 int nWidth, nDecimals;
881 for( int i = 0; i < DBFGetFieldCount(myHDBF); i++ )
884 eType = DBFGetFieldInfo( myHDBF, i, chField, &nWidth, &nDecimals );
885 if( eType == FTString )
886 FT = DBF_FieldType_String;
887 else if( eType == FTInteger )
888 FT = DBF_FieldType_Integer;
889 else if( eType == FTDouble )
890 FT = DBF_FieldType_Double;
891 else if( eType == FTInvalid )
892 FT = DBF_FieldType_Invalid;
894 FTVect.push_back(FT);
899 int HYDROData_ShapeFile::DBF_GetNbRecords()
903 return DBFGetRecordCount(myHDBF);
906 void HYDROData_ShapeFile::DBF_GetAttributeList(int theIndexOfField, std::vector<DBF_AttrValue>& theAttrV)
908 int nWidth, nDecimals;
911 for( int i = 0; i < DBFGetRecordCount(myHDBF); i++ )
914 DBF_AttrValue anAttr;
915 eType = DBFGetFieldInfo( myHDBF, theIndexOfField, chField, &nWidth, &nDecimals );
917 if( DBFIsAttributeNULL( myHDBF, i, theIndexOfField ) )
919 anAttr.myIsNull = true;
920 DBF_FieldType FT = DBF_FieldType_None;
921 if( eType == FTString )
922 FT = DBF_FieldType_String;
923 else if( eType == FTInteger )
924 FT = DBF_FieldType_Integer;
925 else if( eType == FTDouble )
926 FT = DBF_FieldType_Double;
927 else if( eType == FTInvalid )
928 FT = DBF_FieldType_Invalid;
929 anAttr.myFieldType = FT;
937 const char* chAttr = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
938 anAttr.myIsNull = false;
939 anAttr.myFieldType = DBF_FieldType_String;
940 anAttr.myRawValue = chAttr;
941 anAttr.myStrVal = QString(chAttr);
947 int iAttr = DBFReadIntegerAttribute( myHDBF, i, theIndexOfField );
948 anAttr.myIsNull = false;
949 anAttr.myFieldType = DBF_FieldType_Integer;
950 anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
951 anAttr.myIntVal = iAttr;
957 double dAttr = DBFReadDoubleAttribute( myHDBF, i, theIndexOfField );
958 anAttr.myIsNull = false;
959 anAttr.myFieldType = DBF_FieldType_Double;
960 anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
961 anAttr.myDoubleVal = dAttr;
968 theAttrV.push_back(anAttr);
973 bool HYDROData_ShapeFile::DBF_WriteFieldAndValues(const QString& theFileName, const QString& theFieldName, DBF_FieldType theType, const std::vector<DBF_AttrValue>& theAttrV, bool bUseRawValue)
975 // Check that given field type is equal to field types of attributes values
976 for (size_t i = 0; i < theAttrV.size(); i++)
978 if (theAttrV[i].myFieldType != theType)
983 hDBF = DBFCreate( theFileName.toStdString().c_str() );
987 if (theType != DBF_FieldType_String && theType != DBF_FieldType_Integer && theType != DBF_FieldType_Double)
990 return false; //cant handle any other cases
996 case DBF_FieldType_String:
998 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTString, nWidth, 0);
1002 case DBF_FieldType_Integer:
1004 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTInteger, nWidth, 0);
1008 case DBF_FieldType_Double:
1010 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTDouble, nWidth, 0);
1017 if (DBFGetFieldCount( hDBF ) != 1)
1022 if (DBFGetRecordCount( hDBF ) != 0)
1031 for (size_t i = 0; i < theAttrV.size(); i++)
1033 if (!theAttrV[i].myIsNull)
1034 stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myRawValue.c_str());
1036 stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 );
1047 for (size_t i = 0; i < theAttrV.size(); i++)
1049 if (!theAttrV[i].myIsNull)
1052 case DBF_FieldType_String:
1054 stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myStrVal.toStdString().c_str());
1058 case DBF_FieldType_Integer:
1060 stat = DBFWriteIntegerAttribute(hDBF, (int)i, 0, theAttrV[i].myIntVal);
1064 case DBF_FieldType_Double:
1066 stat = DBFWriteDoubleAttribute(hDBF, (int)i, 0, theAttrV[i].myDoubleVal);
1073 stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 );