QGIS – Using Data Defined Override for SVG-Marker Lines

data-defined-overridemarkersqgissvgvisualisation

After not getting enough detail on that part of my problem in this post, I am now trying in a separate question.

I am unable to do data defined override with SVG markers on a line layer. I'm not sure whether it's a syntax problem or caused by something else.

I have an integer field with numbers 1-25 and want to define different centroid markers on my lines for each value with DDO. DDO is mandatory because I need to use multiple overrides to style by more than one field.

These are the variations that don't get any errors, but still don't show my chosen symbols:

case
    when "Schild" = 1 then '\\srv-daten\dokument\projectfolder\Projektdaten_Erhebung\Verkehrszeichen_Piktogramme\Zeichen_237_-_Sonderweg_Radfahrer_StVO_1992.svg'
    when "Schild" = 2 then '//srv-daten/dokument/projectfolder/Projektdaten_Erhebung/Verkehrszeichen_Piktogramme/Zeichen_237_-_Sonderweg_Radfahrer_StVO_1992.svg'
    when "Schild" = 3 then 'O:\dokument\projectfolder\Projektdaten_Erhebung\Verkehrszeichen_Piktogramme\Zeichen_237_-_Sonderweg_Radfahrer_StVO_1992.svg'
    when "Schild" = 4 then 'O:/dokument/projectfolder/Projektdaten_Erhebung/Verkehrszeichen_Piktogramme/Zeichen_237_-_Sonderweg_Radfahrer_StVO_1992.svg'
    when "Schild" = 6 then '@project_folder\Projektdaten_Erhebung\Verkehrszeichen_Piktogramme\Zeichen_237_-_Sonderweg_Radfahrer_StVO_1992.svg'
    when "Schild" = 7 then '@project_folder/Projektdaten_Erhebung/Verkehrszeichen_Piktogramme/Zeichen_237_-_Sonderweg_Radfahrer_StVO_1992.svg'
end

I also tried using file_path() and file_name() which generate errors and can not be applied.

The SVG-files are on a network drive in the project folder while QGIS is of course installed on my local drive. Could this be the problem? Or am I just using the expressions wrong?

I am using QGIS 3.20.0 on Windows 10.

Best Answer

A working Example

Let's assume there is a point layer with three point features in it, see the image below

input

Proceed with: RMC > Properties > Symbology > Symbol layer type: SVG Marker. Then go to the Dynamic SVG parameters > Edit....

Approach #1: Using the SVG-file locally stored on your machine

In the Expression String builder use the following expression:

CASE
    WHEN "id" = 1 THEN 'C://TempDaten//socicon_amazon.svg'
    WHEN "id" = 2 THEN 'C://TempDaten//socicon_google+.svg'
    ELSE 'C://TempDaten//socicon_apple.svg'
END

window

and get the following result:

result1

Above I am working with a folder C://TempDaten where all my SVG markers are stored.

Approach #2: Using the SVG-file as a string

Based on the approach suggested by @christoph, then your expression will look like this:

