2

I want to set QML style file to raster file and save as image. This code does not set the style file when I run it as standalone script. It works fine in QGIS's Python editor. There is no error in the code.

print(res) result : {'OUTPUT': <QgsRasterLayer: 'idw_humidity.tif' (gdal)>}  
            
feedback = QgsProcessingFeedback()
res = processing.run("native:setlayerstyle", {'INPUT':'E:\\GIS\\idw_humidity.tif','STYLE':'E:\\GIS\\idw_humidity_style.qml'},feedback=feedback)
print(res)
        
fileName = "E:/GIS/idw_humidity.tiff"
fileInfo = QFileInfo(fileName)
path = fileInfo.filePath()
baseName = fileInfo.baseName()
rlayer = QgsRasterLayer(path, baseName)
QgsProject.instance().addMapLayer(rlayer)
if rlayer.isValid():
   print ("Layer loaded!")
#rlayer.renderer().setOpacity(0.5) 
extent = rlayer.extent()
rlayer.triggerRepaint()
 
0

1 Answer 1

0

It is not working as standalone script

feedback = QgsProcessingFeedback()
res=processing.run("native:setlayerstyle", {'INPUT':'E:\\GIS\\idw_humidity.tif','STYLE':'E:\\GIS\\idw_humidity_style.qml'},feedback=feedback)
 print(res)

It is working

fileInfo = QFileInfo(fileName)
path=fileInfo.filePath()
baseName = fileInfo.baseName()
print(baseName)
rlayer = QgsRasterLayer(path, baseName)
rlayer.loadNamedStyle(QML_file)
QgsProject.instance().addMapLayer(rlayer)
if rlayer.isValid():
   print ("Layer loaded!")
extent = rlayer.extent()
rlayer.triggerRepaint()

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