Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[OGR provider] Fix conflict between layers using same OGR datasource …
…and layer with subset strings (fixes #43361)
  • Loading branch information
rouault authored and nyalldawson committed May 31, 2021
1 parent 647b0f0 commit 0889d1b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 0 additions & 2 deletions src/core/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -5235,7 +5235,6 @@ QgsOgrLayerUniquePtr QgsOgrProviderUtils::getLayer( const QString &dsName,
ds->hDS, layerIndex );
if ( hLayer )
{
OGR_L_SetAttributeFilter( hLayer, nullptr );
layerName = QString::fromUtf8( OGR_L_GetName( hLayer ) );
}
}
Expand Down Expand Up @@ -5292,7 +5291,6 @@ QgsOgrLayerUniquePtr QgsOgrProviderUtils::getLayer( const QString &dsName,
ds->hDS, layerIndex );
if ( hLayer )
{
OGR_L_SetAttributeFilter( hLayer, nullptr );
layerName = QString::fromUtf8( OGR_L_GetName( hLayer ) );
}
}
Expand Down
26 changes: 26 additions & 0 deletions tests/src/python/test_provider_shapefile.py
Expand Up @@ -22,12 +22,14 @@
from osgeo import gdal
from qgis.core import (
QgsApplication,
QgsDataProvider,
QgsSettings,
QgsFeature,
QgsField,
QgsGeometry,
QgsVectorLayer,
QgsFeatureRequest,
QgsRectangle,
QgsVectorDataProvider,
QgsWkbTypes,
QgsVectorLayerExporter,
Expand Down Expand Up @@ -1079,6 +1081,30 @@ def testEncoding(self):
self.assertTrue(vl.isValid())
self.assertEqual([f.attributes() for f in vl.dataProvider().getFeatures()], [['abcŐ'], ['abcŐabcŐabcŐ']])

def testLayersOnSameOGRLayerWithAndWithoutFilter(self):
"""Test fix for https://github.com/qgis/QGIS/issues/43361"""
file_path = os.path.join(TEST_DATA_DIR, 'provider', 'shapefile.shp')
uri = '{}|layerId=0|subset="name" = \'Apple\''.format(file_path)
options = QgsDataProvider.ProviderOptions()
vl1 = QgsVectorLayer(uri, 'vl1', 'ogr')
vl2 = QgsVectorLayer(uri, 'vl2', 'ogr')
vl3 = QgsVectorLayer('{}|layerId=0'.format(file_path), 'vl3', 'ogr')
self.assertEqual(vl1.featureCount(), 1)
vl1_extent = QgsGeometry.fromRect(vl1.extent())
self.assertEqual(vl2.featureCount(), 1)
vl2_extent = QgsGeometry.fromRect(vl2.extent())
self.assertEqual(vl3.featureCount(), 5)
vl3_extent = QgsGeometry.fromRect(vl3.extent())

reference = QgsGeometry.fromRect(QgsRectangle(-68.2, 70.8, -68.2, 70.8))
assert QgsGeometry.compare(vl1_extent.asPolygon()[0], reference.asPolygon()[0],
0.00001), 'Expected {}, got {}'.format(reference.asWkt(), vl1_extent.asWkt())
assert QgsGeometry.compare(vl2_extent.asPolygon()[0], reference.asPolygon()[0],
0.00001), 'Expected {}, got {}'.format(reference.asWkt(), vl2_extent.asWkt())
reference = QgsGeometry.fromRect(QgsRectangle(-71.123, 66.33, -65.32, 78.3))
assert QgsGeometry.compare(vl3_extent.asPolygon()[0], reference.asPolygon()[0],
0.00001), 'Expected {}, got {}'.format(reference.asWkt(), vl3_extent.asWkt())


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

0 comments on commit 0889d1b

Please sign in to comment.