]> SALOME platform Git repositories - modules/hydro.git/commitdiff
Salome HOME
DBF Reader #1
authorisn <isn@opencascade.com>
Thu, 15 Oct 2015 13:18:12 +0000 (16:18 +0300)
committerisn <isn@opencascade.com>
Thu, 15 Oct 2015 13:18:12 +0000 (16:18 +0300)
src/HYDROData/HYDROData_ShapeFile.cxx
src/HYDROData/HYDROData_ShapeFile.h
src/HYDRO_tests/reference_data/CLC06.cst [new file with mode: 0644]
src/HYDRO_tests/reference_data/CLC06.dbf [new file with mode: 0644]
src/HYDRO_tests/reference_data/CLC06.prj [new file with mode: 0644]
src/HYDRO_tests/reference_data/CLC06.shp [new file with mode: 0644]
src/HYDRO_tests/reference_data/CLC06.shx [new file with mode: 0644]
src/HYDRO_tests/test_HYDROData_ShapeFile.cxx
src/HYDRO_tests/test_HYDROData_ShapeFile.h

index 8cf702f0c72a81794796ddb2c3772333f4a9cd52..87d9bc2e81665f4607535bd9a7cabc05fe877ea8 100644 (file)
@@ -716,3 +716,66 @@ void HYDROData_ShapeFile::DBF_GetFieldTypeList(std::vector<DBF_FieldType>& FTVec
 
 }
 
+int HYDROData_ShapeFile::DBF_GetNbRecords()
+{
+  if(myHDBF == NULL)
+    return 0;
+  return DBFGetRecordCount(myHDBF);
+}
+
+void HYDROData_ShapeFile::DBF_GetAttributeList(int theIndexOfField, std::vector<DBF_AttrValue>& theAttrV)
+{
+  int nWidth, nDecimals;
+  char chField[12];
+  
+  for( int i = 0; i < DBFGetRecordCount(myHDBF); i++ )
+  {      
+    DBFFieldType eType;
+    DBF_AttrValue anAttr;
+    eType = DBFGetFieldInfo( myHDBF, theIndexOfField, chField, &nWidth, &nDecimals );
+
+    if( DBFIsAttributeNULL( myHDBF, i, theIndexOfField ) )
+    {
+      anAttr.myIsNull = true;
+    }
+    else
+    {
+      switch( eType )
+      {
+        case FTString:
+        {
+          const char* chAttr = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
+          anAttr.myIsNull = false;
+          anAttr.myFieldType = DBF_FieldType_String;
+          anAttr.myRawValue = chAttr;
+          anAttr.myStrVal = QString(chAttr);
+          break;
+        }
+       
+        case FTInteger:
+        {
+          int iAttr = DBFReadIntegerAttribute( myHDBF, i, theIndexOfField );
+          anAttr.myIsNull = false;
+          anAttr.myFieldType = DBF_FieldType_Integer;
+          anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
+          anAttr.myStrVal = QString::number(iAttr);
+          break;
+        }
+          
+        case FTDouble:
+        {
+          double dAttr = DBFReadDoubleAttribute( myHDBF, i, theIndexOfField );
+          anAttr.myIsNull = false;
+          anAttr.myFieldType = DBF_FieldType_Double;
+          anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
+          anAttr.myStrVal = QString::number(dAttr, 'g', 20);
+          break;
+        }
+        default:
+          break;
+      }
+    }
+    theAttrV.push_back(anAttr);
+  }
+
+}
\ No newline at end of file
index 0f1cd3d2f819fef44a4fbe9961c75791706829d1..11517a6647d937b6106706dad2aae02b2a3c28f7 100644 (file)
@@ -52,10 +52,24 @@ enum ShapeType
 
 enum DBF_FieldType
 {
+  DBF_FieldType_None,
   DBF_FieldType_String,
   DBF_FieldType_Integer, 
   DBF_FieldType_Double,
-  DBF_FieldType_Invalid    
+  DBF_FieldType_Invalid
+};
+
+struct DBF_AttrValue
+{
+  DBF_AttrValue() 
+  {
+    myIsNull = true;
+    myFieldType = DBF_FieldType_None;
+  }
+  QString myStrVal;
+  std::string myRawValue;
+  DBF_FieldType myFieldType;
+  bool myIsNull;
 };
 
 public:
@@ -92,7 +106,9 @@ public:
   int DBF_GetNbFields();
   QStringList DBF_GetFieldList();
   void DBF_GetFieldTypeList(std::vector<DBF_FieldType>& FTVect);
+  int DBF_GetNbRecords();
   void DBF_CloseDBF();
+  void DBF_GetAttributeList(int theIndexOfField, std::vector<DBF_AttrValue>& theAttrV);
 
 private:
   void ProcessFace(TopoDS_Face theFace, SHPHandle theShpHandle);
