Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport LTR] [OGR provider] Fix conflict between layers using same OGR datasource … #43394

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/core/providers/ogr/qgsogrprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5195,7 +5195,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 @@ -5252,7 +5251,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
Original file line number Diff line number Diff line change
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()