Take the 2-minute tour ×
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 at 15:53

2 Answers 2

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)
    iface.mapCanvas().mapRenderer().setDestinationCrs(my_crs)
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

Your Answer

 
discard

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.