TransWikia.com

PyQGIS3 Layout : item position relative to another item size

Geographic Information Systems Asked on April 1, 2021

I created an atlas layout with a map, an attribute table, and a label.

I would like to make the y position of the label move according to the size of the attribute table on each atlas page.

The code below allows to display these three elements on the layout, but the label position is determined only according to the size of the attribute table on the last page.

from collections import *

project = QgsProject.instance()
manager = project.layoutManager()
layoutName = "TEST"

layouts_list = manager.printLayouts()
for layout in layouts_list:
    if layout.name() == layoutName:
        manager.removeLayout(layout)

layout = QgsPrintLayout(project)
layout.initializeDefaults()
layout.setName(layoutName)

myAtlas=layout.atlas()
myAtlas.setFilterFeatures(True)

manager.addLayout(layout)

layout.atlas().setEnabled(True)

pc = layout.pageCollection()
pc.page(0).setPageSize('Letter', QgsLayoutItemPage.Orientation.Landscape)
cover = project.mapLayersByName('client_Atlas')[0] #os.path.basename(path) + 
myAtlas.setCoverageLayer(cover)
myAtlas.setHideCoverage(True)
myAtlas.setSortFeatures(True)
myAtlas.setSortExpression('diagramme')
myAtlas.setFilterExpression('diagramme')

""" Map""" 
map = QgsLayoutItemMap(layout)
map.setId('map1')
map.setRect(20, 20, 20, 20)#Set Map Extent
map.setAtlasDriven(True)
map.setAtlasMargin(0.2)
rectangle = QgsRectangle(-244516.858, 169608.638, -243208.118, 170778.898)
map.setExtent(rectangle)
layout.addLayoutItem(map)
map.attemptMove(QgsLayoutPoint(2.7, 30.830, QgsUnitTypes.LayoutMillimeters))
map.attemptResize(QgsLayoutSize(211, 171.8, QgsUnitTypes.LayoutMillimeters))

""" Attribute table """
pdf = project.mapLayersByName('plan_de_ferme')[0]
pdf_table = QgsLayoutItemAttributeTable.create(layout)
pdf_table.setVectorLayer(pdf)
pdf_fields = ['champ','sup','supepnd']
pdf_table.setDisplayedFields(pdf_fields, True)

pdf_table.setDisplayOnlyVisibleFeatures(True)
pdf_table.setFilterToAtlasFeature(True)
pdf_table.setMap(map)
layout.addMultiFrame(pdf_table)
frame2 = QgsLayoutFrame(layout, pdf_table)
frame2.attemptResize(QgsLayoutSize(60.6, 153.3),True)
frame2.attemptMove(QgsLayoutPoint(213, 30.73, QgsUnitTypes.LayoutMillimeters))
pdf_table.addFrame(frame2)

""" Total text label """
title = QgsLayoutItemLabel(layout)
page_field = cover.fields().indexFromName('sup_sum')
for f in cover.getFeatures(): # get sum values for each page
    Tsupha = "[%attribute( get_feature('client_Atlas' ,'diagramme', attribute('diagramme')),'sup_sum')%]"
    Tsupepndha = "[%attribute(get_feature('client_Atlas','diagramme',attribute('diagramme')),'supepnd_su')%]"
    Tsupac = "[%attribute( get_feature('client_Atlas' ,'diagramme', attribute('diagramme')),'supac')%]"
title.setText('Total : ' + Tsupha + '   ' + Tsupepndha + '   ' + Tsupac)
myFont=QtGui.QFont("Ms Shell Dlg 2", 10)
title.setFont(myFont)
title.setHAlign(Qt.AlignLeft)
title.setVAlign(Qt.AlignTop)
title.setMinimumSize(QgsLayoutSize(110.39, 8.41, QgsUnitTypes.LayoutMillimeters))

field = 'diagramme' # attribute that contains categories
c = Counter([f[field] for f in pdf.getFeatures()]) #get the number of features for each category/page

cover = project.mapLayersByName('client_Atlas')[0]
features = cover.getFeatures()
length = 0
for feat in features : #count the number of features in the cover layer
    length += 1
for i in range (0,length):
    nbrow = c.get(i+1) + 1 # number of row + 1 for the headers
    totalH = nbrow*6 # multiplied by the heigth of rows in layout's attribute table
    Ytab = 30.73 + totalH + 5 # y position + heigth of attribute table + 5 mm
    title.attemptMove(QgsLayoutPoint(213, Ytab, QgsUnitTypes.LayoutMillimeters))
    layout.addLayoutItem(title)

myAtlas.beginRender()
for page in range(myAtlas.count()):
    myAtlas.next()
    
myAtlas.endRender()

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP