I am using QGIS 3.10.0 A Coruna. I would like to label a building layer with with its building number. However, sometimes I have 2-3 buildings with the same number, and I would like to override the labeling by typing in a SQL-Command asking it to make sure that every value is only used once, and that the label is positioned in the middle of the buildings.

I cannot sum up the building into a multipart, because they have, apart from the building number, different attributes I need to keep. Also, I thought about adding a column, where I use the value just once. The problem is that there are often changes within the project, and with an SQL-Command changes could be done automatically.

Is there a IF CASE WHEN THEN that can solve this problem?

  • is the building number unique within your layer or is it only unique with a combination to an other field (e.g. street)? – eurojam Jan 16 '20 at 20:50
  • No, there is no combination with other fields. The number of the building ist different than a usual "address", they have a specific classification within our project. – rtaani Jan 17 '20 at 13:09

I will describe 2 possibilities, the first is more easy and elegant but works only from QGIS 3.10. The second also works for older versions.

Possibility 1: Use the geometry generator for labels. It is new since version 3.10 but very powerful. The expression will be

convex_hull(collect ($geometry, "id"))

this will build a convex hull around buildings with the same ID and will be used for labeling.

enter image description here

Possibility 2: You can use Virtual Layers to achieve the same like above. A Virtual Layer will reflect all changes in the original layer. The only thing is, if you have a lot of objects, virtual layers will slow down your project. so let's have a look to the statement to get the buildings with the same ID together:

select st_convexhull(st_union(geometry)), id from buildings group by id

this will build a convex hull around buildings with the same ID. Then we can use the new virtual layer only for labeling:

enter image description here

  • Wow! Thanks, that worked perfectly!! – rtaani Jan 17 '20 at 16:38
  • Every time, I find some awesome new ways to work with QGIS. Just... mind-blown. – Saijin_Naib Jan 17 '20 at 17:31

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.