2

In my project I have a many-to-one relationship.

For example I have a vector layer "Towns" and a data layer "TownsInfo".

Each town record consists of "Name" and "Id". Each TownsInfo record consist of "Id", "TownId", "Population" and "Source".

I want to show "Population" of town as label where "Source" is 'Wikipedia' for example.

How can I use "Population" and "Source" attributes from "Towns" expression builder?

Update 1

I've meant getting information only from records where "Source" is 'Wikipedia'

  • Are you sure you have a many-to-one relationship? From your description of the layers it sounds like there should be only one TownsInfo record for each town, and vice versa. – csk Sep 20 '18 at 15:57
  • No, there are several TownsInfo for each town – Alexander Novikov Sep 21 '18 at 9:18
5

I've found out a solution.

I've made rule-based filtering with expression:

relation_aggregate( 'TownsInformationRelation', 'max', CASE WHEN "Source" = 'Wikipedia' THEN "Population" ELSE 0 END )

And also I've used filter:

relation_aggregate( 'TownsInformationRelation', 'count', CASE WHEN "Source" = 'Wikipedia' THEN 'true' ELSE NULL END )

Thanks!

3

I was able to achieve what I think you are after as follows:

First, I set the layer relations in project properties as seen in this screenshot:

enter image description here

The attribute table of the 'Towns' vector layer looks like this:

enter image description here

The attribute table of the non-spatial 'TownsInfo' Layer looks like this:

enter image description here

I entered the following expression in the Label Expression Generator:

attribute(get_feature('TownsInfo', 'ID', ID), 'POPULATION')  || ', '  || attribute(get_feature('TownsInfo', 'ID', ID), 'SOURCE')

The result looks like this:

enter image description here

0

the question is a little unclear, but I think what you are trying to do is use both the 'population' and 'source' fields in your label simultaneously so a given label might say something like '30,000, Wikipedia'. Please correct me if I am wrong.

I think you need to concatenate the two field columns 'pop' and 'source'. This would essentially create a new 3rd column in your attribute table which would contain rows which are a combination of the two original fields, so again these would read something like '30,000 Wikipedia'.

ESRI provide a guide on how to do this here: https://support.esri.com/en/technical-article/000005365

Once you have created your new, combined column, simply set this column as the label data source using the 'label as' function.

There may be other ways to go about this, but this is the first that springs to mind for me. Good luck and feel free to direct further queries this way but try to be more descriptive and concise in your question delivery.

  • This answer is for Arc products, but the question is about QGIS. – csk Sep 20 '18 at 15:55
  • Sorry I should clarify, I am aware that this is for arcmap, but the process is the same so the instructions would still be valid. – Maggie Sep 21 '18 at 7:08
  • With the use of expressions, QGIS can do away with concatenation into a new field, which is very helpful if some field values might get updated. – Gabriel C. Oct 11 '18 at 13:09

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.