]> SALOME platform Git repositories - modules/hydro.git/commitdiff
Salome HOME
initial ver.
authorisn <isn@opencascade.com>
Thu, 15 Oct 2015 09:09:56 +0000 (12:09 +0300)
committerisn <isn@opencascade.com>
Thu, 15 Oct 2015 09:09:56 +0000 (12:09 +0300)
src/HYDROData/HYDROData_ShapeFile.cxx
src/HYDROData/HYDROData_ShapeFile.h
src/HYDRO_tests/CMakeLists.txt
src/HYDRO_tests/reference_data/cyprus_natural.dbf [new file with mode: 0644]
src/HYDRO_tests/reference_data/cyprus_natural.prj [new file with mode: 0644]
src/HYDRO_tests/reference_data/cyprus_natural.shp [new file with mode: 0644]
src/HYDRO_tests/reference_data/cyprus_natural.shx [new file with mode: 0644]
src/HYDRO_tests/test_HYDROData_ShapeFile.cxx [new file with mode: 0644]
src/HYDRO_tests/test_HYDROData_ShapeFile.h [new file with mode: 0644]

index 16f72c780f1c5af021d46f1f69c6579afd619053..8cf702f0c72a81794796ddb2c3772333f4a9cd52 100644 (file)
@@ -634,4 +634,85 @@ int HYDROData_ShapeFile::TryOpenShapeFile(QString theFileName)
   fclose (pFileSHP);
   fclose (pFileSHX);
   return 0;
-}
\ No newline at end of file
+}
+
+
+bool HYDROData_ShapeFile::CheckDBFFileExisting(const QString& theSHPFilePath, QString& thePathToDBFFile)
+{
+  QString aSHPfile = theSHPFilePath.simplified();
+  QString aDBFfile = theSHPFilePath.simplified().replace( ".shp", ".dbf", Qt::CaseInsensitive);
+  FILE* pFileDBF = NULL;
+  pFileDBF = fopen (aDBFfile.toAscii().data(), "r");
+
+  if (pFileDBF == NULL)
+  {
+    return false;
+  }
+  
+  fclose (pFileDBF);
+  thePathToDBFFile = aDBFfile;
+  return true;
+}
+
+
+bool HYDROData_ShapeFile::DBF_OpenDBF(const QString& thePathToDBFFile)
+{
+  myHDBF = DBFOpen( thePathToDBFFile.toAscii().data(), "r" );
+  if(myHDBF != NULL)
+    return true;
+  else
+    return false;
+}
+
+int HYDROData_ShapeFile::DBF_GetNbFields()
+{
+  if(myHDBF == NULL)
+    return 0;
+  return DBFGetFieldCount(myHDBF);
+}
+
+void HYDROData_ShapeFile::DBF_CloseDBF()
+{
+  if(myHDBF != NULL)
+     DBFClose( myHDBF );
+}
+
+QStringList HYDROData_ShapeFile::DBF_GetFieldList()
+{
+  QStringList FieldList;
+  int  nWidth, nDecimals;
+  char chField[12];
+
+  for( int i = 0; i < DBFGetFieldCount(myHDBF); i++ )
+  {
+      DBFFieldType eType;
+      eType = DBFGetFieldInfo( myHDBF, i, chField, &nWidth, &nDecimals );
+      FieldList.append(QString(chField));
+  }
+
+  return FieldList;
+}
+
+void HYDROData_ShapeFile::DBF_GetFieldTypeList(std::vector<DBF_FieldType>& FTVect)
+{
+  int  nWidth, nDecimals;
+  char chField[12];
+  DBF_FieldType FT;
+  for( int i = 0; i < DBFGetFieldCount(myHDBF); i++ )
+  {
+    DBFFieldType eType;
+    eType = DBFGetFieldInfo( myHDBF, i, chField, &nWidth, &nDecimals );
+    if( eType == FTString )
+      FT = DBF_FieldType_String;
+    else if( eType == FTInteger )
+      FT = DBF_FieldType_Integer;
+    else if( eType == FTDouble )
+      FT = DBF_FieldType_Double;
+    else if( eType == FTInvalid )
+      FT = DBF_FieldType_Invalid;
+
+    FTVect.push_back(FT);
+  }
+
+}
+
index e23c9d6e15f856d62b0ac18027bc634b2c191a3a..0f1cd3d2f819fef44a4fbe9961c75791706829d1 100644 (file)
@@ -50,6 +50,14 @@ enum ShapeType
   ShapeType_Polygon
 };
 
