4

I have polygon featureclass. which is shown in map as Original symbolgy

I want to "outline xpattern" symbology Outline xpattern from symblogy

I am not able to do it programmatically by python.

Few of my attempts are: Attempt1:

layer = QgsProject.instance().mapLayersByName("New_Shapefile")[0] # replace "polygon_layer_name" with the actual name of your polygon layer

symbol = QgsFillSymbol.createSimple({'outline_style': 'no', 'outline_width': '0.26', 'outline_color': '0,0,0,255', 'pattern': '20'})

renderer = QgsSingleSymbolRenderer(symbol)

layer.setRenderer(renderer)

layer.triggerRepaint()

Attempt 2:

layer = QgsProject.instance().mapLayersByName("polygon_layer_name")[0] # replace "polygon_layer_name" with the actual name of your polygon layer

fill_symbol = QgsFillSymbol.createSimple({'color': '247,247,247,255'})

layer_1 = QgsFillSymbolLayer(fill_symbol)
layer_2 = QgsFillSymbolLayer(fill_symbol)
layer_3 = QgsFillSymbolLayer(fill_symbol)

symbol = QgsFillSymbol([layer_1, layer_2, layer_3])

renderer = QgsSingleSymbolRenderer(symbol)

layer.setRenderer(renderer)

layer.triggerRepaint()

1 Answer 1

3

You were close, you just needed QgsMarkerLineSymbolLayer for the outline points.

layer = QgsProject.instance().mapLayersByName("polygon_layer_name")[0] # replace "polygon_layer_name" with the actual name of your polygon layer

red = QColor(255,0,0)
green = QColor(0,255,0)
whiteish = QColor(247,247,247)

fill = QgsSimpleFillSymbolLayer()
fill.setColor(whiteish)

marker_r = QgsMarkerLineSymbolLayer(interval=9)
marker_r.setColor(red)
marker_r.setOffsetAlongLine(4.5)

marker_g = QgsMarkerLineSymbolLayer(interval=9)
marker_g.setColor(green)

fill_symbol = QgsFillSymbol([fill, marker_r, marker_g])
renderer = QgsSingleSymbolRenderer(fill_symbol)

layer.setRenderer(renderer)

layer.triggerRepaint()

UPDATE: you can also use the predefined style like this:

layer = QgsProject.instance().mapLayersByName("polygon_layer_name")[0] # replace "polygon_layer_name" with the actual name of your polygon layer

symbol = QgsStyle.defaultStyle().symbol("outline xpattern")
layer.renderer().setSymbol(symbol)
layer.triggerRepaint()
3
  • I haven't managed to find if/where the "outline xpattern" symbology is defined and whether or not it is directly accessible from pyqgis, it should probably be. So here is a close enough copy of the symbology might be missing a couple parameter not sure
    – Kalak
    Commented Feb 9, 2023 at 11:30
  • Nevermind I found it
    – Kalak
    Commented Feb 9, 2023 at 11:49
  • Thanks..this worked!
    – Rpandia31
    Commented Feb 10, 2023 at 4:03

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.