A while back, I was tasked to assign RGBA colour values (derived from an existing field) to each administrative boundary in a shape file.
The first approach I tried was to assign the RGBA colour values to each administrative boundary manually. This manual approach was fine for shape files with small number of administrative boundaries. However, as I dealt with more shape files with larger numbers of administrative boundaries, it was obvious that this approach was not going to work. It would take me ages to complete assigning colour values to all the boundaries!
My supervisor and I explored different approaches to do this tedious task quicker. After a few tries, we decided on an approach that fits our needs and is relatively quick to do. In this article, I'll share how we assigned colour values to a shape file representing more than 1000 Switzerland's municipalities.
Chosen Approach
- Select layer and right click then
Open Attribute Table
- Find out which field to use for colour assignment. In this case:
GDENR
- Under
Processing
tab, selectToolbox
. In theProcessing Toolbox
, expandVector table
and double clickField calculator
- In the
Field calculator
window,- Ensure
Input layer
is the layer to reference from - Enter a name in the
Output field name
. In this case:B
- Set
Output field type
to beInteger
- Set
Output field width
to be3
- In the
Expression
tab, key inGDENR%256
forB
- Click on
OK
and a temporary layer will be created
- Ensure
- Check newly created temporary layer for newly created field
- Repeat step 4 for the rest of the colour channels, modifying only steps 4.2 and 4.5
- Enter either
R
,G
orA
inOutput field name
- For
R
, key infloor((GDENR/65536)%256)
in theExpression
tab - For
G
, key infloor((GDENR/256)%256)
in theExpression
tab - For
A
, key in255
in theExpression
tab
- Enter either
That's it! Hope this helps and do share if you know of better ways to do this in the comments.
Top comments (0)