Intro
I am working on a QGIS project with two layers: a Parent Layer (e.g., Soil Class) and a Child Layer (e.g., Polygons). This is a One-to-Many Relationship: --> Each polygon is assigned to exactly one soil class. Each soil class can be associated with multiple polygons. Here is what I have done so far:
- Configured the relationship in Project Properties → Relations, linking the key fields (e.g., soil_class_id) in the Soil Class layer with those in the Polygons layer.
- Added a Relation Reference Widget to the attribute form of the Polygons layer for editing related records
Example
Soil Class Layer (Parent Layer):
soil_class_id | soil_class_name |
---|---|
1 | Clay |
2 | Sand |
3 | Loam |
Polygons Layer (Child Layer):
polygon_id | soil_class_id | area (m2) |
---|---|---|
101 | 1 | 500 |
102 | 2 | 300 |
103 | 1 | 400 |
104 | 3 | 600 |
results layer after field calculator:
polygon_id | soil_class_id | area (m2) | reference using the relation object itself |
---|---|---|---|
101 | 1 | 500 | Clay |
102 | 2 | 300 | Sand |
103 | 1 | 400 | Clay |
104 | 3 | 600 | Loam |
My Question:
How can I display related polygons directly in the attribute table of the Parent Layer (Soil Class), using the relation reference? (Instead of using get_attribute
.)
I understand that it’s possible to show a relation column in the attribute form using this expression. However, I am looking for a more efficient solution, especially since I am working with large datasets (more than 10,000 entries) and need a fast processing method.
1 Answer 1
Short answer
Make settings correctly for your relation, than you can use the function represent_value()
with the fieldname: represent_value ("soil_class_id")
Detailed answer
Setup everything so that in the polygon layer's attribute table, you can choose directly soil class name. As you don't provide details about how you made the settings, here a step by step description with screenshots:
Define relation in the project properties. Link
soil_class_id
fromSoilCLass
Layer tosoil_class_id
frompolygon
layer:In the polygon's layer properties, go to to
Attribute Forms
and selectsoil_class_id
field. SetWidget Type
toRelation Reference
, asdisplay expression
selectsoil_class_name
and select the relation you have defined before. enter image description hereNow, if you open the attribute table, your numbers in the field
soil_class_id
are gone and instead of it, you have the soil class name displayed and you have a dropdown menu to select from the soil class names defined in your Soil Class table/layer.
The Field soil_class_id is still an integer field, but it is displayed as a textstring with the name of the corresponding entry in the SoilClass
layer:
To generate the textstring from the related layer/table for each numer in
soil_class_id
field, simply use the functionrepresent_value()
with the fieldname:represent_value ("soil_class_id")
relation_aggregate
function useful, but as @Babel mentions, you will need to decide how to handle the aggregated data.