3

As you can see in the screenshot, I have a layer with a lot of empty categories (0 features). Is it a way to automatically remove this categories from TOC? To do it manually, it is complicated cause I have a lot of empty categories.

I don't want to reclassified, cause I don't want to lose my symbology! enter image description here

EDIT @Mayo: Are you sure it works in this case? See second capture. In case, value is the same as legend, it works... but in the capture, you'll see it is different. And after using the python code, the category is empty. enter image description here

4
  • 1
    Do you want to remove permanently these items ? Or just "dynamically" hide them like the answer below ?
    – etrimaille
    Commented Aug 11, 2022 at 14:33
  • I think you can use the standard Windows selection methods (e.g. holding the Ctrl key to click multiple individual categories, shift to click a range from a starting point) to highlight all the categories you don't want, and then hitting the minus sign to delete them. Sure it's manual but it's pretty easy.
    – John
    Commented Aug 11, 2022 at 14:47
  • @johns: but when you have more than 50 categories that are empty, I try to find a more "easy" solution! And as you know, in the symbology panel, you don't find the "count value"... so it is not easy when you have a lot of value!
    – katagena
    Commented Aug 11, 2022 at 15:56
  • @etrimaille: I want them to disappeared "permanently" from the TOC.
    – katagena
    Commented Aug 11, 2022 at 15:59

3 Answers 3

7

Paste this script in the python console, select the layer where you want to remove the nonexistent categories and click run.

layer = iface.activeLayer() # get the active layer
renderer = layer.renderer().clone() # get layer renderer
exp = QgsExpression(renderer.classAttribute()) # get the category expression
field_map = []
# evaluate the expression for each feature and store results in a list
for feature in layer.getFeatures():
    exp_context = QgsExpressionContext()
    exp_context.setFeature(feature)
    field_map.append(exp.evaluate(exp_context))
    
categories = renderer.categories()
for cat in categories.copy():
    if not field_map.count(cat.value()): # if there's no feature with this value
        renderer.deleteCategory(renderer.categoryIndexForValue(cat.value())) # remove the category
layer.setRenderer(renderer)
0
4

You can use the filter by map content in the table of content.

enter image description here

1
  • The problem is this solution acts on all the layers... and it is not desired!
    – katagena
    Commented Aug 11, 2022 at 16:00
-1

You can select all categories that have zero features, rightclick - move to new heading. give the heading a name. Click the ...(3 dots in front of the new heading name), and choose remove heading and classes.

1
  • As I said, if you have more then a 100 categories, you can’t select all the layers…
    – katagena
    Commented Sep 4, 2023 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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