TransWikia.com

Using an Expression to Return the Name of Field being Symbolized, within the Layer being Symbolized? (QGIS)

Geographic Information Systems Asked on July 9, 2021

I’m working in QGIS, formatting maps in their layout view. I’d like to swiftly and accurately make >50 nearly identical maps using a single layer feature with a large attribute file. Each map would be the same map, except for the field (column of attribute table) whose data is being symbolized in the map layout.

I’d like for the name of the field to be automatically updated and displayed on the map in a label box in layout view (or some analogous text display).

It appears inserting an expression into the label item is the answer to my dilemma but there don’t appear to be any expressions which return the name of the field being symbolized within the layer being symbolized. Is this correct?

I have access to ArcGIS desktop and online, should I switch to their programs for this niche functionality? (I’d prefer not if possible haha)

One Answer

You can get the attribute column name of your layer by using a dedicated Python function in the "Function Editor"

@qgsfunction(args='auto', group='Custom')
def getRenderColumnFromLayerName(layer_name, feature, parent, context):
    """
    Get the column name used for category from the layer name
    <h2>Example usage:</h2>
    <ul>
      <li>getRenderColumnFromLayerName(layer) -> 'pop_est'</li>
    </ul>
    """
    layers = QgsProject.instance().mapLayersByName(layer_name)
    layer = layers[0]
    return layer.renderer().classAttribute()

Then, call the function with getRenderColumnFromLayerName('your_layer_name') in the window "Insert expression" in the label configuration of your layout

See below GIF to illustrate

get attribute column name from QGIS renderer

PS: dirty Python code as I do not try to catch any error here

Correct answer by ThomasG77 on July 9, 2021

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