Salome HOME
tests 1 to 7 OK, 8 and above failed
[modules/hydro.git] / src / HYDROData / HYDROData_ShapeFile.cxx
index 3538a6ea2fb86aced858736bfbe744803c0bc6b9..a66bc81b960090322510265ec18a2ce52ad394d8 100644 (file)
@@ -78,24 +78,25 @@ HYDROData_ShapeFile::~HYDROData_ShapeFile()
   Free();
 }
 
-void HYDROData_ShapeFile::Export(const QString& aFileName, 
-  NCollection_Sequence<Handle_HYDROData_PolylineXY> aPolyXYSeq,
-  NCollection_Sequence<Handle_HYDROData_Polyline3D> aPoly3DSeq,
-  QStringList& aNonExpList)
+void HYDROData_ShapeFile::Export(Handle(HYDROData_Document) theDocument,
+                                 const QString& aFileName,
+                                 NCollection_Sequence<Handle_HYDROData_PolylineXY> aPolyXYSeq,
+                                 NCollection_Sequence<Handle_HYDROData_Polyline3D> aPoly3DSeq,
+                                 QStringList& aNonExpList)
 {
   SHPHandle hSHPHandle;
   if (!aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty())
   {
     hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARC );        
     for (int i = 1; i <= aPolyXYSeq.Size(); i++)
-      if (WriteObjectPolyXY(hSHPHandle, aPolyXYSeq(i)) != 1)
+      if (WriteObjectPolyXY(theDocument, hSHPHandle, aPolyXYSeq(i)) != 1)
         aNonExpList.append(aPolyXYSeq(i)->GetName());
   }
   else if (aPolyXYSeq.IsEmpty() && !aPoly3DSeq.IsEmpty())
   {
     hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARCZ );
     for (int i = 1; i <= aPoly3DSeq.Size(); i++)
-      if (WriteObjectPoly3D(hSHPHandle, aPoly3DSeq(i)) != 1)
+      if (WriteObjectPoly3D(theDocument, hSHPHandle, aPoly3DSeq(i)) != 1)
         aNonExpList.append(aPoly3DSeq(i)->GetName());
   }
   if (hSHPHandle->nRecords > 0)
@@ -109,8 +110,11 @@ void HYDROData_ShapeFile::Export(const QString& aFileName,
   }
 }
 
-void HYDROData_ShapeFile::Export(const QString& aFileName, const Handle_HYDROData_LandCoverMap& aLCM, 
-                                 QStringList& aNonExpList, bool bCheckLinear, bool bUseDiscr, double theDefl)
+void HYDROData_ShapeFile::Export(Handle(HYDROData_Document) theDocument,
+                                 const QString& aFileName,
+                                 const Handle_HYDROData_LandCoverMap& aLCM,
+                                 QStringList& aNonExpList,
+                                 bool bCheckLinear, bool bUseDiscr, double theDefl)
 {
   if (bCheckLinear && !aLCM->CheckLinear())
     return;
@@ -123,7 +127,7 @@ void HYDROData_ShapeFile::Export(const QString& aFileName, const Handle_HYDRODat
     for( ; It.More(); It.Next())
     {
       TopoDS_Face aFace = It.Face();
-      if (WriteObjectPolygon(hSHPHandle, aFace, bUseDiscr, theDefl) != 1)
+      if (WriteObjectPolygon(theDocument, hSHPHandle, aFace, bUseDiscr, theDefl) != 1)
         aNonExpList.append(aLCM->GetName() + "_" +  QString::number(It.Index()));
     }
   }
@@ -139,7 +143,9 @@ void HYDROData_ShapeFile::Export(const QString& aFileName, const Handle_HYDRODat
 }
 
 
-int HYDROData_ShapeFile::WriteObjectPolyXY(SHPHandle theShpHandle, Handle_HYDROData_PolylineXY thePoly )
+int HYDROData_ShapeFile::WriteObjectPolyXY(Handle(HYDROData_Document) theDocument,
+                                           SHPHandle theShpHandle,
+                                           Handle_HYDROData_PolylineXY thePoly )
 {
   SHPObject    *aSHPObj;
   std::vector<double> x, y;
@@ -155,13 +161,17 @@ int HYDROData_ShapeFile::WriteObjectPolyXY(SHPHandle theShpHandle, Handle_HYDROD
     HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPoints(i);
     for (int j = 1; j <= aPointList.Size(); j++)
     {
-      x.push_back( aPointList(j).X());
-      y.push_back( aPointList(j).Y()); 
+      gp_XY P = aPointList(j);
+      theDocument->Transform(P, false);
+      x.push_back( P.X());
+      y.push_back( P.Y());
     }
     if (thePoly->IsClosedSection(i))
     {
-      x.push_back( aPointList(1).X());
-      y.push_back( aPointList(1).Y()); 
+      gp_XY P = aPointList(1);
+      theDocument->Transform(P, false);
+      x.push_back( P.X());
+      y.push_back( P.Y());
     }
   }
     
@@ -171,7 +181,9 @@ int HYDROData_ShapeFile::WriteObjectPolyXY(SHPHandle theShpHandle, Handle_HYDROD
   return 1;
 }
 
-int HYDROData_ShapeFile::WriteObjectPoly3D(SHPHandle theShpHandle, Handle_HYDROData_Polyline3D thePoly )
+int HYDROData_ShapeFile::WriteObjectPoly3D(Handle(HYDROData_Document) theDocument,
+                                           SHPHandle theShpHandle,
+                                           Handle_HYDROData_Polyline3D thePoly )
 {
   SHPObject    *aSHPObj;
   std::vector<double> x, y, z;
@@ -187,14 +199,18 @@ int HYDROData_ShapeFile::WriteObjectPoly3D(SHPHandle theShpHandle, Handle_HYDROD
     HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPolylineXY()->GetPoints(i);
     for (int j = 1; j <= aPointList.Size(); j++)
     {
-      x.push_back( aPointList(j).X());
-      y.push_back( aPointList(j).Y()); 
+      gp_XY P = aPointList(j);
+      theDocument->Transform(P, false);
+      x.push_back( P.X());
+      y.push_back( P.Y());
       z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(j).X(), aPointList(j).Y())));
     }
     if ( thePoly->GetPolylineXY()->IsClosedSection(i))
     {
-      x.push_back( aPointList(1).X());
-      y.push_back( aPointList(1).Y()); 
+      gp_XY P = aPointList(1);
+      theDocument->Transform(P, false);
+      x.push_back( P.X());
+      y.push_back( P.Y());
       z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(1).X(), aPointList(1).Y())));
 
     }
