Skip to content

Instantly share code, notes, and snippets.

@Korto19
Last active December 22, 2022 15:26
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 Korto19/421fb9fb40f9f8609fdd4dad619385c1 to your computer and use it in GitHub Desktop.
Save Korto19/421fb9fb40f9f8609fdd4dad619385c1 to your computer and use it in GitHub Desktop.
Field Calc custom expression that get Simple Fill parameters for data driven parmeters or populate a field
from qgis.core import *
from qgis.gui import *
from qgis.utils import iface
@qgsfunction(args='auto', group='Custom')
def get_cat_param( vlayer, cat_field, param, feature, parent):
"""
Dato un layer <b>categorizzato con riempimento semplice</b> restituisce i parametri di impostazione
<ol>
<li>parametro: <b>layer</b></li>
<li>parametro: <b>campo categorizzazione </b></li>
<li>parametro scelto tra:</li>
<ul>
<li><b>'color'</b> &nbsp; colore riempimento</li>
<li><b>'style'</b> &nbsp; stile riempimento</li>
<li><b>'outline_color'</b> &nbsp; colore tratto</li>
<li><b>'outline_width'</b> &nbsp; spessore tratto</li>
<li><b>'outline_style'</b> &nbsp; stile tratto</li>
<li><b>'joinstyle'</b> &nbsp; stile unione</li>
<li><b>'offset'</b> &nbsp; spostamento</li>
</ul>
</ol>
<p>Per campi di categorizzazione numerici formattatarli come nell'esempio<p>
<h4>Example usage:</h4>
<ul>
<li>get_cat_param('layer', 'campo_cat' ,'color') -> '228,52,199,255'</li>
<li>get_cat_param('layer', to_string(format_number('"'nomeCampoCategoria'"',2)),'color') -> '228,52,199,255'</li>
</ul>
"""
layer = QgsProject.instance().mapLayersByName(vlayer)[0]
renderer = layer.renderer()
ret_val = ''
if param not in ('color', 'style', 'outline_color', 'outline_width', 'outline_style', 'joinstyle', 'offset'):
ret_val = "<strong><font color='red'>NOT VALID PARAM</strong>"
else:
if layer.renderer().type() == "categorizedSymbol":
campo = renderer.legendClassificationAttribute()
for cat in renderer.categories():
if str(cat_field) == str(cat.value()):
ret_val = cat.symbol().symbolLayer(0).properties()[param]
break
else:
ret_val = "<strong><font color='red'>Categorized by " + campo + "</strong>"
else:
ret_val = "<strong><font color='red'>NOT CATEGORIZED LAYER</strong>"
return ret_val
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment