3

I use QGIS and have a geological map for a large area. I present this after ID codes based on the geology ("Geokartnr" in photo).

I want to use international/nation color codes for each rock type, this way it will be comparable to other peoples work. The international colors is already added to the attribute table as a RGB code ("RGBfargko"). Is there any way I can sort/present the maps by "geokartnr" code and present this by the color codes in the RGB codes in the attribute table? Or do I need to change all colors manually?

Screenshot of the attribute table

  • Be aware that by using an expression to set the color as stated answer below you wont be able to automatically generate a legend.... (so the problem is you either set the color manually OR create the legend manually....) – J.R Mar 21 at 10:00
  • @J.R Good point, I think its kind of bug, because I suppose that it will generate legend if you use Categorized symbology (with rgb field as category). You can manually change a symbol of every each category, but if the data defined values are set for color, the value is just not taken from the expression. Or I am missing something? – Oto Kaláb Mar 21 at 11:08
5

In the layer styles panel if you click on simple marker, there is a small box to the right of the fill color combo box - if you click on it you can select edit from the menu and enter the expression editor.

enter image description here You will need an expression like this, to convert your string's spaces to commas and then into a colour:

color_rgb( replace("rgbfargeko",' ',','))

Then click ok and your colors should show up.

  • You missed a closing bracket ). – ahmadhanb Mar 22 at 1:55
4

AS @J. Monticolo supposed, use Data defined expression in symbology properties.

enter image description here

For expression you can simply use:

replace("rgbfargko",' ',',')

Which takse "rgbfargko" string and replace spaces whit commas for proper color formatting. See:

enter image description here

enter image description here

3

In the layer properties, in the symbology tab, define the color by an expression (link to the QGIS doc) :

color_rgb(
    array_get(string_to_array("rgbfargko", ' '), 0),
    array_get(string_to_array("rgbfargko", ' '), 1),
    array_get(string_to_array("rgbfargko", ' '), 2)
)

Explanation of the code :

1- function string_to_array : transform the field data into an array by splitting at the white space character ; 2- function array_get : get elements of the array, the first item has an index of 0 ; 3- function color_rgb : with 3 arguments (R, G, B), this function return a QGIS color.

EDIT The solution of @Ian Turton is more visual and simple !

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.