safe.utilities.styling module

InaSAFE Disaster risk assessment tool developed by AusAid -
IS Utilities implementation.

Contact : ole.moller.nielsen@gmail.com

Note

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

safe.utilities.styling.add_extrema_to_style(style)[source]

Add a min and max to each style class in a style dictionary.

When InaSAFE provides style classes they are specific values, not ranges. However QGIS wants to work in ranges, so this helper will address that by updating the dictionary to include a min max value for each class.

It is assumed that we will start for 0 as the min for the first class and the quantity of each class shall constitute the max. For all other classes , min shall constitute the smalles increment to a float that can meaningfully be made by python (as determined by numpy.nextafter()).

Parameters:style (list(dict)) – A list of dictionaries of the form as per the example below.
Returns:A new dictionary list with min max attributes added to each entry.
Return type:list(dict)

Example input:

style_classes = [dict(colour='#38A800', quantity=2, transparency=0),
                 dict(colour='#38A800', quantity=5, transparency=50),
                 dict(colour='#79C900', quantity=10, transparency=50),
                 dict(colour='#CEED00', quantity=20, transparency=50),
                 dict(colour='#FFCC00', quantity=50, transparency=34),
                 dict(colour='#FF6600', quantity=100, transparency=77),
                 dict(colour='#FF0000', quantity=200, transparency=24),
                 dict(colour='#7A0000', quantity=300, transparency=22)]

Example output:

style_classes = [dict(colour='#38A800', quantity=2, transparency=0,
                      min=0, max=2),
                 dict(colour='#38A800', quantity=5, transparency=50,
                      min=2.0000000000002, max=5),
                 ),
                 dict(colour='#79C900', quantity=10, transparency=50,
                      min=5.0000000000002, max=10),),
                 dict(colour='#CEED00', quantity=20, transparency=50,
                      min=5.0000000000002, max=20),),
                 dict(colour='#FFCC00', quantity=50, transparency=34,
                      min=20.0000000000002, max=50),),
                 dict(colour='#FF6600', quantity=100, transparency=77,
                      min=50.0000000000002, max=100),),
                 dict(colour='#FF0000', quantity=200, transparency=24,
                      min=100.0000000000002, max=200),),
                 dict(colour='#7A0000', quantity=300, transparency=22,
                      min=200.0000000000002, max=300),)]
safe.utilities.styling.mmi_colour(mmi_value)[source]

Return the colour for an mmi value.

Parameters:mmi_value – float or int required.
Returns str:html hex code colour for the value
safe.utilities.styling.mmi_ramp(raster_layer)[source]

Generate an mmi ramp using standardised range of 1-12

A standarised range is used so that two shakemaps of different intensities can be properly compared visually with colours stretched accross the same range.

The colours used are the ‘standard’ colours commonly shown for the mercalli scale e.g. on wikipedia and other sources.

Parameters:raster_layer (QgsRasterLayer) – A raster layer that will have an mmi style applied.
safe.utilities.styling.setRasterStyle(raster_layer, style)[source]

Set QGIS raster style based on InaSAFE style dictionary.

This function will set both the colour map and the transparency for the passed in layer.

Parameters:
  • raster_layer (QgsVectorLayer) – A QGIS raster layer that will be styled.
  • style (dict) – Dictionary of the form as in the example below.
