Graphics Reference
In-Depth Information
layer_registry = QgsMapLayerRegistry.instance()
for layer in layer_registry.mapLayersByName("population"):
layer_registry.removeMapLayer(layer.id())
Next, we open the World Borders Dataset into a new layer called "population" :
layer = iface.addVectorLayer("/path/to/
TM_WORLD_BORDERS-0.3.shp",
"population", "ogr")
We then need to set up our various population ranges:
from PyQt4.QtGui import QColor
ranges = []
for min_pop,max_pop,color in [(0, 99999,
"#332828"),
(100000, 999999,
"#4c3535"),
(1000000, 4999999,
"#663d3d"),
(5000000, 9999999,
"#804040"),
(10000000, 19999999,
"#993d3d"),
(20000000, 49999999,
"#b33535"),
(50000000, 999999999,
"#cc2828")]:
symbol = QgsSymbolV2.defaultSymbol(layer.geometryType())
symbol.setColor(QColor(color))
ranges.append(QgsRendererRangeV2(min_pop, max_pop,
symbol, ""))
Now that we have our population ranges and their associated colors, we simply set up a
graduated symbol renderer to choose a symbol based on the value of the pop2005 attrib-
ute, and tell the map to redraw itself:
Search WWH ::




Custom Search