@@ -206,7 +222,9 @@ int HYDROData_ShapeFile::WriteObjectPoly3D(SHPHandle theShpHandle, Handle_HYDROD
   return 1;
 }
 
-int HYDROData_ShapeFile::WriteObjectPolygon(SHPHandle theShpHandle, const TopoDS_Shape& theInputShape,
+int HYDROData_ShapeFile::WriteObjectPolygon(Handle(HYDROData_Document) theDocument,
+                                            SHPHandle theShpHandle,
+                                            const TopoDS_Shape& theInputShape,
                                             bool bUseDiscr, double theDefl)
 {
   if (theInputShape.IsNull())
@@ -214,7 +232,7 @@ int HYDROData_ShapeFile::WriteObjectPolygon(SHPHandle theShpHandle, const TopoDS
 
   if (theInputShape.ShapeType() == TopAbs_FACE)
   {
-    ProcessFace(TopoDS::Face(theInputShape), theShpHandle, bUseDiscr, theDefl);
+    ProcessFace(theDocument, TopoDS::Face(theInputShape), theShpHandle, bUseDiscr, theDefl);
   }
   else if (theInputShape.ShapeType() == TopAbs_COMPOUND)
   {
@@ -224,7 +242,7 @@ int HYDROData_ShapeFile::WriteObjectPolygon(SHPHandle theShpHandle, const TopoDS
       TopoDS_Face aF = TopoDS::Face(Ex.Current());   
       if (aF.IsNull())
         continue;
-      ProcessFace(aF, theShpHandle, bUseDiscr, theDefl);
+      ProcessFace(theDocument, aF, theShpHandle, bUseDiscr, theDefl);
     }
   }
   else
@@ -234,7 +252,9 @@ int HYDROData_ShapeFile::WriteObjectPolygon(SHPHandle theShpHandle, const TopoDS
  
 }
 
-void HYDROData_ShapeFile::ProcessFace(const TopoDS_Face& theFace, SHPHandle theShpHandle,
+void HYDROData_ShapeFile::ProcessFace(Handle(HYDROData_Document) theDocument,
+                                      const TopoDS_Face& theFace,
+                                      SHPHandle theShpHandle,
                                       bool bUseDiscr, double theDefl )
 {
   if (theFace.ShapeType() != TopAbs_FACE)
@@ -334,12 +354,16 @@ void HYDROData_ShapeFile::ProcessFace(const TopoDS_Face& theFace, SHPHandle theS
     //any other wires (holes) - in anticlockwise direction
     for (int j = aNPnts.Size(); j >= 1; j--)
     { 
-      x.push_back( aNPnts(j).X());
-      y.push_back( aNPnts(j).Y()); 
+      gp_XY P = gp_XY(aNPnts(j).X(), aNPnts(j).Y());
+      theDocument->Transform(P, false);
+      x.push_back( P.X());
+      y.push_back( P.Y());
     }
     //first point is same as the last one => closed polygon
-    x.push_back( aNPnts.Last().X());
-    y.push_back( aNPnts.Last().Y()); 
+    gp_XY P = gp_XY(aNPnts.Last().X(), aNPnts.Last().Y());
+    theDocument->Transform(P, false);
+    x.push_back( P.X());
+    y.push_back( P.Y());
 
   }
   
@@ -355,7 +379,8 @@ bool HYDROData_ShapeFile::Parse(SHPHandle theHandle, ShapeType theType, int& the
   mySHPObjects.clear();
   SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
   theShapeTypeOfFile = aShapeType;
-  bool ToRead = (theType == ShapeType_Polyline && (aShapeType == 3 || aShapeType == 13 || aShapeType == 23)) ||
+  bool ToRead = (theType == ShapeType_Polyline && 
+    (aShapeType == 3 || aShapeType == 13 || aShapeType == 23 || aShapeType == 5)) ||
    (theType == ShapeType_Polygon && aShapeType == 5);
   if (ToRead) 
   {
@@ -538,7 +563,7 @@ void HYDROData_ShapeFile::Free()
 
 
 void HYDROData_ShapeFile::ReadSHPPolyXY(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName, 
-  int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
+  int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities, bool bReadAsPolyline)
 {
 
   Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
@@ -553,19 +578,28 @@ void HYDROData_ShapeFile::ReadSHPPolyXY(Handle(HYDROData_Document) theDocument,
     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] )
+    if (!bReadAsPolyline)
     {
-      IsClosed = true;
-      aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true);
+      bool IsClosed = false;
+      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--;
     }
     else
-      aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false);
-    
-    if (IsClosed)
+    {
+      //polygon; contours always closed
+      aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true);
       EndIndex--;
+    }
     for ( int k = StartIndex; k < EndIndex ; k++ )
     {
       HYDROData_PolylineXY::Point aSectPoint = gp_XY(anObj->padfX[k], anObj->padfY[k]);
@@ -623,7 +657,11 @@ void HYDROData_ShapeFile::ReadSHPPoly3D(Handle(HYDROData_Document) theDocument,
       HYDROData_PolylineXY::Point aSectPoint = gp_XY(anObj->padfX[k], anObj->padfY[k]);
       theDocument->Transform(aSectPoint, true);
       aPolylineXY->AddPoint( i, aSectPoint );
-      aAPoints.Append(gp_XYZ (aSectPoint.X(), aSectPoint.Y(), anObj->padfZ[k]));
+      HYDROData_Bathymetry::AltitudePoint p;
+      p.X = aSectPoint.X();
+      p.Y = aSectPoint.Y();
+      p.Z = anObj->padfZ[k];
+      aAPoints.push_back(p);
     }
   }
 
@@ -651,7 +689,21 @@ void HYDROData_ShapeFile::ReadSHPPoly3D(Handle(HYDROData_Document) theDocument,
 
 }
 
-
+void HYDROData_ShapeFile::GetFreeIndices(std::vector<int>& theAllowedIndexes, QString strName, size_t theObjsSize,
+                                         QStringList theExistingNames, QString theBaseFileName)
+{
+  int anInd = 0;
+  for (;theAllowedIndexes.size() < theObjsSize;)
+  {
+    if (!theExistingNames.contains(theBaseFileName + strName + QString::number(anInd)))
+    {
+      theAllowedIndexes.push_back(anInd);
+      anInd++;
+    }
+    else
+      anInd++;
+  }
+}
 
 int HYDROData_ShapeFile::ImportPolylines(Handle(HYDROData_Document) theDocument, const QString& theFileName, 
   NCollection_Sequence<Handle_HYDROData_Entity>& theEntities, int& theShapeTypeOfFile)
@@ -678,21 +730,12 @@ int HYDROData_ShapeFile::ImportPolylines(Handle(HYDROData_Document) theDocument,
     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++;
-    }
-    
+    size_t anObjsSize = mySHPObjects.size();
+    GetFreeIndices(anAllowedIndexes, "_PolyXY_", anObjsSize, anExistingNames, aBaseFileName);
     for (size_t i = 0; i < mySHPObjects.size(); i++ )
     {
-      ReadSHPPolyXY(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
+      ReadSHPPolyXY(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities, false);
     }
     aStat = 1;
   }
@@ -712,9 +755,23 @@ int HYDROData_ShapeFile::ImportPolylines(Handle(HYDROData_Document) theDocument,
         anInd++;
     }
     for (size_t i = 0; i < mySHPObjects.size(); i++ )
+    {
       ReadSHPPoly3D(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
+    }
     aStat = 1;
   }
+  else if (aHSHP->nShapeType == 5)
+  {
+    //import polygon's contours as polylines
+    size_t anObjsSize = mySHPObjects.size();
+    GetFreeIndices(anAllowedIndexes, "_PolyXY_", anObjsSize, anExistingNames, aBaseFileName);
+    for (size_t i = 0; i < mySHPObjects.size(); i++ )
+    {
+      ReadSHPPolyXY(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities, true);
+    }
+    aStat = 2;
+  }
   else  
   {
     aStat = 0;