I manipulating netcdf raster data from the QGIS python console. The metadata of my netcdf file does not provide the Reference system. However I know it is 'EPSG:4326' I am writing a python script to perform tasks automatically, I don't want user interaction.

I am using the following commands :

fileName = "/path/to/raster/file.tif"
fileInfo = QFileInfo(fileName)
baseName = fileInfo.baseName()
rlayer = QgsRasterLayer(fileName, baseName)`

But the QGIS windows asked me the Coordinate reference system of this layer. I tried to specified the crs while loading with something like

rlayer = QgsRasterLayer(filename=fileName, basename=baseName, crs='EPSG:4326')

But QGIS crashes.

Is it possible to specify a qml file where the crs is defined ?


Apparently the named parameter notation is causing the crash. Not sure if that should work. Anyway you can set the CRS with QgsMapLayer.setCrs() after you have made sure the crs prompt is disabled:

from PyQt4.QtCore import QFileInfo,QSettings
from qgis.core import QgsRasterLayer, QgsCoordinateReferenceSystem

s = QSettings()
oldValidation = s.value( "/Projections/defaultBehaviour" )
s.setValue( "/Projections/defaultBehaviour", "useGlobal" )

fileName = "/path/to/raster/file.tif"
fileInfo = QFileInfo(fileName)
baseName = fileInfo.baseName()
rlayer = QgsRasterLayer(fileName, baseName)
rlayer.setCrs( QgsCoordinateReferenceSystem(4326, QgsCoordinateReferenceSystem.EpsgCrsId) )

s.setValue( "/Projections/defaultBehaviour", oldValidation )
  • Thank you for your advice. However, the qgis windows used to specify the crs appears with the command rlayer = QgsRasterLayer(fileName, baseName). This is why I was looking for an way to pass it as an argument of the method. – Renaud Jun 20 '12 at 13:41
  • right, then you need to disable the prompt first (and restore it afterwards). See edited answer above. – jef Jun 20 '12 at 16:10
  • QGIS 3 seems to have this setting spelt defaultBehavior (US spelling) and not defaultBehaviour (English spelling). – Spacedman Jul 10 '18 at 7:18

According to: http://www.qgis.org/pyqgis-cookbook/loadlayer.html#raster-layers and http://qgis.org/api/classQgsRasterLayer.html#a65b118c0319a090c9efbf259bfebd55f you provide too much informations.

The CRS used should be the one of the canvas (I suppose...).

And you can have it like this:

from qgis.utils import iface
actual_crs = iface.mapCanvas().mapRenderer().destinationCrs()

If you want to change it:

from qgis import core
from qgis.utils import iface

if iface.mapCanvas().mapRenderer().hasCrsTransformEnabled():    
    my_crs = core.QgsCoordinateReferenceSystem(4326, core.QgsCoordinateReferenceSystem.EpsgCrsId)

I think other way to solve this problem is to set the crs directly to the file stored on disk before creating layer reference in PyQGIS by using the QgsRasterLayer constructor. It would avoid that QGIS window asked for the Coordinate Reference System.To test my suggestion I used a raster layer that it was created without crs (out_pyqgis.tif) and in the QGIS Python Console I assigned the corresponding crs to EPSG 4326 by using this code.

from osgeo import osr, gdal
from gdalconst import *
dataset = gdal.Open('path_to_my_raster/out_pyqgis.tif', GA_Update)
band = dataset.GetRasterBand(1)
srs = osr.SpatialReference()
dataset.SetProjection( srs.ExportToWkt() )
dataset = None

It worked as expected. No more pop up CRS window and the QgsRasterLayer constructor could be used in the condensed way:

rlayer = QgsRasterLayer(fileName, baseName)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.