CASE
    WHEN "id" = 1 THEN 'data:image/svg+xml;utf8,<svg xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 600 330" height="330" width="600" xml:space="preserve" version="1.1" id="svg5238"><metadata id="metadata5244"><rdf:RDF><cc:Work rdf:about=""><dc:format>image/svg+xml</dc:format><dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage"/><dc:title/></cc:Work></rdf:RDF></metadata><defs id="defs5242"/><g transform="matrix(1.25,0,0,-1.25,0,330)" id="g5246"><g id="g5248"><path id="path5250" style="fill:#ffffff;fill-opacity:1;fill-rule:nonzero;stroke:none" d="m 31.996,263.758 c -17.512,0 -31.754,-14.25 -31.754,-31.766 l 0,-199.996 C 0.242,14.484 14.484,0.242 31.996,0.242 l 416.008,0 c 17.512,0 31.754,14.242 31.754,31.754 l 0,199.996 c 0,17.516 -14.242,31.766 -31.754,31.766"/><path id="path5252" style="fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none" d="M 31.996,0.469 C 14.613,0.469 0.48,14.613 0.48,31.996 l 0,199.996 c 0,17.383 14.133,31.528 31.516,31.528 l 416.008,0 c 17.383,0 31.516,-14.145 31.516,-31.528 l 0,-199.996 c 0,-17.383 -14.133,-31.527 -31.516,-31.527 l -416.008,0 z M 448.004,264 31.996,264 C 14.32,264 0,249.672 0,231.992 L 0,31.996 C 0,14.328 14.32,0 31.996,0 L 448.004,0 C 465.672,0 480,14.328 480,31.996 l 0,199.996 C 480,249.672 465.672,264 448.004,264"/><path id="path5254" style="fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none" d="m 31.996,19.996 416.008,0 c 6.629,0 12,5.371 12,12 l 0,199.996 c 0,6.629 -5.371,12 -12,12 l -416.008,0 c -6.629,0 -12,-5.371 -12,-12 l 0,-199.996 c 0,-6.629 5.371,-12 12,-12 z m 0,-12 c -13.258,0 -24,10.742 -24,24 l 0,199.996 c 0,13.258 10.742,24 24,24 l 416.008,0 c 13.258,0 24,-10.742 24,-24 l 0,-199.996 c 0,-13.258 -10.742,-24 -24,-24"/><path id="path5256" style="fill:#1a1a1a;fill-opacity:1;fill-rule:nonzero;stroke:none" d="m 143.789,48.43 0,46.179 33.606,21.993 -1.758,4.062 c -2.149,4.965 -3.258,10.316 -3.258,15.727 0,4.035 0.617,8.05 1.832,11.902 l -12.824,23.285 -30.789,-6.598 -6.598,6.598 26.391,30.781 0,13.2 8.797,-8.801 c 14.953,0 28.89,-7.602 36.984,-20.18 l 8.625,-13.406 18.914,9.484 -1.262,2.25 c -1.109,1.973 -1.691,4.199 -1.691,6.461 0,7.289 5.91,13.192 13.195,13.192 4.77,0 9.168,-2.575 11.504,-6.735 l 17.895,-31.863 c 0.921,-1.641 1.402,-3.496 1.402,-5.379 0,-3.488 -1.649,-6.77 -4.453,-8.84 l 15.922,2.805 c 2.015,0.355 4.058,0.535 6.109,0.535 6.93,0 13.703,-2.043 19.473,-5.883 l 5.847,5.852 c 4.946,4.949 11.66,7.73 18.66,7.73 11.934,0 22.387,-8.015 25.485,-19.547 l 4.203,-15.64 -16.496,-8.793 -3.512,20.054 c -1.199,6.84 -7.137,11.829 -14.078,11.829 -3.793,0 -7.426,-1.504 -10.109,-4.184 l -3.2,-3.203 c 5.743,-6.442 8.911,-14.77 8.911,-23.399 0,-4.066 -0.707,-8.105 -2.082,-11.929 l 17.472,-28.871 -6.594,-41.782 -12.484,-4.281 6.602,41.785 -31.133,24.285 4.027,-30.882 -19.789,-37.383 -13.191,0 19.789,37.383 -19.504,34.496 c -10.172,-2.465 -20.606,-3.715 -31.074,-3.715 -9.946,0 -19.856,1.129 -29.547,3.351 l 20.75,-60.523 -2.199,-10.992 -10.993,0 2.2,10.992 -26.391,60.961 -28.59,-16.981 0,-30.785 -10.996,-6.597 z m 74.77,103.351 17.55,8.801 -7.007,12.473 -18.321,-9.184 7.778,-12.09 z m 17.593,68.172 c 0,-7.289 -5.91,-13.195 -13.195,-13.195 -7.285,0 -13.191,5.906 -13.191,13.195 0,7.285 5.906,13.195 13.191,13.195 7.285,0 13.195,-5.91 13.195,-13.195"/></g></g></svg>'
    WHEN "id" = 2 THEN 'data:image/svg+xml;utf8,<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" version="1.0" width="420" height="360" id="svg2"> <defs id="defs4"/><g id="layer1"><g transform="translate(2.930158,-2.251167)" id="g1892"><path d="M 52.413594,38.866939 L 52.913594,62.616939 L 6.4448442,115.14819 L 18.069844,127.27319 L 71.101094,116.14819 L 94.319844,156.05444 C 84.123594,180.44952 92.866694,191.61419 98.382344,208.08569 L 40.788594,244.42944 L 40.788594,324.74194 L 61.507344,313.61694 L 61.007344,259.08569 L 108.97609,232.80444 L 153.91359,334.83569 L 149.97609,355.21072 L 170.60109,355.55442 L 173.63234,335.33569 L 137.75734,231.30444 C 171.91348,240.9542 206.38281,242.73849 241.31984,231.30444 L 275.13234,291.39819 L 240.72609,355.11692 L 264.53859,355.64822 L 300.41359,289.39819 L 292.81984,238.86694 L 345.35109,280.30444 L 333.72609,350.49192 L 356.97609,344.42944 L 368.06984,272.71069 L 338.28859,221.71069 C 342.53567,195.71256 337.04829,175.97794 325.97609,162.58569 L 336.56984,153.39819 C 354.39122,147.20989 365.19314,153.71935 371.41359,168.52319 L 377.41359,205.24194 L 407.69484,189.67944 L 399.60109,160.46069 C 389.57588,134.24426 359.48885,116.39751 325.78859,140.33569 C 325.78859,140.33569 315.08625,151.23801 313.81984,151.71069 C 295.95766,139.97227 271.36688,138.29839 248.22609,146.86694 L 244.16359,146.89819 C 251.46329,143.16506 251.87072,133.73733 249.38234,126.74194 L 213.53859,64.616939 C 199.79552,48.550019 166.87985,63.298099 173.13234,86.866939 L 177.81984,95.554439 L 146.19484,110.42944 C 130.20815,82.201549 113.44726,54.451059 68.569844,54.523189 L 52.413594,38.866939 z M 187.22609,113.58569 L 198.97609,133.30444 L 171.00734,147.21069 L 170.60109,147.21069 C 165.62405,141.82773 161.30591,135.77545 157.28859,129.39819 L 187.22609,113.58569 z " style="fill:black;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" id="path1882"/><path d="M 241.42858 621.29077 A 23.392857 23.392857 0 1 1  194.64286,621.29077 A 23.392857 23.392857 0 1 1  241.42858 621.29077 z" transform="translate(-40.44356,-589.0438)" style="opacity:1;fill:black;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" id="path1884"/></g></g></svg>'
    ELSE 'data:image/svg+xml;utf8,<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" version="1.0" width="520" height="350" id="svg2"><defs id="defs4"/><g id="layer1"><path d="M 0,107.46706 C 2.6406914,100.7933 50.773488,53.053348 78.505879,27.72301 L 30.247665,3.050815 L 93.792453,2.926998 L 112.74757,23.138076 L 129.41392,6.5673646 C 135.97495,12.87042 135.92319,27.78109 129.66155,43.812541 L 212.56479,55.197028 L 483.37841,55.027243 C 489.07662,58.144853 503.82382,55.371694 510.55882,84.220611 L 510.69673,194.62974 L 518.92203,204.91396 C 514.46061,218.13141 510.29531,231.71901 504.58003,243.36914 C 498.76012,233.01359 494.50125,219.09378 493.63075,200.15925 L 493.46096,82.12981 C 488.11829,74.007569 481.80193,68.968661 472.52108,73.317306 C 476.38297,79.87317 479.81006,86.663156 479.44445,95.495368 L 479.56827,346.80894 L 448.62366,346.93275 C 463.67595,309.05339 454.22451,271.3084 424.43265,236.3998 L 377.04115,347.0106 L 347.64612,347.0106 C 357.70692,330.60416 369.4145,316.83256 371.61077,287.84288 C 371.13578,258.92453 367.3052,232.30538 349.1638,215.49179 L 261.35015,230.82433 L 270.58008,346.88678 L 240.73575,346.97871 C 251.70918,312.74347 239.80505,269.95579 218.83719,245.45994 L 205.5176,347.0106 L 174.57301,346.93275 C 216.5578,238.86207 152.61888,185.07857 105.60844,129.04013 L 22.008277,128.94818 C 11.283857,126.30558 5.1805853,117.50144 0,107.46706 z " style="fill:black;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" id="path33566"/></g></svg>'
