[Backport release-3_36] Fix loading of raster styles if the layer/table name is not in the datasource url (#57365)

* Fix loading of raster styles if the layer/table name is not in the provider uri

* Fix it in other places too

* Add helper function to get layer name and use identifier only for gpkg

---------

Co-authored-by: Marco Hugentobler <marco.hugentobler@sourcepole.ch>
This commit is contained in:
qgis-bot 2024-05-18 00:39:46 +02:00 committed by GitHub
parent 2df9655469
commit 4b08a9cb90
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 8 deletions

View File

@ -37,7 +37,6 @@
#include "qgsrasterpyramid.h"
#include "qgspointxy.h"
#include "qgssettings.h"
#include "qgsogrutils.h"
#include "qgsruntimeprofiler.h"
#include "qgsprovidersublayerdetails.h"
#include "qgsproviderutils.h"
@ -4592,8 +4591,8 @@ int QgsGdalProviderMetadata::listStyles( const QString &uri, QStringList &ids, Q
errCause = QObject::tr( "Cannot open %1." ).arg( uri );
return -1;
}
QVariantMap uriParts = QgsGdalProviderBase::decodeGdalUri( uri );
QString layerName = uriParts["layerName"].toString();
QString layerName = getLayerNameForStyle( uri, ds );
return QgsOgrUtils::listStyles( ds.get(), layerName, "", ids, names, descriptions, errCause );
}
@ -4606,8 +4605,8 @@ bool QgsGdalProviderMetadata::styleExists( const QString &uri, const QString &st
errCause = QObject::tr( "Cannot open %1." ).arg( uri );
return false;
}
QVariantMap uriParts = QgsGdalProviderBase::decodeGdalUri( uri );
QString layerName = uriParts["layerName"] .toString();
QString layerName = getLayerNameForStyle( uri, ds );
return QgsOgrUtils::styleExists( ds.get(), layerName, "", styleId, errCause );
}
@ -4646,8 +4645,8 @@ bool QgsGdalProviderMetadata::saveStyle( const QString &uri, const QString &qmlS
errCause = QObject::tr( "Cannot open %1." ).arg( uri );
return false;
}
QVariantMap uriParts = QgsGdalProviderBase::decodeGdalUri( uri );
QString layerName = uriParts["layerName"].toString();
QString layerName = getLayerNameForStyle( uri, ds );
return QgsOgrUtils::saveStyle( ds.get(), layerName, "", qmlStyle, sldStyle, styleName, styleDescription, uiFileContent, useAsDefault, errCause );
}
@ -4666,9 +4665,27 @@ QString QgsGdalProviderMetadata::loadStoredStyle( const QString &uri, QString &s
errCause = QObject::tr( "Cannot open %1." ).arg( uri );
return QString();
}
QString layerName = getLayerNameForStyle( uri, ds );
return QgsOgrUtils::loadStoredStyle( ds.get(), layerName, "", styleName, errCause );
}
QString QgsGdalProviderMetadata::getLayerNameForStyle( const QString &uri, gdal::dataset_unique_ptr &ds )
{
QVariantMap uriParts = QgsGdalProviderBase::decodeGdalUri( uri );
QString layerName = uriParts["layerName"].toString();
return QgsOgrUtils::loadStoredStyle( ds.get(), layerName, "", styleName, errCause );
if ( layerName.isEmpty() )
{
GDALDriverH driver = GDALGetDatasetDriver( ds.get() );
if ( driver )
{
if ( GDALGetDriverShortName( driver ) == QStringLiteral( "GPKG" ) )
{
layerName = GDALGetMetadataItem( ds.get(), "IDENTIFIER", "" );
}
}
}
return layerName;
}
QgsGdalProviderMetadata::QgsGdalProviderMetadata():

View File

@ -24,6 +24,7 @@
#include "qgsgdalproviderbase.h"
#include "qgsrectangle.h"
#include "qgscolorrampshader.h"
#include "qgsogrutils.h"
#include "qgsrasterbandstats.h"
#include "qgsprovidermetadata.h"
#include "qgsprovidersublayerdetails.h"
@ -415,6 +416,9 @@ class QgsGdalProviderMetadata final: public QgsProviderMetadata
const QString &uiFileContent, bool useAsDefault, QString &errCause ) override;
QString loadStyle( const QString &uri, QString &errCause ) override;
QString loadStoredStyle( const QString &uri, QString &styleName, QString &errCause ) override;
private:
//! Get layer name from gdal url
static QString getLayerNameForStyle( const QString &uri, gdal::dataset_unique_ptr &ds );
};
///@endcond