]> SALOME platform Git repositories - modules/hydro.git/commitdiff
Salome HOME
Import of shapefile - bug fixes; #refs 614
authorisn <isn@opencascade.com>
Tue, 30 Jun 2015 08:50:46 +0000 (11:50 +0300)
committerisn <isn@opencascade.com>
Tue, 30 Jun 2015 08:51:14 +0000 (11:51 +0300)
src/HYDROData/HYDROData_ShapeFile.cxx
src/HYDROData/HYDROData_ShapeFile.h
src/HYDROGUI/HYDROGUI_ImportLandCoverOp.cxx
src/HYDROGUI/HYDROGUI_ImportPolylineOp.cxx

index 395f9182a70dda852a612ffaecf5d69f3dcd7af4..1b0ee89af43e13b7b85254feb85b074c3350d353 100644 (file)
@@ -95,7 +95,9 @@ void HYDROData_ShapeFile::Export(const QString& aFileName,
   else
   {
     SHPClose( hSHPHandle );
-    remove (aFileName.toStdString().c_str());
+    QString aFN = aFileName.simplified();
+    remove (aFN.toStdString().c_str());
+    remove (aFN.replace( ".shp", ".shx", Qt::CaseInsensitive).toStdString().c_str());
   }
 }
 
@@ -266,11 +268,12 @@ void HYDROData_ShapeFile::ProcessFace(TopoDS_Face theFace, SHPHandle theShpHandl
     return;
 }
 
-bool HYDROData_ShapeFile::Parse(SHPHandle theHandle, ShapeType theType)
+bool HYDROData_ShapeFile::Parse(SHPHandle theHandle, ShapeType theType, int& theShapeTypeOfFile)
 {
   int aShapeType;
   mySHPObjects.clear();
   SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
+  theShapeTypeOfFile = aShapeType;
   bool ToRead = (theType == ShapeType_Polyline && (aShapeType == 3 || aShapeType == 13 || aShapeType == 23)) ||
    (theType == ShapeType_Polygon && aShapeType == 5);
   if (ToRead) 
@@ -341,12 +344,15 @@ void HYDROData_ShapeFile::ReadSHPPolygon(SHPObject* anObj, int i, TopoDS_Face& F
   }
 }
 
-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" );
-  if (!Parse(myHSHP, HYDROData_ShapeFile::ShapeType_Polygon))
-    return false;
+  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));
    
@@ -358,10 +364,10 @@ bool HYDROData_ShapeFile::ImportLandCovers(const QString theFileName, QStringLis
        ReadSHPPolygon(mySHPObjects[i], i, aF);
        theFaces.Append(aF);
     }
-    return true;
+    return 1;
   }
   else
-    return false;
+    return 0;
 }
 
 void HYDROData_ShapeFile::Free()