+enum DBF_FieldType
+{
+  DBF_FieldType_String,
+  DBF_FieldType_Integer, 
+  DBF_FieldType_Double,
+  DBF_FieldType_Invalid    
+};
+
 public:
   HYDRODATA_EXPORT HYDROData_ShapeFile(  );
   virtual HYDRODATA_EXPORT ~HYDROData_ShapeFile();
@@ -77,12 +85,24 @@ public:
   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);
+
+  //DBF I/O Methods
+  bool CheckDBFFileExisting(const QString& theSHPFilePath, QString& thePathToDBFFile);
+  bool DBF_OpenDBF(const QString& thePathToDBFFile);
+  int DBF_GetNbFields();
+  QStringList DBF_GetFieldList();
+  void DBF_GetFieldTypeList(std::vector<DBF_FieldType>& FTVect);
+  void DBF_CloseDBF();
+
 private:
   void ProcessFace(TopoDS_Face theFace, SHPHandle theShpHandle);
   int TryOpenShapeFile(QString theFileName);
 private:
   std::vector<SHPObject*> mySHPObjects;
   SHPHandle myHSHP;
+  DBFHandle    myHDBF;
+
+  friend class test_HYDROData_ShapeFile;
 };
 
 #endif
index 9437e76295b88ff6e64dcbd841e34c9198e2fba7..8ed144a6e3cab99cfa3b07530e74ca28c6c0f993 100644 (file)
@@ -11,6 +11,7 @@ set(PROJECT_HEADERS
   test_HYDROData_OperationsFactory.h
   test_HYDROData_PolylineXY.h
   test_HYDROData_Profile.h
+  test_HYDROData_ShapeFile.h
   test_HYDROData_StricklerTable.h
   test_HYDROGUI_ListModel.h
 
@@ -28,6 +29,7 @@ set(PROJECT_SOURCES
   test_HYDROData_OperationsFactory.cxx
   test_HYDROData_PolylineXY.cxx
   test_HYDROData_Profile.cxx
+  test_HYDROData_ShapeFile.cxx
   test_HYDROData_StricklerTable.cxx
   test_HYDROGUI_ListModel.cxx
 
diff --git a/src/HYDRO_tests/reference_data/cyprus_natural.dbf b/src/HYDRO_tests/reference_data/cyprus_natural.dbf
new file mode 100644 (file)
index 0000000..ba0bfd7
Binary files /dev/null and b/src/HYDRO_tests/reference_data/cyprus_natural.dbf differ
diff --git a/src/HYDRO_tests/reference_data/cyprus_natural.prj b/src/HYDRO_tests/reference_data/cyprus_natural.prj
new file mode 100644 (file)
index 0000000..379ef7c
--- /dev/null
@@ -0,0 +1 @@
+GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]
\ No newline at end of file
diff --git a/src/HYDRO_tests/reference_data/cyprus_natural.shp b/src/HYDRO_tests/reference_data/cyprus_natural.shp
new file mode 100644 (file)
index 0000000..05f9ebc
Binary files /dev/null and b/src/HYDRO_tests/reference_data/cyprus_natural.shp differ
diff --git a/src/HYDRO_tests/reference_data/cyprus_natural.shx b/src/HYDRO_tests/reference_data/cyprus_natural.shx
new file mode 100644 (file)
index 0000000..b8fbc3a
Binary files /dev/null and b/src/HYDRO_tests/reference_data/cyprus_natural.shx differ
diff --git a/src/HYDRO_tests/test_HYDROData_ShapeFile.cxx b/src/HYDRO_tests/test_HYDROData_ShapeFile.cxx
new file mode 100644 (file)
index 0000000..0edbb45
--- /dev/null
@@ -0,0 +1,71 @@
+// Copyright (C) 2014-2015  EDF-R&D
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+
+#include <test_HYDROData_ShapeFile.h>
+#include <HYDROData_ShapeFile.h>
+#include <QStringList>
+#include <vector>
+
+const QString REF_PATH = qgetenv( "HYDRO_REFERENCE_DATA" );
+
+void test_HYDROData_ShapeFile::test_openDbf()
+{
+  QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
+  HYDROData_ShapeFile aSHPFile;
+  bool Stat = aSHPFile.DBF_OpenDBF(aDBFPath);
+  CPPUNIT_ASSERT_EQUAL( true, Stat );  
+  aSHPFile.DBF_CloseDBF();
+}
+
+void test_HYDROData_ShapeFile::test_NbFieldsDbf()
+{
+  QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
+  HYDROData_ShapeFile aSHPFile;
+  aSHPFile.DBF_OpenDBF(aDBFPath);
+  int NbF = aSHPFile.DBF_GetNbFields();
+  CPPUNIT_ASSERT_EQUAL( 2, NbF );  
+  aSHPFile.DBF_CloseDBF();
+}
+
+void test_HYDROData_ShapeFile::test_FieldListDbf()
+{
+  QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
+  HYDROData_ShapeFile aSHPFile;
+  aSHPFile.DBF_OpenDBF(aDBFPath);
+
+  QStringList FL = aSHPFile.DBF_GetFieldList();
+  CPPUNIT_ASSERT_EQUAL(true, "NAME" == FL[0]);
+  CPPUNIT_ASSERT_EQUAL(true, "TYPE" == FL[1]);
+  aSHPFile.DBF_CloseDBF();
+}
+
+void test_HYDROData_ShapeFile::test_FieldTypeListDbf()
+{
+  QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
+  HYDROData_ShapeFile aSHPFile;
+  aSHPFile.DBF_OpenDBF(aDBFPath);
+
+  std::vector<HYDROData_ShapeFile::DBF_FieldType> FTVect;
+  aSHPFile.DBF_GetFieldTypeList(FTVect);
+  CPPUNIT_ASSERT_EQUAL(HYDROData_ShapeFile::DBF_FieldType_String, FTVect[0]);
+  CPPUNIT_ASSERT_EQUAL(HYDROData_ShapeFile::DBF_FieldType_String, FTVect[1]);
+  aSHPFile.DBF_CloseDBF();
+}
+
+
+
diff --git a/src/HYDRO_tests/test_HYDROData_ShapeFile.h b/src/HYDRO_tests/test_HYDROData_ShapeFile.h
new file mode 100644 (file)
index 0000000..6c3739a
--- /dev/null
@@ -0,0 +1,39 @@
+// Copyright (C) 2014-2015  EDF-R&D
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+#include <cppunit/extensions/HelperMacros.h>
+
+class test_HYDROData_ShapeFile : public CppUnit::TestFixture
+{
+  CPPUNIT_TEST_SUITE( test_HYDROData_ShapeFile );
+  CPPUNIT_TEST( test_openDbf );     
+  CPPUNIT_TEST( test_NbFieldsDbf );     
+  CPPUNIT_TEST( test_FieldListDbf );
+  CPPUNIT_TEST( test_FieldTypeListDbf );
+
+  CPPUNIT_TEST_SUITE_END();
+
+public:
+  void test_openDbf();
+  void test_NbFieldsDbf();
+  void test_FieldListDbf();
+  void test_FieldTypeListDbf();
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION( test_HYDROData_ShapeFile );
+CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( test_HYDROData_ShapeFile, "HYDROData_ShapeFile" );
+