Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atlas crash using python #25079

Closed
qgib opened this issue Sep 21, 2017 · 10 comments
Closed

Atlas crash using python #25079

qgib opened this issue Sep 21, 2017 · 10 comments
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! Crash/Data Corruption High Priority PyQGIS Related to the PyQGIS API

Comments

@qgib
Copy link
Contributor

qgib commented Sep 21, 2017

Author Name: Loïc BARTOLETTI (@lbartoletti)
Original Redmine Issue: 17180
Affected QGIS version: 2.18.13
Redmine category:python_bindings_/_sipify


Hi,

I asked a question on https://gis.stackexchange.com/questions/256143/duplicate-composition-with-python-qgis about a need to duplicate a composer and deal with it in python.

It's maybe a bad code from me, but since it crashes QGis (tested on Windows and FreeBSD), I open a ticket here to let you know.

Thanks.


Related issue(s): #25080 (relates)
Redmine related issue(s): 17181


@qgib
Copy link
Contributor Author

qgib commented Sep 22, 2017

Author Name: Giovanni Manghi (@gioman)


This ticket (#25079) or this #25080 ?


  • status_id was changed from Open to Feedback

@qgib
Copy link
Contributor Author

qgib commented Sep 22, 2017

Author Name: Loïc BARTOLETTI (@lbartoletti)


Both #25080 (comment)

@qgib
Copy link
Contributor Author

qgib commented Sep 22, 2017

Author Name: Giovanni Manghi (@gioman)


Please add a description.


  • status_id was changed from Feedback to Open

@qgib
Copy link
Contributor Author

qgib commented Sep 22, 2017

Author Name: Loïc BARTOLETTI (@lbartoletti)


For this issue, when you use an invalid output dir, you don't have a crash but it's return my print debug "exportAsPdf", so I suspect a problem into this method (can't see or produce logs now).

@qgib
Copy link
Contributor Author

qgib commented Sep 22, 2017

Author Name: Loïc BARTOLETTI (@lbartoletti)


Since it's not available in Qgis 2, I'm trying to duplicate a composition with pyqgis translating "this code":

QgsComposition *QgsLayoutManager::duplicateComposition( const QString &name, const QString &newName )
from Qgis 3.
Duplication is fine, but every time I want to export an atlas of this new composition, qgis crash. Do you have any idea?

Here is a reproductible example of my code (using with python console and a small project):


    from qgis.PyQt.QtXml import *
    from qgis.PyQt.QtGui import *
    from qgis.PyQt.QtWidgets import *
    from qgis.PyQt.QtCore import *

    import os

    def saveAsTemplate(composition):
        doc = QDomDocument()
        composerElem = doc.createElement(  "Composer"  )
        doc.appendChild( composerElem )
        composition.writeXML( composerElem, doc )
        composition.atlasComposition().writeXML( composerElem, doc )
        return doc

    def duplicateComposition(composition):
        currentDoc = saveAsTemplate(composition)
        compositionElem = currentDoc.documentElement().firstChildElement( "Composition" )
        if compositionElem.isNull():
            print( "selected composer could not be stored as temporary template" )
            return None
            
        newComposition = QgsComposition(composition.mapSettings())
        
        if not newComposition.loadFromTemplate(currentDoc):
            print( " Cannot load template ")
            del newComposition
            return None
            
        return newComposition
        

    myComposition = iface.activeComposers()[0].composition()
    newComposition = duplicateComposition(myComposition)

    atlas = newComposition.atlasComposition()
    atlasLayer = iface.activeLayer()
    atlas.setCoverageLayer(atlasLayer)
    atlas.setPageNameExpression(atlasLayer.name())
    atlas.setEnabled( True )
    ret = newComposition.setAtlasMode( QgsComposition.ExportAtlas )
    if ret is False:
        print "Error"


    outputDir = "C:/temp/export_atlas"
    atlas.beginRender()
    print("atlas")
    num = atlas.numFeatures()
    for i in range(0, num):
        ret = atlas.prepareForFeature( i )
        if ret is False:
            print "prepareForFeatureror"
        printer = QPrinter()
        printer.setOutputFormat(QPrinter.PdfFormat)
        ret = newComposition.exportAsPDF(os.path.join(outputDir, str(i)+".pdf"))
        if ret is False:
            print "exportAsPDF"

    atlas.endRender()


"Original message":https://gis.stackexchange.com/questions/256143/duplicate-composition-with-python-qgis

@qgib
Copy link
Contributor Author

qgib commented Sep 28, 2017

Author Name: Jürgen Fischer (@jef-n)


@qgib
Copy link
Contributor Author

qgib commented Sep 28, 2017

Author Name: Denis Rouzaud (@3nids)


  • assigned_to_id removed Denis Rouzaud

@qgib
Copy link
Contributor Author

qgib commented Feb 22, 2018

Author Name: Giovanni Manghi (@gioman)


  • priority_id was changed from Normal to High

@qgib
Copy link
Contributor Author

qgib commented Jan 20, 2019

Author Name: Jürgen Fischer (@jef-n)


Please test with QGIS 3.4 - QGIS 2.18 reached it's end of life.


  • status_id was changed from Open to Feedback

@qgib
Copy link
Contributor Author

qgib commented Jan 21, 2019

Author Name: Loïc BARTOLETTI (@lbartoletti)


  • status_id was changed from Feedback to Closed

@qgib qgib closed this as completed Jan 21, 2019
@qgib qgib added Bug Either a bug report, or a bug fix. Let's hope for the latter! High Priority PyQGIS Related to the PyQGIS API Crash/Data Corruption labels May 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! Crash/Data Corruption High Priority PyQGIS Related to the PyQGIS API
Projects
None yet
Development

No branches or pull requests

1 participant