I'm attempting to write some stand-alone python code that opens a qgis project and print composer template in order to generate pdf figures. I have this running on my Windows 7 machine, but I need to move it onto our Ubuntu Linux server.
The code crashes when trying to load a template into the print composer, printing the following error to the console:
Segmentation fault (core dumped)
This is the code I'm trying to run using the python interpreter:
from qgis.
from qgis.gui import *
from PyQt4.QtCore import *
from PyQt4.QtXml import *
from PyQt4.QtGui import *
import sys
import os
app = QgsApplication(sys.argv, False)
app.initQgis()
os.chdir('/path/to/output')
renderer = QgsMapRenderer()
project_path = "/path/to/project/project.qgs"
template_path = "/path/to/template/template.qpt"
project = QgsProject.instance()
project.read(QFileInfo(project_path))
root = project.layerTreeRoot()
layers = []
for child in root.children():
if child.isVisible():
layers.append(child.layerId())
renderer.setLayerSet(layers)
template_file = file(template_path)
template_content = template_file.read()
template_file.close()
document = QDomDocument()
document.setContent(template_content)
c = QgsComposition(renderer)
c.loadFromTemplate(document)
c.refreshItems()
map_item = c.getComposerItemById('map')
map_item.setNewScale(14400000000,True)
#export to PDF and also save a QGS file
c.exportAsPDF('Fig' + str(1) + '.pdf')
project.write(QFileInfo('Fig' + str(1) + '.qgs'))
#clear project instance
project.clear()
app.exitQgis()
app.exit()
sys.exit()
The line that causes the problem is:
c.loadFromTemplate(document)
The qpt file was created in Windows, I'm not sure if that would be a problem. The qgs file was also created in Windows and it works fine.
EDIT: I figured out that its the ComposerLabel tag in the qpt file that is causing the problem. Also, I'm using QGS 2.18.
Answer
It seems that the problem was the version of QGIS. I managed to install the long-term release version and the problem went away.
No comments:
Post a Comment