Last active
December 22, 2022 15:26
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> colore riempimento</li> | |
<li><b>'style'</b> stile riempimento</li> | |
<li><b>'outline_color'</b> colore tratto</li> | |
<li><b>'outline_width'</b> spessore tratto</li> | |
<li><b>'outline_style'</b> stile tratto</li> | |
<li><b>'joinstyle'</b> stile unione</li> | |
<li><b>'offset'</b> 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