Skip to content

Instantly share code, notes, and snippets.

@pigreco
Created June 2, 2022 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pigreco/a6c519073c30f3dc276489c4ff198728 to your computer and use it in GitHub Desktop.
Save pigreco/a6c519073c30f3dc276489c4ff198728 to your computer and use it in GitHub Desktop.
Funzione personalizzata per il field calc, estrae i valori delle label dei simboli categorizzati
from qgis.core import *
from qgis.gui import *
from qgis.utils import iface
@qgsfunction(args='auto', group='Custom')
def get_catg_label(value_in, feature, parent):
"""
Restituisce il testo della legenda dell'elemento categorizzato:
<br>
<p><b>il campo in input e' quello utilizzato per la categorizzazione</b>
<br>
<h2>Example usage:</h2>
<ul>
<li>get_catg_label("Area") -> 'Parcheggio'</li>
</ul>
"""
layer = iface.activeLayer()
renderer = layer.renderer()
if layer.renderer().type() == "categorizedSymbol":
for cat in renderer.categories():
#print('val ', type(cat.value()), type(value_in))
if str(value_in) == str(cat.value()):
#print('res ',cat.value(), value_in)
cat_label_out = cat.label()
break
else:
cat_label_out = ''
return cat_label_out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment