I'm developing an application to automate reports that include several QGIS maps. The main drag on the application, as noted below, is the time required to instantiate the QgsProject and to load the composition.

gui_flag = True 
app = QgsApplication(sys.argv, True) 
QgsApplication.setPrefixPath("/usr", True) 

project_path = 'project.qgs' 
template_path = 'template.qpt' 

canvas = QgsMapCanvas() 
canvas.resize(QSize(1450, 850)) 
QgsProject.instance().read(QFileInfo(project_path)) #takes 39.9 sec to execute
root = QgsProject.instance().layerTreeRoot() 
bridge = QgsLayerTreeMapCanvasBridge(root, canvas) 
registry = QgsMapLayerRegistry.instance() 

template_file = file(template_path) 
template_content = template_file.read() 
document = QDomDocument() 

map_settings = canvas.mapSettings()
composition = QgsComposition(map_settings) 
composition.loadFromTemplate(document) #takes 13 sec. to execute

It's especially slow if the project file has several composers. Pickling the QgsComposition object doesn't work, and neither does pickling the QFileInfo object which I guess is related to the PyQt4 classes that comprise it based on this post. Is there another caching strategy that would work to speed up the load time for a composition object?

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.