6

I am looking for a possibility to visualize an attribute in a point layer in QGIS, by displaying multiple dots or SVG graphics.

To be precise, I have a point layer about a disaster incident containing casualties for each town in a point layer. I would like to visualize this with dots or crosses for each town. For a town with 50 casualties I want to display 50 dots (or crosses) in e.g. 5 rows with 10 dots each.

Please refer to the attached image to get an idea. I do not desperately need that red dot, but I'm looking for displaying the integer attribute in amount of dots/crosses.

enter image description here

1

3 Answers 3

5

You can use the Geometry Generator in your style layer

collect_geometries(array_foreach(generate_series(0,"fatalities"-1,1),translate($geometry, 5*(@element%3), 5*floor(@element/3)) ))

This will generate as many crosses than there are fatalities. Adjust the X & Y offset rules to match the desired column layout.

And set the SVG symbol to be crosses

An answer which uses the same underlying technique: QGIS Symbology - Display more symbols for each point depending on attribute numeric value

2
  • 1
    Shouldn't the translate part rather be like translate ($geometry, 5*(@element%3), 5*floor(@element/3)), where 5 can be replaces by any number for the distance between points?
    – Babel
    Commented Jun 9, 2023 at 12:29
  • 1
    Correct, thanks. I edited the answer (was not able to check in Qgis before posting)
    – Kasper
    Commented Jun 9, 2023 at 13:06
3

Python is ideal for repeating text characters. Just multiply a certain text character, for example '●' with a number, and it will show the '●' character repeated based on the defined number.

Knowing that you can create a custom function in the field expression to repeat '●' based on the casualties field.

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

@qgsfunction(args='auto', group='Custom', referenced_columns=[])
def repeat_txt(numfield, feature, parent):
    return '●' * numfield

In the Layer styling → labeling, go to expression → Custom → double-click to add repeat_txt function with casualties number field like this:

repeat_txt("casualties_number_field")

enter image description here

where "Num" is the field for the number of casualties.

The result is like this:

enter image description here

If you want the circles to be organized every 5 circles per line you need to update the expression as follows

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

@qgsfunction(args='auto', group='Custom', referenced_columns=[])
def repeat_txt(numfield, feature, parent):
    txt = '?' * numfield
    txt_wrap = re.sub("(.{5})", "\\1\n",txt , 0, re.DOTALL)
    return txt_wrap

The re function came from the brilliant answer on StackExchange Insert a newline character every 64 characters using Python

Then reuse the same expression

repeat_txt("casualties_number_field")

The result is like this (the numbers are shown for your reference)

enter image description here

If you do not want the symbol, you can turn it off the symbology and select No Symbols

This is the final result:

enter image description here

2

Inspired by the solution by @ahmadhanb, you can set a label with the following expression for the input (Value). Labels are easier to handle than geometry generator and the expression is simpler.

Use the following expression and replace your_field_name to the name of the field containing the numbers and select an icon like the dagger character (UNICODE 2020, html 8224), included already in the expression; you could also reproduce it with the expression char(8224). Alternatively, use the latin cross (Unicode 271D):

array_to_string (
    array_foreach (
        generate_series (1,"your_field_name"),
        '†'  ||  if (@element%3=0,'\n','')
    ),''
)

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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