3

I am writing an QGIS Python processing script, the result of this script is a formatted PDF. In one of the steps, I need to set map canvas extent to my shapefile extent, but it is not working.

What is the difference on this context from a standalone script, for instance?

This is how I am trying to set map canvas extent:

# Reference to map Canvas
canvas = QgsMapCanvas()

# allow anti aliasing
canvas.enableAntiAliasing(flag=True)
        
# canvas color
canvas.setCanvasColor(Qt.white)
        
#  canvas extension
canvas.setExtent(fieldlayer.extent())
        
#  set canvas layer
canvas.setLayers([fieldlayer])

In this context, I can't start qgs application through the usual command lines because the script would crash:

#  Start qgs
os.environ["QT_QPA_PLATFORM"] = "offscreen"

QgsApplication.setPrefixPath("/usr", True)
qgs = QgsApplication([], False)
qgs.initQgis()

### qgs manipulation here ###

# Exit qgs
qgs.exitQgis()

The whole script can be found here:

https://1drv.ms/u/s!AqMcxdEHKstsmhpHooyFXagqS2-d

2
  • Why do you run a QGIS instance in the Processing script? A processing script doesn't need a QGIS instance to run, as a standalone script does. Commented Mar 19, 2022 at 21:38
  • Thank you! I just figure it out... Commented Apr 1, 2022 at 23:05

1 Answer 1

2

You don't need to start a new QGIS instance in Processing script. Maybe you should change geo_pdf_layout_print method to this:

def geo_pdf_layout_print(self, qgz_project, class_shp, qgz_final_path, feedback):

    project = QgsProject.instance() ### <--
    project.read(qgz_project)
    project.write()

    fieldlayer = QgsVectorLayer(class_shp, Path(class_shp).stem, "ogr")
    project.addMapLayer(fieldlayer, False)

    layertree = project.layerTreeRoot()
    layertree.insertChildNode(1, QgsLayerTreeLayer(fieldlayer))

    layoutmanager = project.layoutManager()
    layout = layoutmanager.layoutByName("0001_MG")

    mapqgis = [i for i in layout.items() if isinstance(i, QgsLayoutItemMap)][1]
    mapqgis.setRect(20, 20, 20, 20)        
    mapqgis.attemptMove(QgsLayoutPoint(17.64, 37.4, QgsUnitTypes.LayoutMillimeters))
    mapqgis.attemptResize(QgsLayoutSize(176.4, 190, QgsUnitTypes.LayoutMillimeters))
    mapqgis.zoomToExtent(fieldlayer.extent())

    feedback.pushInfo(f"\n\n\nEXT = {fieldlayer.extent()}\n\n\n")

    canvas = iface.mapCanvas() ### <--
    canvas.enableAntiAliasing(flag=True)        
    canvas.setCanvasColor(Qt.white)
    canvas.setExtent(fieldlayer.extent())
    canvas.setLayers([fieldlayer])
    project.write(qgz_final_path)

I have no chance to test it.

1
  • i had to add an import (from qgis.utils import iface) but it worked. Only one issue, the canvas didn't set the specified zoom... maybe the reason is that i have two data frames in my qgis project. Commented Apr 1, 2022 at 23:14

Your Answer

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

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