QGIS – Styling Lines with Centroids by Two Different Fields

geometry-generatorlineqgissvgsymbology

I have a line layer showing sectors of roads that I want to style by two different fields without duplicating the layer.

  • "Field 1" is an integer ranging from 0-25 defining road usage (street, path, bikelane, etc.)
  • "Field 2" is an integer ranging from 0-16 defining road signs that are valid within the sector

I want the line to be colored by "field 1" (I need to be able to manually define colors for each number) and I need a centroid with different SVG-graphics depending on "field 2".

Is there a way to achieve that without manually adding rule-based options for all 25×16 possible outcomes?

I can now run the line color from "field 1" via Data driven override. I am still not able to do the same with SVG graphics.

I added a marker line with a single centroid SVG-marker. I cannot manage to insert the expressions to set different files for my conditions.

I've tried full 'paths\filename.svg' with both / and \ or only start in from @project_folder. This is the only way I don't get any parsing errors. I also tried using file_path() and file_name() which generate errors.

Best Answer

To achieve the desired output

result

apply the following settings for

Colour

Use the following expression in the Expression String Builder

CASE
    WHEN "Field1" = 2 THEN '#264653'
    WHEN "Field1" = 9 THEN '#2A9D8F'
    WHEN "Field1" = 16 THEN '#E9C46A'
    WHEN "Field1" = 18 THEN '#F4A261'
    ELSE '#E76F51'
END

colour

Line centroid symbology

Firstly Add symbol layer and make an SVG-marker out of it. After trying the following expression in the Expression String Builder for Dynamic SVG parameters

CASE
    WHEN "Field2" = 1 THEN 'P://Test//qgis_test//socicon_amazon.svg'
    WHEN "Field2" = 3 THEN 'P://Test//qgis_test//socicon_apple.svg'
    WHEN "Field2" = 8 THEN 'P://Test//qgis_test//socicon_github.svg'
    WHEN "Field2" = 13 THEN 'P://Test//qgis_test//socicon_google+.svg'
    ELSE 'P://Test//qgis_test//socicon_mail.svg'
END

centroid

Note: Central point was used for Marker placement.


Currently changing(overriding) simultaneously the colour and style of the SVG-file can not be done.

An Attempt

With this approach, you can get control over your symbology, see the image below.

attempt1

Unfortunately, in this case, it is not possible to adjust the colour that easily. I was hoping to apply the method as was mentioned in one of my previous solutions.

result1

Working Solution #1

Using the Categorized symbology

solution1

Working Solution #2

Using the Rule-based symbology

solution2

Disadvantage: In both approaches you will need manually to change the colour (⚠️ not dependent on the "Field2")

I am on Windows 10 with QGIS 3.18.1-Zürich.


References:

Related Question