Skip to content
Permalink
Browse files

[ogr] Read Geopackage layer identifier and description into QGIS

layer metadata automatically

Fixes #21293
  • Loading branch information
nyalldawson committed Feb 18, 2019
1 parent b711ef0 commit 5ced04e400b7481c25b37cb409d27c5064bf5ffa
@@ -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 )
{
@@ -855,6 +870,11 @@ QStringList QgsOgrProvider::subLayers() const
return _subLayers( true );
}

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

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

ability |= ReadLayerMetadata;

if ( updateModeActivated )
leaveUpdateMode();

@@ -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;
@@ -248,6 +249,8 @@ class QgsOgrProvider : public QgsVectorDataProvider
//! Original layer (not a SQL result layer)
QgsOgrLayerUniquePtr mOgrOrigLayer;

QgsLayerMetadata mLayerMetadata;

//! path to filename
QString mFilePath;

@@ -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.
You can’t perform that action at this time.