Creating a complex vector layer symbol

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.

Getting ready

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.

How to do it…

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:

  1. First, we load our line shapefile:
    lyr = QgsVectorLayer("/Users/joellawhead/qgis_data/shapes/paths.shp", "Route", "ogr")
    
  2. Next, we get the symbol list and reference the default symbol:
    symbolList = lyr.rendererV2().symbols()
    symbol = symbolList[0]
    
  3. Then, we create a shorter variable name for the symbol layer registry:
    symLyrReg = QgsSymbolLayerV2Registry
    
  4. Now, we set up the line style for a simple line using a Python dictionary:
    lineStyle = {'width':'0.26', 'color':'0,0,0'}
    
  5. Then, we create an abstract symbol layer for a simple line:
    symLyr1Meta = symLyrReg.instance().symbolLayerMetadata("SimpleLine")
    
  6. We instantiate a symbol layer from the abstract layer using the line style properties:
    symLyr1 = symLyr1Meta.createSymbolLayer(lineStyle)
    
  7. Now, we add the symbol layer to the layer's symbol:
    symbol.appendSymbolLayer(symLyr1)
    
  8. Now, in order to create the rails on the railroad, we begin building a marker line style with another Python dictionary, as follows:
    markerStyle = {}
    markerStyle['width'] = '0.26'
    markerStyle['color'] = '0,0,0'
    markerStyle['interval'] = '3'
    markerStyle['interval_unit'] = 'MM'
    markerStyle['placement'] = 'interval'
    markerStyle['rotate'] = '1'
    
  9. Then, we create the marker line abstract symbol layer for the second symbol:
    symLyr2Meta = symLyrReg.instance().symbolLayerMetadata("MarkerLine")
    
  10. We instatiate the symbol layer, as shown here:
    symLyr2 = symLyr2Meta.createSymbolLayer(markerStyle)
    
  11. Now, we must work with a subsymbol that defines the markers along the marker line:
    sybSym = symLyr2.subSymbol()
    
  12. We must delete the default subsymbol:
    sybSym.deleteSymbolLayer(0)
    
  13. Now, we set up the style for our rail marker using a dictionary:
    railStyle = {'size':'2', 'color':'0,0,0', 'name':'line', 'angle':'0'}
    
  14. Now, we repeat the process of building a symbol layer and add it to the subsymbol:
    railMeta = symLyrReg.instance().symbolLayerMetadata("SimpleMarker")
    rail = railMeta.createSymbolLayer(railStyle) 
    sybSym.appendSymbolLayer(rail)
    
  15. Then, we add the subsymbol to the second symbol layer:
    symbol.appendSymbolLayer(symLyr2)
    
  16. Finally, we add the layer to the map:
    QgsMapLayerRegistry.instance().addMapLayer(lyr)
    

How it works…

First, we must create a simple line symbol. The marker line by itself will render correctly, but the underlying simple line will be a randomly chosen color. We must also change the subsymbol of the marker line because the default subsymbol is a simple circle.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset