Skip to content

Commit

Permalink
Add polygon layers to provider tests, so we can test ExactIntersect
Browse files Browse the repository at this point in the history
feature requests
  • Loading branch information
nyalldawson committed Jan 31, 2016
1 parent ce2b3c2 commit 96d8986
Show file tree
Hide file tree
Showing 15 changed files with 117 additions and 1 deletion.
17 changes: 17 additions & 0 deletions tests/src/python/providertestbase.py
Expand Up @@ -259,6 +259,23 @@ def testGetFeaturesFilterRectTests(self):
features = [f['pk'] for f in self.provider.getFeatures(QgsFeatureRequest().setFilterRect(extent))]
assert set(features) == set([2, 4]), 'Got {} instead'.format(features)

def testGetFeaturesPolyFilterRectTests(self):
""" Test fetching features from a polygon layer with filter rect"""
try:
if not self.poly_provider:
return
except:
return

extent = QgsRectangle(-73, 70, -63, 80)
features = [f['pk'] for f in self.poly_provider.getFeatures(QgsFeatureRequest().setFilterRect(extent))]
# Some providers may return the exact intersection matches (2, 3) even without the ExactIntersect flag, so we accept that too
assert set(features) == set([2, 3]) or set(features) == set([1, 2, 3]), 'Got {} instead'.format(features)

# Test with exact intersection
features = [f['pk'] for f in self.poly_provider.getFeatures(QgsFeatureRequest().setFilterRect(extent).setFlags(QgsFeatureRequest.ExactIntersect))]
assert set(features) == set([2, 3]), 'Got {} instead'.format(features)

def testRectAndExpression(self):
extent = QgsRectangle(-70, 67, -60, 80)
result = set([f['pk'] for f in self.provider.getFeatures(
Expand Down
46 changes: 46 additions & 0 deletions tests/src/python/test_provider_memory.py
Expand Up @@ -65,6 +65,29 @@ def setUpClass(cls):

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

# poly layer
cls.poly_vl = QgsVectorLayer(u'Polygon?crs=epsg:4326&field=pk:integer&key=pk',
u'test', u'memory')
assert (cls.poly_vl.isValid())
cls.poly_provider = cls.poly_vl.dataProvider()

f1 = QgsFeature()
f1.setAttributes([1])
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))'))

f2 = QgsFeature()
f2.setAttributes([2])
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))'))

f3 = QgsFeature()
f3.setAttributes([3])
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))'))

f4 = QgsFeature()
f4.setAttributes([4])

cls.poly_provider.addFeatures([f1, f2, f3, f4])

@classmethod
def tearDownClass(cls):
"""Run after all tests"""
Expand Down Expand Up @@ -251,6 +274,29 @@ def setUpClass(cls):

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

# poly layer
cls.poly_vl = QgsVectorLayer(u'Polygon?crs=epsg:4326&index=yes&field=pk:integer&key=pk',
u'test', u'memory')
assert (cls.poly_vl.isValid())
cls.poly_provider = cls.poly_vl.dataProvider()

f1 = QgsFeature()
f1.setAttributes([1])
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))'))

f2 = QgsFeature()
f2.setAttributes([2])
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))'))

f3 = QgsFeature()
f3.setAttributes([3])
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))'))

f4 = QgsFeature()
f4.setAttributes([4])

cls.poly_provider.addFeatures([f1, f2, f3, f4])

@classmethod
def tearDownClass(cls):
"""Run after all tests"""
Expand Down
5 changes: 4 additions & 1 deletion tests/src/python/test_provider_postgres.py
Expand Up @@ -38,10 +38,13 @@ def setUpClass(cls):
cls.dbconn = u'dbname=\'qgis_test\' host=localhost port=5432 user=\'postgres\' password=\'postgres\''
if 'QGIS_PGTEST_DB' in os.environ:
cls.dbconn = os.environ['QGIS_PGTEST_DB']
# Create test layer
# Create test layers
cls.vl = QgsVectorLayer(cls.dbconn + ' sslmode=disable key=\'pk\' srid=4326 type=POINT table="qgis_test"."someData" (geom) sql=', 'test', 'postgres')
assert(cls.vl.isValid())
cls.provider = cls.vl.dataProvider()
cls.poly_vl = QgsVectorLayer(cls.dbconn + ' sslmode=disable key=\'pk\' srid=4326 type=POLYGON table="qgis_test"."some_poly_data" (geom) sql=', 'test', 'postgres')
assert(cls.poly_vl.isValid())
cls.poly_provider = cls.poly_vl.dataProvider()

