DEV Community

Cover image for Quick Way to Assign Colour Values to Administrative Boundaries in QGIS
Muhammad Salihin Bin Zaol-kefli
Muhammad Salihin Bin Zaol-kefli

Posted on • Updated on

Quick Way to Assign Colour Values to Administrative Boundaries in QGIS

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

  1. Select layer and right click then Open Attribute Table
  2. Find out which field to use for colour assignment. In this case: GDENR Alt Text
  3. Under Processing tab, select Toolbox. In the Processing Toolbox, expand Vector table and double click Field calculator Alt Text
  4. In the Field calculator window,
    1. Ensure Input layer is the layer to reference from
    2. Enter a name in the Output field name. In this case: B
    3. Set Output field type to be Integer
    4. Set Output field width to be 3
    5. In the Expression tab, key in GDENR%256 for B
    6. Click on OK and a temporary layer will be created Alt Text
  5. Check newly created temporary layer for newly created field Alt Text
  6. Repeat step 4 for the rest of the colour channels, modifying only steps 4.2 and 4.5
    • Enter either R, G or A in Output field name
    • For R, key in floor((GDENR/65536)%256) in the Expression tab
    • For G, key in floor((GDENR/256)%256) in the Expression tab
    • For A, key in 255 in the Expression tab

That's it! Hope this helps and do share if you know of better ways to do this in the comments.

Top comments (0)