Skip to content

Commit 96d8986

Browse files
committed
Add polygon layers to provider tests, so we can test ExactIntersect
feature requests
1 parent ce2b3c2 commit 96d8986

15 files changed

+117
-1
lines changed

tests/src/python/providertestbase.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,23 @@ def testGetFeaturesFilterRectTests(self):
259259
features = [f['pk'] for f in self.provider.getFeatures(QgsFeatureRequest().setFilterRect(extent))]
260260
assert set(features) == set([2, 4]), 'Got {} instead'.format(features)
261261

262+
def testGetFeaturesPolyFilterRectTests(self):
263+
""" Test fetching features from a polygon layer with filter rect"""
264+
try:
265+
if not self.poly_provider:
266+
return
267+
except:
268+
return
269+
270+
extent = QgsRectangle(-73, 70, -63, 80)
271+
features = [f['pk'] for f in self.poly_provider.getFeatures(QgsFeatureRequest().setFilterRect(extent))]
272+
# Some providers may return the exact intersection matches (2, 3) even without the ExactIntersect flag, so we accept that too
273+
assert set(features) == set([2, 3]) or set(features) == set([1, 2, 3]), 'Got {} instead'.format(features)
274+
275+
# Test with exact intersection
276+
features = [f['pk'] for f in self.poly_provider.getFeatures(QgsFeatureRequest().setFilterRect(extent).setFlags(QgsFeatureRequest.ExactIntersect))]
277+
assert set(features) == set([2, 3]), 'Got {} instead'.format(features)
278+
262279
def testRectAndExpression(self):
263280
extent = QgsRectangle(-70, 67, -60, 80)
264281
result = set([f['pk'] for f in self.provider.getFeatures(

tests/src/python/test_provider_memory.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,29 @@ def setUpClass(cls):
6565

6666
cls.provider.addFeatures([f1, f2, f3, f4, f5])
6767

68+
# poly layer
69+
cls.poly_vl = QgsVectorLayer(u'Polygon?crs=epsg:4326&field=pk:integer&key=pk',
70+
u'test', u'memory')
71+
assert (cls.poly_vl.isValid())
72+
cls.poly_provider = cls.poly_vl.dataProvider()
73+
74+
f1 = QgsFeature()
75+
f1.setAttributes([1])
76+
f1.setGeometry(QgsGeometry.fromWkt('Polygon ((-69.03664108 81.35818902, -69.09237722 80.24346619, -73.718477 80.1319939, -73.718477 76.28620011, -74.88893598 76.34193625, -74.83319983 81.35818902, -69.03664108 81.35818902))'))
77+
78+
f2 = QgsFeature()
79+
f2.setAttributes([2])
80+
f2.setGeometry(QgsGeometry.fromWkt('Polygon ((-67.58750139 81.1909806, -66.30557012 81.24671674, -66.30557012 76.89929767, -67.58750139 76.89929767, -67.58750139 81.1909806))'))
81+
82+
f3 = QgsFeature()
83+
f3.setAttributes([3])
84+
f3.setGeometry(QgsGeometry.fromWkt('Polygon ((-68.36780737 75.78457483, -67.53176524 72.60761475, -68.64648808 73.66660144, -70.20710006 72.9420316, -68.36780737 75.78457483))'))
85+
86+
f4 = QgsFeature()
87+
f4.setAttributes([4])
88+
89+
cls.poly_provider.addFeatures([f1, f2, f3, f4])
90+
6891
@classmethod
6992
def tearDownClass(cls):
7093
"""Run after all tests"""
@@ -251,6 +274,29 @@ def setUpClass(cls):
251274

252275
cls.provider.addFeatures([f1, f2, f3, f4, f5])
253276

277+
# poly layer
278+
cls.poly_vl = QgsVectorLayer(u'Polygon?crs=epsg:4326&index=yes&field=pk:integer&key=pk',
279+
u'test', u'memory')
280+
assert (cls.poly_vl.isValid())
281+
cls.poly_provider = cls.poly_vl.dataProvider()
282+
283+
f1 = QgsFeature()
284+
f1.setAttributes([1])
285+
f1.setGeometry(QgsGeometry.fromWkt('Polygon ((-69.0 81.4, -69.0 80.2, -73.7 80.2, -73.7 76.3, -74.9 76.3, -74.9 81.4, -69.0 81.4))'))
286+
287+
f2 = QgsFeature()
288+
f2.setAttributes([2])
289+
f2.setGeometry(QgsGeometry.fromWkt('Polygon ((-67.6 81.2, -66.3 81.2, -66.3 76.9, -67.6 76.9, -67.6 81.2))'))
290+
291+
f3 = QgsFeature()
292+
f3.setAttributes([3])
293+
f3.setGeometry(QgsGeometry.fromWkt('Polygon ((-68.4 75.8, -67.5 72.6, -68.6 73.7, -70.2 72.9, -68.4 75.8))'))
294+
295+
f4 = QgsFeature()
296+
f4.setAttributes([4])
297+
298+
cls.poly_provider.addFeatures([f1, f2, f3, f4])
299+
254300
@classmethod
255301
def tearDownClass(cls):
256302
"""Run after all tests"""

tests/src/python/test_provider_postgres.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,13 @@ def setUpClass(cls):
3838
cls.dbconn = u'dbname=\'qgis_test\' host=localhost port=5432 user=\'postgres\' password=\'postgres\''
3939
if 'QGIS_PGTEST_DB' in os.environ:
4040
cls.dbconn = os.environ['QGIS_PGTEST_DB']
41-
# Create test layer
41+
# Create test layers
4242
cls.vl = QgsVectorLayer(cls.dbconn + ' sslmode=disable key=\'pk\' srid=4326 type=POINT table="qgis_test"."someData" (geom) sql=', 'test', 'postgres')
4343
assert(cls.vl.isValid())
4444
cls.provider = cls.vl.dataProvider()
45+
cls.poly_vl = QgsVectorLayer(cls.dbconn + ' sslmode=disable key=\'pk\' srid=4326 type=POLYGON table="qgis_test"."some_poly_data" (geom) sql=', 'test', 'postgres')
46+
assert(cls.poly_vl.isValid())
47+
cls.poly_provider = cls.poly_vl.dataProvider()
4548

4649
@classmethod
4750
def tearDownClass(cls):

tests/src/python/test_provider_shapefile.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,17 @@ def setUpClass(cls):
4343
for file in glob.glob(os.path.join(srcpath, 'shapefile.*')):
4444
shutil.copy(os.path.join(srcpath, file), cls.basetestpath)
4545
shutil.copy(os.path.join(srcpath, file), cls.repackfilepath)
46+
for file in glob.glob(os.path.join(srcpath, 'shapefile_poly.*')):
47+
shutil.copy(os.path.join(srcpath, file), cls.basetestpath)
4648
cls.basetestfile = os.path.join(cls.basetestpath, 'shapefile.shp')
4749
cls.repackfile = os.path.join(cls.repackfilepath, 'shapefile.shp')
50+
cls.basetestpolyfile = os.path.join(cls.basetestpath, 'shapefile_poly.shp')
4851
cls.vl = QgsVectorLayer(u'{}|layerid=0'.format(cls.basetestfile), u'test', u'ogr')
4952
assert (cls.vl.isValid())
5053
cls.provider = cls.vl.dataProvider()
54+
cls.vl_poly = QgsVectorLayer(u'{}|layerid=0'.format(cls.basetestpolyfile), u'test', u'ogr')
55+
assert (cls.vl_poly.isValid())
56+
cls.poly_provider = cls.vl_poly.dataProvider()
5157

5258
@classmethod
5359
def tearDownClass(cls):

tests/src/python/test_provider_spatialite.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ def setUpClass(cls):
5252
assert(cls.vl.isValid())
5353
cls.provider = cls.vl.dataProvider()
5454

55+
cls.vl_poly = QgsVectorLayer('dbname=\'{}/provider/spatialite.db\' table="somepolydata" (geom) sql='.format(TEST_DATA_DIR), 'test', 'spatialite')
56+
assert(cls.vl_poly.isValid())
57+
cls.poly_provider = cls.vl_poly.dataProvider()
58+
5559
# create test db
5660
cls.dbname = os.path.join(tempfile.gettempdir(), "test.sqlite")
5761
if os.path.exists(cls.dbname):

tests/src/python/test_provider_virtual.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ def setUpClass(cls):
6868
assert (cls.vl.isValid())
6969
cls.provider = cls.vl.dataProvider()
7070

71+
shp_poly = os.path.join(TEST_DATA_DIR, 'provider/shapefile_poly.shp')
72+
d = QgsVirtualLayerDefinition()
73+
d.addSource("vtab2", shp_poly, "ogr")
74+
d.setUid("pk")
75+
cls.poly_vl = QgsVectorLayer(d.toString(), u'test_poly', u'virtual')
76+
assert (cls.poly_vl.isValid())
77+
cls.poly_provider = cls.poly_vl.dataProvider()
78+
7179
@classmethod
7280
def tearDownClass(cls):
7381
"""Run after all tests"""

tests/src/python/test_qgsdelimitedtextprovider.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,20 @@ def setUpClass(cls):
370370
assert cls.vl.isValid(), "{} is invalid".format(cls.basetestfile)
371371
cls.provider = cls.vl.dataProvider()
372372

373+
cls.basetestpolyfile = os.path.join(srcpath, 'delimited_wkt_poly.csv')
374+
375+
url = QUrl.fromLocalFile(cls.basetestpolyfile)
376+
url.addQueryItem("crs", "epsg:4326")
377+
url.addQueryItem("type", "csv")
378+
url.addQueryItem("wktField", "wkt")
379+
url.addQueryItem("spatialIndex", "no")
380+
url.addQueryItem("subsetIndex", "no")
381+
url.addQueryItem("watchFile", "no")
382+
383+
cls.vl_poly = QgsVectorLayer(url.toString(), u'test_polygon', u'delimitedtext')
384+
assert cls.vl_poly.isValid(), "{} is invalid".format(cls.basetestpolyfile)
385+
cls.poly_provider = cls.vl_poly.dataProvider()
386+
373387
@classmethod
374388
def tearDownClass(cls):
375389
"""Run after all tests"""
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pk,wkt
2+
1,"Polygon ((-69.0 81.4, -69.0 80.2, -73.7 80.2, -73.7 76.3, -74.9 76.3, -74.9 81.4, -69.0 81.4))"
3+
2,"Polygon ((-67.6 81.2, -66.3 81.2, -66.3 76.9, -67.6 76.9, -67.6 81.2))"
4+
3,"Polygon ((-68.4 75.8, -67.5 72.6, -68.6 73.7, -70.2 72.9, -68.4 75.8))"
5+
4,
6+
109 Bytes
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]
552 Bytes
Binary file not shown.
132 Bytes
Binary file not shown.

tests/testdata/provider/spatialite.db

12 KB
Binary file not shown.

tests/testdata/provider/testdata_pg.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ CREATE TABLE qgis_test."someData" (
4242
geom public.geometry(Point,4326)
4343
);
4444

45+
CREATE TABLE qgis_test."some_poly_data" (
46+
pk SERIAL NOT NULL,
47+
geom public.geometry(Polygon,4326)
48+
);
4549

4650
--
4751
-- TOC entry 4068 (class 0 OID 377761)
@@ -57,6 +61,12 @@ INSERT INTO qgis_test."someData" (pk, cnt, name, name2, num_char, geom) VALUES
5761
(4, 400, 'Honey', 'Honey', '4', '0101000020E610000014AE47E17A5450C03333333333935340')
5862
;
5963

64+
INSERT INTO qgis_test."some_poly_data" (pk, geom) VALUES
65+
(1, ST_GeomFromText('Polygon ((-69.0 81.4, -69.0 80.2, -73.7 80.2, -73.7 76.3, -74.9 76.3, -74.9 81.4, -69.0 81.4))', 4326) ),
66+
(2, ST_GeomFromText('Polygon ((-67.6 81.2, -66.3 81.2, -66.3 76.9, -67.6 76.9, -67.6 81.2))', 4326) ),
67+
(3, ST_GeomFromText('Polygon ((-68.4 75.8, -67.5 72.6, -68.6 73.7, -70.2 72.9, -68.4 75.8))', 4326) ),
68+
(4, NULL)
69+
;
6070

6171
--
6272
-- TOC entry 3953 (class 2606 OID 377768)

0 commit comments

Comments
 (0)