I want to apply unique symbol to each feature. I have used following code for doing that
from qgis.core import *
from PyQt4.QtGui import *
# supply path to where is your qgis installed
QgsApplication.setPrefixPath("/path/to/qgis/installation", True)
# load providers
QgsApplication.initQgis()
# Get the active layer (must be a vector layer)
layer = qgis.utils.iface.activeLayer();
iter = layer.getFeatures();
for feature in iter:
geom = feature.geometry()
print "Feature ID %d: " % feature.id()
qgis.utils.iface.mapCanvas().setSelectionColor( QColor("yellow") );
layer.setSelectedFeatures([feature.id()])
qgis.utils.iface.mapCanvas().zoomToSelected( layer )
qgis.utils.iface.mapCanvas().refresh()
selected_features = layer.selectedFeatures()
for i in selected_features:
attr =i.attributes()
chaltano= str(attr[layer.fieldNameIndex('test')])
renderer = QgsCategorizedSymbolRendererV2("test")
layer.setRendererV2(renderer)
symbol = QgsSymbolV2.defaultSymbol(layer.geometryType())
symbol.setColor(QColor("red"))
cat = QgsRendererCategoryV2(feature.id(), symbol,str(feature.id()))
renderer.addCategory(cat)
Symbol is applying to only last feature. Here, "test" is attribute.