2

I'm struggling to get QGIS to render an atlas from PyQGIS outside of the main application. Version is a recent git master checkout. The coverage layer is loaded, multiple images are rendered but none of the composer label expressions based on the coverage layer fields evaluate (I get syntax errors not seen in the main interface) and the rendered images do not have the atlas-driven map. The non-atlas driven overview map is rendered but does not show the atlas coverage overview.

Atlas export works fine from the GUI application using the same project.

I've tried a few permutations of the code below but I'm not making much progress. Any clues as to what I'm missing here?

Sample syntax error:

Found expression: UTMMajorLabel(round(Y_min(map_get( item_variables( 'main'),'map_extent')),0))
../src/core/expression/qgsexpression.cpp: 441: (replaceExpressionText) [0ms] Expression parser error: syntax error, unexpected '(', expecting $end

Python script:

import sys
from qgis.core import (
    QgsProject, QgsComposition, QgsApplication, QgsProviderRegistry, QgsLayoutManager, QgsMapLayerStore, QgsAtlasComposition, QgsComposerMap)
from qgis.gui import QgsMapCanvas, QgsLayerTreeMapCanvasBridge

##Set these
#export QGIS_PREFIX_PATH=/usr/local/Cellar/qgis3-dev/2.99/QGIS.app/Contents/MacOS
#export PYTHONPATH=/usr/local/Cellar/qgis3-dev/2.99/lib/python3.6/site-packages/


gui_flag = False
QgsApplication.setPrefixPath("/usr/local/Cellar/qgis3-dev/2.99/QGIS.app/Contents/MacOS", True)
app = QgsApplication([], gui_flag)
app.initQgis()

project_path = 'blahblahblah.qgs'

canvas = QgsMapCanvas()
project = QgsProject.instance()
project.read(project_path)
bridge = QgsLayerTreeMapCanvasBridge(
  QgsProject.instance().layerTreeRoot(), canvas)
bridge.setCanvasLayers()

layoutManager=project.layoutManager()
comps=layoutManager.compositions()
composition=layoutManager.compositionByName('Print1')

atlasMap = composition.getComposerMapById(7)
print("Atlas Composer Map: {}".format(atlasMap.displayName()))

atlas = QgsAtlasComposition(composition)
coverage = project.layerStore().mapLayersByName("coverage")[0]
atlas.setCoverageLayer(coverage)

atlas.setEnabled(True)
atlas.setHideCoverage(False)

atlasMap.setFrameEnabled( True);
composition.setAtlasMode(2)

atlasMap.setAtlasDriven(True)
atlasMap.setAtlasScalingMode( QgsComposerMap.Auto )

atlas.beginRender()
print('{} Atlas Features'.format(atlas.numFeatures()))

for i in range(0, 3):
     print('page: {} {}'.format(i,atlas.nameForPage(i)))
     atlas.prepareForFeature( i )
     atlas.
     image = composition.printPageAsRaster(0)
     fname=('{}.jpg'.format(i))
     image.save(fname)
atlas.endRender()

QgsProject.instance().clear()
QgsApplication.exitQgis()

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.