Skip to main content
added nice little warning symbols
Source Link
Matt
  • 17.9k
  • 4
  • 23
  • 59

A PyQGIS solution:

The layer tree

It contains the 'simple fill' (or background) layer, the layer with the overlaying symbol, and a third layer (duplicate of 'simple fill' layer) which will receive the merged symbol - this layer should have Single Symbol symbology to start.

enter image description here

The script
# the current project
p = QgsProject.instance()

# the layer with the simple fill symbology - change the name according to your project
lyr1 = p.mapLayersByName('polygons (simple fill)')[0]

# the layer with the overlaying symbology - change the name according to your project
lyr2 = p.mapLayersByName('polygons (overlay)')[0]

# a duplicate of either lyr1 or lyr2 (set to single symbol) - will receive the merged symbology - change the name according to your project
lyr3 = p.mapLayersByName('polygons (target symbol)')[0]

# get the renderer to merge
source_renderer = lyr2.renderer()

# clone the renderer from 'simple fill' layer ready to accept merged symbol
target_renderer = lyr1.renderer().clone()

# get categories of source and target renderers
cats_source = source_renderer.categories()
cats_target = target_renderer.categories()

# loop through categories and merge symbols
for i,cat in enumerate(cats_target):
    # the symbol of the source layer
    sym_source = cats_source[i].symbol()
    
    # the symbol of the target layer
    sym_target = cats_target[i].symbol()
    
    # append the symbol layer from source to target
    sym_target.appendSymbolLayer(sym_source.symbolLayers()[0])
    
    # update the category's symbol
    target_renderer.updateCategorySymbol(i, sym_target)

# apply the merged renderer to the target layer
lyr3.setRenderer(target_renderer)

# propogate changes to QGIS GUI
lyr3.triggerRepaint()
lyr3.emitStyleChanged()
Result

enter image description here

Warning⚠️ Warning ⚠️

Run the script only once. Running it again will crash QGIS due to mismatches between the original and new symbols.

A PyQGIS solution:

The layer tree

It contains the 'simple fill' (or background) layer, the layer with the overlaying symbol, and a third layer (duplicate of 'simple fill' layer) which will receive the merged symbol - this layer should have Single Symbol symbology to start.

enter image description here

The script
# the current project
p = QgsProject.instance()

# the layer with the simple fill symbology - change the name according to your project
lyr1 = p.mapLayersByName('polygons (simple fill)')[0]

# the layer with the overlaying symbology - change the name according to your project
lyr2 = p.mapLayersByName('polygons (overlay)')[0]

# a duplicate of either lyr1 or lyr2 (set to single symbol) - will receive the merged symbology - change the name according to your project
lyr3 = p.mapLayersByName('polygons (target symbol)')[0]

# get the renderer to merge
source_renderer = lyr2.renderer()

# clone the renderer from 'simple fill' layer ready to accept merged symbol
target_renderer = lyr1.renderer().clone()

# get categories of source and target renderers
cats_source = source_renderer.categories()
cats_target = target_renderer.categories()

# loop through categories and merge symbols
for i,cat in enumerate(cats_target):
    # the symbol of the source layer
    sym_source = cats_source[i].symbol()
    
    # the symbol of the target layer
    sym_target = cats_target[i].symbol()
    
    # append the symbol layer from source to target
    sym_target.appendSymbolLayer(sym_source.symbolLayers()[0])
    
    # update the category's symbol
    target_renderer.updateCategorySymbol(i, sym_target)

# apply the merged renderer to the target layer
lyr3.setRenderer(target_renderer)

# propogate changes to QGIS GUI
lyr3.triggerRepaint()
lyr3.emitStyleChanged()
Result

enter image description here

Warning

Run the script only once. Running it again will crash QGIS due to mismatches between the original and new symbols.

A PyQGIS solution:

The layer tree

It contains the 'simple fill' (or background) layer, the layer with the overlaying symbol, and a third layer (duplicate of 'simple fill' layer) which will receive the merged symbol - this layer should have Single Symbol symbology to start.

enter image description here

The script
# the current project
p = QgsProject.instance()

# the layer with the simple fill symbology - change the name according to your project
lyr1 = p.mapLayersByName('polygons (simple fill)')[0]

