3

Currently I am writing a standalone Python script to modify a few vector layers to have a wine fill pattern inside. The geometry I am working with is a polygon. I am aware of how to change outline colors and simple fill colors; but, does anyone happen to know how to add a pattern inside the polygon?

5

Start with:

symbol = QgsSymbolV2.defaultSymbol(layer.geometryType())
renderer = QgsSingleSymbolRendererV2(symbol)

Now you have 10 types of fill:

  • Simple fill, QgsSimpleFillSymbolLayerV2
  • Gradient fill, QgsGradientFillSymbolLayerV2
  • Centroid fill, QgsCentroidFillSymbolLayerV2
  • Line pattern fill, QgsLinePatternFillSymbolLayer
  • Point pattern fill, QgsPointPatternFillSymbolLayer
  • SVG fill, QgsSVGFillSymbolLayer
  • Outline: marker line, QgsMarkerLineSymbolLayerV2
  • Outline: simple line, QgsSimpleLineSymbolLayerV2
  • Shapeburst fill, QgsShapeburstFillSymbolLayerV2
  • Raster fill, QgsRasterFillSymbolLayer

So for simple fill it will be:

symbol_layer = QgsSimpleFillSymbolLayerV2.create(properties)

properties is a dictionary {'color':'red', 'style': 'f_diagonal', ...} so here you can choose proper 'style' parameter. If you don know nothing about parameters you can style your layer in QGIS, export this style to .qml file, and check proper tags for keywords. They are the same as in pyqgis.

renderer.symbols()[0].changeSymbolLayer(0, symbol_layer)
layer.setRendererV2(renderer)
  • 1
    Minor point, but you missed Shapeburst fill (QgsShapeburstFillSymbolLayerV2) and raster fill (QgsRasterFillSymbolLayer). Also gradient fill should be "QgsGradientFillSymbolLayerV2". – ndawson Jul 12 '16 at 22:35

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.