3

I'm using a QGIS 2.8 in windows 10. I have 2 scripts that are working separately but when I write both in just one script, it doesn't work. The 1st script is to load one raster to QGIs with a defined legend, the 2nd script is to compose a print layout and convert it to an image (png) in a output directory. My entire script is:

from PyQt4.QtGui import *
from PyQt4 import QtGui
from PyQt4.QtCore import * 
from PyQt4.QtXml import *
from PyQt4.QtCore import QSize
from PyQt4 import QtCore
from qgis import core, gui

lyr = QgsRasterLayer("C:/PATRICIA/IG/TESTE/TEST/FWI_20150802.dat", "FWI_20150802")
s = QgsRasterShader()
c = QgsColorRampShader()
c.setColorRampType(QgsColorRampShader.INTERPOLATED)
i = [] 

i.append(QgsColorRampShader.ColorRampItem(8.5, QtGui.QColor('#2b9e27'), '8.5'))
i.append(QgsColorRampShader.ColorRampItem(17.2, QtGui.QColor('#fee717'), '17.2'))
i.append(QgsColorRampShader.ColorRampItem(24.6, QtGui.QColor('#ec7000'), '24.6'))
i.append(QgsColorRampShader.ColorRampItem(38.3, QtGui.QColor('#cc292c'), '38.3'))
i.append(QgsColorRampShader.ColorRampItem(100, QtGui.QColor('#75263b'), '100'))

c.setColorRampItemList(i)
s.setRasterShaderFunction(c)
ps = QgsSingleBandPseudoColorRenderer(lyr.dataProvider(), 1, s)
lyr.setRenderer(ps)
QgsMapLayerRegistry.instance().addMapLayer(lyr)

layers =iface.legendInterface().layers()
canvas=iface.mapCanvas()

for layer in layers:
    myFile = "C:/PATRICIA/IG/TESTE/Template.qpt"
    myTemplateFile = file(myFile, 'rt')
    myTemplateContent = myTemplateFile.read()
    myTemplateFile.close()
    myDocument = QDomDocument()
    myDocument.setContent(myTemplateContent, False)
    newcomp = iface.createNewComposer()
    newcomp.composition().loadFromTemplate(myDocument)
    newcomp.composition().refreshItems()

    c = iface.activeComposers()[0].composition()
    image = c.printPageAsRaster(0)
    image.save('C:/PATRICIA/IG/TESTE/OUTPUT/FWI_Arome.png','png')

My print layout composer appeared without layer! How can I solve this?


My current Script which print layout composer appeared without layer: `# coding: utf-8

#------------------------
from PyQt4.QtGui import *
from PyQt4 import QtGui
from PyQt4.QtCore import * 
from PyQt4.QtXml import *
from PyQt4.QtCore import QSize
from PyQt4 import QtCore
from qgis import core, gui

lyr = QgsRasterLayer("C:/PATRICIA/IG/TESTE/TEST/FWI_20150802.dat", "FWI_20150802")
s = QgsRasterShader()
c = QgsColorRampShader()
c.setColorRampType(QgsColorRampShader.INTERPOLATED)
i = [] 

i.append(QgsColorRampShader.ColorRampItem(8.5, QtGui.QColor('#2b9e27'), '8.5'))
i.append(QgsColorRampShader.ColorRampItem(17.2, QtGui.QColor('#fee717'), '17.2'))
i.append(QgsColorRampShader.ColorRampItem(24.6, QtGui.QColor('#ec7000'), '24.6'))
i.append(QgsColorRampShader.ColorRampItem(38.3, QtGui.QColor('#cc292c'), '38.3'))
i.append(QgsColorRampShader.ColorRampItem(100, QtGui.QColor('#75263b'), '100'))

c.setColorRampItemList(i)
s.setRasterShaderFunction(c)
ps = QgsSingleBandPseudoColorRenderer(lyr.dataProvider(), 1, s)
lyr.setRenderer(ps)
QgsMapLayerRegistry.instance().addMapLayer(lyr)

layers =iface.legendInterface().layers()
canvas=iface.mapCanvas()

for layer in layers:
    myFile = "C:/PATRICIA/IG/TESTE/Template.qpt"
    myTemplateFile = file(myFile, 'rt')
    myTemplateContent = myTemplateFile.read()
    myTemplateFile.close()
    myDocument = QDomDocument()
    myDocument.setContent(myTemplateContent, False)
    newcomp = iface.createNewComposer()
    newcomp.composition().loadFromTemplate(myDocument)
    newcomp.composition().refreshItems()

    c = iface.activeComposers()[0].composition()
    image = c.printPageAsRaster(0)
    image.save('C:/PATRICIA/IG/TESTE/OUTPUT/FWI_Arome.png','png')`
3
  • You should take care to never post code as an image, make it really difficult for the people trying to help. May 20, 2016 at 10:37
  • Is that your current script or did you already unindent the for layer in layers: and all the lines after it?
    – Joseph
    May 20, 2016 at 10:37
  • No, my current script is already unindent the 'for layer in layers:' and all the lines after it just one Tab Backspace. May 20, 2016 at 10:46

1 Answer 1

1

I think when you're adding a layer and you want to add it to the composer to save it as an image, you need to make sure the layer is visible. Try adding this line after your for loop:

iface.legendInterface().setLayerVisible(layer, True)

So now it should look like:

for layer in layers:
    iface.legendInterface().setLayerVisible(layer, True)
    myFile = "C:/PATRICIA/IG/TESTE/Template.qpt"
    myTemplateFile = file(myFile, 'rt')
    myTemplateContent = myTemplateFile.read()
    myTemplateFile.close()
    myDocument = QDomDocument()
    myDocument.setContent(myTemplateContent, False)
    newcomp = iface.createNewComposer()
    newcomp.composition().loadFromTemplate(myDocument)
    newcomp.composition().refreshItems()
    c = iface.activeComposers()[0].composition()
    image = c.printPageAsRaster(0)
    image.save('C:/PATRICIA/IG/TESTE/OUTPUT/FWI_Arome.png','png')

I tested your code and it worked for me.


Note that at the end of your code, you're saving it to a single image file. This works for one layer but if you want to save multiple layers in different image files, you will need to modify your code.

3
  • Thanks Joseph, but using your suggestion it doesn't work again, my layout isn't drawing the map canvas, It's still in white (without map). Can you please suggest me another way to solve it? Thanks. May 20, 2016 at 13:37
  • @PatríciaFernandes - Is your template empty or does it contain a map item?
    – Joseph
    May 20, 2016 at 13:40
  • My template has a map, 2 images, 2 text boxes, 1 scalebar and 1 arrow predefined. The map from template is in white when was saved as template, but it was supposed that when I load my raster using my script, the map will appear and refreshe with the added layer defined in my script. May 20, 2016 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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