2

I try to write a python script in order to do the following:

  1. load a vector layer
  2. open an existing composition
  3. export the composition as PDF

If I use this simplified code in the Python console of QGis. Everything is working fine, the new layer is added and exported with the composition (you must have at least one composer in your project) :

from PyQt4.QtGui import *
vectorLyr = QgsVectorLayer("d:/test.shp", 'test','ogr')
if not vectorLyr.isValid():
    print "Layer %s did not load" % vectorLyr.name()

vectorLyr.updateExtents()
iface.mapCanvas().refresh()
reg = QgsMapLayerRegistry.instance()
reg.addMapLayer(vectorLyr)
#load first composition
comp = iface.activeComposers()[0].composition()
comp.refreshItems()
comp.setUseAdvancedEffects(True)
comp.exportAsPDF('d:/tmp/test.pdf')
# export is correct

but once I try to generalize the code by creating an export function, the layer is no longer exported with the composition:

from PyQt4.QtGui import *

def export(layerPath, layerName):
    vectorLyr = QgsVectorLayer(layerPath, layerName,"ogr")
    if not vectorLyr.isValid():
        print "Layer %s did not load" % vectorLyr.name()

    vectorLyr.updateExtents()
    iface.mapCanvas().refresh()
    reg = QgsMapLayerRegistry.instance()
    reg.addMapLayer(vectorLyr)
    comp = iface.activeComposers()[0].composition()
    comp.refreshItems()
    comp.setUseAdvancedEffects(True)
    comp.exportAsPDF('d:/tmp/test.pdf')
    return

export("d:/test.shp", "test")
#the vector layer added dynamically is not on the map

I'm new to Python and new to QGis API. Any idea of what I'm missing?

1
  • I have reinstalled QGis (from 2.14.2 to 2.16.3 Win 7). None of block codes is running now. BUT, if I add this line after comp = iface... , it works for both (pdf file is correctly generated with the dynamically added layer): print "composer:" + iface.activeComposers()[0].composerWindow().windowTitle() I cannot explain why it works with this line.
    – Yvan
    Oct 6, 2016 at 8:14

1 Answer 1

-1

I am unable to figure out what is exact reason problem ? I am getting error "Layer test did not load". The solution is try passing only location as first parameter and layer name as second parameter. export("d:/", "test"). If I change that works good for me

5
  • the layer "test" is just given as an example. Of course you have to pass an actual layer path. If you do so, you will see that it works for the first block of code and not dor the second one
    – Yvan
    Oct 5, 2016 at 17:37
  • I tried with my actual layer only, I used same name as one you used just for understanding. Just pass the layer path as first parameter and layer name as second parameter in calling function
    – S. Thiyaku
    Oct 5, 2016 at 17:39
  • It does not work for me. In my example, the argument "layerPath" is for full path (path+file name) and layerName is just the name of the layer in the table of contents
    – Yvan
    Oct 5, 2016 at 18:59
  • When I pass full path (path+file name) and layerName, it does not works for me, but when I passes only path and layerName it works. I am using QGIS 2.16.3.
    – S. Thiyaku
    Oct 6, 2016 at 4:09
  • Thank you for your help. It was not the solution for me. I have found something that works but cannot explain why (see comment of the first post)
    – Yvan
    Oct 6, 2016 at 8:15

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