用pyqGIS-样式面板和TOC问题申请将风格涂到栅格层

问题描述 投票:0回答:1

示例代码:

from matplotlib import colormaps import numpy as np lyr = iface.activeLayer() if isinstance(lyr, QgsRasterLayer): if lyr.bandCount() == 1: # define color map by name map = colormaps['viridis'] # get min/max values stats = lyr.dataProvider().bandStatistics(1, QgsRasterBandStats.All) val_min = stats.minimumValue val_max = stats.maximumValue # limit number of color steps to 20 if map.N > 20: colors = map(np.linspace(1, map.N, 20, dtype=np.int16)) else: colors = map(range(1,map.N)) colorsN = len(colors) # raster values for the color steps valList = np.linspace(val_min, val_max, colorsN) # making a colorRamp-list with colorsList = [] for idx, col in enumerate(colors): qcol = QColor(int(col[0]*255), int(col[1]*255), int(col[2]*255)) val = valList[idx] #print(val, qcol) colorsList.append( QgsColorRampShader.ColorRampItem(val, qcol) ) # apply code (cf. https://docs.qgis.org/3.34/en/docs/pyqgis_developer_cookbook/raster.html#single-band-rasters) fcn = QgsColorRampShader() fcn.setColorRampType(QgsColorRampShader.Interpolated) fcn.setColorRampItemList(colorsList) shader = QgsRasterShader() shader.setRasterShaderFunction(fcn) renderer = QgsSingleBandPseudoColorRenderer(lyr.dataProvider(), 1, shader) lyr.setRenderer(renderer) lyr.renderer().createLegendNodes(QgsLayerTreeLayer(lyr.id())) lyr.triggerRepaint() lyr.emitStyleChanged()

screenshot of TOC and style panel in QGIS after code execution

在此答案的帮助下,我最终可以解决它:

与最大问题消失了;仅在样式面板中,彩色坡道在下拉列表中未显示
python qgis pyqgis
1个回答
0
投票

因此,工作代码是:

renderer.setClassificationMin
  • 
    
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.