13

How to set transparency per polygon in a shapefile in QGIS?

For example:

a polygon with field "value" = 1 should be fully transparent whereas field with field "value" = 0.4 should have a 40% transparency value. The color of the polygons should depend on another field. Preferable I would do this using one layer but using two layers with some blending modes is OK as well.

17

You can use data defined properties for this. Use the style tab on the layer properties and click the expression button to the right of the color.

In there you can use the function color_rgba( red, green, blue, alpha ) to create the color. All values need to be between 0 and 255.

Example:

color_rgba( 255, 0, 0, ( 1 - "transparency" ) * 255 )

This will give you a totally red style with the alpha defined from a field "transparency" as per your specification in the question.

In the expression editor you will find a number of other color related functions that may better suit your needs, just explore them and read their documentation directly in the expression editor.

Another example for nominal (qualitative) values which you would normally classify is to work in the hsv space:

There is the function color_hsva( hue, saturation, value, alpha ) to create the color. The values need to be between

  • hue: 0-360 (meaning see below)
  • value and saturation: 0-100
  • alpha: 0-255.

Example:

color_hsva( 
  CASE 
    WHEN "classification" = 'red' THEN 0
    WHEN "classification" = 'blue' THEN 240
  END CASE, -- hue
  80,  -- saturation
  80,  -- value
  ( 1 - "transparency" ) * 255  -- alpha
)

enter image description here

For QGIS >= 2.12 also consider @ndawsons answer.

  • See related answer below for QGIS >= 2.12 – ndawson Oct 9 '15 at 22:10
11

As a follow up to Matthias' answer, in QGIS >= 2.12 you can use a data defined fill color with the expression:

 set_color_part(@value, 'alpha', ( 1 - "transparency" ) * 255 )

What this expression does is takes the original polygon color (@value) , and replaces the alpha channel (opacity) with the value calculated from the field). This means you can set the fill color expression and then happily go ahead and use categorised or graduated renderers to automatically set the base fill color and the opacity rule will still be respected. No need for hard coding the rgb values this way!

  • 1
    Nice and flexible approach! – Matthias Kuhn Oct 10 '15 at 7:31
  • This doesn't seem to work in QGIS 3+. After I add that expression, all polygons take on a single color with the same opacity. – robroc May 29 at 21:55
3

You can also set this values in the Layer Properties of the layer (right click -> Properties).
Use Style -> Categorized then select the column you want to use, for example 'value', choose the colors in the color ramp and click 'Clasify'.
Then you can define the color of each value and its transparency.

enter image description here

If you add new values to the used layer click on Clasify to add them to your existing style.

  • 1
    This will not allow using different fields for transparency and classification like specified in the question. – Matthias Kuhn Oct 9 '15 at 11:27

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.