From: isn Date: Thu, 15 Oct 2015 13:18:12 +0000 (+0300) Subject: DBF Reader #1 X-Git-Tag: v1.5~89^2~5 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=aaf4ecf52eae47da4da3cfa523672526787018f8;p=modules%2Fhydro.git DBF Reader #1 --- diff --git a/src/HYDROData/HYDROData_ShapeFile.cxx b/src/HYDROData/HYDROData_ShapeFile.cxx index 8cf702f0..87d9bc2e 100644 --- a/src/HYDROData/HYDROData_ShapeFile.cxx +++ b/src/HYDROData/HYDROData_ShapeFile.cxx @@ -716,3 +716,66 @@ void HYDROData_ShapeFile::DBF_GetFieldTypeList(std::vector& FTVec } +int HYDROData_ShapeFile::DBF_GetNbRecords() +{ + if(myHDBF == NULL) + return 0; + return DBFGetRecordCount(myHDBF); +} + +void HYDROData_ShapeFile::DBF_GetAttributeList(int theIndexOfField, std::vector& 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 diff --git a/src/HYDROData/HYDROData_ShapeFile.h b/src/HYDROData/HYDROData_ShapeFile.h index 0f1cd3d2..11517a66 100644 --- a/src/HYDROData/HYDROData_ShapeFile.h +++ b/src/HYDROData/HYDROData_ShapeFile.h @@ -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& FTVect); + int DBF_GetNbRecords(); void DBF_CloseDBF(); + void DBF_GetAttributeList(int theIndexOfField, std::vector& 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 index 00000000..cd89cb97 --- /dev/null +++ b/src/HYDRO_tests/reference_data/CLC06.cst @@ -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 index 00000000..26b01890 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 index 00000000..552fec7e --- /dev/null +++ b/src/HYDRO_tests/reference_data/CLC06.prj @@ -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 index 00000000..ed5a8d08 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 index 00000000..297e9429 Binary files /dev/null and b/src/HYDRO_tests/reference_data/CLC06.shx differ diff --git a/src/HYDRO_tests/test_HYDROData_ShapeFile.cxx b/src/HYDRO_tests/test_HYDROData_ShapeFile.cxx index 0edbb457..035d6914 100644 --- a/src/HYDRO_tests/test_HYDROData_ShapeFile.cxx +++ b/src/HYDRO_tests/test_HYDROData_ShapeFile.cxx @@ -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 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 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 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(); + } +} diff --git a/src/HYDRO_tests/test_HYDROData_ShapeFile.h b/src/HYDRO_tests/test_HYDROData_ShapeFile.h index 6c3739a5..8a56ab9f 100644 --- a/src/HYDRO_tests/test_HYDROData_ShapeFile.h +++ b/src/HYDRO_tests/test_HYDROData_ShapeFile.h @@ -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 );