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 &&
383 (aShapeType == 3 || aShapeType == 13 || aShapeType == 23 || aShapeType == 5)) ||
384 (theType == ShapeType_Polygon && aShapeType == 5);
387 for (int i = 0; i < theHandle->nRecords; i++)
388 mySHPObjects.push_back(SHPReadObject(theHandle, i));
395 void HYDROData_ShapeFile::ReadSHPPolygon(Handle(HYDROData_Document) theDocument, SHPObject* anObj, int i, TopoDS_Face& F)
400 int nParts = anObj->nParts;
401 Handle(Geom_Plane) aPlaneSur = new Geom_Plane(gp_Pnt(0,0,0), gp_Dir(0,0,1));
406 NCollection_Sequence<TopoDS_Wire> allWires;
408 TopTools_SequenceOfShape aWires;
409 for ( int i = 0 ; i < nParts ; i++ )
411 BRepBuilderAPI_MakeWire aBuilder;
412 int StartIndex = anObj->panPartStart[i];
415 EndIndex = anObj->panPartStart[i + 1];
417 EndIndex = anObj->nVertices;
422 //First point is same as the last point
423 int NbPnts = EndIndex - StartIndex - 1;
424 NCollection_Array1<TopoDS_Vertex> VPoints(0, NbPnts);
426 for ( int k = StartIndex; k < EndIndex; k++ )
428 gp_Pnt P (anObj->padfX[k], anObj->padfY[k], 0);
429 theDocument->Transform(P, true);
430 VPoints.ChangeValue(j) = BRepLib_MakeVertex(P).Vertex();
434 for ( int k = 0; k < VPoints.Size() - 1; k++ )
436 gp_Pnt P1 = BRep_Tool::Pnt(VPoints(k));
437 gp_Pnt P2 = BRep_Tool::Pnt(VPoints(k + 1));
438 if (P1.Distance(P2) < Precision::Confusion())
440 Handle(Geom_TrimmedCurve) aTC = GC_MakeSegment(P1, P2).Value();
442 if ( k != VPoints.Size() - 2)
443 E = BRepLib_MakeEdge(aTC, VPoints(k), VPoints(k + 1)).Edge();
445 //the last edge => use first and last vertices
446 E = BRepLib_MakeEdge(aTC, VPoints.First(), VPoints.Value(VPoints.Upper() - 1)).Edge();
448 //If SHP file is correct then the outer wire and the holes will have the correct orientations
449 W.Closed (Standard_True);
450 W.Orientation(TopAbs_FORWARD);
457 if (allWires.Size() > 1)
459 NCollection_Sequence<Bnd_Box> BBs;
460 //try to find the largest bbox
461 for (int i = 1; i <= allWires.Size(); i++)
463 TopoDS_Wire W = allWires(i);
465 BRepBndLib::AddClose(W, BB);
468 for (int i = 1; i <= BBs.Size(); i++)
471 for (int j = 1; j <= BBs.Size(); j++)
475 Standard_Real iXmax, iXmin, iYmax, iYmin, z0, z1;
476 Standard_Real jXmax, jXmin, jYmax, jYmin;
477 BBs(i).Get(iXmin, iYmin, z0, iXmax, iYmax, z1);
478 BBs(j).Get(jXmin, jYmin, z0, jXmax, jYmax, z1);
479 if (!(iXmin > jXmin &&
493 OutWIndex = 1; //one wire => no need to check
495 for (int i = 1; i <= allWires.Size(); i++)
499 TopoDS_Wire W = allWires(i);
501 BB.UpdateFace(DF, aPlaneSur, TopLoc_Location(), Precision::Confusion());
503 BRepTopAdaptor_FClass2d FClass(DF, Precision::PConfusion());
504 if ( i == OutWIndex && FClass.PerformInfinitePoint() == TopAbs_IN)
506 if ( i != OutWIndex && FClass.PerformInfinitePoint() == TopAbs_OUT)
512 //Add surface to the face
513 BB.UpdateFace(F, aPlaneSur, TopLoc_Location(), Precision::Confusion());
514 F.Closed(Standard_True);
517 int HYDROData_ShapeFile::ImportPolygons(Handle(HYDROData_Document) theDocument, const QString theFileName, QStringList& thePolygonsList, TopTools_SequenceOfShape& theFaces, int& theShapeTypeOfFile)
520 int Stat = TryOpenShapeFile(theFileName);
523 myHSHP = SHPOpen( theFileName.toLatin1().data(), "rb" );
524 if (!Parse(myHSHP, HYDROData_ShapeFile::ShapeType_Polygon, theShapeTypeOfFile))
526 for (size_t i = 0; i < mySHPObjects.size(); i++)
527 thePolygonsList.append("polygon_" + QString::number(i + 1));
530 if (myHSHP->nShapeType == 5)
536 for (size_t i = 0; i < mySHPObjects.size(); i++)
538 ReadSHPPolygon(theDocument, mySHPObjects[i], i, aF);
551 void HYDROData_ShapeFile::Free()
553 for (size_t i = 0; i < mySHPObjects.size(); i++ )
554 free (mySHPObjects[i]);
556 mySHPObjects.clear();
565 void HYDROData_ShapeFile::ReadSHPPolyXY(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName,
566 int theInd, NCollection_Sequence<Handle(HYDROData_Entity)>& theEntities, bool bReadAsPolyline)
569 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
571 int nParts = anObj->nParts;
572 for ( int i = 0 ; i < nParts ; i++ )
574 int StartIndex = anObj->panPartStart[i];
577 EndIndex = anObj->panPartStart[i + 1];
579 EndIndex = anObj->nVertices;
581 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
582 if (!bReadAsPolyline)
584 bool IsClosed = false;
585 if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
586 anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] )
589 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toLatin1()), aSectType, true);
592 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toLatin1()), aSectType, false);
599 //polygon; contours always closed
600 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toLatin1()), aSectType, true);
603 for ( int k = StartIndex; k < EndIndex ; k++ )
605 HYDROData_PolylineXY::Point aSectPoint = gp_XY(anObj->padfX[k], anObj->padfY[k]);
606 theDocument->Transform(aSectPoint, true);
607 aPolylineXY->AddPoint( i, aSectPoint );
612 aPolylineXY->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
613 aPolylineXY->SetName( theFileName + "_PolyXY_" + QString::number(theInd) );
615 aPolylineXY->Update();
616 theEntities.Append(aPolylineXY);
620 void HYDROData_ShapeFile::ReadSHPPoly3D(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName,
621 int theInd, NCollection_Sequence<Handle(HYDROData_Entity)>& theEntities)
623 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
625 Handle(HYDROData_Polyline3D) aPolylineObj = Handle(HYDROData_Polyline3D)::DownCast( theDocument->CreateObject( KIND_POLYLINE ) );
627 Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( theDocument->CreateObject( KIND_BATHYMETRY ) );
628 HYDROData_Bathymetry::AltitudePoints aAPoints;
630 int nParts = anObj->nParts;
631 for ( int i = 0 ; i < nParts ; i++ )
633 //bool aSectClosure = true;
634 int StartIndex = anObj->panPartStart[i];
637 EndIndex = anObj->panPartStart[i + 1];
639 EndIndex = anObj->nVertices;
641 bool IsClosed = false;
642 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
643 if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
644 anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] &&
645 anObj->padfZ[StartIndex] == anObj->padfZ[EndIndex - 1])
648 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toLatin1()), aSectType, true );
651 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toLatin1()), aSectType, false );
655 for ( int k = StartIndex ; k < EndIndex ; k++ )
657 HYDROData_PolylineXY::Point aSectPoint = gp_XY(anObj->padfX[k], anObj->padfY[k]);
658 theDocument->Transform(aSectPoint, true);
659 aPolylineXY->AddPoint( i, aSectPoint );
660 HYDROData_Bathymetry::AltitudePoint p;
661 p.X = aSectPoint.X();
662 p.Y = aSectPoint.Y();
663 p.Z = anObj->padfZ[k];
664 aAPoints.push_back(p);
669 QString aBathName = theFileName + "_bath_" + QString::number(theInd);
670 QString aPolyXYName = theFileName + "_polyXY_" + QString::number(theInd);
671 QString aPoly3DName = theFileName + "_poly3D_" + QString::number(theInd);
673 aPolylineXY->SetName( aPolyXYName );
674 aPolylineXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
675 aPolylineXY->Update();
677 aBath->SetAltitudePoints(aAPoints);
678 aBath->SetName( aBathName );
680 aPolylineObj->SetPolylineXY (aPolylineXY, false);
681 aPolylineObj->SetAltitudeObject(aBath);
683 aPolylineObj->SetBorderColor( aPolylineObj->DefaultBorderColor() );
684 aPolylineObj->SetName( aPoly3DName );
686 aPolylineObj->Update();
687 theEntities.Append(aPolylineXY);
688 theEntities.Append(aPolylineObj);
692 void HYDROData_ShapeFile::GetFreeIndices(std::vector<int>& theAllowedIndexes, QString strName, size_t theObjsSize,
693 QStringList theExistingNames, QString theBaseFileName)
696 for (;theAllowedIndexes.size() < theObjsSize;)
698 if (!theExistingNames.contains(theBaseFileName + strName + QString::number(anInd)))
700 theAllowedIndexes.push_back(anInd);
708 int HYDROData_ShapeFile::ImportPolylines(Handle(HYDROData_Document) theDocument, const QString& theFileName,
709 NCollection_Sequence<Handle(HYDROData_Entity)>& theEntities, int& theShapeTypeOfFile, ImportShapeType theShapeTypesToImport)
712 int aStat = TryOpenShapeFile(theFileName);
716 HYDROData_Iterator anIter( theDocument );
718 QStringList anExistingNames;
719 std::vector<int> anAllowedIndexes;
720 for( ; anIter.More(); anIter.Next() )
721 anExistingNames.push_back(anIter.Current()->GetName());
724 aHSHP = SHPOpen( theFileName.toLatin1().data(), "rb" );
726 QFileInfo aFileInfo(theFileName);
727 QString aBaseFileName = aFileInfo.baseName();
729 if (!Parse(aHSHP, HYDROData_ShapeFile::ShapeType_Polyline, theShapeTypeOfFile))
732 int sh_type = aHSHP->nShapeType;
734 if ((theShapeTypesToImport == HYDROData_ShapeFile::ImportShapeType_All ||
735 theShapeTypesToImport == HYDROData_ShapeFile::ImportShapeType_Polyline)
736 && (sh_type == 3 || sh_type == 23))
738 size_t anObjsSize = mySHPObjects.size();
739 GetFreeIndices(anAllowedIndexes, "_PolyXY_", anObjsSize, anExistingNames, aBaseFileName);
741 for (size_t i = 0; i < mySHPObjects.size(); i++ )
743 ReadSHPPolyXY(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities, false);
747 else if ((theShapeTypesToImport == HYDROData_ShapeFile::ImportShapeType_All ||
748 theShapeTypesToImport == HYDROData_ShapeFile::ImportShapeType_Polyline3D) && sh_type == 13)
751 for (;anAllowedIndexes.size() < mySHPObjects.size();)
753 if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)) &&
754 !anExistingNames.contains(aBaseFileName + "_Poly3D_" + QString::number(anInd)) &&
755 !anExistingNames.contains(aBaseFileName + "_Bath_" + QString::number(anInd)))
757 anAllowedIndexes.push_back(anInd);
763 for (size_t i = 0; i < mySHPObjects.size(); i++ )
765 ReadSHPPoly3D(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
769 else if ((theShapeTypesToImport == HYDROData_ShapeFile::ImportShapeType_All ||
770 theShapeTypesToImport == HYDROData_ShapeFile::ImportShapeType_Polygon) && sh_type == 5)
772 //import polygon's contours as polylines
773 size_t anObjsSize = mySHPObjects.size();
774 GetFreeIndices(anAllowedIndexes, "_PolyXY_", anObjsSize, anExistingNames, aBaseFileName);
776 for (size_t i = 0; i < mySHPObjects.size(); i++ )
778 ReadSHPPolyXY(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities, true);
787 for (size_t i = 0; i < mySHPObjects.size(); i++ )
788 free (mySHPObjects[i]);
790 mySHPObjects.clear();
795 QString HYDROData_ShapeFile::GetShapeTypeName(int theType)
802 return "point (unsupported by HYDRO)";
804 return "arc/polyline (supported by HYDRO)";
806 return "polygon (supported by HYDRO)";
808 return "multipoint (unsupported by HYDRO)";
810 return "pointZ (unsupported by HYDRO)";
812 return "arcZ/polyline (supported by HYDRO)";
814 return "polygonZ (unsupported by HYDRO)";
816 return "multipointZ (unsupported by HYDRO)";
818 return "pointM (unsupported by HYDRO)";
820 return "arcM/polyline (supported by HYDRO)";
822 return "polygonM (unsupported by HYDRO)";
824 return "multipointM (unsupported by HYDRO)";
826 return "multipatch (unsupported by HYDRO)";
832 int HYDROData_ShapeFile::TryOpenShapeFile(QString theFileName)
834 QString aSHPfile = theFileName.simplified();
835 QString aSHXfile = theFileName.simplified().replace( theFileName.simplified().size() - 4, 4, ".shx");
837 QString anExt = theFileName.split('.', QString::SkipEmptyParts).back();
838 if (anExt.toLower() != "shp")
841 FILE* pFileSHP = NULL;
842 pFileSHP = fopen (aSHPfile.toLatin1().data(), "r");
843 FILE* pFileSHX = NULL;
844 pFileSHX = fopen (aSHXfile.toLatin1().data(), "r");
846 if (pFileSHP == NULL || pFileSHX == NULL)
848 if (pFileSHP == NULL)
850 if (pFileSHX == NULL)
860 bool HYDROData_ShapeFile::CheckDBFFileExisting(const QString& theSHPFilePath, QString& thePathToDBFFile)
862 QString aSHPfile = theSHPFilePath.simplified();
863 QString aDBFfile = theSHPFilePath.simplified().replace( theSHPFilePath.simplified().size() - 4, 4, ".dbf");
864 FILE* pFileDBF = NULL;
865 pFileDBF = fopen (aDBFfile.toLatin1().data(), "r");
867 if (pFileDBF == NULL)
873 thePathToDBFFile = aDBFfile;
878 bool HYDROData_ShapeFile::DBF_OpenDBF(const QString& thePathToDBFFile)
880 myHDBF = DBFOpen( thePathToDBFFile.toLatin1().data(), "r" );
887 int HYDROData_ShapeFile::DBF_GetNbFields()
891 return DBFGetFieldCount(myHDBF);
894 void HYDROData_ShapeFile::DBF_CloseDBF()
900 QStringList HYDROData_ShapeFile::DBF_GetFieldList()
902 QStringList FieldList;
903 int nWidth, nDecimals;
906 for( int i = 0; i < DBFGetFieldCount(myHDBF); i++ )
909 eType = DBFGetFieldInfo( myHDBF, i, chField, &nWidth, &nDecimals );
910 FieldList.append(QString(chField));
916 void HYDROData_ShapeFile::DBF_GetFieldTypeList(std::vector<DBF_FieldType>& FTVect)
918 int nWidth, nDecimals;
921 for( int i = 0; i < DBFGetFieldCount(myHDBF); i++ )
924 eType = DBFGetFieldInfo( myHDBF, i, chField, &nWidth, &nDecimals );
925 if( eType == FTString )
926 FT = DBF_FieldType_String;
927 else if( eType == FTInteger )
928 FT = DBF_FieldType_Integer;
929 else if( eType == FTDouble )
930 FT = DBF_FieldType_Double;
931 else if( eType == FTInvalid )
932 FT = DBF_FieldType_Invalid;
934 FTVect.push_back(FT);
939 int HYDROData_ShapeFile::DBF_GetNbRecords()
943 return DBFGetRecordCount(myHDBF);
946 void HYDROData_ShapeFile::DBF_GetAttributeList(int theIndexOfField, std::vector<DBF_AttrValue>& theAttrV)
948 int nWidth, nDecimals;
951 for( int i = 0; i < DBFGetRecordCount(myHDBF); i++ )
954 DBF_AttrValue anAttr;
955 eType = DBFGetFieldInfo( myHDBF, theIndexOfField, chField, &nWidth, &nDecimals );
957 if( DBFIsAttributeNULL( myHDBF, i, theIndexOfField ) )
959 anAttr.myIsNull = true;
960 DBF_FieldType FT = DBF_FieldType_None;
961 if( eType == FTString )
962 FT = DBF_FieldType_String;
963 else if( eType == FTInteger )
964 FT = DBF_FieldType_Integer;
965 else if( eType == FTDouble )
966 FT = DBF_FieldType_Double;
967 else if( eType == FTInvalid )
968 FT = DBF_FieldType_Invalid;
969 anAttr.myFieldType = FT;
977 const char* chAttr = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
978 anAttr.myIsNull = false;
979 anAttr.myFieldType = DBF_FieldType_String;
980 anAttr.myRawValue = chAttr;
981 anAttr.myStrVal = QString(chAttr);
987 int iAttr = DBFReadIntegerAttribute( myHDBF, i, theIndexOfField );
988 anAttr.myIsNull = false;
989 anAttr.myFieldType = DBF_FieldType_Integer;
990 anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
991 anAttr.myIntVal = iAttr;
997 double dAttr = DBFReadDoubleAttribute( myHDBF, i, theIndexOfField );
998 anAttr.myIsNull = false;
999 anAttr.myFieldType = DBF_FieldType_Double;
1000 anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
1001 anAttr.myDoubleVal = dAttr;
1008 theAttrV.push_back(anAttr);
1013 bool HYDROData_ShapeFile::DBF_WriteFieldAndValues(const QString& theFileName, const QString& theFieldName, DBF_FieldType theType, const std::vector<DBF_AttrValue>& theAttrV, bool bUseRawValue)
1015 // Check that given field type is equal to field types of attributes values
1016 for (size_t i = 0; i < theAttrV.size(); i++)
1018 if (theAttrV[i].myFieldType != theType)
1023 hDBF = DBFCreate( theFileName.toStdString().c_str() );
1027 if (theType != DBF_FieldType_String && theType != DBF_FieldType_Integer && theType != DBF_FieldType_Double)
1030 return false; //cant handle any other cases
1036 case DBF_FieldType_String:
1038 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTString, nWidth, 0);
1042 case DBF_FieldType_Integer:
1044 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTInteger, nWidth, 0);
1048 case DBF_FieldType_Double:
1050 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTDouble, nWidth, 0);
1057 if (DBFGetFieldCount( hDBF ) != 1)
1062 if (DBFGetRecordCount( hDBF ) != 0)
1071 for (size_t i = 0; i < theAttrV.size(); i++)
1073 if (!theAttrV[i].myIsNull)
1074 stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myRawValue.c_str());
1076 stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 );
1087 for (size_t i = 0; i < theAttrV.size(); i++)
1089 if (!theAttrV[i].myIsNull)
1092 case DBF_FieldType_String:
1094 stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myStrVal.toStdString().c_str());
1098 case DBF_FieldType_Integer:
1100 stat = DBFWriteIntegerAttribute(hDBF, (int)i, 0, theAttrV[i].myIntVal);
1104 case DBF_FieldType_Double:
1106 stat = DBFWriteDoubleAttribute(hDBF, (int)i, 0, theAttrV[i].myDoubleVal);
1113 stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 );