-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
test_qgsfeatureiterator.py
280 lines (225 loc) · 11.2 KB
/
test_qgsfeatureiterator.py
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
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsFeatureIterator.
.. note:: 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.
"""
__author__ = 'Matthias Kuhn'
__date__ = '18/09/2013'
__copyright__ = 'Copyright 2013, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import qgis # NOQA
import os
from qgis.core import QgsVectorLayer, QgsFeatureRequest, QgsFeature, QgsField, NULL, QgsMapLayerRegistry, QgsVectorJoinInfo
from qgis.testing import start_app, unittest
from qgis.PyQt.QtCore import QVariant
from utilities import unitTestDataPath
start_app()
TEST_DATA_DIR = unitTestDataPath()
class TestQgsFeatureIterator(unittest.TestCase):
def __init__(self, methodName):
"""Run once on class initialization."""
unittest.TestCase.__init__(self, methodName)
def test_FilterExpression(self):
# create point layer
myShpFile = os.path.join(TEST_DATA_DIR, 'points.shp')
pointLayer = QgsVectorLayer(myShpFile, 'Points', 'ogr')
ids = [feat.id() for feat in pointLayer.getFeatures(QgsFeatureRequest().setFilterExpression('Staff > 3'))]
expectedIds = [1, 5, 6, 7, 8]
myMessage = '\nExpected: {0} features\nGot: {1} features'.format(repr(expectedIds), repr(ids))
assert ids == expectedIds, myMessage
pointLayer.startEditing()
self.addFeatures(pointLayer)
ids = [feat.id() for feat in pointLayer.getFeatures(QgsFeatureRequest().setFilterExpression('Staff > 3'))]
expectedIds = [-2, 1, 5, 6, 7, 8]
myMessage = '\nExpected: {0} features\nGot: {1} features'.format(repr(expectedIds), repr(ids))
assert ids == expectedIds, myMessage
pointLayer.rollBack()
ids = [feat.id() for feat in pointLayer.getFeatures(QgsFeatureRequest().setFilterExpression('Staff > 3'))]
expectedIds = [1, 5, 6, 7, 8]
myMessage = '\nExpected: {0} features\nGot: {1} features'.format(repr(expectedIds), repr(ids))
assert ids == expectedIds, myMessage
def test_FilterExpressionWithAccents(self):
myShpFile = os.path.join(TEST_DATA_DIR, 'france_parts.shp')
layer = QgsVectorLayer(myShpFile, 'poly', 'ogr')
layer.setProviderEncoding("ISO-8859-1")
ids = [feat.id() for feat in layer.getFeatures(QgsFeatureRequest().setFilterExpression(u"TYPE_1 = 'Région'"))]
expectedIds = [0, 1, 2, 3]
myMessage = '\nExpected: {0} features\nGot: {1} features'.format(repr(expectedIds), repr(ids))
assert ids == expectedIds, myMessage
layer.setProviderEncoding("UTF-8")
ids = [feat.id() for feat in layer.getFeatures(QgsFeatureRequest().setFilterExpression(u"TYPE_1 = 'Région'"))]
expectedIds = []
myMessage = '\nExpected: {0} features\nGot: {1} features'.format(repr(expectedIds), repr(ids))
assert ids == expectedIds, myMessage
def test_FilterFids(self):
# create point layer
myShpFile = os.path.join(TEST_DATA_DIR, 'points.shp')
pointLayer = QgsVectorLayer(myShpFile, 'Points', 'ogr')
ids = [feat.id() for feat in pointLayer.getFeatures(QgsFeatureRequest().setFilterFids([7, 8, 12, 30]))]
expectedIds = [7, 8, 12]
self.assertEquals(set(ids), set(expectedIds))
pointLayer.startEditing()
self.addFeatures(pointLayer)
ids = [feat.id() for feat in pointLayer.getFeatures(QgsFeatureRequest().setFilterFids([-4, 7, 8, 12, 30]))]
expectedIds = [-4, 7, 8, 12]
self.assertEquals(set(ids), set(expectedIds))
pointLayer.rollBack()
ids = [feat.id() for feat in pointLayer.getFeatures(QgsFeatureRequest().setFilterFids([-2, 7, 8, 12, 30]))]
expectedIds = [7, 8, 12]
self.assertEquals(set(ids), set(expectedIds))
def addFeatures(self, vl):
feat = QgsFeature()
fields = vl.pendingFields()
feat.setFields(fields)
feat['Staff'] = 4
vl.addFeature(feat)
feat = QgsFeature()
fields = vl.pendingFields()
feat.setFields(fields)
feat['Staff'] = 2
vl.addFeature(feat)
def test_ExpressionFieldNested(self):
myShpFile = os.path.join(TEST_DATA_DIR, 'points.shp')
layer = QgsVectorLayer(myShpFile, 'Points', 'ogr')
self.assertTrue(layer.isValid())
cnt = layer.pendingFields().count()
idx = layer.addExpressionField('"Staff"*2', QgsField('exp1', QVariant.LongLong))
idx = layer.addExpressionField('"exp1"-1', QgsField('exp2', QVariant.LongLong))
fet = next(layer.getFeatures(QgsFeatureRequest().setSubsetOfAttributes(['exp2'], layer.fields())))
self.assertEqual(fet['Class'], NULL)
# nested virtual fields should make all these attributes be fetched
self.assertEqual(fet['Staff'], 2)
self.assertEqual(fet['exp2'], 3)
self.assertEqual(fet['exp1'], 4)
def test_ExpressionFieldNestedGeometry(self):
myShpFile = os.path.join(TEST_DATA_DIR, 'points.shp')
layer = QgsVectorLayer(myShpFile, 'Points', 'ogr')
self.assertTrue(layer.isValid())
cnt = layer.pendingFields().count()
idx = layer.addExpressionField('$x*2', QgsField('exp1', QVariant.LongLong))
idx = layer.addExpressionField('"exp1"/1.5', QgsField('exp2', QVariant.LongLong))
fet = next(layer.getFeatures(QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry).setSubsetOfAttributes(['exp2'], layer.fields())))
# nested virtual fields should have made geometry be fetched
self.assertEqual(fet['exp2'], -156)
self.assertEqual(fet['exp1'], -234)
def test_ExpressionFieldNestedCircular(self):
""" test circular virtual field definitions """
myShpFile = os.path.join(TEST_DATA_DIR, 'points.shp')
layer = QgsVectorLayer(myShpFile, 'Points', 'ogr')
self.assertTrue(layer.isValid())
cnt = layer.pendingFields().count()
idx = layer.addExpressionField('"exp3"*2', QgsField('exp1', QVariant.LongLong))
idx = layer.addExpressionField('"exp1"-1', QgsField('exp2', QVariant.LongLong))
idx = layer.addExpressionField('"exp2"*3', QgsField('exp3', QVariant.LongLong))
# really just testing that this doesn't hang/crash... there's no good result here!
fet = next(layer.getFeatures(QgsFeatureRequest().setSubsetOfAttributes(['exp2'], layer.fields())))
self.assertEqual(fet['Class'], NULL)
def test_JoinUsingExpression(self):
""" test joining a layer using a virtual field """
joinLayer = QgsVectorLayer(
"Point?field=x:string&field=y:integer&field=z:integer",
"joinlayer", "memory")
pr = joinLayer.dataProvider()
f1 = QgsFeature()
f1.setAttributes(["foo", 246, 321])
f2 = QgsFeature()
f2.setAttributes(["bar", 456, 654])
self.assertTrue(pr.addFeatures([f1, f2]))
layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer",
"addfeat", "memory")
pr = layer.dataProvider()
f = QgsFeature()
f.setAttributes(["test", 123])
self.assertTrue(pr.addFeatures([f]))
layer.addExpressionField('"fldint"*2', QgsField('exp1', QVariant.LongLong))
QgsMapLayerRegistry.instance().addMapLayers([layer, joinLayer])
join = QgsVectorJoinInfo()
join.targetFieldName = "exp1"
join.joinLayerId = joinLayer.id()
join.joinFieldName = "y"
join.memoryCache = True
layer.addJoin(join)
f = QgsFeature()
fi = layer.getFeatures()
self.assertTrue(fi.nextFeature(f))
attrs = f.attributes()
self.assertEqual(attrs[0], "test")
self.assertEqual(attrs[1], 123)
self.assertEqual(attrs[2], "foo")
self.assertEqual(attrs[3], 321)
self.assertEqual(attrs[4], 246)
self.assertFalse(fi.nextFeature(f))
QgsMapLayerRegistry.instance().removeMapLayers([layer.id(), joinLayer.id()])
def test_JoinUsingExpression2(self):
""" test joining a layer using a virtual field (the other way!) """
joinLayer = QgsVectorLayer(
"Point?field=x:string&field=y:integer&field=z:integer",
"joinlayer", "memory")
pr = joinLayer.dataProvider()
f1 = QgsFeature()
f1.setAttributes(["foo", 246, 321])
f2 = QgsFeature()
f2.setAttributes(["bar", 456, 654])
self.assertTrue(pr.addFeatures([f1, f2]))
joinLayer.addExpressionField('"y"/2', QgsField('exp1', QVariant.LongLong))
layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer",
"addfeat", "memory")
pr = layer.dataProvider()
f = QgsFeature()
f.setAttributes(["test", 123])
self.assertTrue(pr.addFeatures([f]))
QgsMapLayerRegistry.instance().addMapLayers([layer, joinLayer])
join = QgsVectorJoinInfo()
join.targetFieldName = "fldint"
join.joinLayerId = joinLayer.id()
join.joinFieldName = "exp1"
join.memoryCache = True
layer.addJoin(join)
f = QgsFeature()
fi = layer.getFeatures()
self.assertTrue(fi.nextFeature(f))
attrs = f.attributes()
self.assertEqual(attrs[0], "test")
self.assertEqual(attrs[1], 123)
self.assertEqual(attrs[2], "foo")
self.assertEqual(attrs[3], 246)
self.assertEqual(attrs[4], 321)
self.assertFalse(fi.nextFeature(f))
QgsMapLayerRegistry.instance().removeMapLayers([layer.id(), joinLayer.id()])
def test_JoinUsingFeatureRequestExpression(self):
""" test requesting features using a filter expression which requires joined columns """
joinLayer = QgsVectorLayer(
"Point?field=x:string&field=y:integer&field=z:integer",
"joinlayer", "memory")
pr = joinLayer.dataProvider()
f1 = QgsFeature()
f1.setAttributes(["foo", 123, 321])
f2 = QgsFeature()
f2.setAttributes(["bar", 124, 654])
self.assertTrue(pr.addFeatures([f1, f2]))
layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer",
"addfeat", "memory")
pr = layer.dataProvider()
f1 = QgsFeature()
f1.setAttributes(["test", 123])
f2 = QgsFeature()
f2.setAttributes(["test", 124])
self.assertTrue(pr.addFeatures([f1, f2]))
QgsMapLayerRegistry.instance().addMapLayers([layer, joinLayer])
join = QgsVectorJoinInfo()
join.targetFieldName = "fldint"
join.joinLayerId = joinLayer.id()
join.joinFieldName = "y"
join.memoryCache = True
layer.addJoin(join)
f = QgsFeature()
fi = layer.getFeatures(QgsFeatureRequest().setFlags(QgsFeatureRequest.SubsetOfAttributes).setFilterExpression('joinlayer_z=654'))
self.assertTrue(fi.nextFeature(f))
self.assertEqual(f['fldint'], 124)
self.assertEqual(f['joinlayer_z'], 654)
QgsMapLayerRegistry.instance().removeMapLayers([layer.id(), joinLayer.id()])
if __name__ == '__main__':
unittest.main()