Skip to content
This repository has been archived by the owner on May 27, 2021. It is now read-only.

Commit

Permalink
forgot files 2
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-penasa committed Feb 12, 2021
1 parent 9f7cb8e commit 28382e1
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions qgis_plugin/qgismappy/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from qgis.PyQt.QtCore import NULL
from qgis.core import QgsCategorizedSymbolRenderer, QgsSymbol, QgsRendererCategory


def resetCategoriesIfNeeded(layer, units_field, unassigned=True):
prev_rend = layer.renderer()

if not isinstance(prev_rend, QgsCategorizedSymbolRenderer):
renderer = QgsCategorizedSymbolRenderer(units_field)
layer.setRenderer(renderer)
else:
renderer = prev_rend

prev_cats = renderer.categories()
id = layer.fields().indexFromName(units_field)
uniques = list(layer.uniqueValues(id))
uniques_clean = []

for u in uniques:
if u not in [None, NULL]:
uniques_clean.append(u)

values = sorted(uniques_clean)

if unassigned:
if None in uniques or NULL in uniques:
values.append("")

categories = []

for current, value in enumerate(values):

already_in = False
for prev in prev_cats:
if prev.value() == value:
already_in = True
continue

if not already_in:
if value =="":
name = "Unassigned"
else:
name = str(value)
symbol = QgsSymbol.defaultSymbol(layer.geometryType())
category = QgsRendererCategory(value, symbol, name)
categories.append(category)

for cat in categories:
renderer.addCategory(cat)

# layer.setRenderer(renderer)
layer.rendererChanged.emit()
layer.dataSourceChanged.emit()

layer.triggerRepaint()

0 comments on commit 28382e1

Please sign in to comment.