Monday, 5 August 2019

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


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.



Answer



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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...