[QGIS-Developer] Processing Algorithm Gradient Selection?

Nyall Dawson nyall.dawson at gmail.com
Thu Feb 17 15:33:20 PST 2022


On Fri, 18 Feb 2022 at 02:22, C Hamilton <adenaculture at gmail.com> wrote:
>
> I am thinking of adding an algorithm that automates the creation of a density heat map of a point vector layer by the following steps.
> 1. Call the "Create grid" algorithm.
> 2. Call the "Count points in polygon" algorithm.
> 3. Style it with "Graduated" style and choose a gradient from a selection of heatmaps. This would need to be able to automatically scale to the min and max values and allow the user to select the mode.
>
> All except for the last can be easily modeled in QGIS; however, there is no color ramp QgsProcessingParameter for a heatmap.Is there a way to use QgsProcessingParameterEnum to display a gradient image. Is there a QGIS API call that would retrieve a set of gradients that could be added to the QgsProcessingParameterEnum?

My approach would be:

1. Get a list of all color ramp names via
QgsStyle.defaultStyle().colorRampNames()
2. Use a QgsProcessingParameterString parameter for the color ramp
name instead of a QgsProcessingParameterEnum, and use the approach
described at https://qgis.org/pyqgis/master/core/QgsProcessingParameterString.html#qgis.core.QgsProcessingParameterString
to show the parameter as a combobox containing only the existing color
ramp names.

i.e.

ramp_names = QgsStyle.defaultStyle().colorRampNames()
ramp_name_param = QgsProcessingParameterString('RAMP_NAME', 'Color ramp name')
ramp_name_param.setMetadata( {'widget_wrapper': {'value_hints': ramp_names } } )
self.addParameter(ramp_name_param)

Why use a string parameter instead of an enum? Well, the enum
parameters are designed for a fixed set of choices which are
internally all referred to by their numerical index. It works well for
choices which are static, and which won't ever vary
installation-by-installation. But color ramps WILL vary install by
install, as users may have renamed/added/removed ramps. So the
numerical indexes of different ramps won't necessarily match across
different installs. Ultimately, if another script/process/model uses
your algorithm and has a hard coded value for this parameter, of say
"5", corresponding to a desired color ramp on the developers machine,
then different users will get totally different ramps corresponding to
this value!

With the string approach the parameter value would always be hardcoded
as a string value (the ramp name), so things remain nice and
predictable across different installations*.

Nyall

* of course, a user may still have deleted the desired color ramp, so
you should put a run-time check in your processAlgorithm step to test
whether the parameter value for RAMP_NAME is actually available in the
default style. Something like:

ramp_name = self.parameterAsString(parameters, 'RAMP_NAME', context)
ramp = QgsStyle.defaultStyle().colorRamp(ramp_name)
if ramp is None:
    raise QgsProcessingException('Color ramp {} is not
available'.format(ramp_name))






>
> Any ideas would be appreciated. This can be manually done in 3 steps by separately running each of the algorithms and then styling the resulting vector layer, but it would be nice if it could be done with one algorithm.
>
> Thanks!!!
>
> Calvin
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer


More information about the QGIS-Developer mailing list