I am 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 for the Coordinate reference system of this layer. I tried to specify 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?


3 Answers 3


Apparently, the named parameter notation is causing the crash. Not sure if that should work. Anyway, you can set the CRS with setCrs() method 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
    Commented Jun 20, 2012 at 13:41
  • right, then you need to disable the prompt first (and restore it afterwards). See edited answer above.
    – jef
    Commented Jun 20, 2012 at 16:10
  • QGIS 3 seems to have this setting spelt defaultBehavior (US spelling) and not defaultBehaviour (English spelling).
    – Spacedman
    Commented Jul 10, 2018 at 7:18

I think another 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 the QGIS window asking for the Coordinate Reference System. To test my suggestion I used a raster layer that 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 a condensed way:

rlayer = QgsRasterLayer(fileName, baseName)

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 canvases (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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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