Example:
style_classes = [dict(colour=’#38A800’, quantity=2, transparency=0),
dict(colour=’#38A800’, quantity=5, transparency=50), dict(colour=’#79C900’, quantity=10, transparency=50), dict(colour=’#CEED00’, quantity=20, transparency=50), dict(colour=’#FFCC00’, quantity=50, transparency=34), dict(colour=’#FF6600’, quantity=100, transparency=77), dict(colour=’#FF0000’, quantity=200, transparency=24), dict(colour=’#7A0000’, quantity=300, transparency=22)]
Returns:A two tuple containing a range list and a transparency list.
Return type:(list, list)
safe.utilities.styling.set_raster_style(raster_layer, style)[source]

Set QGIS raster style based on InaSAFE style dictionary for QGIS >= 2.0.

This function will set both the colour map and the transparency for the passed in layer.

Parameters:
  • raster_layer (QgsVectorLayer) – A QGIS raster layer that will be styled.
  • style (list) – List of the form as in the example below.

Example:

style_classes = [dict(colour='#38A800', quantity=2, transparency=0),
                 dict(colour='#38A800', quantity=5, transparency=50),
                 dict(colour='#79C900', quantity=10, transparency=50),
                 dict(colour='#CEED00', quantity=20, transparency=50),
                 dict(colour='#FFCC00', quantity=50, transparency=34),
                 dict(colour='#FF6600', quantity=100, transparency=77),
                 dict(colour='#FF0000', quantity=200, transparency=24),
                 dict(colour='#7A0000', quantity=300, transparency=22)]
Returns:A two tuple containing a range list and a transparency list.
Return type:(list, list)
safe.utilities.styling.set_vector_categorized_style(vector_layer, style)[source]

Set categorized QGIS vector style based on InaSAFE style dictionary.

For opaque a value of 0 can be used. For fully transparent, a value of 100 can be used. The calling function should take care to scale the transparency level to between 0 and 100.

Parameters:
  • vector_layer (QgsVectorLayer) – A QGIS vector layer that will be styled.
  • style (dict) – Dictionary of the form as in the example below.
Returns:

None - Sets and saves style for vector_layer

Example:

{'target_field': 'DMGLEVEL',
'style_classes':
[{'transparency': 1, 'value': 1, 'colour': '#fecc5c',
  'label': 'Low damage', 'size' : 1},
{'transparency': 55, 'value': 2, 'colour': '#fd8d3c',
 'label': 'Medium damage', 'size' : 1},
{'transparency': 80, 'value': 3, 'colour': '#f31a1c',
 'label': 'High damage', 'size' : 1}]}

Note

The transparency and size keys are optional. Size applies to points only.

Note

We should change ‘value’ in style classes to something more meaningful e.g. discriminant value

Note

you can optionally pass border_color also, if not color will be used

Note

you can optionally pass border_width also, if not QGIS defaults will be used

Note

you can optionally pass data_defined also, this has to be a dictionary of property: expressions like {‘color’: ‘color_hsv(%s, “pop”/%s*100, %s)’ % (hue, max, val)}

safe.utilities.styling.set_vector_graduated_style(vector_layer, style)[source]

Set graduated QGIS vector style based on InaSAFE style dictionary.

For opaque a value of 0 can be used. For fully transparent, a value of 100 can be used. The calling function should take care to scale the transparency level to between 0 and 100.

Parameters:
  • vector_layer (QgsVectorLayer, QgsMapLayer) – A QGIS vector layer that will be styled.
  • style (dict) – Dictionary of the form as in the example below
Returns:

None - Sets and saves style for vector_layer

Example style:

{'target_field': 'DMGLEVEL',
'style_classes':
[{'transparency': 1, 'max': 1.5, 'colour': '#fecc5c',
  'min': 0.5, 'label': '[0.5 - 1.5] Low damage', 'size' : 1},
{'transparency': 55, 'max': 2.5, 'colour': '#fd8d3c',
 'min': 1.5, 'label': '[1.5 - 2.5] Medium damage', 'size' : 1},
{'transparency': 80, 'max': 3.5, 'colour': '#f31a1c',
 'min': 2.5, 'label': '[2.5 - 3.5] High damage', 'size' : 1}]}

Note

The transparency and size keys are optional. Size applies to points only.

Note

you can optionally pass border_color also, if not color will be used

Note

you can optionally pass data_defined also, this has to be a dictionary of property: expressions like {‘color’: ‘color_hsv(%s, “pop”/%s*100, %s)’ % (hue, max, val)}