1

The code snipped mentioned in Automatic pyqgis categorized renderer classification and Apply symbol to each feature (Categorized symbol) automatically create categorized renderers and determine categories using PyQGIS.

I (using QGIS 2.18.18 LTR) have a layer with uniqueValues for a field pre_cluster_id like [0, 1, 2, 4, 5, 6, 7, 8, 9, NULL] for which the above referred to approaches correctly create a category with value 0 and Legend 0, but incorrectly (from my understanding) create a category with value 0 and legend NULL hiding the first one, screenshot:

enter image description here

Created with the Classify Button, there is a category with no value, covering all except [0, ..., 9] (including NULL):

enter image description here

Hence, question: Using PyQGIS, how can I create a QgsRendererCategoryV2() with no value, i.e. all other values (or a category for NULL value, respectively)?

1

The NULL in the uniqueValues is of type QPyNullVariant (cp. https://nathanw.net/2013/08/31/qgis-2-0-dealing-with-null-values-in-pyqgis/) and a category created as

QgsRendererCategoryV2(NULL, mysymbol, 'legend_text')

does not create a category with no value, but with value 0. For a category with no value it needs to be created as

QgsRendererCategoryV2(None, mysymbol, 'legend_text')

Therefore, a slight alteration in the codes mentioned in my question solves this issue:

[...]
# create renderer object
if unique_value == NULL:
    category = QgsRendererCategoryV2(None, symbol, 'any other values')
else:
    category = QgsRendererCategoryV2(cl_no, symbol, str(cl_no))
# entry for the list of category items
categories.append(category)
[...]

But, to be honest, this is only half the answer. I could not find out how to explicitly create a category for the NULL values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.