8
8
"""
9
9
from builtins import next
10
10
from builtins import str
11
+
11
12
__author__ = 'Tim Sutton'
12
13
__date__ = '20/08/2012'
13
14
__copyright__ = 'Copyright 2012, The QGIS Project'
24
25
QgsCoordinateReferenceSystem ,
25
26
QgsVectorFileWriter ,
26
27
QgsFeatureRequest ,
27
- QgsWkbTypes
28
+ QgsWkbTypes ,
29
+ QgsRectangle ,
30
+ QgsCoordinateTransform
28
31
)
29
32
from qgis .PyQt .QtCore import QDate , QTime , QDateTime , QVariant , QDir
30
33
import os
31
34
import osgeo .gdal # NOQA
32
35
from osgeo import gdal , ogr
33
36
from qgis .testing import start_app , unittest
34
- from utilities import writeShape , compareWkt
37
+ from utilities import writeShape , compareWkt , unitTestDataPath
35
38
39
+ TEST_DATA_DIR = unitTestDataPath ()
36
40
start_app ()
37
41
38
42
@@ -41,7 +45,6 @@ def GDAL_COMPUTE_VERSION(maj, min, rev):
41
45
42
46
43
47
class TestFieldValueConverter (QgsVectorFileWriter .FieldValueConverter ):
44
-
45
48
def __init__ (self , layer ):
46
49
QgsVectorFileWriter .FieldValueConverter .__init__ (self )
47
50
self .layer = layer
@@ -66,7 +69,6 @@ def convert(self, idx, value):
66
69
67
70
68
71
class TestQgsVectorFileWriter (unittest .TestCase ):
69
-
70
72
mMemoryLayer = None
71
73
72
74
def testWrite (self ):
@@ -142,7 +144,57 @@ def testDateTimeWriteShapefile(self):
142
144
# shapefiles do not support datetime types
143
145
datetime_idx = created_layer .fields ().lookupField ('dt_f' )
144
146
self .assertIsInstance (f .attributes ()[datetime_idx ], str )
145
- self .assertEqual (f .attributes ()[datetime_idx ], QDateTime (QDate (2014 , 3 , 5 ), QTime (13 , 45 , 22 )).toString ("yyyy/MM/dd hh:mm:ss.zzz" ))
147
+ self .assertEqual (f .attributes ()[datetime_idx ],
148
+ QDateTime (QDate (2014 , 3 , 5 ), QTime (13 , 45 , 22 )).toString ("yyyy/MM/dd hh:mm:ss.zzz" ))
149
+
150
+ def testWriterWithExtent (self ):
151
+ """Check writing using extent filter."""
152
+ source_file = os .path .join (TEST_DATA_DIR , 'points.shp' )
153
+ source_layer = QgsVectorLayer (source_file , 'Points' , 'ogr' )
154
+ self .assertTrue (source_layer .isValid ())
155
+
156
+ options = QgsVectorFileWriter .SaveVectorOptions ()
157
+ options .driverName = 'ESRI Shapefile'
158
+ options .filterExtent = QgsRectangle (- 111 , 26 , - 96 , 38 )
159
+
160
+ dest_file_name = os .path .join (str (QDir .tempPath ()), 'extent_no_transform.shp' )
161
+ write_result = QgsVectorFileWriter .writeAsVectorFormat (
162
+ source_layer ,
163
+ dest_file_name ,
164
+ options )
165
+ self .assertEqual (write_result , QgsVectorFileWriter .NoError )
166
+
167
+ # Open result and check
168
+ created_layer = QgsVectorLayer ('{}|layerid=0' .format (dest_file_name ), 'test' , 'ogr' )
169
+ features = [f for f in created_layer .getFeatures ()]
170
+ self .assertEqual (len (features ), 5 )
171
+ for f in features :
172
+ self .assertTrue (f .geometry ().intersects (options .filterExtent ))
173
+
174
+ def testWriterWithExtentAndReprojection (self ):
175
+ """Check writing using extent filter with reprojection."""
176
+ source_file = os .path .join (TEST_DATA_DIR , 'points.shp' )
177
+ source_layer = QgsVectorLayer (source_file , 'Points' , 'ogr' )
178
+ self .assertTrue (source_layer .isValid ())
179
+
180
+ options = QgsVectorFileWriter .SaveVectorOptions ()
181
+ options .driverName = 'ESRI Shapefile'
182
+ options .filterExtent = QgsRectangle (- 12511460 , 3045157 , - 10646621 , 4683497 )
183
+ options .ct = QgsCoordinateTransform (source_layer .crs (), QgsCoordinateReferenceSystem .fromEpsgId (3785 ))
184
+
185
+ dest_file_name = os .path .join (str (QDir .tempPath ()), 'extent_transform.shp' )
186
+ write_result = QgsVectorFileWriter .writeAsVectorFormat (
187
+ source_layer ,
188
+ dest_file_name ,
189
+ options )
190
+ self .assertEqual (write_result , QgsVectorFileWriter .NoError )
191
+
192
+ # Open result and check
193
+ created_layer = QgsVectorLayer ('{}|layerid=0' .format (dest_file_name ), 'test' , 'ogr' )
194
+ features = [f for f in created_layer .getFeatures ()]
195
+ self .assertEqual (len (features ), 5 )
196
+ for f in features :
197
+ self .assertTrue (f .geometry ().intersects (options .filterExtent ))
146
198
147
199
def testDateTimeWriteTabfile (self ):
148
200
"""Check writing date and time fields to an MapInfo tabfile."""
@@ -236,12 +288,14 @@ def testWriteShapefileWithZ(self):
236
288
g = f .geometry ()
237
289
wkt = g .exportToWkt ()
238
290
expWkt = 'PointZ (1 2 3)'
239
- self .assertTrue (compareWkt (expWkt , wkt ), "saving geometry with Z failed: mismatch Expected:\n %s\n Got:\n %s\n " % (expWkt , wkt ))
291
+ self .assertTrue (compareWkt (expWkt , wkt ),
292
+ "saving geometry with Z failed: mismatch Expected:\n %s\n Got:\n %s\n " % (expWkt , wkt ))
240
293
241
294
# also try saving out the shapefile version again, as an extra test
242
295
# this tests that saving a layer with z WITHOUT explicitly telling the writer to keep z values,
243
296
# will stay retain the z values
244
- dest_file_name = os .path .join (str (QDir .tempPath ()), 'point_{}_copy.shp' .format (QgsWkbTypes .displayString (t )))
297
+ dest_file_name = os .path .join (str (QDir .tempPath ()),
298
+ 'point_{}_copy.shp' .format (QgsWkbTypes .displayString (t )))
245
299
crs = QgsCoordinateReferenceSystem ()
246
300
crs .createFromId (4326 , QgsCoordinateReferenceSystem .EpsgCrsId )
247
301
write_result = QgsVectorFileWriter .writeAsVectorFormat (
@@ -257,7 +311,8 @@ def testWriteShapefileWithZ(self):
257
311
f = next (created_layer_from_shp .getFeatures (QgsFeatureRequest ()))
258
312
g = f .geometry ()
259
313
wkt = g .exportToWkt ()
260
- self .assertTrue (compareWkt (expWkt , wkt ), "saving geometry with Z failed: mismatch Expected:\n %s\n Got:\n %s\n " % (expWkt , wkt ))
314
+ self .assertTrue (compareWkt (expWkt , wkt ),
315
+ "saving geometry with Z failed: mismatch Expected:\n %s\n Got:\n %s\n " % (expWkt , wkt ))
261
316
262
317
def testWriteShapefileWithMultiConversion (self ):
263
318
"""Check writing geometries to an ESRI shapefile with conversion to multi."""
@@ -296,7 +351,9 @@ def testWriteShapefileWithMultiConversion(self):
296
351
g = f .geometry ()
297
352
wkt = g .exportToWkt ()
298
353
expWkt = 'MultiPoint ((1 2))'
299
- self .assertTrue (compareWkt (expWkt , wkt ), "saving geometry with multi conversion failed: mismatch Expected:\n %s\n Got:\n %s\n " % (expWkt , wkt ))
354
+ self .assertTrue (compareWkt (expWkt , wkt ),
355
+ "saving geometry with multi conversion failed: mismatch Expected:\n %s\n Got:\n %s\n " % (
356
+ expWkt , wkt ))
300
357
301
358
def testWriteShapefileWithAttributeSubsets (self ):
302
359
"""Tests writing subsets of attributes to files."""
@@ -379,7 +436,9 @@ def testWriteShapefileWithAttributeSubsets(self):
379
436
g = f .geometry ()
380
437
wkt = g .exportToWkt ()
381
438
expWkt = 'Point (1 2)'
382
- self .assertTrue (compareWkt (expWkt , wkt ), "geometry not saved correctly when saving without attributes : mismatch Expected:\n %s\n Got:\n %s\n " % (expWkt , wkt ))
439
+ self .assertTrue (compareWkt (expWkt , wkt ),
440
+ "geometry not saved correctly when saving without attributes : mismatch Expected:\n %s\n Got:\n %s\n " % (
441
+ expWkt , wkt ))
383
442
self .assertEqual (f ['FID' ], 0 )
384
443
385
444
def testValueConverter (self ):
0 commit comments