2

In the QGIS python console, I am able to get the active layer with layer = iface.activeLayer() but I need to be able to change the size and color of both centroids and polygons. For a polygon layer, this can change the outline width and the fill color:

    layer = iface.activeLayer()
    from PyQt4 import QtGui
    myVectorLayer = layer
    myRenderer  = layer.rendererV2()
    if layer.geometryType() == QGis.Polygon:
        mySymbol1 = QgsFillSymbolV2.createSimple({'color':'blue', 
                                                  'color_border':'black',
                                                  'width_border':'.5'})
        myRenderer.setSymbol(mySymbol1)
    layer.triggerRepaint()
    iface.legendInterface().refreshLayerSymbology(layer)
    iface.mapCanvas().refresh()

If a centroid layer is selected, and the same approach is used, but with if layer.geometryType() == QGis.Point:. Things don't work as expected. The color of the centroid turns transparent. The color is applied in the legend. And, sometimes this code produces a minidump and crash. Since it is easy to change the size and color of a centroid, using properties, style, single symbol, then size and color; the question remains: Is it possible to do this via python? I suppose that QgsCentroidFillSymbolLayerV2 might work but I can't figure out how to use it to change line size or color.

    layer = iface.activeLayer()
    from PyQt4 import QtGui
    myVectorLayer = layer
    myRenderer  = layer.rendererV2()
    if layer.geometryType() == QGis.Point:
        mySymbol1 = QgsFillSymbolV2.createSimple({'color':'red', 
                                                  'color_border':'black',
                                                  'width_border':'0'})
        myRenderer.setSymbol(mySymbol1)
    layer.triggerRepaint()
    iface.legendInterface().refreshLayerSymbology(layer)
    iface.mapCanvas().refresh()

For lines: If a line layer is loaded and selected, trying to change the color and size of the line, at the same time and "on-the-fly", with the following code, will crash qGIS.

    vlayer = iface.activeLayer()
    props = { 'color' : 'yellow' }
    sl = QgsSymbolLayerV2Registry.instance().symbolLayerMetadata("SimpleLine").createSymbolLayer(props)
    s = QgsLineSymbolV2([sl])
    vlayer.setRendererV2( QgsSingleSymbolRendererV2( s ) )
    symbols = vlayer.rendererV2().symbols()
    symbol = symbols[0]
    symbol.setWidth(2)
    QgsMapLayerRegistry.instance().addMapLayer(vlayer)

If the layer is deleted and reloaded, the following sometimes works, sometimes crashes. Note, Define pen width of a Line layer in PyQGIS writes that setWidth should be used instead of width.

    props = { 'width' : '1', 'color' : 'blue' }
    sl = QgsSymbolLayerV2Registry.instance().symbolLayerMetadata("SimpleLine").createSymbo        lLayer(props)
    s = QgsLineSymbolV2([sl])
    vlayer.setRendererV2( QgsSingleSymbolRendererV2( s ) )
    symbols = vlayer.rendererV2().symbols()
    QgsMapLayerRegistry.instance().addMapLayer(vlayer)

This changes the width of the line, with no attempt to change the color, and without crashes.

    symbols = vlayer.rendererV2().symbols()
    symbol = symbols[0]
    symbol.setWidth(1)
    QgsMapLayerRegistry.instance().addMapLayer(vlayer)

This, adapted from How to set Marker Line symbol for QgsVectorLayer by using Python? , changes the line width and color, and with no crashes:

    layer = qgis.utils.iface.mapCanvas().currentLayer()
    registry = QgsSymbolLayerV2Registry.instance()
    lineMeta = registry.symbolLayerMetadata("SimpleLine")
    symbol = QgsSymbolV2.defaultSymbol(layer.geometryType())
    # Line layer
    lineLayer = lineMeta.createSymbolLayer({'width': '1', 'color': 'red', 'offset': '-1.0', 'penstyle': 'solid', 'use_custom_dash': '0', 'joinstyle': 'bevel', 'capstyle': 'square'})
    # Replace the default layer with our two custom layers
    symbol.deleteSymbolLayer(0)
    symbol.appendSymbolLayer(lineLayer)
    # Replace the renderer of the current layer
    renderer = QgsSingleSymbolRendererV2(symbol)
    layer.setRendererV2(renderer)

Centroid size and color, taken from: Error while changing the symbol of a layer in QGIS in plugin. This works inside the qGIS python console but throws an indent error (that I am trying to resolve) when loaded as part of a qGis plug-in.

    from PyQt4.QtGui import QColor
    layer = qgis.utils.iface.activeLayer()
    symbol_layer = QgsSimpleMarkerSymbolLayerV2()
    symbol_layer.setSize(8.0)
    symbol_layer.setColor(QColor(224,242,0))
    layer.rendererV2().symbols()[0].changeSymbolLayer(0, symbol_layer)
    properties = symbol_layer.properties()
    layer.triggerRepaint()

enter image description here

Fixed: I had pasted in my code. Somehow a "space" or "tab" was not correctly formatted, hence the indentation error. So, the above will work in a plugin, and to change a centroid's size and color. Here's another way to do the same thing:

    layer = iface.activeLayer()
    renderer = layer.rendererV2()
    symbol1 = QgsMarkerSymbolV2.createSimple({'color': 'yellow','size':'8'})
    renderer.setSymbol(symbol1) 
    layer.triggerRepaint()
    iface.legendInterface().refreshLayerSymbology(layer)
    iface.mapCanvas().refresh()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.