10

I am new to the QGIS python console. I am trying to load a raster layer from the console, set the crs and a specific style from a qml file.

layerPath = os.path.join(raster_dir,raster_name)
fileInfo = QtCore.QFileInfo(layerPath)
baseName = fileInfo.baseName()
layer = QgsRasterLayer(layerPath, baseName)

I would like to set up the projection info and then load it to QGIS

QgsMapLayerRegistry.instance().addMapLayer(layer)

I do have a grey raster on the windows. How can I set up the qml file to load ?

Thanks for your help

underdark
  • 84,148
  • 21
  • 231
  • 413
Renaud
  • 871
  • 3
  • 9
  • 20

1 Answers1

18

use QgsMapLayer::loadNamedStyle

uri = "/home/user/style.qml"
layer.loadNamedStyle(uri) 
underdark
  • 84,148
  • 21
  • 231
  • 413
jef
  • 2,717
  • 16
  • 18
  • Thank for your help, Unfortunatly, it does'nt not work. How shall write the uri ? – Renaud Jun 06 '12 at 09:12
  • Thank you for your help, However I cannot manage to get it working.When I run the following command – Renaud Jun 06 '12 at 09:31
  • 1
    Thank you for your help, However I cannot manage to get it working.When I run the following command uri = "file:///home/renaud/palette.qml";layer.loadNamedStyle(uri) I get the error message : Style not found in databased. Thank for your help ? How do I jump line in this post ? – Renaud Jun 06 '12 at 09:37
  • looks like theUri is a bit misleading - using the plain filename should work (ie. /home/renaud/palette.qml) – jef Jun 06 '12 at 13:25
  • Thank You, It is working fine now. One more time, keep it simple is the best way ! – Renaud Jun 07 '12 at 17:53