6

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?

  • 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 Jan 23 '18 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 Jan 23 '18 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 Jan 23 '18 at 10:22
  • Agreed! I had a few hundreds categories hence, most of the objects were in different areas of the map.. – axel_ande Jan 23 '18 at 10:27
4

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 PyQt4.QtGui import QColor
style_dict = {}
layer_1 = QgsMapLayerRegistry.instance().mapLayersByName("Line example")[0]
renderer = layer_1.rendererV2()
if renderer.type() == "categorizedSymbol":
    for cat in renderer.categories():
        style_dict[cat.value()] = (cat.symbol().color(), cat.label())

categories = []
layer_2 = QgsMapLayerRegistry.instance().mapLayersByName("Point example")[0]
for classes, (color, label) in style_dict.items():
    symbol = QgsSymbolV2.defaultSymbol(layer_2.geometryType())
    symbol.setColor(QColor(color))
    category = QgsRendererCategoryV2(classes, symbol, label)
    categories.append(category)

field = 'ID'
renderer = QgsCategorizedSymbolRendererV2(field, categories)
layer_2.setRendererV2(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()
  • 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 Jan 31 '18 at 4:03
  • 1
    @axel_ande - Edited post to mention how to copy the same field name :) – Joseph Jan 31 '18 at 10:38
1

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, privacy policy and cookie policy

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