# the layer with the overlaying symbology - change the name according to your project
lyr2 = p.mapLayersByName('polygons (overlay)')[0]

# a duplicate of either lyr1 or lyr2 (set to single symbol) - will receive the merged symbology - change the name according to your project
lyr3 = p.mapLayersByName('polygons (target symbol)')[0]

# get the renderer to merge
source_renderer = lyr2.renderer()

# clone the renderer from 'simple fill' layer ready to accept merged symbol
target_renderer = lyr1.renderer().clone()

# get categories of source and target renderers
cats_source = source_renderer.categories()
cats_target = target_renderer.categories()

# loop through categories and merge symbols
for i,cat in enumerate(cats_target):
    # the symbol of the source layer
    sym_source = cats_source[i].symbol()
    
    # the symbol of the target layer
    sym_target = cats_target[i].symbol()
    
    # append the symbol layer from source to target
    sym_target.appendSymbolLayer(sym_source.symbolLayers()[0])
    
    # update the category's symbol
    target_renderer.updateCategorySymbol(i, sym_target)

# apply the merged renderer to the target layer
lyr3.setRenderer(target_renderer)

# propogate changes to QGIS GUI
lyr3.triggerRepaint()
lyr3.emitStyleChanged()
Result

enter image description here

⚠️ Warning ⚠️

Run the script only once. Running it again will crash QGIS due to mismatches between the original and new symbols.

deleted 62 characters in body
Source Link
Matt
  • 17.9k
  • 4
  • 23
  • 59

A PyQGIS solution. Whether it can be considered simple is subjective, of course.:

The layer tree

It contains the 'simple fill' (or background) layer, the layer with the overlaying symbol, and a third layer (duplicate of 'simple fill' layer) which will receive the merged symbol - this layer should have Single Symbol symbology to start.

enter image description here

The script
# the current project
p = QgsProject.instance()

# the layer with the simple fill symbology - change the name according to your project
lyr1 = p.mapLayersByName('polygons (simple fill)')[0]

# the layer with the overlaying symbology - change the name according to your project
lyr2 = p.mapLayersByName('polygons (overlay)')[0]

# a duplicate of either lyr1 or lyr2 (set to single symbol) - will receive the merged symbology - change the name according to your project
lyr3 = p.mapLayersByName('polygons (target symbol)')[0]

# get the renderer to merge
source_renderer = lyr2.renderer()

# clone the renderer from 'simple fill' layer ready to accept merged symbol
target_renderer = lyr1.renderer().clone()

# get categories of source and target renderers
cats_source = source_renderer.categories()
cats_target = target_renderer.categories()

# loop through categories and merge symbols
for i,cat in enumerate(cats_target):
    # the symbol of the source layer
    sym_source = cats_source[i].symbol()
    
    # the symbol of the target layer
    sym_target = cats_target[i].symbol()
    
    # append the symbol layer from source to target
    sym_target.appendSymbolLayer(sym_source.symbolLayers()[0])
    
    # update the category's symbol
    target_renderer.updateCategorySymbol(i, sym_target)

# apply the merged renderer to the target layer
lyr3.setRenderer(target_renderer)

# propogate changes to QGIS GUI
lyr3.triggerRepaint()
lyr3.emitStyleChanged()
Result

enter image description here

Warning

Run the script only once. Running it again will crash QGIS due to mismatches between the original and new symbols.

A PyQGIS solution. Whether it can be considered simple is subjective, of course.

The layer tree

It contains the 'simple fill' (or background) layer, the layer with the overlaying symbol, and a third layer (duplicate of 'simple fill' layer) which will receive the merged symbol - this layer should have Single Symbol symbology to start.

enter image description here

The script
# the current project
p = QgsProject.instance()

# the layer with the simple fill symbology - change the name according to your project
lyr1 = p.mapLayersByName('polygons (simple fill)')[0]

# the layer with the overlaying symbology - change the name according to your project
lyr2 = p.mapLayersByName('polygons (overlay)')[0]

# a duplicate of either lyr1 or lyr2 (set to single symbol) - will receive the merged symbology - change the name according to your project
lyr3 = p.mapLayersByName('polygons (target symbol)')[0]

# get the renderer to merge
source_renderer = lyr2.renderer()

# clone the renderer from 'simple fill' layer ready to accept merged symbol
target_renderer = lyr1.renderer().clone()

