1

I'm writing this script for exporting PDFs, I managed to get an output PDF as I wanted but it's not showing the Map window. If I get into the Composer, the map is blank but magically refreshes and shows when I click on it.

I tried refreshing the layout after I set extent and size of the map, but it didn't work. I also tried to open the layout through iface.openLayoutDesigner(layout) but that freezed QGIS and forced me to terminate it.

Here is the processing part of my script:

def processAlgorithm(self, parameters, context, feedback):
    input_featuresource = self.parameterAsSource(parameters,'INPUT',context)
    nomenclatura = '1.1.E.3.43' #str(input_featuresource)

    project = QgsProject.instance()
    QgsExpressionContextUtils.setProjectVariable(project,'Manzana_actual',nomenclatura)

    pScope = QgsExpressionContextUtils.projectScope(project)
    tx = pScope.variable('Manzana_actual')

    layer = project.instance().mapLayersByName('Mzs,Qt,Chs')[0]
    iface.setActiveLayer(layer)

    query = '"EJIDO" = ' + tx.split('.')[0] + ' AND "CIRC" = ' + tx.split('.')[1] + ' AND "RADIO" = \'' + tx.split('.')[2] + '\' AND "CC" = ' + tx.split('.')[3] + ' AND "MZNA" = \'' + tx.split('.')[4] + '\''
    layer.selectByExpression(query)  


    iface.actionZoomToSelected().trigger()

    projectLayoutManager = project.layoutManager()
    layout = projectLayoutManager.layoutByName("Manzanero")
    #iface.openLayoutDesigner(layout)

    vg = layout.itemById('ventanaGrafica')

    vg.setExtent(iface.mapCanvas().extent())
    vg.attemptResize(QgsLayoutSize(20.85,18.85,QgsUnitTypes.LayoutCentimeters))
    layout.refresh()

    base_path = os.path.join(QgsProject.instance().homePath())
    pdf_path = os.path.join(base_path, str(tx.replace('.', '_') + '.pdf'))

    exporter = QgsLayoutExporter(layout)
    exporter.exportToPdf(pdf_path, QgsLayoutExporter.PdfExportSettings())

    return {'OUTPUT': pdf_path}

I took out the comments cause they were not written in English.

1
  • Also tryed with vg.refresh() and vg.refreshFrame() (vg = QgsLayoutItemMap); and replacing vg.setExtent(...) and vg.attemptResize(...) with vg.zoomToExtent(QgsMapCanvas().extent()). Still the same result.
    – Opazo
    Sep 18, 2019 at 14:23

0

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.