Friday, 20 October 2017

symbology - Applying catgorized symbol to each feature using PyQGIS


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.



Answer



To draw a layer with a categorized renderer you first should create appropriate categories. A category needs value, symbol, and label. If you know the values at design time you may define these 3 parameters for each category. For an example see QGIS Python Programming Cookbook.


After defining categories you create a QgsCategorizedSymbolRendererV2() with these categories, and assign this renderer to the layer.


My code below creates a category for each unique value in a given field, with a random color. I override the default symbol to allow for some flexibility in styling.


from random import randrange


# Get the active layer (must be a vector layer)
layer = qgis.utils.iface.activeLayer()

# get unique values
fni = layer.fieldNameIndex('test')
unique_values = layer.dataProvider().uniqueValues(fni)

# define categories
categories = []
for unique_value in unique_values:

# initialize the default symbol for this geometry type
symbol = QgsSymbolV2.defaultSymbol(layer.geometryType())

# configure a symbol layer
layer_style = {}
layer_style['color'] = '%d, %d, %d' % (randrange(0,256), randrange(0,256), randrange(0,256))
layer_style['outline'] = '#000000'
symbol_layer = QgsSimpleFillSymbolLayerV2.create(layer_style)

# replace default symbol layer with the configured one

if symbol_layer is not None:
symbol.changeSymbolLayer(0, symbol_layer)

# create renderer object
category = QgsRendererCategoryV2(unique_value, symbol, str(unique_value))
# entry for the list of category items
categories.append(category)

# create renderer object
renderer = QgsCategorizedSymbolRendererV2('test', categories)


# assign the created renderer to the layer
if renderer is not None:
layer.setRendererV2(renderer)

layer.triggerRepaint()

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...