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 #include "HYDRO_trace.hxx"
69 #pragma warning( disable: 4996 )
72 //SHP->TFaces (Import)
75 HYDROData_ShapeFile::HYDROData_ShapeFile() : myHSHP(NULL)
79 HYDROData_ShapeFile::~HYDROData_ShapeFile()
84 void HYDROData_ShapeFile::Export(Handle(HYDROData_Document) theDocument,
85 const QString& aFileName,
86 NCollection_Sequence<Handle(HYDROData_PolylineXY)> aPolyXYSeq,
87 NCollection_Sequence<Handle(HYDROData_Polyline3D)> aPoly3DSeq,
88 QStringList& aNonExpList)
90 DEBTRACE("aFileName " << aFileName.toStdString());
92 QStringList anExpList;
93 if (!aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty())
95 hSHPHandle = SHPCreate( aFileName.toLatin1().data(), SHPT_ARC );
96 for (int i = 1; i <= aPolyXYSeq.Size(); i++)
97 if (WriteObjectPolyXY(theDocument, hSHPHandle, aPolyXYSeq(i)) != 1)
98 aNonExpList.append(aPolyXYSeq(i)->GetName());
101 anExpList.append(aPolyXYSeq(i)->GetName());
102 DEBTRACE(" aName: " << aPolyXYSeq(i)->GetName().toStdString());
105 else if (aPolyXYSeq.IsEmpty() && !aPoly3DSeq.IsEmpty())
107 hSHPHandle = SHPCreate( aFileName.toLatin1().data(), SHPT_ARCZ );
108 for (int i = 1; i <= aPoly3DSeq.Size(); i++)
109 if (WriteObjectPoly3D(theDocument, hSHPHandle, aPoly3DSeq(i)) != 1)
110 aNonExpList.append(aPoly3DSeq(i)->GetName());
113 anExpList.append(aPoly3DSeq(i)->GetName());
114 DEBTRACE(" aName: " << aPoly3DSeq(i)->GetName().toStdString());
117 if (hSHPHandle->nRecords > 0)
119 SHPClose( hSHPHandle );
120 std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
121 for (int i=0; i<anExpList.size(); i++)
123 HYDROData_ShapeFile::DBF_AttrValue aCurAttrV;
124 aCurAttrV.myStrVal = anExpList[i];
125 aCurAttrV.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String;
126 aCurAttrV.myIsNull = false;
127 theAttrV.push_back(aCurAttrV);
129 QString theDBFFileName = aFileName.section('.',0,-2) + ".dbf";
130 DEBTRACE("theDBFFileName " << theDBFFileName.toStdString());
131 DBF_WriteFieldAndValues(theDBFFileName, "name", HYDROData_ShapeFile::DBF_FieldType_String, theAttrV, false);
135 SHPClose( hSHPHandle );
136 QString aFN = aFileName.simplified();
137 remove (aFN.toStdString().c_str());
138 remove ((aFN.simplified().replace( aFN.simplified().size() - 4, 4, ".shx")).toStdString().c_str());
142 void HYDROData_ShapeFile::Export(Handle(HYDROData_Document) theDocument,
143 const QString& aFileName,
144 const Handle(HYDROData_LandCoverMap)& aLCM,
145 QStringList& aNonExpList,
146 bool bCheckLinear, bool bUseDiscr, double theDefl)
148 if (bCheckLinear && !aLCM->CheckLinear())
151 SHPHandle hSHPHandle = NULL;
152 if ( !aLCM.IsNull() && !aLCM->IsEmpty())
154 hSHPHandle = SHPCreate( aFileName.toLatin1().data(), SHPT_POLYGON );
155 HYDROData_LandCoverMap::Explorer It( aLCM );
156 for( ; It.More(); It.Next())
158 TopoDS_Face aFace = It.Face();
159 if (WriteObjectPolygon(theDocument, hSHPHandle, aFace, bUseDiscr, theDefl) != 1)
160 aNonExpList.append(aLCM->GetName() + "_" + QString::number(It.Index()));
163 if (hSHPHandle && hSHPHandle->nRecords > 0)
164 SHPClose( hSHPHandle );
167 SHPClose( hSHPHandle );
168 QString aFN = aFileName.simplified();
169 remove (aFN.toStdString().c_str());
170 remove ((aFN.simplified().replace( aFN.simplified().size() - 4, 4, ".shx")).toStdString().c_str());
175 int HYDROData_ShapeFile::WriteObjectPolyXY(Handle(HYDROData_Document) theDocument,
176 SHPHandle theShpHandle,
177 Handle(HYDROData_PolylineXY) thePoly )
180 std::vector<double> x, y;
181 std::vector<int> anPartStart;
182 for (int i = 0; i < thePoly->NbSections(); i++)
184 anPartStart.push_back(x.size());
185 HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPoints(i, true);
186 for (int j = 1; j <= aPointList.Size(); j++)
188 gp_XY P = aPointList(j);
189 //theDocument->Transform(P, false);
193 if (thePoly->IsClosedSection(i))
195 gp_XY P = aPointList(1);
196 //theDocument->Transform(P, false);
202 aSHPObj = SHPCreateObject (SHPT_ARC, -1, thePoly->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
203 SHPWriteObject( theShpHandle, -1, aSHPObj );
204 SHPDestroyObject( aSHPObj );
208 int HYDROData_ShapeFile::WriteObjectPoly3D(Handle(HYDROData_Document) theDocument,
209 SHPHandle theShpHandle,
210 Handle(HYDROData_Polyline3D) thePoly )
213 std::vector<double> x, y, z;
214 std::vector<int> anPartStart;
215 anPartStart.push_back(0); //one section only
217 HYDROData_Polyline3D::Polyline3DPoints aPointList3D = thePoly->GetProfilePoints3D(true);
218 for (int j = 1; j <= aPointList3D.Size(); j++)
220 const gp_XYZ& P = aPointList3D(j);
221 //theDocument->Transform(P, false);
226 if ( thePoly->GetPolylineXY()->IsClosedSection(0))
228 const gp_XYZ& P = aPointList3D(1);
229 //theDocument->Transform(P, false);
235 aSHPObj = SHPCreateObject( SHPT_ARCZ, -1, 1, &anPartStart[0], NULL, x.size(), &x[0], &y[0], &z[0], NULL );
236 SHPWriteObject( theShpHandle, -1, aSHPObj );
237 SHPDestroyObject( aSHPObj );
241 int HYDROData_ShapeFile::WriteObjectPolygon(Handle(HYDROData_Document) theDocument,
242 SHPHandle theShpHandle,
243 const TopoDS_Shape& theInputShape,
244 bool bUseDiscr, double theDefl)
246 if (theInputShape.IsNull())
249 if (theInputShape.ShapeType() == TopAbs_FACE)
251 ProcessFace(theDocument, TopoDS::Face(theInputShape), theShpHandle, bUseDiscr, theDefl);
253 else if (theInputShape.ShapeType() == TopAbs_COMPOUND)
255 TopExp_Explorer Ex(theInputShape, TopAbs_FACE);
256 for (; Ex.More(); Ex.Next())
258 TopoDS_Face aF = TopoDS::Face(Ex.Current());
261 ProcessFace(theDocument, aF, theShpHandle, bUseDiscr, theDefl);
271 void HYDROData_ShapeFile::ProcessFace(Handle(HYDROData_Document) theDocument,
272 const TopoDS_Face& theFace,
273 SHPHandle theShpHandle,
274 bool bUseDiscr, double theDefl )
276 if (theFace.ShapeType() != TopAbs_FACE)
279 std::vector<double> x, y;
280 std::vector<int> anPartStart;
281 TopoDS_Wire OuterW = BRepTools::OuterWire(theFace);
282 NCollection_Sequence<TopoDS_Wire> aWires;
284 //write an outer wire first
285 aWires.Append(OuterW);
286 TopExp_Explorer Ex(theFace, TopAbs_WIRE);
287 for (; Ex.More(); Ex.Next())
289 TopoDS_Wire aW = TopoDS::Wire(Ex.Current());
290 if (aW.IsEqual(OuterW))
296 for (int k = 1; k <= aWires.Length(); k++)
298 TopoDS_Wire aW = aWires(k);
302 if (aW.Orientation() == TopAbs_INTERNAL)
303 //cant write internal wires/edges
305 // Try to reorder edges
306 Handle(ShapeFix_Wire) aSFW = new ShapeFix_Wire( aW, theFace, Precision::Confusion() );
307 aSFW->ModifyTopologyMode() = Standard_False;
308 aSFW->ModifyGeometryMode() = Standard_False;
310 Handle(ShapeExtend_WireData) aSEWD = aSFW->WireData();
311 Standard_Integer nbE = aSEWD->NbEdges();
315 anPartStart.push_back(x.size());
316 NCollection_Sequence<gp_Pnt2d> aPnts;
317 for (Standard_Integer i = 1; i <= nbE; i++)
319 TopoDS_Edge E = aSEWD->Edge(i);
322 TopoDS_Vertex aV = TopExp::LastVertex(E, 1);
325 gp_Pnt P = BRep_Tool::Pnt(aV);
326 aPnts.Append(gp_Pnt2d(P.X(), P.Y()));
330 BRepAdaptor_Curve Cur( E );
331 GCPnts_QuasiUniformDeflection Discr( Cur, theDefl );
332 if( !Discr.IsDone() )
333 continue; //skip edge?
334 double NewDefl = theDefl/2.0;
335 while (Discr.NbPoints() < 2)
337 Discr.Initialize(Cur, NewDefl);
338 NewDefl = NewDefl/2.0;
341 if (E.Orientation() == TopAbs_FORWARD)
343 for( int i = 1; i <= Discr.NbPoints(); i++ )
345 gp_Pnt P = Discr.Value( i );
346 aPnts.Append(gp_Pnt2d(P.X(), P.Y()));
351 for( int i = Discr.NbPoints(); i > 0; i-- )
353 gp_Pnt P = Discr.Value( i );
354 aPnts.Append(gp_Pnt2d(P.X(), P.Y()));
359 NCollection_Sequence<gp_Pnt2d> aNPnts;
360 aNPnts.Append(aPnts.First());
361 for (int j = 1; j <= aPnts.Size() - 1; j++)
363 if (!aPnts(j).IsEqual(aPnts(j + 1), Precision::Confusion()))
364 aNPnts.Append(aPnts(j + 1));
367 //assume that the orientation of external wire & internal wires is correct
368 //so just write all points "as-is"
369 //External wire will be written in clockwise direction
370 //any other wires (holes) - in anticlockwise direction
371 for (int j = aNPnts.Size(); j >= 1; j--)
373 gp_XY P = gp_XY(aNPnts(j).X(), aNPnts(j).Y());
374 theDocument->Transform(P, false);
378 //first point is same as the last one => closed polygon
379 gp_XY P = gp_XY(aNPnts.Last().X(), aNPnts.Last().Y());
380 theDocument->Transform(P, false);
386 aSHPObj = SHPCreateObject( SHPT_POLYGON, -1, NbWires, &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
387 SHPWriteObject( theShpHandle, -1, aSHPObj );
388 SHPDestroyObject( aSHPObj );
392 bool HYDROData_ShapeFile::Parse(SHPHandle theHandle, ShapeType theType)
395 mySHPObjects.clear();
396 SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
397 //theShapeTypeOfFile = aShapeType;
398 bool ToRead = (theType == ShapeType_Polyline &&
399 (aShapeType == 3 || aShapeType == 13 || aShapeType == 23 || aShapeType == 5)) ||
400 (theType == ShapeType_Polygon && aShapeType == 5);
403 for (int i = 0; i < theHandle->nRecords; i++)
404 mySHPObjects.push_back(SHPReadObject(theHandle, i));
411 void HYDROData_ShapeFile::ReadSHPPolygon(Handle(HYDROData_Document) theDocument, SHPObject* anObj, int i, TopoDS_Face& F)
416 int nParts = anObj->nParts;
417 Handle(Geom_Plane) aPlaneSur = new Geom_Plane(gp_Pnt(0,0,0), gp_Dir(0,0,1));
422 NCollection_Sequence<TopoDS_Wire> allWires;
424 TopTools_SequenceOfShape aWires;
425 for ( int i = 0 ; i < nParts ; i++ )
427 BRepBuilderAPI_MakeWire aBuilder;
428 int StartIndex = anObj->panPartStart[i];
431 EndIndex = anObj->panPartStart[i + 1];
433 EndIndex = anObj->nVertices;
438 //First point is same as the last point
439 int NbPnts = EndIndex - StartIndex - 1;
440 NCollection_Array1<TopoDS_Vertex> VPoints(0, NbPnts);
442 for ( int k = StartIndex; k < EndIndex; k++ )
444 gp_Pnt P (anObj->padfX[k], anObj->padfY[k], 0);
445 theDocument->Transform(P, true);
446 VPoints.ChangeValue(j) = BRepLib_MakeVertex(P).Vertex();
450 for ( int k = 0; k < VPoints.Size() - 1; k++ )
452 gp_Pnt P1 = BRep_Tool::Pnt(VPoints(k));
453 gp_Pnt P2 = BRep_Tool::Pnt(VPoints(k + 1));
454 if (P1.Distance(P2) < Precision::Confusion())
456 Handle(Geom_TrimmedCurve) aTC = GC_MakeSegment(P1, P2).Value();
458 if ( k != VPoints.Size() - 2)
459 E = BRepLib_MakeEdge(aTC, VPoints(k), VPoints(k + 1)).Edge();
461 //the last edge => use first and last vertices
462 E = BRepLib_MakeEdge(aTC, VPoints.First(), VPoints.Value(VPoints.Upper() - 1)).Edge();
464 //If SHP file is correct then the outer wire and the holes will have the correct orientations
465 W.Closed (Standard_True);
466 W.Orientation(TopAbs_FORWARD);
473 if (allWires.Size() > 1)
475 NCollection_Sequence<Bnd_Box> BBs;
476 //try to find the largest bbox
477 for (int i = 1; i <= allWires.Size(); i++)
479 TopoDS_Wire W = allWires(i);
481 BRepBndLib::AddClose(W, BB);
484 for (int i = 1; i <= BBs.Size(); i++)
487 for (int j = 1; j <= BBs.Size(); j++)
491 Standard_Real iXmax, iXmin, iYmax, iYmin, z0, z1;
492 Standard_Real jXmax, jXmin, jYmax, jYmin;
493 BBs(i).Get(iXmin, iYmin, z0, iXmax, iYmax, z1);
494 BBs(j).Get(jXmin, jYmin, z0, jXmax, jYmax, z1);
495 if (!(iXmin > jXmin &&
509 OutWIndex = 1; //one wire => no need to check
511 for (int i = 1; i <= allWires.Size(); i++)
515 TopoDS_Wire W = allWires(i);
517 BB.UpdateFace(DF, aPlaneSur, TopLoc_Location(), Precision::Confusion());
519 BRepTopAdaptor_FClass2d FClass(DF, Precision::PConfusion());
520 if ( i == OutWIndex && FClass.PerformInfinitePoint() == TopAbs_IN)
522 if ( i != OutWIndex && FClass.PerformInfinitePoint() == TopAbs_OUT)
528 //Add surface to the face
529 BB.UpdateFace(F, aPlaneSur, TopLoc_Location(), Precision::Confusion());
530 F.Closed(Standard_True);
533 int HYDROData_ShapeFile::ImportPolygons(Handle(HYDROData_Document) theDocument, const QString theFileName,
534 QStringList& thePolygonsList, TopTools_SequenceOfShape& theFaces)
537 int Stat = TryOpenShapeFile(theFileName);
540 myHSHP = SHPOpen( theFileName.toLatin1().data(), "rb" );
541 if (!Parse(myHSHP, HYDROData_ShapeFile::ShapeType_Polygon))
543 for (size_t i = 0; i < mySHPObjects.size(); i++)
544 thePolygonsList.append("polygon_" + QString::number(i + 1));
547 if (myHSHP->nShapeType == 5)
553 for (size_t i = 0; i < mySHPObjects.size(); i++)
555 ReadSHPPolygon(theDocument, mySHPObjects[i], i, aF);
568 void HYDROData_ShapeFile::Free()
570 for (size_t i = 0; i < mySHPObjects.size(); i++ )
571 free (mySHPObjects[i]);
573 mySHPObjects.clear();
582 void HYDROData_ShapeFile::ReadSHPPolyXY(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName,
583 int theInd, NCollection_Sequence<Handle(HYDROData_Entity)>& theEntities, bool bReadAsPolyline)
585 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
587 int nParts = anObj->nParts;
588 for ( int i = 0 ; i < nParts ; i++ )
590 int StartIndex = anObj->panPartStart[i];
593 EndIndex = anObj->panPartStart[i + 1];
595 EndIndex = anObj->nVertices;
597 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
598 if (!bReadAsPolyline)
600 bool IsClosed = false;
601 if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
602 anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] )
605 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toLatin1()), aSectType, true);
608 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toLatin1()), aSectType, false);
615 //polygon; contours always closed
616 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toLatin1()), aSectType, true);
619 for ( int k = StartIndex; k < EndIndex ; k++ )
621 HYDROData_PolylineXY::Point aSectPoint = gp_XY(anObj->padfX[k], anObj->padfY[k]);
622 theDocument->Transform(aSectPoint, true);
623 aPolylineXY->AddPoint( i, aSectPoint );
628 aPolylineXY->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
629 aPolylineXY->SetName( theFileName + "_PolyXY_" + QString::number(theInd) );
631 aPolylineXY->Update();
632 theEntities.Append(aPolylineXY);
636 void HYDROData_ShapeFile::ReadSHPPoly3D(Handle(HYDROData_Document) theDocument, SHPObject* anObj,
637 const Handle(HYDROData_PolylineXY)& aPolylineXY,
638 NCollection_Sequence<Handle(HYDROData_Entity)>& theEntities)
640 Handle(HYDROData_Polyline3D) aPolylineObj = Handle(HYDROData_Polyline3D)::DownCast( theDocument->CreateObject( KIND_POLYLINE ) );
641 Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( theDocument->CreateObject( KIND_BATHYMETRY ) );
643 HYDROData_Bathymetry::AltitudePoints aAPoints;
645 int nParts = anObj->nParts;
646 for ( int i = 0 ; i < nParts ; i++ )
648 //bool aSectClosure = true;
649 int StartIndex = anObj->panPartStart[i];
652 EndIndex = anObj->panPartStart[i + 1];
654 EndIndex = anObj->nVertices;
656 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
657 if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
658 anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] &&
659 anObj->padfZ[StartIndex] == anObj->padfZ[EndIndex - 1])
661 for ( int k = StartIndex ; k < EndIndex ; k++ )
663 gp_XYZ xyz(anObj->padfX[k], anObj->padfY[k], anObj->padfZ[k]);
664 theDocument->Transform(xyz, true);
665 HYDROData_Bathymetry::AltitudePoint p;
669 aAPoints.push_back(p);
673 QString PolyXYName = aPolylineXY->GetName();
674 QString aPoly3DName = PolyXYName, aBathyName = PolyXYName;
675 aPoly3DName.replace(PolyXYName.lastIndexOf("_PolyXY_"), 8, "_Poly3D_");
676 aBathyName.replace(PolyXYName.lastIndexOf("_Bath_"), 6, "_Poly3D_");
678 aBath->SetAltitudePoints(aAPoints);
679 aBath->SetName( aBathyName );
681 aPolylineObj->SetPolylineXY (aPolylineXY, false);
682 aPolylineObj->SetAltitudeObject(aBath);
684 aPolylineObj->SetBorderColor( aPolylineObj->DefaultBorderColor() );
685 aPolylineObj->SetName( aPoly3DName );
687 aPolylineObj->Update();
689 theEntities.Append(aPolylineObj);
693 void HYDROData_ShapeFile::ReadSHPPoly3D(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName,
694 int theInd, bool XYOnly, NCollection_Sequence<Handle(HYDROData_Entity)>& theEntities)
696 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
698 Handle(HYDROData_Polyline3D) aPolylineObj;
699 Handle(HYDROData_Bathymetry) aBath;
702 Handle(HYDROData_Polyline3D) aPolylineObj = Handle(HYDROData_Polyline3D)::DownCast( theDocument->CreateObject( KIND_POLYLINE ) );
703 Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( theDocument->CreateObject( KIND_BATHYMETRY ) );
706 HYDROData_Bathymetry::AltitudePoints aAPoints;
708 int nParts = anObj->nParts;
709 for ( int i = 0 ; i < nParts ; i++ )
711 //bool aSectClosure = true;
712 int StartIndex = anObj->panPartStart[i];
715 EndIndex = anObj->panPartStart[i + 1];
717 EndIndex = anObj->nVertices;
719 bool IsClosed = false;
720 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
721 if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
722 anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] &&
723 anObj->padfZ[StartIndex] == anObj->padfZ[EndIndex - 1])
726 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toLatin1()), aSectType, true );
729 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toLatin1()), aSectType, false );
733 for ( int k = StartIndex ; k < EndIndex ; k++ )
735 HYDROData_PolylineXY::Point aSectPoint = gp_XY(anObj->padfX[k], anObj->padfY[k]);
736 theDocument->Transform(aSectPoint, true);
737 aPolylineXY->AddPoint( i, aSectPoint );
740 HYDROData_Bathymetry::AltitudePoint p;
741 p.X = aSectPoint.X();
742 p.Y = aSectPoint.Y();
743 p.Z = anObj->padfZ[k];
744 aAPoints.push_back(p);
750 QString aBathName = theFileName + "_bath_" + QString::number(theInd);
751 QString aPolyXYName = theFileName + "_polyXY_" + QString::number(theInd);
752 QString aPoly3DName = theFileName + "_poly3D_" + QString::number(theInd);
754 aPolylineXY->SetName( aPolyXYName );
755 aPolylineXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
756 aPolylineXY->Update();
760 aBath->SetAltitudePoints(aAPoints);
761 aBath->SetName( aBathName );
763 aPolylineObj->SetPolylineXY (aPolylineXY, false);
764 aPolylineObj->SetAltitudeObject(aBath);
766 aPolylineObj->SetBorderColor( aPolylineObj->DefaultBorderColor() );
767 aPolylineObj->SetName( aPoly3DName );
769 aPolylineObj->Update();
772 theEntities.Append(aPolylineXY);
775 theEntities.Append(aPolylineObj);
779 void HYDROData_ShapeFile::GetFreeIndices(std::vector<int>& theAllowedIndexes, QString strName, size_t theObjsSize,
780 QStringList theExistingNames, QString theBaseFileName)
783 for (;theAllowedIndexes.size() < theObjsSize;)
785 if (!theExistingNames.contains(theBaseFileName + strName + QString::number(anInd)))
787 theAllowedIndexes.push_back(anInd);
795 int HYDROData_ShapeFile::OpenAndParse(const QString& theFileName)
798 int aStat = TryOpenShapeFile(theFileName);
803 myHSHP = SHPOpen( theFileName.toLatin1().data(), "rb" );
807 if (!Parse(myHSHP, HYDROData_ShapeFile::ShapeType_Polyline))
811 //sh_type = aHSHP->nShapeType;
814 int HYDROData_ShapeFile::ImportPolylines(Handle(HYDROData_Document) theDocument,
815 const QString& theFileName,
816 NCollection_IndexedDataMap<Handle(HYDROData_Entity), SHPObject*>& theEntitiesToSHPObj,
817 ImportShapeType theShapeTypesToImport)
820 int sh_type = myHSHP->nShapeType;
821 QFileInfo aFileInfo(theFileName);
822 QString aBaseFileName = aFileInfo.baseName();
823 HYDROData_Iterator anIter( theDocument );
825 QStringList anExistingNames;
826 std::vector<int> anAllowedIndexes;
827 for( ; anIter.More(); anIter.Next() )
828 anExistingNames.push_back(anIter.Current()->GetName());
830 NCollection_Sequence<Handle(HYDROData_Entity)> Ents;
831 if ((theShapeTypesToImport == HYDROData_ShapeFile::ImportShapeType_All ||
832 theShapeTypesToImport == HYDROData_ShapeFile::ImportShapeType_Polyline)
833 && (sh_type == 3 || sh_type == 23))
835 size_t anObjsSize = mySHPObjects.size();
836 GetFreeIndices(anAllowedIndexes, "_PolyXY_", anObjsSize, anExistingNames, aBaseFileName);
838 for (size_t i = 0; i < mySHPObjects.size(); i++ )
840 ReadSHPPolyXY(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], Ents, false);
841 for (int k=1;k<=Ents.Size();k++)
842 theEntitiesToSHPObj.Add(Ents(k), mySHPObjects[i]);
846 else if ((theShapeTypesToImport == HYDROData_ShapeFile::ImportShapeType_All ||
847 theShapeTypesToImport == HYDROData_ShapeFile::ImportShapeType_Polyline3D) && sh_type == 13)
850 for (;anAllowedIndexes.size() < mySHPObjects.size();)
852 if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)) &&
853 !anExistingNames.contains(aBaseFileName + "_Poly3D_" + QString::number(anInd)) &&
854 !anExistingNames.contains(aBaseFileName + "_Bath_" + QString::number(anInd)))
856 anAllowedIndexes.push_back(anInd);
862 for (size_t i = 0; i < mySHPObjects.size(); i++ )
864 //ReadSHPPoly3D(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], false, theEntities);
865 ReadSHPPolyXY(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], Ents, false); //TODO check Z coord on closeness
866 for (int k=1;k<=Ents.Size();k++)
867 theEntitiesToSHPObj.Add(Ents(k), mySHPObjects[i]);
871 else if ((theShapeTypesToImport == HYDROData_ShapeFile::ImportShapeType_All ||
872 theShapeTypesToImport == HYDROData_ShapeFile::ImportShapeType_Polygon) && sh_type == 5)
874 //import polygon's contours as polylines
875 size_t anObjsSize = mySHPObjects.size();
876 GetFreeIndices(anAllowedIndexes, "_PolyXY_", anObjsSize, anExistingNames, aBaseFileName);
878 for (size_t i = 0; i < mySHPObjects.size(); i++ )
880 ReadSHPPolyXY(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], Ents, true);
881 for (int k=1;k<=Ents.Size();k++)
882 theEntitiesToSHPObj.Add(Ents(k), mySHPObjects[i]);
894 int HYDROData_ShapeFile::GetShapeType() const
897 return myHSHP->nShapeType;
902 int HYDROData_ShapeFile::ImportPolylines3D(Handle(HYDROData_Document) theDocument,
903 NCollection_IndexedDataMap<Handle(HYDROData_Entity), SHPObject*> thePolyXYToObj,
904 NCollection_Sequence<Handle(HYDROData_Entity)>& theEntities,
905 ImportShapeType theShapeTypesToImport)
908 int sh_type = myHSHP->nShapeType;
909 if ((theShapeTypesToImport == HYDROData_ShapeFile::ImportShapeType_All ||
910 theShapeTypesToImport == HYDROData_ShapeFile::ImportShapeType_Polyline3D) && sh_type == 13)
912 //for (size_t i = 0; i < mySHPObjects.size(); i++ )
913 for (int i = 1; i <= thePolyXYToObj.Extent(); i++ )
915 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( thePolyXYToObj.FindKey(i) );
916 ReadSHPPoly3D(theDocument, thePolyXYToObj(i), aPolylineXY, theEntities);
927 QString HYDROData_ShapeFile::GetShapeTypeName(int theType)
934 return "point (unsupported by HYDRO)";
936 return "arc/polyline (supported by HYDRO)";
938 return "polygon (supported by HYDRO)";
940 return "multipoint (unsupported by HYDRO)";
942 return "pointZ (unsupported by HYDRO)";
944 return "arcZ/polyline (supported by HYDRO)";
946 return "polygonZ (unsupported by HYDRO)";
948 return "multipointZ (unsupported by HYDRO)";
950 return "pointM (unsupported by HYDRO)";
952 return "arcM/polyline (supported by HYDRO)";
954 return "polygonM (unsupported by HYDRO)";
956 return "multipointM (unsupported by HYDRO)";
958 return "multipatch (unsupported by HYDRO)";
964 int HYDROData_ShapeFile::TryOpenShapeFile(QString theFileName)
966 QString aSHPfile = theFileName.simplified();
967 QString aSHXfile = theFileName.simplified().replace( theFileName.simplified().size() - 4, 4, ".shx");
969 QString anExt = theFileName.split('.', QString::SkipEmptyParts).back();
970 if (anExt.toLower() != "shp")
973 FILE* pFileSHP = NULL;
974 pFileSHP = fopen (aSHPfile.toLatin1().data(), "r");
975 FILE* pFileSHX = NULL;
976 pFileSHX = fopen (aSHXfile.toLatin1().data(), "r");
978 if (pFileSHP == NULL || pFileSHX == NULL)
980 if (pFileSHP == NULL)
982 if (pFileSHX == NULL)
992 bool HYDROData_ShapeFile::CheckDBFFileExisting(const QString& theSHPFilePath, QString& thePathToDBFFile)
994 QString aSHPfile = theSHPFilePath.simplified();
995 QString aDBFfile = theSHPFilePath.simplified().replace( theSHPFilePath.simplified().size() - 4, 4, ".dbf");
996 FILE* pFileDBF = NULL;
997 pFileDBF = fopen (aDBFfile.toLatin1().data(), "r");
999 if (pFileDBF == NULL)
1005 thePathToDBFFile = aDBFfile;
1010 bool HYDROData_ShapeFile::DBF_OpenDBF(const QString& thePathToDBFFile)
1012 myHDBF = DBFOpen( thePathToDBFFile.toLatin1().data(), "r" );
1019 int HYDROData_ShapeFile::DBF_GetNbFields()
1023 return DBFGetFieldCount(myHDBF);
1026 void HYDROData_ShapeFile::DBF_CloseDBF()
1032 QStringList HYDROData_ShapeFile::DBF_GetFieldList()
1034 QStringList FieldList;
1035 int nWidth, nDecimals;
1038 for( int i = 0; i < DBFGetFieldCount(myHDBF); i++ )
1041 eType = DBFGetFieldInfo( myHDBF, i, chField, &nWidth, &nDecimals );
1042 FieldList.append(QString(chField));
1048 void HYDROData_ShapeFile::DBF_GetFieldTypeList(std::vector<DBF_FieldType>& FTVect)
1050 int nWidth, nDecimals;
1053 for( int i = 0; i < DBFGetFieldCount(myHDBF); i++ )
1056 eType = DBFGetFieldInfo( myHDBF, i, chField, &nWidth, &nDecimals );
1057 if( eType == FTString )
1058 FT = DBF_FieldType_String;
1059 else if( eType == FTInteger )
1060 FT = DBF_FieldType_Integer;
1061 else if( eType == FTDouble )
1062 FT = DBF_FieldType_Double;
1063 else if( eType == FTInvalid )
1064 FT = DBF_FieldType_Invalid;
1066 FTVect.push_back(FT);
1071 int HYDROData_ShapeFile::DBF_GetNbRecords()
1075 return DBFGetRecordCount(myHDBF);
1078 void HYDROData_ShapeFile::DBF_GetAttributeList(int theIndexOfField, std::vector<DBF_AttrValue>& theAttrV)
1080 int nWidth, nDecimals;
1083 for( int i = 0; i < DBFGetRecordCount(myHDBF); i++ )
1086 DBF_AttrValue anAttr;
1087 eType = DBFGetFieldInfo( myHDBF, theIndexOfField, chField, &nWidth, &nDecimals );
1089 if( DBFIsAttributeNULL( myHDBF, i, theIndexOfField ) )
1091 anAttr.myIsNull = true;
1092 DBF_FieldType FT = DBF_FieldType_None;
1093 if( eType == FTString )
1094 FT = DBF_FieldType_String;
1095 else if( eType == FTInteger )
1096 FT = DBF_FieldType_Integer;
1097 else if( eType == FTDouble )
1098 FT = DBF_FieldType_Double;
1099 else if( eType == FTInvalid )
1100 FT = DBF_FieldType_Invalid;
1101 anAttr.myFieldType = FT;
1109 const char* chAttr = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
1110 anAttr.myIsNull = false;
1111 anAttr.myFieldType = DBF_FieldType_String;
1112 anAttr.myRawValue = chAttr;
1113 anAttr.myStrVal = QString(chAttr);
1119 int iAttr = DBFReadIntegerAttribute( myHDBF, i, theIndexOfField );
1120 anAttr.myIsNull = false;
1121 anAttr.myFieldType = DBF_FieldType_Integer;
1122 anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
1123 anAttr.myIntVal = iAttr;
1129 double dAttr = DBFReadDoubleAttribute( myHDBF, i, theIndexOfField );
1130 anAttr.myIsNull = false;
1131 anAttr.myFieldType = DBF_FieldType_Double;
1132 anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
1133 anAttr.myDoubleVal = dAttr;
1140 theAttrV.push_back(anAttr);
1145 bool HYDROData_ShapeFile::DBF_WriteFieldAndValues(const QString& theFileName, const QString& theFieldName, DBF_FieldType theType, const std::vector<DBF_AttrValue>& theAttrV, bool bUseRawValue)
1147 // Check that given field type is equal to field types of attributes values
1148 for (size_t i = 0; i < theAttrV.size(); i++)
1150 if (theAttrV[i].myFieldType != theType)
1155 hDBF = DBFCreate( theFileName.toStdString().c_str() );
1159 if (theType != DBF_FieldType_String && theType != DBF_FieldType_Integer && theType != DBF_FieldType_Double)
1162 return false; //cant handle any other cases
1168 case DBF_FieldType_String:
1170 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTString, nWidth, 0);
1174 case DBF_FieldType_Integer:
1176 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTInteger, nWidth, 0);
1180 case DBF_FieldType_Double:
1182 DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTDouble, nWidth, 0);
1188 DEBTRACE("theAttrV.size() " << theAttrV.size());
1190 if (DBFGetFieldCount( hDBF ) != 1)
1195 if (DBFGetRecordCount( hDBF ) != 0)
1204 for (size_t i = 0; i < theAttrV.size(); i++)
1206 DEBTRACE(" rawValue");
1207 if (!theAttrV[i].myIsNull)
1208 stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myRawValue.c_str());
1210 stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 );
1221 for (size_t i = 0; i < theAttrV.size(); i++)
1223 DEBTRACE(" typeValue " << theAttrV[i].myIsNull);
1224 if (!theAttrV[i].myIsNull)
1227 case DBF_FieldType_String:
1229 DEBTRACE(" write dbf " << i << " " << theAttrV[i].myStrVal.toStdString());
1230 stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myStrVal.toStdString().c_str());
1234 case DBF_FieldType_Integer:
1236 stat = DBFWriteIntegerAttribute(hDBF, (int)i, 0, theAttrV[i].myIntVal);
1240 case DBF_FieldType_Double:
1242 stat = DBFWriteDoubleAttribute(hDBF, (int)i, 0, theAttrV[i].myDoubleVal);
1249 stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 );
1250 DEBTRACE(" stat " << stat);