5

I have applied random categorized colours to several lines (streets), based on the street ID, so that each street shows in adifferent colour.

I now want the same colours to be applied to the house numbers (point). I have the street ID in the layer of the house numbers, but i can't fint a way to apply the same colours to the house numbers that was used for the streets. I.e. if a street has randomly been appied red, i want all the houses (point) on that street to also have the colour red.

1

1 Answer 1

0

Starting from a categorized layer, through this custom function:

from qgis.core import *
from qgis.gui import *
from qgis.utils import iface

@qgsfunction(args='auto', group='Custom')
def get_catg_color(value, feature, parent):
    """  
    <h2>Example usage:</h2>
    <ul>
      <li>get_color("nomeCampoCategoria") -> '228,52,199,255'</li>
      <li>get_color(to_string(format_number("nomeCampoCategoria",2))) -> '228,52,199,255'</li>
    </ul>
    """
    
    layer = iface.activeLayer()
    renderer = layer.renderer()

    if layer.renderer().type() == "categorizedSymbol":
        campo = renderer.legendClassificationAttribute()

        for cat in renderer.categories():
            if str(value) == cat.value():
                colorato = cat.symbol().symbolLayer(0).properties()['color']
                break
            
    return colorato

it is possible to capture the color and write it as an attribute of the layer itself:

enter image description here

to make elements of another layer acquire the same color following the proximity criterion, just use in the color property of the second layer, the expression:

overlay_nearest('Reg01012021_WGS84',"colore")[0]

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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