Skip to content

Commit 1e4df2d

Browse files
author
timlinux
committedSep 17, 2007
Added line and polygon tests for filewriter and projected writing test. Fixed cmake so tests run properly on mac - still need ot test on other osses.
git-svn-id: http://svn.osgeo.org/qgis/trunk@7207 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent b88da4f commit 1e4df2d

File tree

2 files changed

+398
-67
lines changed

2 files changed

+398
-67
lines changed
 

‎tests/src/core/CMakeLists.txt

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,13 @@ ADD_CUSTOM_TARGET(applicationtestmoc ALL DEPENDS ${applicationtest_MOC_SRCS})
6262
ADD_EXECUTABLE(applicationtest ${applicationtest_SRCS})
6363
ADD_DEPENDENCIES(applicationtest applicationtestmoc)
6464
TARGET_LINK_LIBRARIES(applicationtest ${QT_LIBRARIES} qgis_core)
65-
INSTALL(TARGETS applicationtest RUNTIME DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
66-
ADD_TEST(application ${CMAKE_CURRENT_BINARY_DIR}/applicationtest)
67-
65+
IF (APPLE)
66+
INSTALL(TARGETS applicationtest RUNTIME DESTINATION ${QGIS_BIN_DIR}/../)
67+
ADD_TEST(application ${QGIS_BIN_DIR}/../applicationtest)
68+
ELSE (APPLE)
69+
INSTALL(TARGETS applicationtest RUNTIME DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
70+
ADD_TEST(application ${CMAKE_CURRENT_BINARY_DIR}/applicationtest)
71+
ENDIF (APPLE)
6872
#
6973
# QgsFileWriter test
7074
#
@@ -75,8 +79,12 @@ ADD_CUSTOM_TARGET(filewritertestmoc ALL DEPENDS ${filewritertest_MOC_SRCS})
7579
ADD_EXECUTABLE(filewritertest ${filewritertest_SRCS})
7680
ADD_DEPENDENCIES(filewritertest filewritertestmoc)
7781
TARGET_LINK_LIBRARIES(filewritertest ${QT_LIBRARIES} qgis_core)
78-
INSTALL(TARGETS filewritertest RUNTIME DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
79-
ADD_TEST(filewriter ${CMAKE_CURRENT_BINARY_DIR}/filewritertest)
80-
82+
IF (APPLE)
83+
INSTALL(TARGETS filewritertest RUNTIME DESTINATION ${QGIS_BIN_DIR}/../)
84+
ADD_TEST(filewriter ${QGIS_BIN_DIR}/../filewritertest)
85+
ELSE (APPLE)
86+
INSTALL(TARGETS filewritertest RUNTIME DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
87+
ADD_TEST(filewriter ${CMAKE_CURRENT_BINARY_DIR}/filewritertest)
88+
ENDIF (APPLE)
8189

8290

‎tests/src/core/testqgsvectorfilewriter.cpp

Lines changed: 384 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -31,88 +31,411 @@
3131
#include <qgsfield.h>
3232
#include <qgis.h> //defines GEOWKT
3333

34+
/** \ingroup UnitTests
35+
* This is a unit test for the QgsVectorFileWriter class.
36+
*
37+
* Possible QVariant::Type s
38+
* QVariant::String
39+
* QVariant::Int
40+
* QVariant::Double
41+
*
42+
* Allowed ogr prvider typeNames:
43+
* Integer
44+
* Real
45+
* String
46+
*
47+
* Constructor for QgsField:
48+
* QgsField::QgsField(QString name,
49+
* QVariant::Type type,
50+
* QString typeName,
51+
* int len,
52+
* int prec,
53+
* QString comment)
54+
*/
3455
class TestQgsVectorFileWriter: public QObject
3556
{
3657
Q_OBJECT;
3758
private slots:
38-
void createVectorFile()
59+
void initTestCase();// will be called before the first testfunction is executed.
60+
void cleanupTestCase(){};// will be called after the last testfunction was executed.
61+
void init(){};// will be called before each testfunction is executed.
62+
void cleanup(){};// will be called after every testfunction.
63+
64+
/** This method tests writing a point to a shapefile */
65+
void createPoint();
66+
/** This method tests writing a polyline to a shapefile */
67+
void createLine();
68+
/** This method tests writing a polygon to a shapefile */
69+
void createPolygon();
70+
/** This method test writing multiple features to a shapefile */
71+
void polygonGridTest();
72+
/** As above but using a projected SRS*/
73+
void projectedPlygonGridTest();
74+
75+
private:
76+
// a little util fn used by all tests
77+
bool cleanupFile(QString theFileBase);
78+
QString mEncoding;
79+
QgsVectorFileWriter::WriterError mError;
80+
QgsSpatialRefSys mSRS;
81+
QgsFieldMap mFields;
82+
QgsPoint mPoint1;
83+
QgsPoint mPoint2;
84+
QgsPoint mPoint3;
85+
};
86+
87+
void TestQgsVectorFileWriter::initTestCase()
88+
{
89+
qDebug("\n\n **************\n"
90+
"Note: if you get a message like \n"
91+
"ERROR 1: /tmp/testpt.shp is not a directory.\n"
92+
"It is caused by the /tmp/testshp.* files already existing\n"
93+
"(the ERROR comes from OGR and is not very intuitive)\n"
94+
"******************\n");
95+
// init QGIS's paths - true means that all path will be inited from prefix
96+
QString qgisPath = QCoreApplication::applicationDirPath ();
97+
QgsApplication::setPrefixPath(qgisPath, TRUE);
98+
//create some objects that will be used in all tests...
99+
100+
std::cout << "Prefix PATH: " << QgsApplication::prefixPath().toLocal8Bit().data() << std::endl;
101+
std::cout << "Plugin PATH: " << QgsApplication::pluginPath().toLocal8Bit().data() << std::endl;
102+
std::cout << "PkgData PATH: " << QgsApplication::pkgDataPath().toLocal8Bit().data() << std::endl;
103+
std::cout << "User DB PATH: " << QgsApplication::qgisUserDbFilePath().toLocal8Bit().data() << std::endl;
104+
105+
mEncoding = "UTF-8";
106+
QgsField myField1("Field1",QVariant::String,"String",10,0,"Field 1 comment");
107+
mFields.insert(0, myField1);
108+
mSRS = QgsSpatialRefSys(GEOWKT);
109+
mPoint1 = QgsPoint(10.0,10.0);
110+
mPoint2 = QgsPoint(15.0,10.0);
111+
mPoint3 = QgsPoint(15.0,12.0);
112+
}
113+
114+
115+
void TestQgsVectorFileWriter::createPoint()
116+
{
117+
118+
//
119+
// Remove old copies that may be lying around
120+
//
121+
QString myFileBase = "/tmp/testpt";
122+
QVERIFY(cleanupFile(myFileBase));
123+
QString myFileName = myFileBase + ".shp";
124+
QgsVectorFileWriter myWriter (myFileName,
125+
mEncoding,
126+
mFields,
127+
QGis::WKBPoint,
128+
&mSRS);
129+
//
130+
// Create a feature
131+
//
132+
//
133+
// NOTE: dont delete this pointer again -
134+
// ownership is passed to the feature which will
135+
// delete it in its dtor!
136+
QgsGeometry * mypPointGeometry = QgsGeometry::fromPoint(mPoint1);
137+
QgsFeature myFeature;
138+
myFeature.setGeometry(mypPointGeometry);
139+
myFeature.addAttribute(0,"HelloWorld");
140+
//
141+
// Write the feature to the filewriter
142+
// and check for errors
143+
//
144+
QVERIFY(myWriter.addFeature(myFeature));
145+
mError = myWriter.hasError();
146+
if(mError==QgsVectorFileWriter::ErrDriverNotFound)
147+
{
148+
std::cout << "Driver not found error" << std::endl;
149+
}
150+
else if (mError==QgsVectorFileWriter::ErrCreateDataSource)
151+
{
152+
std::cout << "Create data source error" << std::endl;
153+
}
154+
else if (mError==QgsVectorFileWriter::ErrCreateLayer)
155+
{
156+
std::cout << "Create layer error" << std::endl;
157+
}
158+
QVERIFY(mError==QgsVectorFileWriter::NoError);
159+
}
160+
161+
void TestQgsVectorFileWriter::createLine()
162+
{
163+
//
164+
// Remove old copies that may be lying around
165+
//
166+
QString myFileBase = "/tmp/testln";
167+
QVERIFY(cleanupFile(myFileBase));
168+
QString myFileName = myFileBase + ".shp";
169+
QgsVectorFileWriter myWriter (myFileName,
170+
mEncoding,
171+
mFields,
172+
QGis::WKBLineString,
173+
&mSRS);
174+
//
175+
// Create a feature
176+
//
177+
QgsPolyline myPolyline;
178+
myPolyline << mPoint1 << mPoint2 << mPoint3;
179+
//
180+
// NOTE: dont delete this pointer again -
181+
// ownership is passed to the feature which will
182+
// delete it in its dtor!
183+
QgsGeometry * mypLineGeometry = QgsGeometry::fromPolyline(myPolyline);
184+
QgsFeature myFeature;
185+
myFeature.setTypeName("WKBLineString");
186+
myFeature.setGeometry(mypLineGeometry);
187+
myFeature.addAttribute(0,"HelloWorld");
188+
//
189+
// Write the feature to the filewriter
190+
// and check for errors
191+
//
192+
QVERIFY(myWriter.addFeature(myFeature));
193+
mError = myWriter.hasError();
194+
if(mError==QgsVectorFileWriter::ErrDriverNotFound)
195+
{
196+
std::cout << "Driver not found error" << std::endl;
197+
}
198+
else if (mError==QgsVectorFileWriter::ErrCreateDataSource)
199+
{
200+
std::cout << "Create data source error" << std::endl;
201+
}
202+
else if (mError==QgsVectorFileWriter::ErrCreateLayer)
203+
{
204+
std::cout << "Create layer error" << std::endl;
205+
}
206+
QVERIFY(mError==QgsVectorFileWriter::NoError);
207+
}
208+
209+
void TestQgsVectorFileWriter::createPolygon()
210+
{
211+
212+
//
213+
// Remove old copies that may be lying around
214+
//
215+
QString myFileBase = "/tmp/testply";
216+
QVERIFY(cleanupFile(myFileBase));
217+
QString myFileName = myFileBase + ".shp";
218+
QgsVectorFileWriter myWriter (myFileName,
219+
mEncoding,
220+
mFields,
221+
QGis::WKBPolygon,
222+
&mSRS);
223+
//
224+
// Create a polygon feature
225+
//
226+
QgsPolyline myPolyline;
227+
myPolyline << mPoint1 << mPoint2 << mPoint3 << mPoint1;
228+
QgsPolygon myPolygon;
229+
myPolygon << myPolyline;
230+
//polygon: first item of the list is outer ring,
231+
// inner rings (if any) start from second item
232+
//
233+
// NOTE: dont delete this pointer again -
234+
// ownership is passed to the feature which will
235+
// delete it in its dtor!
236+
QgsGeometry * mypPolygonGeometry = QgsGeometry::fromPolygon(myPolygon);
237+
QgsFeature myFeature;
238+
myFeature.setTypeName("WKBPolygon");
239+
myFeature.setGeometry(mypPolygonGeometry);
240+
myFeature.addAttribute(0,"HelloWorld");
241+
//
242+
// Write the feature to the filewriter
243+
// and check for errors
244+
//
245+
QVERIFY(myWriter.addFeature(myFeature));
246+
mError = myWriter.hasError();
247+
if(mError==QgsVectorFileWriter::ErrDriverNotFound)
248+
{
249+
std::cout << "Driver not found error" << std::endl;
250+
}
251+
else if (mError==QgsVectorFileWriter::ErrCreateDataSource)
252+
{
253+
std::cout << "Create data source error" << std::endl;
254+
}
255+
else if (mError==QgsVectorFileWriter::ErrCreateLayer)
256+
{
257+
std::cout << "Create layer error" << std::endl;
258+
}
259+
QVERIFY(mError==QgsVectorFileWriter::NoError);
260+
}
261+
void TestQgsVectorFileWriter::polygonGridTest()
262+
{
263+
//
264+
// Remove old copies that may be lying around
265+
//
266+
QString myFileBase = "/tmp/testgrid";
267+
QVERIFY(cleanupFile(myFileBase));
268+
QString myFileName = myFileBase + ".shp";
269+
QgsVectorFileWriter myWriter (myFileName,
270+
mEncoding,
271+
mFields,
272+
QGis::WKBPolygon,
273+
&mSRS);
274+
double myInterval=5;
275+
for (int i=-180;i<=180;i+=myInterval)
276+
{
277+
for (int j=-90;j<=90;j+=myInterval)
39278
{
40-
qDebug("\n\n **************\n"
41-
"Note: if you get a message like \n"
42-
"ERROR 1: /tmp/testshp.shp is not a directory.\n"
43-
"It is caused by the /tmp/testshp.* files already existing\n"
44-
"(the ERROR comes from OGR and is not very intuitive)\n"
45-
"******************\n");
46-
// init QGIS's paths - true means that all path will be inited from prefix
47-
QString qgisPath = QCoreApplication::applicationDirPath ();
48-
QgsApplication::setPrefixPath(qgisPath, TRUE);
49-
QgsApplication::setPkgDataPath(QgsApplication::applicationDirPath() +
50-
"/../../../");
51-
52-
std::cout << "Prefix PATH: " << QgsApplication::prefixPath().toLocal8Bit().data() << std::endl;
53-
std::cout << "Plugin PATH: " << QgsApplication::pluginPath().toLocal8Bit().data() << std::endl;
54-
std::cout << "PkgData PATH: " << QgsApplication::pkgDataPath().toLocal8Bit().data() << std::endl;
55-
std::cout << "User DB PATH: " << QgsApplication::qgisUserDbFilePath().toLocal8Bit().data() << std::endl;
56-
57-
QString myEncoding("UTF-8");
58-
QString myFileName("/tmp/testshp.shp");
59-
QgsVectorFileWriter::WriterError myError;
60-
61-
// Possible QVariant::Type s
62-
// QVariant::String
63-
// QVariant::Int
64-
// QVariant::Double
65-
//
66-
// Allowed ogr prvider typeNames:
67-
// Integer
68-
// Real
69-
// String
70-
71-
// Constructor for QgsField:
72-
// QgsField::QgsField(QString name,
73-
// QVariant::Type type,
74-
// QString typeName,
75-
// int len,
76-
// int prec,
77-
// QString comment)
78-
QgsField myField1("Field1",QVariant::String,"String",10,0,"Field 1 comment");
79-
QgsFieldMap myFields;
80-
myFields.insert(0, myField1);
81-
QgsSpatialRefSys mySRS(GEOWKT);
82-
QgsVectorFileWriter myWriter (myFileName,
83-
myEncoding,
84-
myFields,
85-
QGis::WKBPoint,
86-
&mySRS);
87279
//
88-
// Create a feature
280+
// Create a polygon feature
281+
//
282+
QgsPolyline myPolyline;
283+
QgsPoint myPoint1 = QgsPoint(static_cast<float>(i),static_cast<float>(j));
284+
QgsPoint myPoint2 = QgsPoint(static_cast<float>(i+myInterval),static_cast<float>(j));
285+
QgsPoint myPoint3 = QgsPoint(static_cast<float>(i+myInterval),static_cast<float>(j+myInterval));
286+
QgsPoint myPoint4 = QgsPoint(static_cast<float>(i),static_cast<float>(j+myInterval));
287+
myPolyline << myPoint1 << myPoint2 << myPoint3 << myPoint4 << myPoint1;
288+
QgsPolygon myPolygon;
289+
myPolygon << myPolyline;
290+
//polygon: first item of the list is outer ring,
291+
// inner rings (if any) start from second item
89292
//
90-
QgsPoint myPoint(10.0,10.0);
91-
QgsGeometry * mypGeometry = QgsGeometry::fromPoint(myPoint);
293+
// NOTE: dont delete this pointer again -
294+
// ownership is passed to the feature which will
295+
// delete it in its dtor!
296+
QgsGeometry * mypPolygonGeometry = QgsGeometry::fromPolygon(myPolygon);
92297
QgsFeature myFeature;
93-
myFeature.setGeometry(mypGeometry);
94-
myFeature.addAttribute(1,"HelloWorld");
298+
myFeature.setTypeName("WKBPolygon");
299+
myFeature.setGeometry(mypPolygonGeometry);
300+
myFeature.addAttribute(0,"HelloWorld");
95301
//
96-
// Write the featyre to the filewriter
302+
// Write the feature to the filewriter
303+
// and check for errors
97304
//
98305
QVERIFY(myWriter.addFeature(myFeature));
99-
myError = myWriter.hasError();
100-
if(myError==QgsVectorFileWriter::ErrDriverNotFound)
306+
mError = myWriter.hasError();
307+
if(mError==QgsVectorFileWriter::ErrDriverNotFound)
101308
{
102309
std::cout << "Driver not found error" << std::endl;
103310
}
104-
else if (myError==QgsVectorFileWriter::ErrCreateDataSource)
311+
else if (mError==QgsVectorFileWriter::ErrCreateDataSource)
105312
{
106313
std::cout << "Create data source error" << std::endl;
107314
}
108-
else if (myError==QgsVectorFileWriter::ErrCreateLayer)
315+
else if (mError==QgsVectorFileWriter::ErrCreateLayer)
109316
{
110317
std::cout << "Create layer error" << std::endl;
111318
}
112-
QVERIFY(myError==QgsVectorFileWriter::NoError);
113-
// other possible outcomes...
319+
QVERIFY(mError==QgsVectorFileWriter::NoError);
114320
}
115-
};
321+
}
322+
}
323+
324+
void TestQgsVectorFileWriter::projectedPlygonGridTest()
325+
{
326+
//
327+
// Remove old copies that may be lying around
328+
//
329+
QString myFileBase = "/tmp/testprjgrid";
330+
QVERIFY(cleanupFile(myFileBase));
331+
QString myFileName = myFileBase + ".shp";
332+
//
333+
// We are testing projected coordinate
334+
// system vector writing to lets use something fun...
335+
// Jamaica National Grid
336+
// QGIS SRSID: 1286
337+
// PostGIS SRID: 24200
338+
// +proj=lcc +lat_1=18 +lat_0=18 +lon_0=-77 +k_0=1 +x_0=250000
339+
// +y_0=150000 +ellps=clrk66 +units=m +no_defs
340+
//
341+
mSRS = QgsSpatialRefSys(1286,QgsSpatialRefSys::QGIS_SRSID);
342+
QgsVectorFileWriter myWriter (myFileName,
343+
mEncoding,
344+
mFields,
345+
QGis::WKBPolygon,
346+
&mSRS);
347+
double myInterval=1000; //1km2
348+
for (int i=0;i<=10000;i+=myInterval) //10km
349+
{
350+
for (int j=0;j<=10000;j+=myInterval)//10km
351+
{
352+
//
353+
// Create a polygon feature
354+
//
355+
QgsPolyline myPolyline;
356+
QgsPoint myPoint1 = QgsPoint(static_cast<float>(i),static_cast<float>(j));
357+
QgsPoint myPoint2 = QgsPoint(static_cast<float>(i+myInterval),static_cast<float>(j));
358+
QgsPoint myPoint3 = QgsPoint(static_cast<float>(i+myInterval),static_cast<float>(j+myInterval));
359+
QgsPoint myPoint4 = QgsPoint(static_cast<float>(i),static_cast<float>(j+myInterval));
360+
myPolyline << myPoint1 << myPoint2 << myPoint3 << myPoint4 << myPoint1;
361+
QgsPolygon myPolygon;
362+
myPolygon << myPolyline;
363+
//polygon: first item of the list is outer ring,
364+
// inner rings (if any) start from second item
365+
//
366+
// NOTE: dont delete this pointer again -
367+
// ownership is passed to the feature which will
368+
// delete it in its dtor!
369+
QgsGeometry * mypPolygonGeometry = QgsGeometry::fromPolygon(myPolygon);
370+
QgsFeature myFeature;
371+
myFeature.setTypeName("WKBPolygon");
372+
myFeature.setGeometry(mypPolygonGeometry);
373+
myFeature.addAttribute(0,"HelloWorld");
374+
//
375+
// Write the feature to the filewriter
376+
// and check for errors
377+
//
378+
QVERIFY(myWriter.addFeature(myFeature));
379+
mError = myWriter.hasError();
380+
if(mError==QgsVectorFileWriter::ErrDriverNotFound)
381+
{
382+
std::cout << "Driver not found error" << std::endl;
383+
}
384+
else if (mError==QgsVectorFileWriter::ErrCreateDataSource)
385+
{
386+
std::cout << "Create data source error" << std::endl;
387+
}
388+
else if (mError==QgsVectorFileWriter::ErrCreateLayer)
389+
{
390+
std::cout << "Create layer error" << std::endl;
391+
}
392+
QVERIFY(mError==QgsVectorFileWriter::NoError);
393+
}
394+
}
395+
}
396+
bool TestQgsVectorFileWriter::cleanupFile(QString theFileBase)
397+
{
398+
//
399+
// Remove old copies that may be lying around
400+
//
401+
QFileInfo myInfo(theFileBase + ".shp");
402+
if (myInfo.exists())
403+
{
404+
if(!QFile::remove(theFileBase + ".shp"))
405+
{
406+
qDebug("Removing file failed : " + theFileBase.toLocal8Bit() + ".shp");
407+
return false;
408+
}
409+
}
410+
myInfo.setFile(theFileBase + ".shx");
411+
if (myInfo.exists())
412+
{
413+
if(!QFile::remove(theFileBase + ".shx"));
414+
{
415+
qDebug("Removing file failed : " + theFileBase.toLocal8Bit() + ".shx");
416+
return false;
417+
}
418+
}
419+
myInfo.setFile(theFileBase + ".dbf");
420+
if (myInfo.exists())
421+
{
422+
if(!QFile::remove(theFileBase + ".dbf"));
423+
{
424+
qDebug("Removing file failed : " + theFileBase.toLocal8Bit() + ".dbf");
425+
return false;
426+
}
427+
}
428+
myInfo.setFile(theFileBase + ".prj");
429+
if (myInfo.exists())
430+
{
431+
if(!QFile::remove(theFileBase + ".prj"));
432+
{
433+
qDebug("Removing file failed : " + theFileBase.toLocal8Bit() + ".prj");
434+
return false;
435+
}
436+
}
437+
return true;
438+
}
116439

117440
QTEST_MAIN(TestQgsVectorFileWriter)
118441
#include "moc_testqgsvectorfilewriter.cxx"

0 commit comments

Comments
 (0)
Please sign in to comment.