[QGIS-Developer] Mesh Layers: Way to update/repaint the QgsMeshRendererScalarSettingsWidget after recreating color ramp

Richard Duivenvoorde rdmailings at duif.net
Wed Nov 30 00:16:00 PST 2022


On 11/29/22 17:25, Vincent Cloarec wrote:
> Hi Richard,
> 
> Can you precise how do you set the color ramp to the mesh layer ? Maybe you set only classes (color/value) ?

Hi Vincent,

Thanks for looking in my mail. These are my first Mesh experiments (using pyqgis). So maybe I'm totally off.
So yes, I'm only setting classes... Hoping that with that I'd also set the Color Ramp?

Usecase is: some model creates nc files (with 100 datagroups), the plugin or script should select one group (the one showing) and then either create some color ramp on the fly (logarithmic).
OR select a ramp which is not in the dropdown, but in the plugin as a .txt file like the one below

Thanks for any hints.

Regards,

Richard Duivenvoorde


++++ Below some for me working Python code: ++++

l = iface.mapCanvas().currentLayer()
# rs = RenderSettings
rs = l.rendererSettings()
# going to change the rendering settings...
# srs = ScalarRenderingSettings
srs = rs.scalarSettings(rs.activeScalarDatasetGroup())

# Trying to set the dataResamplingMethod to None (instead of NeighbourAverage)
# dataResamplingMethod = 1
# QgsMeshRendererScalarSettings.None = 0
# QgsMeshRendererScalarSettings.NeighbourAverage = 1
print(f'dataResamplingMethod: {srs.dataResamplingMethod()}')
# TODO: QgsMeshRendererScalarSettings.NeighbourAverage.None not usable in python?? https://github.com/qgis/QGIS/issues/51033
srs.setDataResamplingMethod(1)  # 0 = None, 1 = NeighbourAverage

# INTERPOLATION:DISCRETE
# https://api.qgis.org/api/qgscolorrampshaderwidget_8cpp_source.html
parsing_ok, color_ramp_items, color_ramp_shader_type, errors = QgsRasterRendererUtils.parseColorMapFile('/home/richard/z/22/rivm/1128_netcdf_styling/rgb3class.txt')
if parsing_ok:
     print(f'{len(color_ramp_items)} colorRampItems')
     color_ramp_shader_types = {
         0: 'QgsColorRampShader.Interpolated',
         1: 'QgsColorRampShader.Discrete'
     }
     print(f'ColorRampShaderType: {color_ramp_shader_types[color_ramp_shader_type]}')

     for cr in color_ramp_items:
         print(f'{cr} -- value: {cr.value} color: {cr.color} label: {cr.label}')
     print(f'Parsing OK')

     shader = QgsColorRampShader(minimumValue=0, maximumValue=24, colorRamp=None, type=QgsColorRampShader.Discrete, classificationMode=QgsColorRampShader.EqualInterval)
     shader.setColorRampItemList(color_ramp_items)
     shader.createColorRamp()
     srs.setColorRampShader(shader)
     srs.setOpacity(0.77)  # some transparency
else:
     print(f'Failed parsing the file, errors: {errors}')

# UPDATE the rendererSettings
rs.setScalarSettings(rs.activeScalarDatasetGroup(), srs)
l.setRendererSettings(rs)

l.rendererChanged.emit()
l.styleChanged.emit()

#QgsProject.instance().addMapLayer(l)


++++ Some (not logarithmic) color file export ++++

# QGIS Generated Color Map Export File
INTERPOLATION:DISCRETE
2,255,0,0,255,<= 2.000000
4,0,255,0,255,2.000000 - 4.000000
6,0,0,255,255,4.000000 - 6.000000
10,100,100,100,255,6.000000 - 10.000000





More information about the QGIS-Developer mailing list