#include <HYDROData_Bathymetry.h>
#include <HYDROData_LandCover.h>
#include <HYDROData_Profile.h>
+#include <HYDROData_Iterator.h>
#include <QFile>
#include <QFileInfo>
#include <BRepLib.hxx>
#include <ShapeFix_Shape.hxx>
#include <TopTools_SequenceOfShape.hxx>
+#include <QColor>
+#include <BRepTopAdaptor_FClass2d.hxx>
HYDROData_ShapeFile::HYDROData_ShapeFile() : myHSHP(NULL)
{
if (WriteObjectLC(hSHPHandle, aLCSeq(i)) != 1)
aNonExpList.append(aLCSeq(i)->GetName());
}
- SHPClose( hSHPHandle );
-
+ if (hSHPHandle->nRecords > 0)
+ SHPClose( hSHPHandle );
+ else
+ {
+ SHPClose( hSHPHandle );
+ QString aFN = aFileName.simplified();
+ remove (aFN.toStdString().c_str());
+ remove (aFN.replace( ".shp", ".shx", Qt::CaseInsensitive).toStdString().c_str());
+ }
}
int HYDROData_ShapeFile::WriteObjectPolyXY(SHPHandle theShpHandle, Handle_HYDROData_PolylineXY thePoly )
return;
}
-void HYDROData_ShapeFile::Parse(SHPHandle theHandle)
+bool HYDROData_ShapeFile::Parse(SHPHandle theHandle, ShapeType theType, int& theShapeTypeOfFile)
{
int aShapeType;
mySHPObjects.clear();
SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
- if (aShapeType == 5)
+ theShapeTypeOfFile = aShapeType;
+ bool ToRead = (theType == ShapeType_Polyline && (aShapeType == 3 || aShapeType == 13 || aShapeType == 23)) ||
+ (theType == ShapeType_Polygon && aShapeType == 5);
+ if (ToRead)
{
for (int i = 0; i < theHandle->nRecords; i++)
mySHPObjects.push_back(SHPReadObject(theHandle, i));
+ return true;
}
+ else
+ return false;
}
-void HYDROData_ShapeFile::ProcessSHP(SHPObject* anObj, int i, TopoDS_Face& F)
+void HYDROData_ShapeFile::ReadSHPPolygon(SHPObject* anObj, int i, TopoDS_Face& F)
{
TopoDS_Wire W;
TopoDS_Edge E;
int nParts = anObj->nParts;
gp_Pln pln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
- BRepBuilderAPI_MakeFace aFBuilder(pln);
//Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
//sfs->FixFaceTool()->FixOrientationMode() = 1;
-
+ TopTools_SequenceOfShape aWires;
for ( int i = 0 ; i < nParts ; i++ )
{
BRepBuilderAPI_MakeWire aBuilder;
aBuilder.Build();
W = TopoDS::Wire(aBuilder.Shape());
- W.Reverse();
- aFBuilder.Add(W);
+ W.Orientation(TopAbs_FORWARD);
+ BRepBuilderAPI_MakeFace aDB(pln, W);
+ TopoDS_Face aDummyFace = TopoDS::Face(aDB.Shape());
+ BRepTopAdaptor_FClass2d FClass(aDummyFace, Precision::PConfusion());
+ if ( i == 0 && FClass.PerformInfinitePoint() == TopAbs_OUT)
+ W.Reverse();
+ if ( i > 0 && FClass.PerformInfinitePoint() != TopAbs_IN)
+ W.Reverse();
+
+ aWires.Append(W);
}
+
+ BRepBuilderAPI_MakeFace aFBuilder(pln, TopoDS::Wire(aWires(1)));
+ for (int i = 2; i <= aWires.Length(); i++)
+ aFBuilder.Add(TopoDS::Wire(aWires(i)));
+ TopoDS_Face DF = TopoDS::Face(aFBuilder.Shape());
- aFBuilder.Build();
- TopoDS_Face DF = aFBuilder.Face();
BRepLib::BuildCurves3d(DF);
- bool IsInf = DF.Infinite();
if(!DF.IsNull())
{
//sfs->Init ( DF );
}
}
-bool HYDROData_ShapeFile::ImportLandCovers(const QString theFileName, QStringList& thePolygonsList, TopTools_SequenceOfShape& theFaces)
+int HYDROData_ShapeFile::ImportLandCovers(const QString theFileName, QStringList& thePolygonsList, TopTools_SequenceOfShape& theFaces, int& theShapeTypeOfFile)
{
Free();
+ int Stat = TryOpenShapeFile(theFileName);
+ if (Stat != 0)
+ return Stat;
myHSHP = SHPOpen( theFileName.toAscii().data(), "rb" );
- Parse(myHSHP);
- for (int i = 0; i < mySHPObjects.size(); i++)
+ if (!Parse(myHSHP, HYDROData_ShapeFile::ShapeType_Polygon, theShapeTypeOfFile))
+ return 0;
+ for (size_t i = 0; i < mySHPObjects.size(); i++)
thePolygonsList.append("polygon_" + QString::number(i + 1));
TopoDS_Face aF;
if (myHSHP->nShapeType == 5)
{
- for (int i = 0; i < mySHPObjects.size(); i++)
+ for (size_t i = 0; i < mySHPObjects.size(); i++)
{
- ProcessSHP(mySHPObjects[i], i, aF);
+ ReadSHPPolygon(mySHPObjects[i], i, aF);
theFaces.Append(aF);
}
- return true;
+ return 1;
}
else
- return false;
+ return 0;
}
void HYDROData_ShapeFile::Free()
SHPClose(myHSHP);
myHSHP = NULL;
}
+}
+
+
+void HYDROData_ShapeFile::ReadSHPPolyXY(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName,
+ int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
+{
+
+ Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
+
+ int nParts = anObj->nParts;
+ for ( int i = 0 ; i < nParts ; i++ )
+ {
+ int StartIndex = anObj->panPartStart[i];
+ int EndIndex;
+ if (i != nParts - 1)
+ EndIndex = anObj->panPartStart[i + 1];
+ else
+ EndIndex = anObj->nVertices;
+
+ bool IsClosed = false;
+ HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
+ if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
+ anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] )
+ {
+ IsClosed = true;
+ aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true);
+ }
+ else
+ aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false);
+
+ if (IsClosed)
+ EndIndex--;
+ for ( int k = StartIndex; k < EndIndex ; k++ )
+ {
+ HYDROData_PolylineXY::Point aSectPoint;
+ aSectPoint.SetX( anObj->padfX[k] );
+ aSectPoint.SetY( anObj->padfY[k] );
+ aPolylineXY->AddPoint( i, aSectPoint );
+ }
+
+ }
+
+ aPolylineXY->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
+ aPolylineXY->SetName( theFileName + "_PolyXY_" + QString::number(theInd) );
+
+ aPolylineXY->Update();
+ theEntities.Append(aPolylineXY);
+
+}
+
+void HYDROData_ShapeFile::ReadSHPPoly3D(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName,
+ int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
+{
+ Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
+
+ Handle(HYDROData_Polyline3D) aPolylineObj = Handle(HYDROData_Polyline3D)::DownCast( theDocument->CreateObject( KIND_POLYLINE ) );
+
+ Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( theDocument->CreateObject( KIND_BATHYMETRY ) );
+ HYDROData_Bathymetry::AltitudePoints aAPoints;
+
+ int nParts = anObj->nParts;
+ for ( int i = 0 ; i < nParts ; i++ )
+ {
+ //bool aSectClosure = true;
+ int StartIndex = anObj->panPartStart[i];
+ int EndIndex;
+ if (i != nParts - 1)
+ EndIndex = anObj->panPartStart[i + 1];
+ else
+ EndIndex = anObj->nVertices;
+
+ bool IsClosed = false;
+ HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
+ if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
+ anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] &&
+ anObj->padfZ[StartIndex] == anObj->padfZ[EndIndex - 1])
+ {
+ IsClosed = true;
+ aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true );
+ }
+ else
+ aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false );
+
+ if (IsClosed)
+ EndIndex--;
+ for ( int k = StartIndex ; k < EndIndex ; k++ )
+ {
+ HYDROData_PolylineXY::Point aSectPoint;
+ aSectPoint.SetX( anObj->padfX[k] );
+ aSectPoint.SetY( anObj->padfY[k] );
+ aPolylineXY->AddPoint( i, aSectPoint );
+ aAPoints.Append(gp_XYZ (anObj->padfX[k], anObj->padfY[k], anObj->padfZ[k]));
+ }
+ }
+
+
+ QString aBathName = theFileName + "_bath_" + QString::number(theInd);
+ QString aPolyXYName = theFileName + "_polyXY_" + QString::number(theInd);
+ QString aPoly3DName = theFileName + "_poly3D_" + QString::number(theInd);
+
+ aPolylineXY->SetName( aPolyXYName );
+ aPolylineXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
+ aPolylineXY->Update();
+
+ aBath->SetAltitudePoints(aAPoints);
+ aBath->SetName( aBathName );
+
+ aPolylineObj->SetPolylineXY (aPolylineXY, false);
+ aPolylineObj->SetAltitudeObject(aBath);
+
+ aPolylineObj->SetBorderColor( aPolylineObj->DefaultBorderColor() );
+ aPolylineObj->SetName( aPoly3DName );
+
+ aPolylineObj->Update();
+ theEntities.Append(aPolylineXY);
+ theEntities.Append(aPolylineObj);
+
+}
+
+
+
+int HYDROData_ShapeFile::ImportPolylines(Handle(HYDROData_Document) theDocument, const QString& theFileName,
+ NCollection_Sequence<Handle_HYDROData_Entity>& theEntities, int& theShapeTypeOfFile)
+{
+ //Free();
+ int aStat = TryOpenShapeFile(theFileName);
+ if (aStat != 0)
+ return aStat;
+
+ HYDROData_Iterator anIter( theDocument );
+ int anInd = 0;
+ QStringList anExistingNames;
+ std::vector<int> anAllowedIndexes;
+ for( ; anIter.More(); anIter.Next() )
+ anExistingNames.push_back(anIter.Current()->GetName());
+
+ SHPHandle aHSHP;
+ aHSHP = SHPOpen( theFileName.toAscii().data(), "rb" );
+
+ QFileInfo aFileInfo(theFileName);
+ QString aBaseFileName = aFileInfo.baseName();
+
+ if (!Parse(aHSHP, HYDROData_ShapeFile::ShapeType_Polyline, theShapeTypeOfFile))
+ return 0;
+ if (aHSHP->nShapeType == 3 || aHSHP->nShapeType == 23)
+ {
+ anInd = 0;
+ for (;anAllowedIndexes.size() < mySHPObjects.size();)
+ {
+ if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)))
+ {
+ anAllowedIndexes.push_back(anInd);
+ anInd++;
+ }
+ else
+ anInd++;
+ }
+
+ for (size_t i = 0; i < mySHPObjects.size(); i++ )
+ {
+ ReadSHPPolyXY(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
+ }
+ aStat = 1;
+ }
+ else if (aHSHP->nShapeType == 13)
+ {
+ anInd = 0;
+ for (;anAllowedIndexes.size() < mySHPObjects.size();)
+ {
+ if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)) &&
+ !anExistingNames.contains(aBaseFileName + "_Poly3D_" + QString::number(anInd)) &&
+ !anExistingNames.contains(aBaseFileName + "_Bath_" + QString::number(anInd)))
+ {
+ anAllowedIndexes.push_back(anInd);
+ anInd++;
+ }
+ else
+ anInd++;
+ }
+ for (size_t i = 0; i < mySHPObjects.size(); i++ )
+ ReadSHPPoly3D(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
+ aStat = 1;
+ }
+ else
+ {
+ aStat = 0;
+ }
+
+ for (size_t i = 0; i < mySHPObjects.size(); i++ )
+ free (mySHPObjects[i]);
+
+ mySHPObjects.clear();
+ SHPClose(aHSHP);
+ return aStat;
+}
+
+QString HYDROData_ShapeFile::GetShapeTypeName(int theType)
+{
+ switch (theType)
+ {
+ case 0:
+ return "null shape";
+ case 1:
+ return "point (unsupported by HYDRO)";
+ case 3:
+ return "arc/polyline (supported by HYDRO)";
+ case 5:
+ return "polygon (supported by HYDRO)";
+ case 8:
+ return "multipoint (unsupported by HYDRO)";
+ case 11:
+ return "pointZ (unsupported by HYDRO)";
+ case 13:
+ return "arcZ/polyline (supported by HYDRO)";
+ case 15:
+ return "polygonZ (unsupported by HYDRO)";
+ case 18:
+ return "multipointZ (unsupported by HYDRO)";
+ case 21:
+ return "pointM (unsupported by HYDRO)";
+ case 23:
+ return "arcM/polyline (supported by HYDRO)";
+ case 25:
+ return "polygonM (unsupported by HYDRO)";
+ case 28:
+ return "multipointM (unsupported by HYDRO)";
+ case 31:
+ return "multipatch (unsupported by HYDRO)";
+ default:
+ return "unknown";
+ }
+}
+
+int HYDROData_ShapeFile::TryOpenShapeFile(QString theFileName)
+{
+ QString aSHPfile = theFileName.simplified();
+ QString aSHXfile = theFileName.simplified().replace( ".shp", ".shx", Qt::CaseInsensitive);
+ FILE* pFileSHP = NULL;
+ pFileSHP = fopen (aSHPfile.toAscii().data(), "r");
+ FILE* pFileSHX = NULL;
+ pFileSHX = fopen (aSHXfile.toAscii().data(), "r");
+
+ if (pFileSHP == NULL || pFileSHX == NULL)
+ {
+ if (pFileSHP == NULL)
+ return -1;
+ if (pFileSHX == NULL)
+ return -2;
+ }
+
+ fclose (pFileSHP);
+ fclose (pFileSHX);
+ return 0;
}
\ No newline at end of file