Geographic Information Systems Asked by Casairo on March 29, 2021
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?
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 !
Answered by J. Monticolo on March 29, 2021
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.
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.
Answered by Ian Turton on March 29, 2021
AS @J. Monticolo supposed, use Data defined expression in symbology properties.
For expression you can simply use:
replace("rgbfargko",' ',',')
Which takse "rgbfargko" string and replace spaces whit commas for proper color formatting. See:
Answered by Oto Kaláb on March 29, 2021
I have an attribute table with fields:
I found this script somewhere, (slightly modified by myself) and run it in the Python plugin window in QGIS. The first few lines define the index of the above fields and then applies it to change the renderer's symbol colours. You can easily separate your RGB code into Red, Green, and Blue fields. I guess you can also change the program to read the RGB code directly from the attribute table, but I have not come to that.
The script reads:
# The fields of importance are
#STRATNAME Field Index=Strat_idx
#Red Field Index=Red_idx
#Green Field Index=Green_idx
#Blue Field Index=Blue_idx
from PyQt5 import QtGui
layer = iface.activeLayer()
#Get fields matrix
fields= layer.fields()
#Find field numbers for fields used in rendering
strat_idx= layer.fields().indexFromName('STRATNAME')
print('StratName: ',strat_idx)
# Can also use indexOf(fieldname) to find field number
Red_idx= layer.fields().indexOf('Red')
print('Red: ',Red_idx)
Green_idx= layer.fields().indexFromName('Green')
print('Green: ',Green_idx)
Blue_idx= layer.fields().indexFromName('Blue')
print('Blue: ',Blue_idx)
MyRenderer=layer.renderer()
#define the categories array that contain the unique-renderer values
categories = MyRenderer.categories()
#define the array that will contain the number of variables for #the renderers
#and the number of unique values in the legend
#print (len(categories)) #Number of legend items
Matrix = [[0 for x in range(4)] for x in range(len(categories))]
#initialise the counter
ind=0
for category in categories:
# print category.value()#
Matrix[ind][0]=category.value()
#print('index:',ind,' ', Matrix[ind][0]) #
ind=ind+1
iter = layer.getFeatures()
# retrieve every feature with its attributes
for feature in iter:
# fetch attributes
for index in range(len(Matrix)):
if feature[strat_idx] == Matrix[index][0]:
#print('ok')
Matrix[index][1]=feature[Red_idx]
Matrix[index][2]=feature[Green_idx]
Matrix[index][3]=feature[Blue_idx]
break
#Classification variable =featur[0]
#col_R=feature[1]
#col_G=feature[2]
#col_B=feature[3]
for category in categories:
for index in range(len(Matrix)):
if category.label() == Matrix[index][0]:
colR=int(Matrix[index][1])
colG=int(Matrix[index][2])
colB=int(Matrix[index][3])
category.symbol().setColor(QColor.fromRgb(colR,colG,colB))
#Set the outline color transparent
category.symbol().symbolLayer(0).setStrokeColor(QColor('#00000000'))
expresssion='STRATNAME'
renderer = QgsCategorizedSymbolRenderer(expresssion, categories)
layer.setRenderer(renderer)
layer.triggerRepaint()
Answered by H J Brynard on March 29, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP