Graphics Reference
In-Depth Information
Using different symbols for different features
within a map
Let's use World Borders Dataset that you downloaded in the previous chapter to draw a
world map, using different symbols for different continents. This is a good example of us-
ing a categorized symbol renderer, though we'll combine it into a script that loads the
shapefile into a map layer and sets up the symbols and map renderer to display the map ex-
actly as you want. We'll then save the resulting map as an image.
Let's start by creating a map layer to display the contents of the World Borders Dataset
shapefile:
layer = iface.addVectorLayer("/path/to/
TM_WORLD_BORDERS-0.3.shp",
"continents", "ogr")
Each unique region code in the World Borders Dataset shapefile corresponds to a continent.
We want to define the name and color to use for each of these regions, and use this inform-
ation to set up the various categories to use when displaying the map:
from PyQt4.QtGui import QColor
categories = []
for value,color,label in [(0, "#660000", "Antarctica"),
(2, "#006600", "Africa"),
(9, "#000066", "Oceania"),
(19, "#660066", "The Americas"),
(142, "#666600", "Asia"),
(150, "#006666", "Europe")]:
symbol = QgsSymbolV2.defaultSymbol(layer.geometryType())
symbol.setColor(QColor(color))
categories.append(QgsRendererCategoryV2(value, symbol,
label))
With these categories set up, we simply update the map layer to use a categorized renderer
based on the value of the region attribute, and then redraw the map:
Search WWH ::




Custom Search