Sign up ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

Do you have any idea ? Is it possible to specify a qml file where the crs is defined ?

Thank you for your help

share|improve this question
How do you disable the crs prompt? – user3163940 Mar 14 '14 at 15:53

3 Answers 3

According to: and 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)
share|improve this answer

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", "useGlobal" ).toString()
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 )
share|improve this answer
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

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)
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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