@@ -496,8 +502,14 @@ void HYDROData_ShapeFile::ReadSHPPoly3D(Handle(HYDROData_Document) theDocument,
 
 
 
-bool HYDROData_ShapeFile::ImportPolylines(Handle(HYDROData_Document) theDocument, const QString& theFileName, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
+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;
@@ -511,9 +523,8 @@ bool HYDROData_ShapeFile::ImportPolylines(Handle(HYDROData_Document) theDocument
   QFileInfo aFileInfo(theFileName);
   QString aBaseFileName = aFileInfo.baseName();
 
-  if (!Parse(aHSHP, HYDROData_ShapeFile::ShapeType_Polyline))
-    return false;
-  bool aStat = false;
+  if (!Parse(aHSHP, HYDROData_ShapeFile::ShapeType_Polyline, theShapeTypeOfFile))
+    return 0;
   if (aHSHP->nShapeType == 3 || aHSHP->nShapeType == 23)
   {
     anInd = 0;
@@ -532,7 +543,7 @@ bool HYDROData_ShapeFile::ImportPolylines(Handle(HYDROData_Document) theDocument
     {
       ReadSHPPolyXY(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
     }
-    aStat = true;
+    aStat = 1;
   }
   else if (aHSHP->nShapeType == 13)
   {
@@ -551,11 +562,11 @@ bool HYDROData_ShapeFile::ImportPolylines(Handle(HYDROData_Document) theDocument
     }
     for (size_t i = 0; i < mySHPObjects.size(); i++ )
       ReadSHPPoly3D(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
-    aStat = true;
+    aStat = 1;
   }
   else  
   {
-    aStat = false;
+    aStat = 0;
   }
   
   for (size_t i = 0; i < mySHPObjects.size(); i++ )
@@ -564,4 +575,63 @@ bool HYDROData_ShapeFile::ImportPolylines(Handle(HYDROData_Document) theDocument
   mySHPObjects.clear();
   SHPClose(aHSHP);
   return aStat;
+}
+
+QString HYDROData_ShapeFile::GetShapeTypeName(int theType)
+{
+  switch (theType)
+  {
+    case 0:
+      return "null shape";
+    case 1:
+      return "point";
+    case 3:
+      return "arc";
+    case 5:
+      return "polygon";
+    case 8:
+      return "multipoint";
+    case 11:
+      return "pointZ";
+    case 13:
+      return "arcZ";
+    case 15:
+      return "polygonZ";
+    case 18:
+      return "multipointZ";
+    case 21:
+      return "pointM";
+    case 23:
+      return "ARCM";
+    case 25:
+      return "polygonM";
+    case 28:
+      return "multipointM";
+    case 31:
+      return "multipatch";
+    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
index 30b1167ac7864af03b3c85bc1a04d4914aea087b..e23c9d6e15f856d62b0ac18027bc634b2c191a3a 100644 (file)
@@ -64,20 +64,22 @@ public:
   int WriteObjectPoly3D(SHPHandle theShpHandle, Handle_HYDROData_Polyline3D thePoly );
   int WriteObjectLC(SHPHandle theShpHandle, Handle_HYDROData_LandCover theLC );
   //Import
-  bool Parse(SHPHandle theHandle, ShapeType theType);
+  bool Parse(SHPHandle theHandle, ShapeType theType, int& theShapeTypeOfFile);
   //Import Landcover
   void ReadSHPPolygon(SHPObject* anObj, int i, TopoDS_Face& F);
-  HYDRODATA_EXPORT bool ImportLandCovers(const QString theFileName, QStringList& thePolygonsList, TopTools_SequenceOfShape& theFaces);
+  HYDRODATA_EXPORT int ImportLandCovers(const QString theFileName, QStringList& thePolygonsList, TopTools_SequenceOfShape& theFaces, int& theShapeTypeOfFile);
   HYDRODATA_EXPORT void Free();
   //Import Polyline
   void ReadSHPPolyXY(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName, 
                         int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities);
   void ReadSHPPoly3D(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName, 
                         int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities);
-  HYDRODATA_EXPORT bool ImportPolylines(Handle(HYDROData_Document) theDocument, const QString& theFileName, 
-                       NCollection_Sequence<Handle_HYDROData_Entity>& theEntities);
+  HYDRODATA_EXPORT int ImportPolylines(Handle(HYDROData_Document) theDocument, const QString& theFileName, 
+                       NCollection_Sequence<Handle_HYDROData_Entity>& theEntities, int& theShapeTypeOfFile);
+  HYDRODATA_EXPORT QString GetShapeTypeName(int theType);
 private:
   void ProcessFace(TopoDS_Face theFace, SHPHandle theShpHandle);
+  int TryOpenShapeFile(QString theFileName);
 private:
   std::vector<SHPObject*> mySHPObjects;
   SHPHandle myHSHP;
index bc49f7efc7b6bc6ea51b08689e841f0494c7af17..6b026e90349e3693d935514f2b18808b5a64e80a 100644 (file)
@@ -222,8 +222,9 @@ void HYDROGUI_ImportLandCoverOp::onFileSelected()
     erasePreview();
 
     Handle(AIS_InteractiveContext) aCtx = NULL;
-
-    if (anImporter.ImportLandCovers(aFileName, aPolygonsList, aFaces))
+    int aShapeTypeOfFile = -1;
+    int aStat = anImporter.ImportLandCovers(aFileName, aPolygonsList, aFaces, aShapeTypeOfFile);
+    if (aStat == 1)
     {
       aPanel->setPolygonNames(aPolygonsList);
 
@@ -276,7 +277,14 @@ void HYDROGUI_ImportLandCoverOp::onFileSelected()
       aPanel->setPolygonNames(QStringList());
       aPanel->setObjectName("");
       QApplication::restoreOverrideCursor();
-      SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_LANDCOVER" ), "Cannot import land cover;\nThe shape type is not polygon" );
+      QString aMess = "Cannot import land cover;\n";
+      if (aStat == -1)
+        aMess += "Cannot open SHP file";
+      else if (aStat == -2)
+        aMess += "Cannot open SHX file";
+      else 
+        aMess += "The shape type of file is " + anImporter.GetShapeTypeName(aShapeTypeOfFile);
+      SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_LANDCOVER" ), aMess);
       commitDocOperation();
       //abort();
     }
index 7ebba03d59649be171fdb8c5350f0ceacaa82864..ff47cf65ccd414af9f7291246b90fd8729b3bfe6 100644 (file)
@@ -94,10 +94,21 @@ void HYDROGUI_ImportPolylineOp::onApply()
     {
       HYDROData_ShapeFile anImporter;
       NCollection_Sequence<Handle_HYDROData_Entity> theEntities;
-      if (anImporter.ImportPolylines(doc(), aFileName, theEntities ))
+      int aShapeTypeOfFile = -1;
+      int aStat = anImporter.ImportPolylines(doc(), aFileName, theEntities, aShapeTypeOfFile ); 
+      if (aStat == 1)
         UpdateView(theEntities);
       else
-        SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_POLYLINE" ), "Cannot import polyline;\nThe shape type is incorrect" );
+      {
+        QString aMess = "Cannot import polyline;\n";
+        if (aStat == -1)
+          aMess += "Cannot open SHP file";
+        else if (aStat == -2)
+          aMess += "Cannot open SHX file";
+        else 
+          aMess += "The shape type of file is " + anImporter.GetShapeTypeName(aShapeTypeOfFile);
+        SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_POLYLINE" ), aMess);
+      }
     }
   }
   if (!aFileNames.empty())