1

I have to apply style(qml file) to raster . I am doing this with Qgis processing script ,

 import processing 
 input ='E:/data/input.tif'
 style_layer='E:/data/newStyleqgis.qml'
 output_file = 'E:/data/abcde.tif'
 processing.runalg("qgis:setstyleforrasterlayer", input,style_layer)

but their is not a option to generate output, style is set but not applied.

  ALGORITHM: Set style for raster layer
  INPUT <ParameterRaster>
  STYLE <ParameterFile>
  OUTPUT <OutputRaster>

is their any option to save it with the style loaded raster or to pass output as a parameter ,so it will save at path specified. Using the script from c# program.

Updates Script :

  from PyQt4.QtCore import *
  from qgis.core import *
  import qgis.utils
  import processing
  layer = "E:/data/abc.tif"
  output = "E:/data/xyz.tif"
  fileInfo = QFileInfo(layer)
  baseName = fileInfo.baseName()
  filename ='abc.tif'
  rlayer = QgsRasterLayer(filename, baseName)
  # Define the minimum extent of the region
  style_layer='E:/data/newStyleqgis.qml'
  processing.runalg("qgis:setstyleforrasterlayer", layer, style_layer)
  extent = rlayer.extent()
  xmin = extent.xMinimum()
  xmax = extent.xMaximum()
  ymin = extent.yMinimum()
  ymax = extent.yMaximum()
  width =  rlayer.width()
  height =  rlayer.height()
 renderer = rlayer.renderer()
 provider= rlayer.dataProvider()
 crs = rlayer.crs().toWkt()
 pipe = QgsRasterPipe()
 pipe.set(provider.clone())
 pipe.set(renderer.clone())
 file_writer = QgsRasterFileWriter(output)
 file_writer.writeRaster(pipe,width,height,extent,layer.crs())

showing error :NoneType' object has no attribute 'clone'

  • Not sure if you can save rasters with a specific style as when they are loaded into QGIS, their style is automatically set to the default Singleband gray. – Joseph Dec 1 '16 at 10:29
  • yes , if it is not saved then it happen so.. but if a layer is saved after style is loaded then whenever we load in qgis style will remains. – User18 Dec 1 '16 at 11:15
  • Sounds like you want to save your raster as a rendered image. Perhaps this post might help: QGis Save Raster as Rendered Image – Joseph Dec 1 '16 at 11:22
  • this post have some code like layer = iface.activeLayer() extent = layer.extent() but i am not loading layer in qgis . i am doing the process from standalone script using proceesing . – User18 Dec 1 '16 at 11:32
  • Check this post on how to get the raster layer and its extent in a standalone script (ignore the processing part). – Joseph Dec 1 '16 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.