Skip to content

Commit cd0559d

Browse files
committedJan 25, 2018
[bugfix][spatialite] Update extent when subsetstring is set in the ctor
Fixes #17863 - Zoom to layer has inconsistent behavior with filter
1 parent e9c9b5d commit cd0559d

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
 

‎src/providers/spatialite/qgsspatialiteprovider.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,10 @@ QgsSpatiaLiteProvider::QgsSpatiaLiteProvider( QString const &uri )
602602
<< QgsVectorDataProvider::NativeType( tr( "Array of decimal numbers (double)" ), SPATIALITE_ARRAY_PREFIX.toUpper() + "REAL" + SPATIALITE_ARRAY_SUFFIX.toUpper(), QVariant::List, 0, 0, 0, 0, QVariant::Double )
603603
<< QgsVectorDataProvider::NativeType( tr( "Array of whole numbers (integer)" ), SPATIALITE_ARRAY_PREFIX.toUpper() + "INTEGER" + SPATIALITE_ARRAY_SUFFIX.toUpper(), QVariant::List, 0, 0, 0, 0, QVariant::LongLong )
604604
);
605+
// Update extent and feature count
606+
if ( ! mSubsetString.isEmpty() )
607+
setSubsetString( mSubsetString, true );
608+
605609
mValid = true;
606610
}
607611

‎tests/src/python/test_provider_spatialite.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import qgis # NOQA
1616

1717
import os
18+
import re
1819
import sys
1920
import shutil
2021
import tempfile
@@ -187,6 +188,37 @@ def setUpClass(cls):
187188
sql = "CREATE TABLE test_defaults (id INTEGER NOT NULL PRIMARY KEY, name TEXT DEFAULT 'qgis ''is good', number INTEGER DEFAULT 5, number2 REAL DEFAULT 5.7, no_default REAL)"
188189
cur.execute(sql)
189190

191+
# simple table with catgorized points
192+
sql = "CREATE TABLE test_filter (id INTEGER NOT NULL PRIMARY KEY, name TEXT NOT NULL)"
193+
cur.execute(sql)
194+
sql = "SELECT AddGeometryColumn('test_filter', 'geometry', 4326, 'POINT', 'XY')"
195+
cur.execute(sql)
196+
sql = "INSERT INTO test_filter (id, name, geometry) "
197+
sql += "VALUES (1, 'ext', GeomFromText('POINT(0 0)', 4326))"
198+
cur.execute(sql)
199+
sql = "INSERT INTO test_filter (id, name, geometry) "
200+
sql += "VALUES (2, 'ext', GeomFromText('POINT(0 3)', 4326))"
201+
cur.execute(sql)
202+
sql = "INSERT INTO test_filter (id, name, geometry) "
203+
sql += "VALUES (3, 'ext', GeomFromText('POINT(3 3)', 4326))"
204+
cur.execute(sql)
205+
sql = "INSERT INTO test_filter (id, name, geometry) "
206+
sql += "VALUES (4, 'ext', GeomFromText('POINT(3 0)', 4326))"
207+
cur.execute(sql)
208+
209+
sql = "INSERT INTO test_filter (id, name, geometry) "
210+
sql += "VALUES (5, 'int', GeomFromText('POINT(1 1)', 4326))"
211+
cur.execute(sql)
212+
sql = "INSERT INTO test_filter (id, name, geometry) "
213+
sql += "VALUES (6, 'int', GeomFromText('POINT(1 2)', 4326))"
214+
cur.execute(sql)
215+
sql = "INSERT INTO test_filter (id, name, geometry) "
216+
sql += "VALUES (7, 'int', GeomFromText('POINT(2 2)', 4326))"
217+
cur.execute(sql)
218+
sql = "INSERT INTO test_filter (id, name, geometry) "
219+
sql += "VALUES (8, 'int', GeomFromText('POINT(2 1)', 4326))"
220+
cur.execute(sql)
221+
190222
cur.execute("COMMIT")
191223
con.close()
192224

@@ -663,6 +695,52 @@ def testCreateAttributeIndex(self):
663695
self.assertEqual(set(indexed_columns), set(['name', 'number']))
664696
con.close()
665697

698+
def testSubsetStringExtent_bug17863(self):
699+
"""Check that the extent is correct when applied in the ctor and when
700+
modified after a subset string is set """
701+
702+
def _lessdigits(s):
703+
return re.sub(r'(\d+\.\d{3})\d+', r'\1', s)
704+
705+
testPath = "dbname=%s table='test_filter' (geometry) key='id'" % self.dbname
706+
707+
subSetString = '"name" = \'int\''
708+
subSet = ' sql=%s' % subSetString
709+
710+
# unfiltered
711+
vl = QgsVectorLayer(testPath, 'test', 'spatialite')
712+
self.assertTrue(vl.isValid())
713+
self.assertEqual(vl.featureCount(), 8)
714+
unfiltered_extent = _lessdigits(vl.extent().toString())
715+
self.assertNotEqual('Empty', unfiltered_extent)
716+
del(vl)
717+
718+
# filter after construction ...
719+
subSet_vl2 = QgsVectorLayer(testPath, 'test', 'spatialite')
720+
self.assertEqual(_lessdigits(subSet_vl2.extent().toString()), unfiltered_extent)
721+
self.assertEqual(subSet_vl2.featureCount(), 8)
722+
# ... apply filter now!
723+
subSet_vl2.setSubsetString(subSetString)
724+
self.assertEqual(subSet_vl2.featureCount(), 4)
725+
self.assertEqual(subSet_vl2.subsetString(), subSetString)
726+
self.assertNotEqual(_lessdigits(subSet_vl2.extent().toString()), unfiltered_extent)
727+
filtered_extent = _lessdigits(subSet_vl2.extent().toString())
728+
del(subSet_vl2)
729+
730+
# filtered in constructor
731+
subSet_vl = QgsVectorLayer(testPath + subSet, 'subset_test', 'spatialite')
732+
self.assertEqual(subSet_vl.subsetString(), subSetString)
733+
self.assertTrue(subSet_vl.isValid())
734+
735+
# This was failing in bug 17863
736+
self.assertEqual(subSet_vl.featureCount(), 4)
737+
self.assertEqual(_lessdigits(subSet_vl.extent().toString()), filtered_extent)
738+
self.assertNotEqual(_lessdigits(subSet_vl.extent().toString()), unfiltered_extent)
739+
740+
self.assertTrue(subSet_vl.setSubsetString(''))
741+
self.assertEqual(subSet_vl.featureCount(), 8)
742+
self.assertEqual(_lessdigits(subSet_vl.extent().toString()), unfiltered_extent)
743+
666744

667745
if __name__ == '__main__':
668746
unittest.main()

0 commit comments

Comments
 (0)