# get categories of source and target renderers
cats_source = source_renderer.categories()
cats_target = target_renderer.categories()

# loop through categories and merge symbols
for i,cat in enumerate(cats_target):
    # the symbol of the source layer
    sym_source = cats_source[i].symbol()
    
    # the symbol of the target layer
    sym_target = cats_target[i].symbol()
    
    # append the symbol layer from source to target
    sym_target.appendSymbolLayer(sym_source.symbolLayers()[0])
    
    # update the category's symbol
    target_renderer.updateCategorySymbol(i, sym_target)

# apply the merged renderer to the target layer
lyr3.setRenderer(target_renderer)

# propogate changes to QGIS GUI
lyr3.triggerRepaint()
lyr3.emitStyleChanged()
Result

enter image description here

Warning

Run the script only once. Running it again will crash QGIS due to mismatches between the original and new symbols.

A PyQGIS solution:

The layer tree

It contains the 'simple fill' (or background) layer, the layer with the overlaying symbol, and a third layer (duplicate of 'simple fill' layer) which will receive the merged symbol - this layer should have Single Symbol symbology to start.

enter image description here

The script
# the current project
p = QgsProject.instance()

# the layer with the simple fill symbology - change the name according to your project
lyr1 = p.mapLayersByName('polygons (simple fill)')[0]

# the layer with the overlaying symbology - change the name according to your project
lyr2 = p.mapLayersByName('polygons (overlay)')[0]

# a duplicate of either lyr1 or lyr2 (set to single symbol) - will receive the merged symbology - change the name according to your project
lyr3 = p.mapLayersByName('polygons (target symbol)')[0]

# get the renderer to merge
source_renderer = lyr2.renderer()

# clone the renderer from 'simple fill' layer ready to accept merged symbol
target_renderer = lyr1.renderer().clone()

# get categories of source and target renderers
cats_source = source_renderer.categories()
cats_target = target_renderer.categories()

# loop through categories and merge symbols
for i,cat in enumerate(cats_target):
    # the symbol of the source layer
    sym_source = cats_source[i].symbol()
    
    # the symbol of the target layer
    sym_target = cats_target[i].symbol()
    
    # append the symbol layer from source to target
    sym_target.appendSymbolLayer(sym_source.symbolLayers()[0])
    
    # update the category's symbol
    target_renderer.updateCategorySymbol(i, sym_target)

# apply the merged renderer to the target layer
lyr3.setRenderer(target_renderer)

# propogate changes to QGIS GUI
lyr3.triggerRepaint()
lyr3.emitStyleChanged()
Result

enter image description here

Warning

Run the script only once. Running it again will crash QGIS due to mismatches between the original and new symbols.

added 74 characters in body
Source Link
Matt
  • 17.9k
  • 4
  • 23
  • 59

A PyQGIS solution. Whether it can be considered simple is subjective, of course.

The layer tree

It contains the 'simple fill' (or background) layer, the layer with the overlaying symbol, and a third layer (duplicate of 'simple fill' layer) which will receive the merged symbol - this layer should have Single Symbol symbology to start.

enter image description here

The script
# the current project
p = QgsProject.instance()

# the layer with the simple fill symbology - change the name according to your project
lyr1 = p.mapLayersByName('polygons (simple fill)')[0]

# the layer with the overlaying symbology - change the name according to your project
lyr2 = p.mapLayersByName('polygons (overlay)')[0]

# a duplicate of either lyr1 or lyr2 (set to single symbol) - will receive the merged symbology - change the name according to your project
lyr3 = p.mapLayersByName('polygons (target symbol)')[0]

# get the renderer to merge
source_renderer = lyr2.renderer()

# clone the renderer from 'simple fill' layer ready to accept merged symbol
target_renderer = lyr1.renderer().clone()

# get categories of source and target renderers
cats_source = source_renderer.categories()
cats_target = target_renderer.categories()

# loop through categories and merge symbols
for i,cat in enumerate(cats_target):
    # the symbol of the source layer
    sym_source = cats_source[i].symbol()
    
    # the symbol of the target layer
    sym_target = cats_target[i].symbol()
    
    # append the symbol layer from source to target
    sym_target.appendSymbolLayer(sym_source.symbolLayers()[0])
    
    # update the category's symbol
    target_renderer.updateCategorySymbol(i, sym_target)

