-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
testqgsvectorfilewriter.cpp
394 lines (378 loc) · 12.5 KB
/
testqgsvectorfilewriter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/***************************************************************************
testqgsvectorfilewriter.cpp
--------------------------------------
Date : Sun Sep 16 12:22:54 AKDT 2007
Copyright : (C) 2007 by Gary E. Sherman
Email : sherman at mrcc dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <QtTest>
#include <QObject>
#include <QString>
#include <QStringList>
#include <QObject>
#include <iostream>
#include <QApplication>
#include <qgsvectorlayer.h> //defines QgsFieldMap
#include <qgsvectorfilewriter.h> //logic for writing shpfiles
#include <qgsfeature.h> //we will need to pass a bunch of these for each rec
#include <qgsgeometry.h> //each feature needs a geometry
#include <qgspoint.h> //we will use point geometry
#include <qgsspatialrefsys.h> //needed for creating a srs
#include <qgsapplication.h> //search path for srs.db
#include <qgsfield.h>
#include <qgis.h> //defines GEOWKT
/** \ingroup UnitTests
* This is a unit test for the QgsVectorFileWriter class.
*
* Possible QVariant::Type s
* QVariant::String
* QVariant::Int
* QVariant::Double
*
* Allowed ogr prvider typeNames:
* Integer
* Real
* String
*
* Constructor for QgsField:
* QgsField::QgsField(QString name,
* QVariant::Type type,
* QString typeName,
* int len,
* int prec,
* QString comment)
*/
class TestQgsVectorFileWriter: public QObject
{
Q_OBJECT;
private slots:
void initTestCase();// will be called before the first testfunction is executed.
void init(){};// will be called before each testfunction is executed.
void cleanup(){};// will be called after every testfunction.
/** This method tests writing a point to a shapefile */
void createPoint();
/** This method tests writing a polyline to a shapefile */
void createLine();
/** This method tests writing a polygon to a shapefile */
void createPolygon();
/** This method test writing multiple features to a shapefile */
void polygonGridTest();
/** As above but using a projected SRS*/
void projectedPlygonGridTest();
private:
// a little util fn used by all tests
bool cleanupFile(QString theFileBase);
QString mEncoding;
QgsVectorFileWriter::WriterError mError;
QgsSpatialRefSys mSRS;
QgsFieldMap mFields;
QgsPoint mPoint1;
QgsPoint mPoint2;
QgsPoint mPoint3;
};
void TestQgsVectorFileWriter::initTestCase()
{
qDebug("\n\n **************\n"
"Note: if you get a message like \n"
"ERROR 1: /tmp/testpt.shp is not a directory.\n"
"It is caused by the /tmp/testshp.* files already existing\n"
"(the ERROR comes from OGR and is not very intuitive)\n"
"******************\n");
// init QGIS's paths - true means that all path will be inited from prefix
QString qgisPath = QCoreApplication::applicationDirPath ();
QgsApplication::setPrefixPath(INSTALL_PREFIX, true);
QgsApplication::showSettings();
//create some objects that will be used in all tests...
mEncoding = "UTF-8";
QgsField myField1("Field1",QVariant::String,"String",10,0,"Field 1 comment");
mFields.insert(0, myField1);
mSRS = QgsSpatialRefSys(GEOWKT);
mPoint1 = QgsPoint(10.0,10.0);
mPoint2 = QgsPoint(15.0,10.0);
mPoint3 = QgsPoint(15.0,12.0);
}
void TestQgsVectorFileWriter::createPoint()
{
//
// Remove old copies that may be lying around
//
QString myFileName = "/testpt.shp";
myFileName = QDir::tempPath() + myFileName;
QVERIFY(QgsVectorFileWriter::deleteShapeFile(myFileName));
QgsVectorFileWriter myWriter (myFileName,
mEncoding,
mFields,
QGis::WKBPoint,
&mSRS);
//
// Create a feature
//
//
// NOTE: dont delete this pointer again -
// ownership is passed to the feature which will
// delete it in its dtor!
QgsGeometry * mypPointGeometry = QgsGeometry::fromPoint(mPoint1);
QgsFeature myFeature;
myFeature.setGeometry(mypPointGeometry);
myFeature.addAttribute(0,"HelloWorld");
//
// Write the feature to the filewriter
// and check for errors
//
QVERIFY(myWriter.addFeature(myFeature));
mError = myWriter.hasError();
if(mError==QgsVectorFileWriter::ErrDriverNotFound)
{
std::cout << "Driver not found error" << std::endl;
}
else if (mError==QgsVectorFileWriter::ErrCreateDataSource)
{
std::cout << "Create data source error" << std::endl;
}
else if (mError==QgsVectorFileWriter::ErrCreateLayer)
{
std::cout << "Create layer error" << std::endl;
}
QVERIFY(mError==QgsVectorFileWriter::NoError);
}
void TestQgsVectorFileWriter::createLine()
{
//
// Remove old copies that may be lying around
//
QString myFileName = "/testln.shp";
myFileName = QDir::tempPath() + myFileName;
QVERIFY(QgsVectorFileWriter::deleteShapeFile(myFileName));
QgsVectorFileWriter myWriter (myFileName,
mEncoding,
mFields,
QGis::WKBLineString,
&mSRS);
//
// Create a feature
//
QgsPolyline myPolyline;
myPolyline << mPoint1 << mPoint2 << mPoint3;
//
// NOTE: dont delete this pointer again -
// ownership is passed to the feature which will
// delete it in its dtor!
QgsGeometry * mypLineGeometry = QgsGeometry::fromPolyline(myPolyline);
QgsFeature myFeature;
myFeature.setTypeName("WKBLineString");
myFeature.setGeometry(mypLineGeometry);
myFeature.addAttribute(0,"HelloWorld");
//
// Write the feature to the filewriter
// and check for errors
//
QVERIFY(myWriter.addFeature(myFeature));
mError = myWriter.hasError();
if(mError==QgsVectorFileWriter::ErrDriverNotFound)
{
std::cout << "Driver not found error" << std::endl;
}
else if (mError==QgsVectorFileWriter::ErrCreateDataSource)
{
std::cout << "Create data source error" << std::endl;
}
else if (mError==QgsVectorFileWriter::ErrCreateLayer)
{
std::cout << "Create layer error" << std::endl;
}
QVERIFY(mError==QgsVectorFileWriter::NoError);
}
void TestQgsVectorFileWriter::createPolygon()
{
//
// Remove old copies that may be lying around
//
QString myFileName = "/testply.shp";
myFileName = QDir::tempPath() + myFileName;
QVERIFY(QgsVectorFileWriter::deleteShapeFile(myFileName));
QgsVectorFileWriter myWriter (myFileName,
mEncoding,
mFields,
QGis::WKBPolygon,
&mSRS);
//
// Create a polygon feature
//
QgsPolyline myPolyline;
myPolyline << mPoint1 << mPoint2 << mPoint3 << mPoint1;
QgsPolygon myPolygon;
myPolygon << myPolyline;
//polygon: first item of the list is outer ring,
// inner rings (if any) start from second item
//
// NOTE: dont delete this pointer again -
// ownership is passed to the feature which will
// delete it in its dtor!
QgsGeometry * mypPolygonGeometry = QgsGeometry::fromPolygon(myPolygon);
QgsFeature myFeature;
myFeature.setTypeName("WKBPolygon");
myFeature.setGeometry(mypPolygonGeometry);
myFeature.addAttribute(0,"HelloWorld");
//
// Write the feature to the filewriter
// and check for errors
//
QVERIFY(myWriter.addFeature(myFeature));
mError = myWriter.hasError();
if(mError==QgsVectorFileWriter::ErrDriverNotFound)
{
std::cout << "Driver not found error" << std::endl;
}
else if (mError==QgsVectorFileWriter::ErrCreateDataSource)
{
std::cout << "Create data source error" << std::endl;
}
else if (mError==QgsVectorFileWriter::ErrCreateLayer)
{
std::cout << "Create layer error" << std::endl;
}
QVERIFY(mError==QgsVectorFileWriter::NoError);
}
void TestQgsVectorFileWriter::polygonGridTest()
{
//
// Remove old copies that may be lying around
//
QString myFileName = "/testgrid.shp";
myFileName = QDir::tempPath() + myFileName;
QVERIFY(QgsVectorFileWriter::deleteShapeFile(myFileName));
QgsVectorFileWriter myWriter (myFileName,
mEncoding,
mFields,
QGis::WKBPolygon,
&mSRS);
double myInterval=5.0;
for (double i=-180.0;i<=180.0;i+=myInterval)
{
for (double j=-90.0;j<=90.0;j+=myInterval)
{
//
// Create a polygon feature
//
QgsPolyline myPolyline;
QgsPoint myPoint1 = QgsPoint(i,j);
QgsPoint myPoint2 = QgsPoint(i+myInterval,j);
QgsPoint myPoint3 = QgsPoint(i+myInterval,j+myInterval);
QgsPoint myPoint4 = QgsPoint(i,j+myInterval);
myPolyline << myPoint1 << myPoint2 << myPoint3 << myPoint4 << myPoint1;
QgsPolygon myPolygon;
myPolygon << myPolyline;
//polygon: first item of the list is outer ring,
// inner rings (if any) start from second item
//
// NOTE: dont delete this pointer again -
// ownership is passed to the feature which will
// delete it in its dtor!
QgsGeometry * mypPolygonGeometry = QgsGeometry::fromPolygon(myPolygon);
QgsFeature myFeature;
myFeature.setTypeName("WKBPolygon");
myFeature.setGeometry(mypPolygonGeometry);
myFeature.addAttribute(0,"HelloWorld");
//
// Write the feature to the filewriter
// and check for errors
//
QVERIFY(myWriter.addFeature(myFeature));
mError = myWriter.hasError();
if(mError==QgsVectorFileWriter::ErrDriverNotFound)
{
std::cout << "Driver not found error" << std::endl;
}
else if (mError==QgsVectorFileWriter::ErrCreateDataSource)
{
std::cout << "Create data source error" << std::endl;
}
else if (mError==QgsVectorFileWriter::ErrCreateLayer)
{
std::cout << "Create layer error" << std::endl;
}
QVERIFY(mError==QgsVectorFileWriter::NoError);
}
}
}
void TestQgsVectorFileWriter::projectedPlygonGridTest()
{
//
// Remove old copies that may be lying around
//
QString myFileName = "/testprjgrid.shp";
myFileName = QDir::tempPath() + myFileName;
QVERIFY(QgsVectorFileWriter::deleteShapeFile(myFileName));
//
// We are testing projected coordinate
// system vector writing to lets use something fun...
// Jamaica National Grid
// QGIS SRSID: 1286
// PostGIS SRID: 24200
// +proj=lcc +lat_1=18 +lat_0=18 +lon_0=-77 +k_0=1 +x_0=250000
// +y_0=150000 +ellps=clrk66 +units=m +no_defs
//
mSRS = QgsSpatialRefSys(1286,QgsSpatialRefSys::QGIS_SRSID);
QgsVectorFileWriter myWriter (myFileName,
mEncoding,
mFields,
QGis::WKBPolygon,
&mSRS);
double myInterval=1000.0; //1km2
for (double i=0.0;i<=10000.0;i+=myInterval) //10km
{
for (double j=0.0;j<=10000.0;j+=myInterval)//10km
{
//
// Create a polygon feature
//
QgsPolyline myPolyline;
QgsPoint myPoint1 = QgsPoint(i,j);
QgsPoint myPoint2 = QgsPoint(i+myInterval,j);
QgsPoint myPoint3 = QgsPoint(i+myInterval,j+myInterval);
QgsPoint myPoint4 = QgsPoint(i,j+myInterval);
myPolyline << myPoint1 << myPoint2 << myPoint3 << myPoint4 << myPoint1;
QgsPolygon myPolygon;
myPolygon << myPolyline;
//polygon: first item of the list is outer ring,
// inner rings (if any) start from second item
//
// NOTE: dont delete this pointer again -
// ownership is passed to the feature which will
// delete it in its dtor!
QgsGeometry * mypPolygonGeometry = QgsGeometry::fromPolygon(myPolygon);
QgsFeature myFeature;
myFeature.setTypeName("WKBPolygon");
myFeature.setGeometry(mypPolygonGeometry);
myFeature.addAttribute(0,"HelloWorld");
//
// Write the feature to the filewriter
// and check for errors
//
QVERIFY(myWriter.addFeature(myFeature));
mError = myWriter.hasError();
if(mError==QgsVectorFileWriter::ErrDriverNotFound)
{
std::cout << "Driver not found error" << std::endl;
}
else if (mError==QgsVectorFileWriter::ErrCreateDataSource)
{
std::cout << "Create data source error" << std::endl;
}
else if (mError==QgsVectorFileWriter::ErrCreateLayer)
{
std::cout << "Create layer error" << std::endl;
}
QVERIFY(mError==QgsVectorFileWriter::NoError);
}
}
}
QTEST_MAIN(TestQgsVectorFileWriter)
#include "moc_testqgsvectorfilewriter.cxx"