Thursday, 12 March 2015

symbology - Define pen width of a Line layer in PyQGIS


I have created a line shapefile layer (memory layer) created in Python Console of QGIS. Now I want to define the thickness of the line features that are being created. I am aware of the method by which the line styles/symbology can be imported from the existing '.qml' file.


self.setLayerSymbology(vlayer, "Parent_PermanentMarks.qml") 

Is there any way by which symbology (line thickness) of the lines can be defined 'on the go' via python console?



Answer



The below script worked for me.


from qgis.utils import iface
from qgis.core import *

from qgis.utils import *
from qgis.gui import *

vlayer = QgsVectorLayer("C:\SampleData\world_borders_lines.shp", "world_borders", "ogr")
props = { 'width' : '2', 'color' : '0,0,255' }
sl = QgsSymbolLayerV2Registry.instance().symbolLayerMetadata("SimpleLine").createSymbolLayer(props)
s = QgsLineSymbolV2([sl])
vlayer.setRendererV2( QgsSingleSymbolRendererV2( s ) )
QgsMapLayerRegistry.instance().addMapLayer(vlayer)

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