9

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

17

use QgsMapLayer::loadNamedStyle

uri = "/home/user/style.qml"
layer.loadNamedStyle(uri) 
  • Thank for your help, Unfortunatly, it does'nt not work. How shall write the uri ? – Renaud Jun 6 '12 at 9:12
  • Thank you for your help, However I cannot manage to get it working.When I run the following command – Renaud Jun 6 '12 at 9: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 6 '12 at 9:37
  • looks like theUri is a bit misleading - using the plain filename should work (ie. /home/renaud/palette.qml) – jef Jun 6 '12 at 13:25
  • Thank You, It is working fine now. One more time, keep it simple is the best way ! – Renaud Jun 7 '12 at 17:53

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.