Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport release-3_34] Fix loading of raster styles if the layer/table name is not in the datasource url #57364

Merged
merged 3 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions src/core/providers/gdal/qgsgdalprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -4562,8 +4561,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 );
}

Expand All @@ -4576,8 +4575,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 );
}

Expand Down Expand Up @@ -4616,8 +4615,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 );
}

Expand All @@ -4636,9 +4635,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():
Expand Down
4 changes: 4 additions & 0 deletions src/core/providers/gdal/qgsgdalprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
Loading