2

I'm working with QGIS 2.8 in windows 10. I've defined the python macros checkbox to QGIS start and simultaneously run a script that has the same path from my "QGIS.qgs" file. In the Project>Project properties>Macros I defined:

import myScript1

def openProject():
    myScript1.run()

def saveProject():
    pass

def closeProject():
    pass

Then, I've wrote a script (myScript1.py) in the same path from the QGIS project.

That worked as I wanted. Now I want to do the same for another script that should run in a sequence of the first one. Imagine I wanted to run first the "myScript1.py", then "myScript2.py". How should I define the python macros checkbox from the QGIS project? Should I write the "myScript2.py" in the same path of "myScript1.py"? The "myScript1.py" is to load a raster and define the legend:

from qgis.core import *
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
from qgis.utils import iface

def run():
    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)

The next script that I want to run in sequence of the above one is "myScript2.py" and is to create a print composer based in a defined "template.qpt" and convert it to an image output (png):

from qgis.core import *
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
from qgis.utils import iface

def run():
    layers =iface.legendInterface().layers()
    canvas=iface.mapCanvas()

    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')

Does anyone can help me?

  • What have you tried for running both scripts? Are you getting any error message from your attempts? Have both scripts worked for you separately? – Germán Carrillo May 20 '16 at 18:45
  • Now, what happens if you combine both run() methods into one? i.e., After the last line of your myScript1.run() method, append the code you have in your myScript2.run() from this line on: myFile = "C:/PATRICIA/IG/TESTE/Template.qpt". Does that work? – Germán Carrillo May 20 '16 at 18:47
  • The two scripts just worked separatly in the python console within QGIS just as I've write above, when I tried to put into just one script it works but the print composer appeared empty (white) in the map area, where should appear the layer loaded automaticaly in the table of contents. Can you explain better how to combine both run() methods into one? just give me an exemple. Thanks a lot! – Patrícia Fernandes May 23 '16 at 13:38

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.