Skip to content

Commit

Permalink
QgsRasterLayer: readXml(): read layer notes even when opening with Fl…
Browse files Browse the repository at this point in the history
…agDontResolveLayers

Fixes qgis#58818

(cherry picked from commit 0c9320c)
  • Loading branch information
rouault authored and nyalldawson committed Oct 17, 2024
1 parent 50404e1 commit 70e1a5d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/core/raster/qgsrasterlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2211,9 +2211,9 @@ bool QgsRasterLayer::readSymbology( const QDomNode &layer_node, QString &errorMe
{
const QString rendererType = rasterRendererElem.attribute( QStringLiteral( "type" ) );
QgsRasterRendererRegistryEntry rendererEntry;
if ( QgsApplication::rasterRendererRegistry()->rendererData( rendererType, rendererEntry ) )
if ( mDataProvider && QgsApplication::rasterRendererRegistry()->rendererData( rendererType, rendererEntry ) )
{
QgsRasterRenderer *renderer = rendererEntry.rendererCreateFunction( rasterRendererElem, dataProvider() );
QgsRasterRenderer *renderer = rendererEntry.rendererCreateFunction( rasterRendererElem, mDataProvider );
mPipe->set( renderer );
}
}
Expand Down Expand Up @@ -2402,8 +2402,8 @@ bool QgsRasterLayer::readXml( const QDomNode &layer_node, QgsReadWriteContext &c
if ( !( mReadFlags & QgsMapLayer::FlagDontResolveLayers ) )
{
QgsDebugError( QStringLiteral( "Raster data provider could not be created for %1" ).arg( mDataSource ) );
return false;
}
return false;
}

QString error;
Expand Down Expand Up @@ -2442,7 +2442,7 @@ bool QgsRasterLayer::readXml( const QDomNode &layer_node, QgsReadWriteContext &c

const QDomNodeList noDataBandList = noDataElement.elementsByTagName( QStringLiteral( "noDataList" ) );

for ( int i = 0; i < noDataBandList.size(); ++i )
for ( int i = 0; mDataProvider && i < noDataBandList.size(); ++i )
{
const QDomElement bandElement = noDataBandList.at( i ).toElement();
bool ok;
Expand Down
26 changes: 26 additions & 0 deletions tests/src/python/test_qgsproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
QgsExpressionContextUtils,
QgsFeature,
QgsGeometry,
QgsLayerNotesUtils,
QgsLabelingEngineSettings,
QgsMapLayer,
QgsProject,
Expand Down Expand Up @@ -1611,6 +1612,31 @@ def test_remember_evaluate_default_values(self):
self.assertEqual(layers[0].dataProvider().providerProperty(QgsDataProvider.EvaluateDefaultValues, None), True)
self.assertEqual(layers[1].dataProvider().providerProperty(QgsDataProvider.EvaluateDefaultValues, None), True)

def testRasterLayerFlagDontResolveLayers(self):
"""
Test that we can read layer notes from a raster layer when opening with FlagDontResolveLayers
"""
tmpDir = QTemporaryDir()
tmpFile = f"{tmpDir.path()}/project.qgs"
copyfile(os.path.join(TEST_DATA_DIR, "landsat_4326.tif"), os.path.join(tmpDir.path(), "landsat_4326.tif"))

project = QgsProject()

l = QgsRasterLayer(os.path.join(tmpDir.path(), "landsat_4326.tif"), "landsat", "gdal")
self.assertTrue(l.isValid())
QgsLayerNotesUtils.setLayerNotes(l, 'my notes')
self.assertTrue(project.addMapLayers([l]))
self.assertTrue(project.write(tmpFile))
del project

# Read the project with FlagDontResolveLayers
project = QgsProject()
self.assertTrue(project.read(tmpFile, QgsProject.FlagDontResolveLayers))
layers = list(project.mapLayers().values())
self.assertEqual(QgsLayerNotesUtils.layerNotes(layers[0]), "my notes")

del project


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

0 comments on commit 70e1a5d

Please sign in to comment.