@classmethod
def tearDownClass(cls):
Expand Down
6 changes: 6 additions & 0 deletions tests/src/python/test_provider_shapefile.py
Expand Up @@ -43,11 +43,17 @@ def setUpClass(cls):
for file in glob.glob(os.path.join(srcpath, 'shapefile.*')):
shutil.copy(os.path.join(srcpath, file), cls.basetestpath)
shutil.copy(os.path.join(srcpath, file), cls.repackfilepath)
for file in glob.glob(os.path.join(srcpath, 'shapefile_poly.*')):
shutil.copy(os.path.join(srcpath, file), cls.basetestpath)
cls.basetestfile = os.path.join(cls.basetestpath, 'shapefile.shp')
cls.repackfile = os.path.join(cls.repackfilepath, 'shapefile.shp')
cls.basetestpolyfile = os.path.join(cls.basetestpath, 'shapefile_poly.shp')
cls.vl = QgsVectorLayer(u'{}|layerid=0'.format(cls.basetestfile), u'test', u'ogr')
assert (cls.vl.isValid())
cls.provider = cls.vl.dataProvider()
cls.vl_poly = QgsVectorLayer(u'{}|layerid=0'.format(cls.basetestpolyfile), u'test', u'ogr')
assert (cls.vl_poly.isValid())
cls.poly_provider = cls.vl_poly.dataProvider()

@classmethod
def tearDownClass(cls):
Expand Down
4 changes: 4 additions & 0 deletions tests/src/python/test_provider_spatialite.py
Expand Up @@ -52,6 +52,10 @@ def setUpClass(cls):
assert(cls.vl.isValid())
cls.provider = cls.vl.dataProvider()

cls.vl_poly = QgsVectorLayer('dbname=\'{}/provider/spatialite.db\' table="somepolydata" (geom) sql='.format(TEST_DATA_DIR), 'test', 'spatialite')
assert(cls.vl_poly.isValid())
cls.poly_provider = cls.vl_poly.dataProvider()

# create test db
cls.dbname = os.path.join(tempfile.gettempdir(), "test.sqlite")
if os.path.exists(cls.dbname):
Expand Down
8 changes: 8 additions & 0 deletions tests/src/python/test_provider_virtual.py
Expand Up @@ -68,6 +68,14 @@ def setUpClass(cls):
assert (cls.vl.isValid())
cls.provider = cls.vl.dataProvider()

shp_poly = os.path.join(TEST_DATA_DIR, 'provider/shapefile_poly.shp')
d = QgsVirtualLayerDefinition()
d.addSource("vtab2", shp_poly, "ogr")
d.setUid("pk")
cls.poly_vl = QgsVectorLayer(d.toString(), u'test_poly', u'virtual')
assert (cls.poly_vl.isValid())
cls.poly_provider = cls.poly_vl.dataProvider()

@classmethod
def tearDownClass(cls):
"""Run after all tests"""
Expand Down
14 changes: 14 additions & 0 deletions tests/src/python/test_qgsdelimitedtextprovider.py
Expand Up @@ -370,6 +370,20 @@ def setUpClass(cls):
assert cls.vl.isValid(), "{} is invalid".format(cls.basetestfile)
cls.provider = cls.vl.dataProvider()

cls.basetestpolyfile = os.path.join(srcpath, 'delimited_wkt_poly.csv')

url = QUrl.fromLocalFile(cls.basetestpolyfile)
url.addQueryItem("crs", "epsg:4326")
url.addQueryItem("type", "csv")
url.addQueryItem("wktField", "wkt")
url.addQueryItem("spatialIndex", "no")
url.addQueryItem("subsetIndex", "no")
url.addQueryItem("watchFile", "no")

cls.vl_poly = QgsVectorLayer(url.toString(), u'test_polygon', u'delimitedtext')
assert cls.vl_poly.isValid(), "{} is invalid".format(cls.basetestpolyfile)
cls.poly_provider = cls.vl_poly.dataProvider()

@classmethod
def tearDownClass(cls):
"""Run after all tests"""
Expand Down
6 changes: 6 additions & 0 deletions tests/testdata/provider/delimited_wkt_poly.csv
@@ -0,0 +1,6 @@
pk,wkt
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))"
2,"Polygon ((-67.6 81.2, -66.3 81.2, -66.3 76.9, -67.6 76.9, -67.6 81.2))"
3,"Polygon ((-68.4 75.8, -67.5 72.6, -68.6 73.7, -70.2 72.9, -68.4 75.8))"
4,

Binary file added tests/testdata/provider/shapefile_poly.dbf
Binary file not shown.
1 change: 1 addition & 0 deletions tests/testdata/provider/shapefile_poly.prj
@@ -0,0 +1 @@
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
1 change: 1 addition & 0 deletions tests/testdata/provider/shapefile_poly.qpj
@@ -0,0 +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"]]
Binary file added tests/testdata/provider/shapefile_poly.shp
Binary file not shown.
Binary file added tests/testdata/provider/shapefile_poly.shx
Binary file not shown.
Binary file modified tests/testdata/provider/spatialite.db
Binary file not shown.
10 changes: 10 additions & 0 deletions tests/testdata/provider/testdata_pg.sql
Expand Up @@ -42,6 +42,10 @@ CREATE TABLE qgis_test."someData" (
geom public.geometry(Point,4326)
);

CREATE TABLE qgis_test."some_poly_data" (
pk SERIAL NOT NULL,
geom public.geometry(Polygon,4326)
);

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

INSERT INTO qgis_test."some_poly_data" (pk, geom) VALUES
(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) ),
(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) ),
(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) ),
(4, NULL)
;

--
-- TOC entry 3953 (class 2606 OID 377768)
Expand Down

0 comments on commit 96d8986

Please sign in to comment.