/
ToolsTest.py
210 lines (161 loc) · 8.07 KB
/
ToolsTest.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
# -*- coding: utf-8 -*-
"""
***************************************************************************
ToolsTest
---------------------
Date : July 2016
Copyright : (C) 2016 by Nyall Dawson
Email : nyall dot dawson at gmail 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. *
* *
***************************************************************************
"""
__author__ = 'Nyall Dawson'
__date__ = 'July 2016'
__copyright__ = '(C) 2016, Nyall Dawson'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import os
import shutil
import tempfile
from qgis.core import (QgsVectorLayer, QgsFeatureRequest)
from qgis.testing import start_app, unittest
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.ProcessingConfig import ProcessingConfig
from processing.tests.TestData import testDataPath, points, invalid_geometries
from processing.tools import vector
testDataPath = os.path.join(os.path.dirname(__file__), 'testdata')
start_app()
class VectorTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.cleanup_paths = []
@classmethod
def tearDownClass(cls):
for path in cls.cleanup_paths:
shutil.rmtree(path)
def testFeatures(self):
ProcessingConfig.initialize()
test_data = points()
test_layer = QgsVectorLayer(test_data, 'test', 'ogr')
# test with all features
features = vector.features(test_layer)
self.assertEqual(len(features), 9)
self.assertEqual(set([f.id() for f in features]), set([0, 1, 2, 3, 4, 5, 6, 7, 8]))
# test with selected features
previous_value = ProcessingConfig.getSetting(ProcessingConfig.USE_SELECTED)
ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, True)
test_layer.selectByIds([2, 4, 6])
features = vector.features(test_layer)
self.assertEqual(len(features), 3)
self.assertEqual(set([f.id() for f in features]), set([2, 4, 6]))
# selection, but not using selected features
ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, False)
test_layer.selectByIds([2, 4, 6])
features = vector.features(test_layer)
self.assertEqual(len(features), 9)
self.assertEqual(set([f.id() for f in features]), set([0, 1, 2, 3, 4, 5, 6, 7, 8]))
# using selected features, but no selection
ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, True)
test_layer.removeSelection()
features = vector.features(test_layer)
self.assertEqual(len(features), 9)
self.assertEqual(set([f.id() for f in features]), set([0, 1, 2, 3, 4, 5, 6, 7, 8]))
# test that feature request is honored
ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, False)
features = vector.features(test_layer, QgsFeatureRequest().setFilterFids([1, 3, 5]))
self.assertEqual(set([f.id() for f in features]), set([1, 3, 5]))
# test that feature request is honored when using selections
ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, True)
test_layer.selectByIds([2, 4, 6])
features = vector.features(test_layer, QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry))
self.assertTrue(all([not f.hasGeometry() for f in features]))
features = vector.features(test_layer, QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry))
self.assertEqual(set([f.id() for f in features]), set([2, 4, 6]))
ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, previous_value)
def testValues(self):
ProcessingConfig.initialize()
test_data = points()
test_layer = QgsVectorLayer(test_data, 'test', 'ogr')
# field by index
res = vector.values(test_layer, 1)
self.assertEqual(res[1], [1, 2, 3, 4, 5, 6, 7, 8, 9])
# field by name
res = vector.values(test_layer, 'id')
self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8, 9])
# two fields
res = vector.values(test_layer, 1, 2)
self.assertEqual(res[1], [1, 2, 3, 4, 5, 6, 7, 8, 9])
self.assertEqual(res[2], [2, 1, 0, 2, 1, 0, 0, 0, 0])
# two fields by name
res = vector.values(test_layer, 'id', 'id2')
self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8, 9])
self.assertEqual(res['id2'], [2, 1, 0, 2, 1, 0, 0, 0, 0])
# two fields by name and index
res = vector.values(test_layer, 'id', 2)
self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8, 9])
self.assertEqual(res[2], [2, 1, 0, 2, 1, 0, 0, 0, 0])
# test with selected features
previous_value = ProcessingConfig.getSetting(ProcessingConfig.USE_SELECTED)
ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, True)
test_layer.selectByIds([2, 4, 6])
res = vector.values(test_layer, 1)
self.assertEqual(set(res[1]), set([5, 7, 3]))
ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, previous_value)
def testUniqueValues(self):
ProcessingConfig.initialize()
test_data = points()
test_layer = QgsVectorLayer(test_data, 'test', 'ogr')
# field by index
v = vector.uniqueValues(test_layer, 2)
self.assertEqual(len(v), len(set(v)))
self.assertEqual(set(v), set([2, 1, 0]))
# field by name
v = vector.uniqueValues(test_layer, 'id2')
self.assertEqual(len(v), len(set(v)))
self.assertEqual(set(v), set([2, 1, 0]))
# test with selected features
previous_value = ProcessingConfig.getSetting(ProcessingConfig.USE_SELECTED)
ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, True)
test_layer.selectByIds([2, 4, 6])
v = vector.uniqueValues(test_layer, 'id')
self.assertEqual(len(v), len(set(v)))
self.assertEqual(set(v), set([5, 7, 3]))
ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, previous_value)
def testOgrLayerNameExtraction(self):
outdir = tempfile.mkdtemp()
self.cleanup_paths.append(outdir)
def _copyFile(dst):
shutil.copyfile(os.path.join(testDataPath, 'custom', 'grass7', 'weighted.csv'), dst)
# OGR provider - single layer
_copyFile(os.path.join(outdir, 'a.csv'))
name = vector.ogrLayerName(outdir)
self.assertEqual(name, 'a')
# OGR provider - multiple layers
_copyFile(os.path.join(outdir, 'b.csv'))
name = vector.ogrLayerName(outdir + '|layerid=0')
self.assertEqual(name, 'a')
name = vector.ogrLayerName(outdir + '|layerid=1')
self.assertEqual(name, 'b')
name = vector.ogrLayerName(outdir + '|layerid=2')
self.assertIsNone(name)
# OGR provider - layername takes precedence
name = vector.ogrLayerName(outdir + '|layername=f')
self.assertEqual(name, 'f')
name = vector.ogrLayerName(outdir + '|layerid=0|layername=f')
self.assertEqual(name, 'f')
name = vector.ogrLayerName(outdir + '|layername=f|layerid=0')
self.assertEqual(name, 'f')
# SQLiite provider
name = vector.ogrLayerName('dbname=\'/tmp/x.sqlite\' table="t" (geometry) sql=')
self.assertEqual(name, 't')
# PostgreSQL provider
name = vector.ogrLayerName('port=5493 sslmode=disable key=\'edge_id\' srid=0 type=LineString table="city_data"."edge" (geom) sql=')
self.assertEqual(name, 'city_data.edge')
if __name__ == '__main__':
unittest.main()