Skip to content
Permalink
Browse files

Merge pull request #37919 from rldhont/fix-processing-gdal-assignproj…

…ection-project-layer

[Processing] Fix GDAL Assign Projection: update QgsRasterLayer crs
  • Loading branch information
rldhont committed Aug 1, 2020
2 parents 8f97788 + f5b819f commit acc750c7e3f870ec8245e7526c1f8a853044097b
@@ -28,7 +28,8 @@
from qgis.core import (QgsProcessingException,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterCrs,
QgsProcessingOutputRasterLayer)
QgsProcessingOutputRasterLayer,
QgsProcessingContext)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.algs.gdal.GdalUtils import GdalUtils

@@ -97,3 +98,37 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
self.setOutputValue(self.OUTPUT, fileName)

return commands

def postProcessAlgorithm(self, context, feedback):
# get output value
fileName = self.output_values.get(self.OUTPUT)
if not fileName:
return {}

# search in context project's layers
if context.project():

for l in context.project().mapLayers().values():

# check the source
if l.source() != fileName:
continue

# reload provider's data
l.dataProvider().reloadData()
l.setCrs(l.dataProvider().crs())
l.triggerRepaint()

# search in context temporary layer store
for l in context.temporaryLayerStore().mapLayers().values():

# check the source
if l.source() != fileName:
continue

# reload provider's data
l.dataProvider().reloadData()
l.setCrs(l.dataProvider().crs())
context.temporaryLayerStore().addMapLayer(l)

return {}
@@ -28,7 +28,9 @@

from qgis.core import (QgsProcessingContext,
QgsProcessingFeedback,
QgsRectangle)
QgsRectangle,
QgsRasterLayer,
QgsProject)

from qgis.testing import (start_app,
unittest)
@@ -133,6 +135,40 @@ def testAssignProjection(self):
'-a_srs EPSG:3111 ' +
source])

@unittest.skipIf(os.environ.get('TRAVIS', '') == 'true',
'gdal_edit.py: not found')
def testRunAssignProjection(self):
# Check that assign projection updates QgsRasterLayer info
# GDAL Assign Projection is based on gdal_edit.py

context = QgsProcessingContext()
feedback = QgsProcessingFeedback()
source = os.path.join(testDataPath, 'dem.tif')
alg = AssignProjection()
alg.initAlgorithm()

with tempfile.TemporaryDirectory() as outdir:
fake_dem = os.path.join(outdir, 'dem-fake-crs.tif')

shutil.copy(source, fake_dem)
self.assertTrue(os.path.exists(fake_dem))

rlayer = QgsRasterLayer(fake_dem, "Fake dem")
self.assertTrue(rlayer.isValid())

self.assertEqual(rlayer.crs().authid(), 'EPSG:4326')

project = QgsProject()
project.setFileName(os.path.join(outdir, 'dem-fake-crs.qgs'))
project.addMapLayer(rlayer)
self.assertEqual(project.count(), 1)

context.setProject(project)

alg.run({'INPUT': fake_dem, 'CRS': 'EPSG:3111'},
context, feedback)
self.assertEqual(rlayer.crs().authid(), 'EPSG:3111')

def testGdalTranslate(self):
context = QgsProcessingContext()
feedback = QgsProcessingFeedback()

0 comments on commit acc750c

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