11

I have two layers in QGIS one with point geometry and one with line geometry. Both layers have the same value in the "identification_id" column so I want to copy the colors (categorized by the identification_id) from the point layer to the segment layer. If both were point layers (or line layers) I would just simply right click on the layers and copy and paste the style..

The layers contains hundreds of different values for the identification_id so making it by hand would take too long time..

Any suggestion?

4
  • You could try to save the layer style and load it for the other layer, though I do not know, whether styles are compatible between different geometries.
    – Erik
    Commented Jan 23, 2018 at 9:38
  • Unfortunally isnt that possible :( The point layer qml got properties like this "outline_color" and the line layer got properties like "line_style"..
    – axel_ande
    Commented Jan 23, 2018 at 9:54
  • 1
    Meh, should have know that. To use a cartographic approach: How many different styles do you have? Do you need all of them? Generally speaking there ought to be no more than 7 different colors, maximum 10 in a map. Maybe you could reduce your styles and then simply "copy" them by choosing from the previously used colors.
    – Erik
    Commented Jan 23, 2018 at 10:22
  • Agreed! I had a few hundreds categories hence, most of the objects were in different areas of the map..
    – axel_ande
    Commented Jan 23, 2018 at 10:27

2 Answers 2

13
+50

One method is to use Python and copy the values, symbol colour and label from the categorisation of the line layer to the point layer. We can store the line symbology attributes in a dictionary and create a new style using the stored values.

You can test the following code, changing the names of the layers and the field name:

from PyQt5.QtGui import QColor
from qgis.core import QgsProject, QgsSymbol, QgsRendererCategory, QgsCategorizedSymbolRenderer
style_dict = {}
layer_1 = QgsProject.instance().mapLayersByName("Point example")[0]
renderer = layer_1.renderer()
if renderer.type() == "categorizedSymbol":
    for cat in renderer.categories():
        style_dict[cat.value()] = (cat.symbol().color(), cat.label())

categories = []
layer_2 = QgsProject.instance().mapLayersByName("Line example")[0]
for classes, (color, label) in style_dict.items():
    symbol = QgsSymbol.defaultSymbol(layer_2.geometryType())
    symbol.setColor(QColor(color))
    category = QgsRendererCategory(classes, symbol, label)
    categories.append(category)

field = 'ID'
renderer = QgsCategorizedSymbolRenderer(field, categories)
layer_2.setRenderer(renderer)
layer_2.triggerRepaint()

Example:

Example


Edit:

If you want to also copy the same field name used in the line layer, replace:

field = 'ID'

with:

field = renderer.classAttribute()
4
  • Is there any method to get the "active field/column" from layer_1? Instead of field = 'ID' I want to write something like field = layer_1.field
    – axel_ande
    Commented Jan 31, 2018 at 4:03
  • 1
    @axel_ande - Edited post to mention how to copy the same field name :)
    – Joseph
    Commented Jan 31, 2018 at 10:38
  • 1
    @Anaphory it works as it is expected after your edit. Commented Nov 22, 2021 at 14:44
  • Is it possible to make an expression with this code? So that it would be possible to render the layer2 directly in the category symbol using the expression?
    – katagena
    Commented Nov 22, 2021 at 21:34
0

One way to solve the problem is by rule based style. This implementation isn't perfect but it get the colors correct, but all gets into the same category.

Add a new rule and name it, then press on ("simple line" or "simple marker") and then the equation symbol next to "Fill" and then edit. I then used excel to write this next part

Case
WHEN identification_id=     6336(<-my unique numbers)    THEN color_rgb(    255 , 0 ,   0   )   
WHEN identification_id=     6667(<-my unique numbers)    THEN color_rgb(    250 ,   0   ,   0   )
(etc. dragged the different parts)
Else color_rgb(1,1,1)
End

Then a formula like this =A2&B2&C2&D2&E2&F2&G2&H2&I2 to get it all into one column and pasted it all into the editor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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