3

I have GeoPackage file with parcels (polygons) and an Excel table with parcel owners. As one parcel can have multiple parcel owners I established a one to many relationship. Now I wish to label the parcels with one or more owners (as many as the parcel has).

The parcel layer and Excel table have mutual Parcel IDs.

How could I do this in QGIS?

4

You can use a custom function in the label expression editor

  • Assuming your parcels attribute table is structured like this:

enter image description here

from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom', referenced_columns=[])
def concat_field_values(feature, parent, context):
    ## get field names of layer using 'context' argument
    fields = context.fields().names()
    
    ## get fields with 'name' in the field name
    fields = [x for x in fields if 'name' in x.lower()]
    
    ## loop through fields and get list of values, excluding null values
    owners = [str(feature.attribute(x)) for x in fields if str(feature.attribute(x)) != "NULL"]
    
    ## join list into string, using the new line escape character
    label_string = '\n'.join(owners)
    
    return label_string

If you are unsure how to make a custom function

  • Click the expression editor button

enter image description here

  • Select the Function Editor tab
  • Make a new function and paste the code
  • Click Save and Load Functions

enter image description here

  • Back in the Expression tab, enter the function name, followed by parentheses

enter image description here

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.