END

and get the output:

result4

Approach #3: Using the SVG-file via its URL

Your expression now is:

CASE
   WHEN "id" = 1 THEN 'https://upload.wikimedia.org/wikipedia/commons/a/a8/RWB-RWBA_Erste_Hilfe.svg'
   WHEN "id" = 2 THEN 'https://upload.wikimedia.org/wikipedia/commons/3/30/Zeichen_275-30_-_Vorgeschriebene_Mindestgeschwindigkeit%2C_StVO_2017.svg'
   ELSE 'https://upload.wikimedia.org/wikipedia/commons/2/22/Sinnbild_Viehtrieb%2C_Tiere%2C_StVO_1992.svg'
END

So, the result may look as following

result5

A HOW TO chapter

Here I tried to sum up some basics regarding the referencing to a SVG file. (Mind that it could vary depending on the OS)

Approach Working ?
/ (single slash) ✅ yes
// (double slash) ✅ yes
\ (single backslash) ❌ no
\\ (double backslash) ✅ yes
@project_folder || '/yourfile.svg' ✅ yes
@project_folder || '//yourfile.svg' ✅ yes
@project_folder || '\\yourfile.svg' ✅ yes

What to keep in mind:

  • if the SVG-file surely exists under the provided path
  • better not to mix different types of slashes (even if it works)
  • if the SVG-file is valid
  • if the path to the SVG is not too complicated
  • make sure that the SVG was used, not the PNG etc.

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


References:

Related Question