0

I'm writing a script on PyQGIS. My goal is to render the features of two layers in according with attribute "NId" that is the same for both layers "terrFiltrati" and "edificiIdonei". The problem is that QGIS crashes when the script processes the part about the graphic building. Here the code:

----------Main---------------------------------------------
simboloEd = QgsMarkerSymbol.createSimple({'name': 'pentagon', 'color': 'green'})
simboloTerr = QgsMarkerSymbol.createSimple({'name': 'star', 'color': 'red'})
simboloEd.symbolLayer(0).setStrokeWidth(0.5)
simboloTerr.symbolLayer(0).setStrokeWidth(0.5)
simboloEd.setSize(14)
simboloTerr.setSize(18)

graphicBuilder.coloreCat(simboloEd, edificiIdonei)
graphicBuilder.coloreCat(simboloTerr, terrFiltrati)

--------------------Script graphicBuilder------------------------------------

def coloreCat(simbolo, layerAtt):
   
    fni = layerAtt.fields().indexFromName('NId')
    unique_ids = layerAtt.dataProvider().uniqueValues(fni)
    
    categorie = []
    for unique_id in unique_ids:

        colorR = random.randrange(0,256)
        colorG = random.randrange(0,256)
        colorB = random.randrange(0,256)
        
        layer_style = {}
        layer_style['color'] = '%d, %d, %d' % (colorR,colorG, colorB)
        layer_style['outline'] = '#000000'
        
        symbolLayer = QgsSimpleFillSymbolLayer.create(layer_style)
        
        if symbolLayer is not None:
            simbolo.changeSymbolLayer(0, symbolLayer)
        
        category = QgsRendererCategory(unique_id, simbolo, str(unique_id))
        categorie.append(category)

    renderer = QgsCategorizedSymbolRenderer('NId', categorie)
    if renderer is not None:
        layerAtt.setRenderer(renderer)
    layerAtt.triggerRepaint()

I noticed that deleting the row category = QgsRendererCategory(unique_id, simbolo, str(unique_id)) the crash is not happening, but neither the rendering.

1
  • If coloreCat() is a class method, you are missing the self (instance) parameter in the function definition.
    – Ben W
    Commented Jul 23 at 10:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.