Using icons as vector layer symbols

In addition to the default symbol types available in QGIS, you can also use TrueType fonts as map symbols. TrueType fonts are scalable vector graphics that can be used as point markers. In this recipe, we'll create this type of symbol.

Getting ready

You can download the point shapefile used in this recipe from https://geospatialpython.googlecode.com/files/NYC_MUSEUMS_GEO.zip.

Extract it to your qgis_data directory in a folder named nyc.

How to do it…

We will load a point shapefile as a layer and then use the character G in a freely-available font called Webdings, which is probably already on your system, to render a building icon on each point in the layer. To do this, we need to perform the following steps:

  1. First, we'll define the path to our point shapefile:
    src = "/qgis_data/nyc/NYC_MUSEUMS_GEO.shp"
    
  2. Then, we'll load the vector layer:
    lyr = QgsVectorLayer(src, "Museums", "ogr")
    
  3. Now, we'll use a Python dictionary to define the font properties:
    fontStyle = {}
    fontStyle['color'] = '#000000'
    fontStyle['font'] = 'Webdings'
    fontStyle['chr'] = 'G'
    fontStyle['size'] = '6'
    
  4. Now, we'll create a font symbol layer:
    symLyr1 = QgsFontMarkerSymbolLayerV2.create(fontStyle)
    
  5. Then, we'll change the default symbol layer of the vector layer to our font's symbol information:
    lyr.rendererV2().symbols()[0].changeSymbolLayer(0, symLyr1)
    
  6. Finally, we'll add the layer to the map:
    QgsMapLayerRegistry.instance().addMapLayer(lyr)
    

How it works…

The font marker symbol layer is just another type of marker layer; however, the range of possibilities with vector fonts is far broader than the built-in fonts in QGIS. Many industries define standard cartographic symbols using customized fonts as markers.

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

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