# apply the merged renderer to the target layer
lyr3.setRenderer(target_renderer)

# propogate changes to QGIS GUI
lyr3.triggerRepaint()
lyr3.emitStyleChanged()
Result

enter image description here

Warning

Run the script only once. Running it again will crash QGIS due to mismatches between the original and new symbols.

A PyQGIS solution. Whether it can be considered simple is subjective, of course.

The layer tree

It contains the 'simple fill' layer, the layer with the overlaying symbol, and a third layer (duplicate of 'simple fill' layer) which will receive the merged symbol.

enter image description here

# the current project
p = QgsProject.instance()

# the layer with the simple fill symbology - change the name according to your project
lyr1 = p.mapLayersByName('polygons (simple fill)')[0]

# the layer with the overlaying symbology - change the name according to your project
lyr2 = p.mapLayersByName('polygons (overlay)')[0]

# a duplicate of either lyr1 or lyr2 (set to single symbol) - will receive the merged symbology - change the name according to your project
lyr3 = p.mapLayersByName('polygons (target symbol)')[0]

# get the renderer to merge
source_renderer = lyr2.renderer()

# clone the renderer from 'simple fill' layer ready to accept merged symbol
target_renderer = lyr1.renderer().clone()

# get categories of source and target renderers
cats_source = source_renderer.categories()
cats_target = target_renderer.categories()

# loop through categories and merge symbols
for i,cat in enumerate(cats_target):
    # the symbol of the source layer
    sym_source = cats_source[i].symbol()
    
    # the symbol of the target layer
    sym_target = cats_target[i].symbol()
    
    # append the symbol layer from source to target
    sym_target.appendSymbolLayer(sym_source.symbolLayers()[0])
    
    # update the category's symbol
    target_renderer.updateCategorySymbol(i, sym_target)

# apply the merged renderer to the target layer
lyr3.setRenderer(target_renderer)

# propogate changes to QGIS GUI
lyr3.triggerRepaint()
lyr3.emitStyleChanged()
Result

enter image description here

Warning

Run the script only once. Running it again will crash QGIS due to mismatches between the original and new symbols.

A PyQGIS solution. Whether it can be considered simple is subjective, of course.

The layer tree

It contains the 'simple fill' (or background) layer, the layer with the overlaying symbol, and a third layer (duplicate of 'simple fill' layer) which will receive the merged symbol - this layer should have Single Symbol symbology to start.

enter image description here

The script
# the current project
p = QgsProject.instance()

# the layer with the simple fill symbology - change the name according to your project
lyr1 = p.mapLayersByName('polygons (simple fill)')[0]

# the layer with the overlaying symbology - change the name according to your project
lyr2 = p.mapLayersByName('polygons (overlay)')[0]

# a duplicate of either lyr1 or lyr2 (set to single symbol) - will receive the merged symbology - change the name according to your project
lyr3 = p.mapLayersByName('polygons (target symbol)')[0]

# get the renderer to merge
source_renderer = lyr2.renderer()

# clone the renderer from 'simple fill' layer ready to accept merged symbol
target_renderer = lyr1.renderer().clone()

# get categories of source and target renderers
cats_source = source_renderer.categories()
cats_target = target_renderer.categories()

# loop through categories and merge symbols
for i,cat in enumerate(cats_target):
    # the symbol of the source layer
    sym_source = cats_source[i].symbol()
    
    # the symbol of the target layer
    sym_target = cats_target[i].symbol()
    
    # append the symbol layer from source to target
    sym_target.appendSymbolLayer(sym_source.symbolLayers()[0])
    
    # update the category's symbol
    target_renderer.updateCategorySymbol(i, sym_target)

# apply the merged renderer to the target layer
lyr3.setRenderer(target_renderer)

# propogate changes to QGIS GUI
lyr3.triggerRepaint()
lyr3.emitStyleChanged()
Result

enter image description here

Warning

Run the script only once. Running it again will crash QGIS due to mismatches between the original and new symbols.

added 23 characters in body
Source Link
Matt
  • 17.9k
  • 4
  • 23
  • 59
Loading
Post Undeleted by Matt
Post Deleted by Matt
Source Link
Matt
  • 17.9k
  • 4
  • 23
  • 59
Loading