Skip to content
Permalink
Browse files

Fix raster set data source (with test)

  • Loading branch information
elpaso committed Nov 2, 2018
1 parent 29cbb23 commit 4481127b9abda375fe6f1613f3ad90ffa3e8df9b
Showing with 64 additions and 10 deletions.
  1. +1 −1 src/core/qgsmaplayerlegend.cpp
  2. +15 −7 src/core/raster/qgsrasterlayer.cpp
  3. +48 −2 tests/src/python/test_qgsrasterlayer.py
@@ -307,7 +307,7 @@ QList<QgsLayerTreeModelLegendNode *> QgsDefaultRasterLayerLegend::createLayerTre
QList<QgsLayerTreeModelLegendNode *> nodes;

// temporary solution for WMS. Ideally should be done with a delegate.
if ( mLayer->dataProvider()->supportsLegendGraphic() )
if ( mLayer->dataProvider() && mLayer->dataProvider()->supportsLegendGraphic() )
{
nodes << new QgsWmsLegendNode( nodeLayer );
}
@@ -119,7 +119,6 @@ QgsRasterLayer::QgsRasterLayer( const QString &uri,
{
QgsDebugMsgLevel( QStringLiteral( "Entered" ), 4 );
setProviderType( providerKey );
init();

QgsDataProvider::ProviderOptions providerOptions;

@@ -789,12 +788,21 @@ void QgsRasterLayer::setDataSource( const QString &dataSource, const QString &ba
{
if ( mDataProvider )
closeDataProvider();

init();

for ( int i = mPipe.size() - 1; i >= 0; --i )
{
mPipe.remove( i );
}

mDataSource = dataSource;
mLayerName = baseName;

setDataProvider( provider, options );

if ( mValid )
{

// load default style
bool defaultLoadedFlag = false;
if ( loadDefaultStyleFlag )
@@ -805,14 +813,10 @@ void QgsRasterLayer::setDataSource( const QString &dataSource, const QString &ba
{
setDefaultContrastEnhancement();
}

// TODO: Connect signals from the dataprovider to the qgisapp

emit statusChanged( tr( "QgsRasterLayer created" ) );

// Load default style
}
emit dataSourceChanged();
emit dataChanged();
}

void QgsRasterLayer::closeDataProvider()
@@ -1267,9 +1271,13 @@ QImage QgsRasterLayer::previewAsImage( QSize size, const QColor &bgColor, QImage
{
QImage myQImage( size, format );

if ( ! isValid( ) )
return QImage();

myQImage.setColor( 0, bgColor.rgba() );
myQImage.fill( 0 ); //defaults to white, set to transparent for rendering on a map


QgsRasterViewPort *myRasterViewPort = new QgsRasterViewPort();

double myMapUnitsPerPixel;
@@ -16,16 +16,25 @@
import qgis # NOQA

import os
import filecmp

from qgis.PyQt.QtCore import QFileInfo
from qgis.PyQt.QtGui import QColor
from qgis.PyQt.QtCore import QSize, QFileInfo, Qt, QTemporaryDir

from qgis.PyQt.QtGui import (
QColor,
QImage,
QPainter,
QResizeEvent
)
from qgis.PyQt.QtXml import QDomDocument


from qgis.core import (QgsRaster,
QgsRasterLayer,
QgsReadWriteContext,
QgsColorRampShader,
QgsContrastEnhancement,
QgsDataProvider,
QgsProject,
QgsMapSettings,
QgsPointXY,
@@ -40,6 +49,7 @@
QgsGradientColorRamp)
from utilities import unitTestDataPath
from qgis.testing import start_app, unittest
from qgis.testing.mocked import get_iface

# Convenience instances in case you may need them
# not used in this test
@@ -48,6 +58,14 @@

class TestQgsRasterLayer(unittest.TestCase):

def setUp(self):
self.iface = get_iface()
QgsProject.instance().removeAllMapLayers()

self.iface.mapCanvas().viewport().resize(400, 400)
# For some reason the resizeEvent is not delivered, fake it
self.iface.mapCanvas().resizeEvent(QResizeEvent(QSize(400, 400), self.iface.mapCanvas().size()))

def testIdentify(self):
myPath = os.path.join(unitTestDataPath(), 'landsat.tif')
myFileInfo = QFileInfo(myPath)
@@ -694,6 +712,34 @@ def testClone(self):
# compare xml documents
self.assertEqual(layer_doc.toString(), clone_doc.toString())

def testSetDataSource(self):
"""Test change data source"""

temp_dir = QTemporaryDir()
options = QgsDataProvider.ProviderOptions()
myPath = os.path.join(unitTestDataPath('raster'),
'band1_float32_noct_epsg4326.tif')
myFileInfo = QFileInfo(myPath)
myBaseName = myFileInfo.baseName()
layer = QgsRasterLayer(myPath, myBaseName)

image = layer.previewAsImage(QSize(400, 400))
self.assertFalse(image.isNull())
self.assertTrue(image.save(os.path.join(temp_dir.path(), 'expected.png'), "PNG"))

layer.setDataSource(myPath.replace('4326.tif', '4326-BAD_SOURCE.tif'), 'bad_layer', 'gdal', options)
self.assertFalse(layer.isValid())
image = layer.previewAsImage(QSize(400, 400))
self.assertTrue(image.isNull())

layer.setDataSource(myPath.replace('4326-BAD_SOURCE.tif', '4326.tif'), 'bad_layer', 'gdal', options)
self.assertTrue(layer.isValid())
image = layer.previewAsImage(QSize(400, 400))
self.assertFalse(image.isNull())
self.assertTrue(image.save(os.path.join(temp_dir.path(), 'actual.png'), "PNG"))

self.assertTrue(filecmp.cmp(os.path.join(temp_dir.path(), 'actual.png'), os.path.join(temp_dir.path(), 'expected.png')))


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

0 comments on commit 4481127

Please sign in to comment.
You can’t perform that action at this time.