Skip to content
Permalink
Browse files

Add polygon layers to provider tests, so we can test ExactIntersect

feature requests
  • Loading branch information
nyalldawson committed Jan 31, 2016
1 parent ce2b3c2 commit 96d8986b9f7ac6f3da02dd6b292d77cb5a1a0619
@@ -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(
@@ -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"""
@@ -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"""
@@ -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):
@@ -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):
@@ -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):
@@ -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"""
@@ -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"""
@@ -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 not shown.
@@ -0,0 +1 @@
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
@@ -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 not shown.
Binary file not shown.
Binary file not shown.
@@ -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)
@@ -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)

0 comments on commit 96d8986

Please sign in to comment.
You can’t perform that action at this time.