Skip to main content
added 29 characters in body
Source Link
Kadir Şahbaz
  • 78k
  • 57
  • 257
  • 404

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()
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()

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()

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()
Bounty Ended with 50 reputation awarded by Anaphory

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 PyQt4PyQt5.QtGui import QColor
from qgis.core import QgsProject, QgsSymbol, QgsRendererCategory, QgsCategorizedSymbolRenderer
style_dict = {}
layer_1 = QgsMapLayerRegistryQgsProject.instance().mapLayersByName("Line"Point example")[0]
renderer = layer_1.rendererV2renderer()
if renderer.type() == "categorizedSymbol":
    for cat in renderer.categories():
        style_dict[cat.value()] = (cat.symbol().color(), cat.label())

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

field = 'ID'
renderer = QgsCategorizedSymbolRendererV2QgsCategorizedSymbolRenderer(field, categories)
layer_2.setRendererV2setRenderer(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()

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()

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()
Edited post in response to comment from OP
Source Link
Joseph
  • 76.3k
  • 7
  • 172
  • 286

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()

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

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()
Source Link
Joseph
  • 76.3k
  • 7
  • 172
  • 286
Loading