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_36] [OGR] Fix load of stored styles #57301

Merged
merged 2 commits into from
May 1, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/core/qgsogrutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2860,9 +2860,9 @@ QString QgsOgrUtils::loadStoredStyle( GDALDatasetH hDS, const QString &layerName
" AND f_table_name=%1"
" AND f_geometry_column=%2"
" ORDER BY CASE WHEN useAsDefault THEN 1 ELSE 2 END"
",update_time DESC LIMIT 1" )
.arg( QgsOgrProviderUtils::quotedValue( layerName ) )
.arg( QgsOgrProviderUtils::quotedValue( geomColumn ) );
",update_time DESC" )
.arg( QgsOgrProviderUtils::quotedValue( layerName ),
QgsOgrProviderUtils::quotedValue( geomColumn ) );
OGR_L_SetAttributeFilter( hLayer, selectQmlQuery.toUtf8().constData() );
OGR_L_ResetReading( hLayer );
OGRFeatureDefnH hLayerDefn = OGR_L_GetLayerDefn( hLayer );
Expand Down
63 changes: 63 additions & 0 deletions tests/src/core/testqgsogrutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
#include "qgssymbol.h"
#include "qgsfielddomain.h"
#include "qgsweakrelation.h"
#include "qgsogrproviderutils.h"
#include "qgssinglesymbolrenderer.h"

class TestQgsOgrUtils: public QObject
{
Expand Down Expand Up @@ -71,6 +73,7 @@ class TestQgsOgrUtils: public QObject
void testVariantTypeToOgrFieldType();
void testOgrStringToVariant_data();
void testOgrStringToVariant();
void testOgrUtilsStoredStyle();

#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3,3,0)
void testConvertFieldDomain();
Expand Down Expand Up @@ -1045,6 +1048,66 @@ void TestQgsOgrUtils::testOgrStringToVariant()
QCOMPARE( res, expected );
}

/**
* Test for issue GH #57251
*/
void TestQgsOgrUtils::testOgrUtilsStoredStyle()
{
// Create a test GPKG file with layer in a temporary directory
QTemporaryDir tempDir;
QVERIFY( tempDir.isValid() );
QString tempDirPath = tempDir.path();
QString testFile = tempDirPath + "/test.gpkg";
// Create datasource
QString error;
QVERIFY( QgsOgrProviderUtils::createEmptyDataSource( testFile, QStringLiteral( "GPKG" ),
QStringLiteral( "UTF-8" ), Qgis::WkbType::Point, QList< QPair<QString, QString> >(),
QgsCoordinateReferenceSystem::fromEpsgId( 4326 ), error ) );

{
// Open the datasource
QgsVectorLayer vl = QgsVectorLayer( testFile, QStringLiteral( "test" ), QStringLiteral( "ogr" ) );
QVERIFY( vl.isValid() );

QgsSingleSymbolRenderer *renderer { static_cast<QgsSingleSymbolRenderer *>( vl.renderer() ) };
QVERIFY( renderer );
QgsSymbol *symbol = renderer->symbol()->clone();

// Store styles in the DB

symbol->setColor( QColor( 255, 0, 0 ) );
renderer->setSymbol( symbol );
vl.saveStyleToDatabase( "style1", "style1", false, QString(), error );

// Default
symbol = renderer->symbol()->clone();
symbol->setColor( QColor( 0, 255, 0 ) );
renderer->setSymbol( symbol );
vl.saveStyleToDatabase( "style2", "style2", true, QString(), error );

symbol = renderer->symbol()->clone();
symbol->setColor( QColor( 0, 0, 255 ) );
renderer->setSymbol( symbol );
vl.saveStyleToDatabase( "style3", "style3", false, QString(), error );
}

gdal::ogr_datasource_unique_ptr hDS( OGROpen( testFile.toUtf8().constData(), false, nullptr ) );

QString styleName;
QgsOgrUtils::loadStoredStyle( hDS.get(), QStringLiteral( "test" ), QStringLiteral( "geom" ), styleName, error );
QCOMPARE( styleName, QStringLiteral( "style2" ) );

QStringList ids;
QStringList names;
QStringList descriptions;
QgsOgrUtils::listStyles( hDS.get(), QStringLiteral( "test" ), QStringLiteral( "geom" ), ids, names, descriptions, error );
QCOMPARE( ids.size(), 3 );
QCOMPARE( names.size(), 3 );
QCOMPARE( descriptions.size(), 3 );
QCOMPARE( QSet<QString>( names.constBegin(), names.constEnd() ), QSet<QString>() << QStringLiteral( "style1" ) << QStringLiteral( "style2" ) << QStringLiteral( "style3" ) );

}

#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3,3,0)
void TestQgsOgrUtils::testConvertFieldDomain()
{
Expand Down