Skip to content

Commit

Permalink
[ogr] Read Geopackage layer identifier and description into QGIS
Browse files Browse the repository at this point in the history
layer metadata automatically

Fixes #21293
  • Loading branch information
nyalldawson committed Feb 18, 2019
1 parent b711ef0 commit 5ced04e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/providers/ogr/qgsogrprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,21 @@ QgsOgrProvider::QgsOgrProvider( QString const &uri, const ProviderOptions &optio

bool supportsBoolean = false;

// layer metadata
mLayerMetadata.setType( QStringLiteral( "dataset" ) );
if ( mOgrOrigLayer )
{
QMutex *mutex = nullptr;
OGRLayerH layer = mOgrOrigLayer->getHandleAndMutex( mutex );
QMutexLocker locker( mutex );
const QString identifier = GDALGetMetadataItem( layer, "IDENTIFIER", nullptr );
if ( !identifier.isEmpty() )
mLayerMetadata.setTitle( identifier ); // see geopackage specs -- "'identifier' is analogous to 'title'"
const QString abstract = GDALGetMetadataItem( layer, "DESCRIPTION", nullptr );
if ( !abstract.isEmpty() )
mLayerMetadata.setAbstract( abstract );
}

#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(2,3,0)
if ( mOgrOrigLayer )
{
Expand Down Expand Up @@ -855,6 +870,11 @@ QStringList QgsOgrProvider::subLayers() const
return _subLayers( true );
}

QgsLayerMetadata QgsOgrProvider::layerMetadata() const
{
return mLayerMetadata;
}

QStringList QgsOgrProvider::subLayersWithoutFeatureCount() const
{
return _subLayers( false );
Expand Down Expand Up @@ -2617,6 +2637,8 @@ void QgsOgrProvider::computeCapabilities()
}
}

ability |= ReadLayerMetadata;

if ( updateModeActivated )
leaveUpdateMode();

Expand Down
3 changes: 3 additions & 0 deletions src/providers/ogr/qgsogrprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class QgsOgrProvider : public QgsVectorDataProvider

QgsCoordinateReferenceSystem crs() const override;
QStringList subLayers() const override;
QgsLayerMetadata layerMetadata() const override;
QStringList subLayersWithoutFeatureCount() const;
QString storageType() const override;
QgsFeatureIterator getFeatures( const QgsFeatureRequest &request ) const override;
Expand Down Expand Up @@ -248,6 +249,8 @@ class QgsOgrProvider : public QgsVectorDataProvider
//! Original layer (not a SQL result layer)
QgsOgrLayerUniquePtr mOgrOrigLayer;

QgsLayerMetadata mLayerMetadata;

//! path to filename
QString mFilePath;

Expand Down
19 changes: 19 additions & 0 deletions tests/src/python/test_provider_ogr_gpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,25 @@ def test_quote_identifier(self):
for i in range(1, len(vl.fields())):
self.assertEqual(vl.uniqueValues(i), {'a', 'b', 'c'})

def testGeopackageLayerMetadata(self):
"""
Geopackage layer description and identifier should be read into layer metadata automatically
"""
tmpfile = os.path.join(self.basetestpath, 'testGeopackageLayerMetadata.gpkg')
ds = ogr.GetDriverByName('GPKG').CreateDataSource(tmpfile)
lyr = ds.CreateLayer('layer1', geom_type=ogr.wkbPoint)
lyr.SetMetadataItem('DESCRIPTION', "my desc")
lyr.SetMetadataItem('IDENTIFIER', "my title") # see geopackage specs -- "'identifier' is analogous to 'title'"
lyr.CreateField(ogr.FieldDefn('attr', ogr.OFTInteger))
f = ogr.Feature(lyr.GetLayerDefn())
f.SetGeometry(ogr.CreateGeometryFromWkt('POINT(0 0)'))
lyr.CreateFeature(f)
f = None
vl1 = QgsVectorLayer(u'{}'.format(tmpfile) + "|layername=" + "layer1", 'test', u'ogr')
self.assertTrue(vl1.isValid())
self.assertEqual(vl1.metadata().title(), 'my title')
self.assertEqual(vl1.metadata().abstract(), 'my desc')


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

0 comments on commit 5ced04e

Please sign in to comment.