Salome HOME
merge BR_LAND_COVER_MAP
[modules/hydro.git] / src / HYDRO_tests / test_HYDROData_ShapeFile.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <test_HYDROData_ShapeFile.h>
20 #include <HYDROData_ShapeFile.h>
21 #include <QStringList>
22 #include <vector>
23
24 const QString REF_PATH = qgetenv( "HYDRO_REFERENCE_DATA" );
25
26 bool test_HYDROData_ShapeFile::compare_two_files(const QString& File1, const QString& File2)
27 {
28   FILE *fp1, *fp2;
29   fp1 = fopen(File1.toStdString().c_str(), "r");
30   fp2 = fopen(File2.toStdString().c_str(), "r");
31   CPPUNIT_ASSERT(fp1);
32   CPPUNIT_ASSERT(fp2);
33   char ch1, ch2;
34   while (((ch1 = fgetc(fp1)) != EOF) &&((ch2 = fgetc(fp2)) != EOF))
35   {
36     if (!ch1 == ch2)
37     {
38       fclose(fp1);
39       fclose(fp2);
40       return false;
41     }
42   }
43
44   fclose(fp1);
45   fclose(fp2);
46   return true;
47 }
48
49 void test_HYDROData_ShapeFile::test_openDbf()
50 {
51   QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
52   HYDROData_ShapeFile aSHPFile;
53   bool Stat = aSHPFile.DBF_OpenDBF(aDBFPath);
54   CPPUNIT_ASSERT_EQUAL( true, Stat );  
55   aSHPFile.DBF_CloseDBF();
56 }
57
58 void test_HYDROData_ShapeFile::test_NbFieldsDbf()
59 {
60   QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
61   HYDROData_ShapeFile aSHPFile;
62   CPPUNIT_ASSERT_EQUAL(true, aSHPFile.DBF_OpenDBF(aDBFPath));
63   int NbF = aSHPFile.DBF_GetNbFields();
64   CPPUNIT_ASSERT_EQUAL( 2, NbF );  
65   aSHPFile.DBF_CloseDBF();
66 }
67
68 void test_HYDROData_ShapeFile::test_FieldListDbf()
69 {
70   QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
71   HYDROData_ShapeFile aSHPFile;
72   CPPUNIT_ASSERT_EQUAL(true, aSHPFile.DBF_OpenDBF(aDBFPath));
73
74   QStringList FL = aSHPFile.DBF_GetFieldList();
75   CPPUNIT_ASSERT_EQUAL(true, "NAME" == FL[0]);
76   CPPUNIT_ASSERT_EQUAL(true, "TYPE" == FL[1]);
77   aSHPFile.DBF_CloseDBF();
78 }
79
80 void test_HYDROData_ShapeFile::test_FieldTypeListDbf()
81 {
82   QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
83   HYDROData_ShapeFile aSHPFile;
84   CPPUNIT_ASSERT_EQUAL(true, aSHPFile.DBF_OpenDBF(aDBFPath));
85
86   std::vector<HYDROData_ShapeFile::DBF_FieldType> FTVect;
87   aSHPFile.DBF_GetFieldTypeList(FTVect);
88   CPPUNIT_ASSERT_EQUAL(HYDROData_ShapeFile::DBF_FieldType_String, FTVect[0]);
89   CPPUNIT_ASSERT_EQUAL(HYDROData_ShapeFile::DBF_FieldType_String, FTVect[1]);
90   aSHPFile.DBF_CloseDBF();
91 }
92
93 void test_HYDROData_ShapeFile::test_NbRecordsDbf()
94 {
95   QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
96   HYDROData_ShapeFile aSHPFile;
97   CPPUNIT_ASSERT_EQUAL(true, aSHPFile.DBF_OpenDBF(aDBFPath));
98   int NbR = aSHPFile.DBF_GetNbRecords();
99   CPPUNIT_ASSERT_EQUAL( 268, NbR );  
100   aSHPFile.DBF_CloseDBF();
101 }
102
103 void test_HYDROData_ShapeFile::test_GetAttrListIndex()
104 {
105   { //cyprus_natural
106     QString aDBFPath = REF_PATH + "/cyprus_natural.dbf";
107     HYDROData_ShapeFile aSHPFile;
108     CPPUNIT_ASSERT_EQUAL(true, aSHPFile.DBF_OpenDBF(aDBFPath));
109     std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
110     aSHPFile.DBF_GetAttributeList(0, theAttrV ); //get all records take from the first field
111
112     //Check Values
113
114     //Non-null attr
115     CPPUNIT_ASSERT_EQUAL( 268, (int)theAttrV.size() ); 
116     CPPUNIT_ASSERT_EQUAL( true, std::string("Ovgos Dam") == theAttrV[22].myRawValue);
117     CPPUNIT_ASSERT_EQUAL( true, QString("Ovgos Dam") == theAttrV[22].myStrVal);
118     CPPUNIT_ASSERT_EQUAL( false, theAttrV[22].myIsNull);
119     CPPUNIT_ASSERT_EQUAL( HYDROData_ShapeFile::DBF_FieldType_String, theAttrV[22].myFieldType);
120
121     //Null attr
122     CPPUNIT_ASSERT_EQUAL( true, theAttrV[35].myIsNull);
123     CPPUNIT_ASSERT_EQUAL( true, theAttrV[35].myRawValue.empty());
124     CPPUNIT_ASSERT_EQUAL( HYDROData_ShapeFile::DBF_FieldType_String, theAttrV[35].myFieldType);
125
126     theAttrV.clear();
127     aSHPFile.DBF_GetAttributeList(1, theAttrV ); //second field
128     CPPUNIT_ASSERT_EQUAL( 268, (int)theAttrV.size() ); 
129     CPPUNIT_ASSERT_EQUAL( true, std::string("water") == theAttrV[3].myRawValue);
130     CPPUNIT_ASSERT_EQUAL( true, QString("water") == theAttrV[3].myStrVal);
131     CPPUNIT_ASSERT_EQUAL( false, theAttrV[3].myIsNull);
132     CPPUNIT_ASSERT_EQUAL( HYDROData_ShapeFile::DBF_FieldType_String, theAttrV[3].myFieldType);
133
134     aSHPFile.DBF_CloseDBF();
135   }
136   { //CLC06
137     QString aDBFPath = REF_PATH + "/CLC06.dbf";
138     HYDROData_ShapeFile aSHPFile;
139     CPPUNIT_ASSERT_EQUAL (true, aSHPFile.DBF_OpenDBF(aDBFPath));
140     std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
141
142     aSHPFile.DBF_GetAttributeList(0, theAttrV ); 
143     CPPUNIT_ASSERT_EQUAL( 3269, (int)theAttrV.size() ); 
144     CPPUNIT_ASSERT_EQUAL( true, std::string("FR-184045") == theAttrV[2].myRawValue);
145     CPPUNIT_ASSERT_EQUAL( true, QString("FR-184045") == theAttrV[2].myStrVal);
146     CPPUNIT_ASSERT_EQUAL( false, theAttrV[2].myIsNull);
147     CPPUNIT_ASSERT_EQUAL( HYDROData_ShapeFile::DBF_FieldType_String, theAttrV[2].myFieldType);
148     //
149     theAttrV.clear();
150     aSHPFile.DBF_GetAttributeList(2, theAttrV ); 
151     CPPUNIT_ASSERT_EQUAL( 3269, (int)theAttrV.size() ); 
152     CPPUNIT_ASSERT_EQUAL( true, std::string("6621.1654324936000000000000000000") == theAttrV[2].myRawValue);
153     CPPUNIT_ASSERT_EQUAL( 6621.1654324935998375, theAttrV[2].myDoubleVal);
154     CPPUNIT_ASSERT_EQUAL( false, theAttrV[2].myIsNull);
155     CPPUNIT_ASSERT_EQUAL( HYDROData_ShapeFile::DBF_FieldType_Double, theAttrV[2].myFieldType);
156
157     aSHPFile.DBF_CloseDBF();
158   }
159 }
160
161
162 void test_HYDROData_ShapeFile::test_DbfWrite()
163 {
164   {
165     HYDROData_ShapeFile aSHPFile; 
166     QString tempFN = REF_PATH + "/temp_dbf1.dbf";
167     QString refFN = REF_PATH + "/ref_dbf1.dbf";
168     std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
169     HYDROData_ShapeFile::DBF_AttrValue theAttr1;
170     theAttr1.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String;
171     theAttr1.myIsNull = false;
172     theAttr1.myRawValue = "test_value1";
173     theAttr1.myStrVal = "test_value1";
174     theAttrV.push_back(theAttr1);
175     HYDROData_ShapeFile::DBF_AttrValue theAttr2;
176     theAttr2.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String;
177     theAttr2.myIsNull = false;
178     theAttr2.myRawValue = "test_value2";
179     theAttr2.myStrVal = "test_value2";
180     theAttrV.push_back(theAttr2);
181     //
182     CPPUNIT_ASSERT(aSHPFile.DBF_WriteFieldAndValues(tempFN, "test_field_name", HYDROData_ShapeFile::DBF_FieldType_String, theAttrV, true));
183
184     CPPUNIT_ASSERT(compare_two_files(tempFN, refFN));
185     CPPUNIT_ASSERT_EQUAL( 0, remove (tempFN.toStdString().c_str()));
186   }
187
188   {
189     HYDROData_ShapeFile aSHPFile; 
190     QString tempFN = REF_PATH + "/temp_dbf2.dbf";
191     QString refFN = REF_PATH + "/ref_dbf2.dbf";
192     std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
193     HYDROData_ShapeFile::DBF_AttrValue theAttr1;
194     theAttr1.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String;
195     theAttr1.myIsNull = false;
196     theAttr1.myRawValue = "test_value1";
197     theAttr1.myStrVal = "test_value1";
198     theAttrV.push_back(theAttr1);
199
200     HYDROData_ShapeFile::DBF_AttrValue theAttr2;
201     theAttr2.myIsNull = false;
202     theAttr2.myRawValue = "test_value_K";
203     theAttr2.myStrVal = "test_value_K";
204     theAttr2.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String;
205     for (int ii = 0; ii < 100; ii++)
206       theAttrV.push_back(theAttr2);
207
208     HYDROData_ShapeFile::DBF_AttrValue theAttr3;
209     theAttr3.myIsNull = true;
210     theAttr3.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String;
211     for (int ii = 0; ii < 100; ii++)
212       theAttrV.push_back(theAttr3);
213     //
214     CPPUNIT_ASSERT(aSHPFile.DBF_WriteFieldAndValues(tempFN, "test_field_name", HYDROData_ShapeFile::DBF_FieldType_String, theAttrV, true));
215
216     CPPUNIT_ASSERT(compare_two_files(tempFN, refFN));
217     CPPUNIT_ASSERT_EQUAL( 0, remove (tempFN.toStdString().c_str()));
218   }
219
220   {
221     HYDROData_ShapeFile aSHPFile; 
222     QString tempFN = REF_PATH + "/temp_dbf3.dbf";
223     QString refFN = REF_PATH + "/ref_dbf3.dbf";
224     std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
225     HYDROData_ShapeFile::DBF_AttrValue theAttr1;
226     theAttr1.myFieldType = HYDROData_ShapeFile::DBF_FieldType_Integer;
227     theAttr1.myIsNull = false;
228
229     for (int ii = 0; ii < 100; ii++)
230     {
231       theAttr1.myIntVal = ii;
232       theAttrV.push_back(theAttr1);
233     }
234
235     //
236     CPPUNIT_ASSERT(aSHPFile.DBF_WriteFieldAndValues(tempFN, "test_field_name", HYDROData_ShapeFile::DBF_FieldType_Integer, theAttrV, false));
237
238     CPPUNIT_ASSERT(compare_two_files(tempFN, refFN));
239     CPPUNIT_ASSERT_EQUAL( 0, remove (tempFN.toStdString().c_str()));
240   }
241
242 }
243