diff --git a/src/HYDRO_tests/reference_data/CLC06.cst b/src/HYDRO_tests/reference_data/CLC06.cst
new file mode 100644 (file)
index 0000000..cd89cb9
--- /dev/null
@@ -0,0 +1 @@
+ISO-8859-1
\ No newline at end of file
diff --git a/src/HYDRO_tests/reference_data/CLC06.dbf b/src/HYDRO_tests/reference_data/CLC06.dbf
new file mode 100644 (file)
index 0000000..26b0189
Binary files /dev/null and b/src/HYDRO_tests/reference_data/CLC06.dbf differ
diff --git a/src/HYDRO_tests/reference_data/CLC06.prj b/src/HYDRO_tests/reference_data/CLC06.prj
new file mode 100644 (file)
index 0000000..552fec7
--- /dev/null
@@ -0,0 +1 @@
+PROJCS["RGF93 / Lambert-93", GEOGCS["RGF93", DATUM["Reseau Geodesique Francais 1993", SPHEROID["GRS 1980", 6378137.0, 298.257222101, AUTHORITY["EPSG","7019"]], TOWGS84[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], AUTHORITY["EPSG","6171"]], PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], UNIT["degree", 0.017453292519943295], AXIS["Geodetic longitude", EAST], AXIS["Geodetic latitude", NORTH], AUTHORITY["EPSG","4171"]], PROJECTION["Lambert_Conformal_Conic_2SP", AUTHORITY["EPSG","9802"]], PARAMETER["central_meridian", 3.0], PARAMETER["latitude_of_origin", 46.5], PARAMETER["standard_parallel_1", 49.0], PARAMETER["false_easting", 700000.0], PARAMETER["false_northing", 6600000.0], PARAMETER["scale_factor", 1.0], PARAMETER["standard_parallel_2", 44.0], UNIT["m", 1.0], AXIS["Easting", EAST], AXIS["Northing", NORTH], AUTHORITY["EPSG","2154"]]
\ No newline at end of file
diff --git a/src/HYDRO_tests/reference_data/CLC06.shp b/src/HYDRO_tests/reference_data/CLC06.shp
new file mode 100644 (file)
index 0000000..ed5a8d0
Binary files /dev/null and b/src/HYDRO_tests/reference_data/CLC06.shp differ
diff --git a/src/HYDRO_tests/reference_data/CLC06.shx b/src/HYDRO_tests/reference_data/CLC06.shx
new file mode 100644 (file)
index 0000000..297e942
Binary files /dev/null and b/src/HYDRO_tests/reference_data/CLC06.shx differ
index 0edbb4578fdeda846948664a41c2c8aab231d468..035d69144e9866b89f469abae4fabeff1e892ec4 100644 (file)
@@ -36,7 +36,7 @@ void test_HYDROData_ShapeFile::test_NbFieldsDbf()
 {
   QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
   HYDROData_ShapeFile aSHPFile;
-  aSHPFile.DBF_OpenDBF(aDBFPath);
+  CPPUNIT_ASSERT_EQUAL(true, aSHPFile.DBF_OpenDBF(aDBFPath));
   int NbF = aSHPFile.DBF_GetNbFields();
   CPPUNIT_ASSERT_EQUAL( 2, NbF );  
   aSHPFile.DBF_CloseDBF();
@@ -46,7 +46,7 @@ void test_HYDROData_ShapeFile::test_FieldListDbf()
 {
   QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
   HYDROData_ShapeFile aSHPFile;
-  aSHPFile.DBF_OpenDBF(aDBFPath);
+  CPPUNIT_ASSERT_EQUAL(true, aSHPFile.DBF_OpenDBF(aDBFPath));
 
   QStringList FL = aSHPFile.DBF_GetFieldList();
   CPPUNIT_ASSERT_EQUAL(true, "NAME" == FL[0]);
@@ -58,7 +58,7 @@ void test_HYDROData_ShapeFile::test_FieldTypeListDbf()
 {
   QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
   HYDROData_ShapeFile aSHPFile;
-  aSHPFile.DBF_OpenDBF(aDBFPath);
+  CPPUNIT_ASSERT_EQUAL(true, aSHPFile.DBF_OpenDBF(aDBFPath));
 
   std::vector<HYDROData_ShapeFile::DBF_FieldType> FTVect;
   aSHPFile.DBF_GetFieldTypeList(FTVect);
@@ -67,5 +67,70 @@ void test_HYDROData_ShapeFile::test_FieldTypeListDbf()
   aSHPFile.DBF_CloseDBF();
 }
 
+void test_HYDROData_ShapeFile::test_NbRecordsDbf()
+{
+  QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
+  HYDROData_ShapeFile aSHPFile;
+  CPPUNIT_ASSERT_EQUAL(true, aSHPFile.DBF_OpenDBF(aDBFPath));
+  int NbR = aSHPFile.DBF_GetNbRecords();
+  CPPUNIT_ASSERT_EQUAL( 268, NbR );  
+  aSHPFile.DBF_CloseDBF();
+}
+
+void test_HYDROData_ShapeFile::test_GetAttrListIndex()
+{
+  { //cyprus_natural
+    QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
+    HYDROData_ShapeFile aSHPFile;
+    CPPUNIT_ASSERT_EQUAL(true, aSHPFile.DBF_OpenDBF(aDBFPath));
+    std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
+    aSHPFile.DBF_GetAttributeList(0, theAttrV ); //get all records take from the first field
 
+    //Check Values
 
+    //Non-null attr
+    CPPUNIT_ASSERT_EQUAL( 268, (int)theAttrV.size() ); 
+    CPPUNIT_ASSERT_EQUAL( true, std::string("Ovgos Dam") == theAttrV[22].myRawValue);
+    CPPUNIT_ASSERT_EQUAL( true, QString("Ovgos Dam") == theAttrV[22].myStrVal);
+    CPPUNIT_ASSERT_EQUAL( false, theAttrV[22].myIsNull);
+    CPPUNIT_ASSERT_EQUAL( HYDROData_ShapeFile::DBF_FieldType_String, theAttrV[22].myFieldType);
+
+    //Null attr
+    CPPUNIT_ASSERT_EQUAL( true, theAttrV[35].myIsNull);
+    CPPUNIT_ASSERT_EQUAL( true, theAttrV[35].myRawValue.empty());
+    CPPUNIT_ASSERT_EQUAL( HYDROData_ShapeFile::DBF_FieldType_None, theAttrV[35].myFieldType);
+
+    theAttrV.clear();
+    aSHPFile.DBF_GetAttributeList(1, theAttrV ); //second field
+    CPPUNIT_ASSERT_EQUAL( 268, (int)theAttrV.size() ); 
+    CPPUNIT_ASSERT_EQUAL( true, std::string("water") == theAttrV[3].myRawValue);
+    CPPUNIT_ASSERT_EQUAL( true, QString("water") == theAttrV[3].myStrVal);
+    CPPUNIT_ASSERT_EQUAL( false, theAttrV[3].myIsNull);
+    CPPUNIT_ASSERT_EQUAL( HYDROData_ShapeFile::DBF_FieldType_String, theAttrV[3].myFieldType);
+
+    aSHPFile.DBF_CloseDBF();
+  }
+  { //CLC06
+    QString aDBFPath = REF_PATH + "/CLC06.dbf";
+    HYDROData_ShapeFile aSHPFile;
+    CPPUNIT_ASSERT_EQUAL (true, aSHPFile.DBF_OpenDBF(aDBFPath));
+    std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
+
+    aSHPFile.DBF_GetAttributeList(0, theAttrV ); 
+    CPPUNIT_ASSERT_EQUAL( 3269, (int)theAttrV.size() ); 
+    CPPUNIT_ASSERT_EQUAL( true, std::string("FR-184045") == theAttrV[2].myRawValue);
+    CPPUNIT_ASSERT_EQUAL( true, QString("FR-184045") == theAttrV[2].myStrVal);
+    CPPUNIT_ASSERT_EQUAL( false, theAttrV[2].myIsNull);
+    CPPUNIT_ASSERT_EQUAL( HYDROData_ShapeFile::DBF_FieldType_String, theAttrV[2].myFieldType);
+    //
+    theAttrV.clear();
+    aSHPFile.DBF_GetAttributeList(2, theAttrV ); 
+    CPPUNIT_ASSERT_EQUAL( 3269, (int)theAttrV.size() ); 
+    CPPUNIT_ASSERT_EQUAL( true, std::string("6621.1654324936000000000000000000") == theAttrV[2].myRawValue);
+    CPPUNIT_ASSERT_EQUAL( true, QString("6621.1654324935998375") == theAttrV[2].myStrVal);
+    CPPUNIT_ASSERT_EQUAL( false, theAttrV[2].myIsNull);
+    CPPUNIT_ASSERT_EQUAL( HYDROData_ShapeFile::DBF_FieldType_Double, theAttrV[2].myFieldType);
+
+    aSHPFile.DBF_CloseDBF();
+  }
+}
index 6c3739a5730fbb97c81f7b742dc490d49fc7f03e..8a56ab9fc444fbe49b8ee633897b17bd05fb88c5 100644 (file)
@@ -24,6 +24,8 @@ class test_HYDROData_ShapeFile : public CppUnit::TestFixture
   CPPUNIT_TEST( test_NbFieldsDbf );     
   CPPUNIT_TEST( test_FieldListDbf );
   CPPUNIT_TEST( test_FieldTypeListDbf );
+  CPPUNIT_TEST( test_NbRecordsDbf );
+  CPPUNIT_TEST( test_GetAttrListIndex );
 
   CPPUNIT_TEST_SUITE_END();
 
@@ -32,6 +34,8 @@ public:
   void test_NbFieldsDbf();
   void test_FieldListDbf();
   void test_FieldTypeListDbf();
+  void test_NbRecordsDbf();
+  void test_GetAttrListIndex ();
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION( test_HYDROData_ShapeFile );