[QGIS-Developer] Enable composer atlas labels and styles using pyqgis

xristosss20 saxri89 at gmail.com
Mon Jun 12 08:02:02 PDT 2017


 try to follow this code to create automate atlas print composer using pyqgis
:

def quick_export(self, ref, stype, scale):

    # Add all layers in map canvas to render
    myMapRenderer = self.iface.mapCanvas().mapRenderer()

    # Load template from file
    myComposition = QgsComposition(myMapRenderer)
    myFile = os.path.join(os.path.dirname(__file__), 'MMR_Template.qpt')
    myTemplateFile = file(myFile, 'rt')
    myTemplateContent = myTemplateFile.read()
    myTemplateFile.close()
    myDocument = QDomDocument()
    myDocument.setContent(myTemplateContent)
    myComposition.loadFromTemplate(myDocument)

    # Get map composition and define scale
    myAtlasMap = myComposition.getComposerMapById(0)
    myAtlasMap.setNewScale(int(scale))

    # Setup Atlas
    myAtlas = QgsAtlasComposition(myComposition)
    myAtlas.setCoverageLayer(atlas_desktop) # Atlas run from desktop_search
    myAtlas.setComposerMap(myAtlasMap)
    myAtlas.setFixedScale(True)
    myAtlas.fixedScale()
    myAtlas.setHideCoverage(False)
    myAtlas.setFilterFeatures(True)
    myAtlas.setFeatureFilter("reference = '%s'" % (str(ref)))
    myAtlas.setFilterFeatures(True)

    # Generate atlas
    myAtlas.beginRender()
    for i in range(0, myAtlas.numFeatures()):
        myAtlas.prepareForFeature( i )
        jobs = r"\\MSUKSERVER\BusinessMan Docs\Jobs"
        job_fol = os.path.join(jobs, str(ref))
        output_jpeg = os.path.join(job_fol, ref + "_BMS_plan.jpg")
        myImage = myComposition.printPageAsRaster(0)
        myImage.save(output_jpeg)
    myAtlas.endRender()

specific in atlas with same changes code say :

    myAtlasMap = myComposition.getComposerMapById(0)
    myAtlas = QgsAtlasComposition(myComposition)
    myAtlas.setCoverageLayer(atlas_desktop)
    myAtlas.setComposerMap(myAtlasMap)
    myAtlas.setFixedScale(True)
    myAtlas.fixedScale()
    myAtlas.setHideCoverage(False)
    myAtlas.setFilterFeatures(True)

this code work for my task and i take  export new 100 atlas feutures maps

but in my template.qpt i have some labels with code like this :

QGIS [%concat( "OBJECTID" )%]

that OBJECTID is the same for all new 100 atlas feutures maps. Any idea why?
If I use manual atlas export, then for any map OBJECTID change.

And the true is Does not work anything related to layer atlas in my
template.qpt i have some base styles rules or base labels rules like this :

intersects($atlasgeometry, $geometry ) 

or

$id = $atlasfeatureid

but that styles not work with this code.

i think so that code just export one by one my feutures from my atlas layer
and the atlas in me template stay False.

any idea ?i want to lern this solution is interesting question



--
View this message in context: http://osgeo-org.1560.x6.nabble.com/Enable-composer-atlas-labels-and-styles-using-pyqgis-tp5323880.html
Sent from the QGIS - Developer mailing list archive at Nabble.com.


More information about the QGIS-Developer mailing list