The true power of QGIS symbology lies in its ability to stack multiple symbols in order to create a single complex symbol. This ability makes it possible to create virtually any type of map symbol you can imagine. In this recipe, we'll merge two symbols to create a single symbol and begin unlocking the potential of complex symbols.
For this recipe, we will need a line shapefile, which you can download and extract from https://geospatialpython.googlecode.com/svn/paths.zip.
Add this shapefile to a directory named shapes
in your qgis_data
directory.
Using the QGISPythonConsole,we will create a classic railroad line symbol by placing a series of short, rotated line markers along a regular line symbol. To do this, we need to perform the following steps:
lyr = QgsVectorLayer("/Users/joellawhead/qgis_data/shapes/paths.shp", "Route", "ogr")
symbolList = lyr.rendererV2().symbols() symbol = symbolList[0]
symLyrReg = QgsSymbolLayerV2Registry
lineStyle = {'width':'0.26', 'color':'0,0,0'}
symLyr1Meta = symLyrReg.instance().symbolLayerMetadata("SimpleLine")
symLyr1 = symLyr1Meta.createSymbolLayer(lineStyle)
symbol.appendSymbolLayer(symLyr1)
markerStyle = {} markerStyle['width'] = '0.26' markerStyle['color'] = '0,0,0' markerStyle['interval'] = '3' markerStyle['interval_unit'] = 'MM' markerStyle['placement'] = 'interval' markerStyle['rotate'] = '1'
symLyr2Meta = symLyrReg.instance().symbolLayerMetadata("MarkerLine")
symLyr2 = symLyr2Meta.createSymbolLayer(markerStyle)
sybSym = symLyr2.subSymbol()
sybSym.deleteSymbolLayer(0)
railStyle = {'size':'2', 'color':'0,0,0', 'name':'line', 'angle':'0'}
railMeta = symLyrReg.instance().symbolLayerMetadata("SimpleMarker") rail = railMeta.createSymbolLayer(railStyle) sybSym.appendSymbolLayer(rail)
symbol.appendSymbolLayer(symLyr2)
QgsMapLayerRegistry.instance().addMapLayer(lyr)