Saturday 16 February 2019

pyqgis - Categorized symbols are not working in QGIS 3.4


I'm trying to generate categorized symbols for a polygon layer in QGIS 3.4. The code below should generate random colour polygons for each record of 'run' layer and I'm not getting any error message. The layers toolbar shows the coloured categories created, but the 'run' layer is not visible at all.


    ls = QgsProject.instance().layerStore()
r_layer = ls.mapLayersByName('run')[0]

# get unique values
fni = r_layer.fields().indexFromName('id')
unique_ids = r_layer.dataProvider().uniqueValues(fni)
QgsMessageLog.logMessage("sstyle for run layer..." + str(unique_ids))

# define categories
categories = []
for unique_id in unique_ids:
# initialize the default symbol for this geometry type
symbol = QgsSymbol.defaultSymbol(r_layer.geometryType())
symbol.setOpacity(0.5)

layer_style = {}
layer_style['color'] = '%d, %d, %d' % (randrange(0, 256), randrange(0, 256), randrange(0, 256))
layer_style['outline'] = '#000000'

symbolLayer = QgsSimpleFillSymbolLayer.create(layer_style)

if symbolLayer is not None:
symbol.changeSymbolLayer(0, symbolLayer)
category = QgsRendererCategory(unique_ids, symbol, str(unique_id))
categories.append(category)

renderer = QgsCategorizedSymbolRenderer('id', categories)
# assign the created renderer to the layer
if renderer is not None:

r_layer.setRenderer(renderer)
r_layer.triggerRepaint()


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...