Skip to content

Commit

Permalink
[OGR provider] Fix conflict between layers using same OGR datasource …
Browse files Browse the repository at this point in the history
…and layer with subset strings (fixes #43361)
  • Loading branch information
rouault authored and nyalldawson committed May 25, 2021
1 parent 6d7e411 commit cf49d25
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
2 changes: 0 additions & 2 deletions src/core/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -5633,7 +5633,6 @@ QgsOgrLayerUniquePtr QgsOgrProviderUtils::getLayer( const QString &dsName,
ds->hDS, layerIndex ); ds->hDS, layerIndex );
if ( hLayer ) if ( hLayer )
{ {
OGR_L_SetAttributeFilter( hLayer, nullptr );
layerName = QString::fromUtf8( OGR_L_GetName( hLayer ) ); layerName = QString::fromUtf8( OGR_L_GetName( hLayer ) );
} }
} }
Expand Down Expand Up @@ -5690,7 +5689,6 @@ QgsOgrLayerUniquePtr QgsOgrProviderUtils::getLayer( const QString &dsName,
ds->hDS, layerIndex ); ds->hDS, layerIndex );
if ( hLayer ) if ( hLayer )
{ {
OGR_L_SetAttributeFilter( hLayer, nullptr );
layerName = QString::fromUtf8( OGR_L_GetName( hLayer ) ); layerName = QString::fromUtf8( OGR_L_GetName( hLayer ) );
} }
} }
Expand Down
25 changes: 25 additions & 0 deletions tests/src/python/test_provider_shapefile.py
Expand Up @@ -30,6 +30,7 @@
QgsVectorLayer, QgsVectorLayer,
QgsFeatureRequest, QgsFeatureRequest,
QgsProviderRegistry, QgsProviderRegistry,
QgsRectangle,
QgsVectorDataProvider, QgsVectorDataProvider,
QgsWkbTypes, QgsWkbTypes,
QgsVectorLayerExporter, QgsVectorLayerExporter,
Expand Down Expand Up @@ -1086,6 +1087,30 @@ def testSkipFeatureCountOnSubLayers(self):
self.assertTrue(len(sublayers) > 1) self.assertTrue(len(sublayers) > 1)
self.assertEqual(sublayers[0].split(QgsDataProvider.sublayerSeparator())[2], '-1') self.assertEqual(sublayers[0].split(QgsDataProvider.sublayerSeparator())[2], '-1')


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__': if __name__ == '__main__':
unittest.main() unittest.main()

0 comments on commit cf49d25

Please sign in to comment.