Skip to content

Commit

Permalink
Add subLayerCount for OGR provider (GPKG file) instead of returning 0
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry authored and github-actions[bot] committed Jan 29, 2021
1 parent 9563945 commit e020611
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/core/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -934,6 +934,19 @@ void QgsOgrProvider::addSubLayerDetailsToSubLayerList( int i, QgsOgrLayer *layer
}
}

uint QgsOgrProvider::subLayerCount() const
{
uint count = layerCount();

QString errCause;
QgsOgrLayerUniquePtr layerStyles = QgsOgrProviderUtils::getLayer( mFilePath, "layer_styles", errCause );
if ( layerStyles )
{
count--;
}
return count;
}

QStringList QgsOgrProvider::subLayers() const
{
return _subLayers( true );
Expand Down
7 changes: 7 additions & 0 deletions src/core/providers/ogr/qgsogrprovider.h
Expand Up @@ -105,6 +105,13 @@ class QgsOgrProvider final: public QgsVectorDataProvider
QgsAbstractFeatureSource *featureSource() const override;

QgsCoordinateReferenceSystem crs() const override;

/**
* Gets the number of sublayer in the OGR datasource.
* layer_styles is not counted.
* \since QGIS 3.16
*/
uint subLayerCount() const override;
QStringList subLayers() const override;
QgsLayerMetadata layerMetadata() const override;
QStringList subLayersWithoutFeatureCount() const;
Expand Down
7 changes: 7 additions & 0 deletions tests/src/python/test_provider_ogr_gpkg.py
@@ -1,6 +1,9 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for the OGR/GPKG provider.
From build dir, run:
ctest -R PyQgsOGRProviderGpkg -V
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
Expand Down Expand Up @@ -328,6 +331,7 @@ def testCurveGeometryType(self):
ds = None

vl = QgsVectorLayer('{}'.format(tmpfile), 'test', 'ogr')
self.assertEqual(1, vl.dataProvider().subLayerCount())
self.assertEqual(vl.dataProvider().subLayers(),
[QgsDataProvider.SUBLAYER_SEPARATOR.join(['0', 'test', '0', 'CurvePolygon', 'geom', ''])])
f = QgsFeature()
Expand Down Expand Up @@ -683,6 +687,7 @@ def testStyle(self):
# Check that layers_style table is not list in subLayers()
vl = QgsVectorLayer(tmpfile, 'test', 'ogr')
sublayers = vl.dataProvider().subLayers()
self.assertEqual(2, vl.dataProvider().subLayerCount())
self.assertEqual(len(sublayers), 2, sublayers)

def testDisablewalForSqlite3(self):
Expand Down Expand Up @@ -960,6 +965,7 @@ def testSublayerWithComplexLayerName(self):
f = None

vl = QgsVectorLayer(u'{}'.format(tmpfile), u'layer', u'ogr')
self.assertEqual(1, vl.dataProvider().subLayerCount())
self.assertEqual(vl.dataProvider().subLayers(),
[QgsDataProvider.SUBLAYER_SEPARATOR.join(['0', 'layer1:', '1', 'Point', 'geom:', ''])])

Expand Down Expand Up @@ -1032,6 +1038,7 @@ def testGeopackageRefreshIfTableListUpdated(self):
ds = None

vl2 = QgsVectorLayer(u'{}'.format(tmpfile), 'test', u'ogr')
self.assertEqual(2, vl2.dataProvider().subLayerCount())
vl2.subLayers()
self.assertEqual(vl2.dataProvider().subLayers(),
[QgsDataProvider.SUBLAYER_SEPARATOR.join(['0', 'test', '0', 'Point', 'geom', '']),
Expand Down

0 comments on commit e020611

Please sign in to comment.