# Using a formula to set the offset of a line pattern on QGIS 2.18

I’ve been trying to fill city boundaries polygons with a 3 colors stripes pattern (red, orange, green), whose respective thickness is proportional to 3 variables, different for each city, but whose sum is always 104 (see figure for example).

I set a line pattern with 3 lines, red, orange and green. Fixing the distance to 4mm and using a field calculator expression, I succeeded in making the width of each color vary:

``````Variable_1/104*4    #for red
Variable_2/104*4    #for orange
Variable_3/104*4    #for green
``````

But the expressions to set the offset does not produce any offset change:

``````Variable_1/104*2                                            #for red
Variable_1/104*4 + Variable_2/104*2                         #for orange
Variable_1/104*4 + Variable_2/104*4 + Variable_3/104*2      #for green
``````

Is there something I did not set correctly?

• I tried to replicate and am having the same problem (QGIS 2.14). The offset expression seems to be ignored. One suggestion is to save your style as a .qml style file and then investigate this. Another possibility would be to find a way to control the drawing order of the lines (so that the thickest line gets drawn first, with others drawn on top), this would achieve the same end result. – Cushen Dec 21 '17 at 4:05

Approaching the problem differently and pre-defining each lines offset (0,1,2,3) and then changing the line colour based on a variable then we can get to the end result.

Assuming as you show, we have 4 equal sized lines.

Tested Example:

Below are the colour expressions and overall QGIS style which will achieve the result.

This works for my test where Variables are named as v1, v2, v3 and the variable values are 26, 52, 78, 0 (equal divisions of 104).

Colour Expressions:

Offset 0 (top line):

``````--offset 0
CASE
WHEN "v2" = (104/4)*2 THEN  '#ffe523'
ELSE  '#277c3e'
END
``````

Offset 1:

``````--offset 1
CASE
WHEN "v1" = (104/4)*2 THEN  '#ffe523'
WHEN "v1" = (104/4)*1 THEN  '#277c3e'
END
``````

Offset 2:

``````--offset 2
CASE
WHEN "v1" = (104/4)*2 THEN  '#f93131'
WHEN "v3" = (104/4)*3 THEN  '#277c3e'
ELSE '#ffe523'
END
``````

Offset 3(bottom line):

``````--Offset 3 always red
'#f93131'
``````

Full style file here:

``````<!DOCTYPE qgis PUBLIC 'http://mrcc.com/qgis.dtd' 'SYSTEM'>
<qgis version="2.14.1-Essen" minimumScale="-4.65661e-10" maximumScale="1e+08" simplifyDrawingHints="1" minLabelScale="0" maxLabelScale="1e+08" simplifyDrawingTol="1" simplifyMaxScale="1" hasScaleBasedVisibilityFlag="0" simplifyLocal="1" scaleBasedLabelVisibilityFlag="0">
<edittypes>
<edittype widgetv2type="TextEdit" name="Type">
<widgetv2config IsMultiline="0" fieldEditable="1" UseHtml="0" labelOnTop="0"/>
</edittype>
<edittype widgetv2type="TextEdit" name="v1">
<widgetv2config IsMultiline="0" fieldEditable="1" UseHtml="0" labelOnTop="0"/>
</edittype>
<edittype widgetv2type="TextEdit" name="v2">
<widgetv2config IsMultiline="0" fieldEditable="1" UseHtml="0" labelOnTop="0"/>
</edittype>
<edittype widgetv2type="TextEdit" name="v3">
<widgetv2config IsMultiline="0" fieldEditable="1" UseHtml="0" labelOnTop="0"/>
</edittype>
</edittypes>
<renderer-v2 forceraster="0" symbollevels="0" type="singleSymbol" enableorderby="0">
<symbols>
<symbol alpha="1" clip_to_extent="1" type="fill" name="0">
<layer pass="0" class="SimpleFill" locked="0">
<prop k="border_width_map_unit_scale" v="0,0,0,0,0,0"/>
<prop k="color" v="0,0,0,0"/>
<prop k="joinstyle" v="bevel"/>
<prop k="offset" v="0,0"/>
<prop k="offset_map_unit_scale" v="0,0,0,0,0,0"/>
<prop k="offset_unit" v="MM"/>
<prop k="outline_color" v="0,0,0,255"/>
<prop k="outline_style" v="solid"/>
<prop k="outline_width" v="0.26"/>
<prop k="outline_width_unit" v="MM"/>
<prop k="style" v="solid"/>
</layer>
<layer pass="0" class="LinePatternFill" locked="0">
<prop k="angle" v="0"/>
<prop k="color" v="0,0,255,255"/>
<prop k="distance" v="4"/>
<prop k="distance_map_unit_scale" v="0,0,0,0,0,0"/>
<prop k="distance_unit" v="MM"/>
<prop k="line_width" v="0.26"/>
<prop k="line_width_map_unit_scale" v="0,0,0,0,0,0"/>
<prop k="line_width_unit" v="MM"/>
<prop k="offset" v="3"/>
<prop k="offset_map_unit_scale" v="0,0,0,0,0,0"/>
<prop k="offset_unit" v="MM"/>
<prop k="outline_width_map_unit_scale" v="0,0,0,0,0,0"/>
<prop k="outline_width_unit" v="MM"/>
<symbol alpha="1" clip_to_extent="1" type="line" name="@0@1">
<layer pass="0" class="SimpleLine" locked="0">
<prop k="capstyle" v="square"/>
<prop k="color_dd_active" v="1"/>
<prop k="color_dd_expression" v="'#f93131'"/>
<prop k="color_dd_field" v=""/>
<prop k="color_dd_useexpr" v="1"/>
<prop k="customdash" v="5;2"/>
<prop k="customdash_map_unit_scale" v="0,0,0,0,0,0"/>
<prop k="customdash_unit" v="MM"/>
<prop k="draw_inside_polygon" v="0"/>
<prop k="joinstyle" v="bevel"/>
<prop k="line_color" v="176,178,179,255"/>
<prop k="line_style" v="solid"/>
<prop k="line_width" v="1"/>
<prop k="line_width_unit" v="MM"/>
<prop k="offset" v="0"/>
<prop k="offset_map_unit_scale" v="0,0,0,0,0,0"/>
<prop k="offset_unit" v="MM"/>
<prop k="use_custom_dash" v="0"/>
<prop k="width_dd_active" v="0"/>
<prop k="width_dd_expression" v="&quot;v3&quot; /104*4"/>
<prop k="width_dd_field" v=""/>
<prop k="width_dd_useexpr" v="1"/>
<prop k="width_map_unit_scale" v="0,0,0,0,0,0"/>
</layer>
</symbol>
</layer>
<layer pass="0" class="LinePatternFill" locked="0">
<prop k="angle" v="0"/>
<prop k="color" v="0,0,255,255"/>
<prop k="distance" v="4"/>
<prop k="distance_map_unit_scale" v="0,0,0,0,0,0"/>
<prop k="distance_unit" v="MM"/>
<prop k="line_width" v="0.26"/>
<prop k="line_width_map_unit_scale" v="0,0,0,0,0,0"/>
<prop k="line_width_unit" v="MM"/>
<prop k="offset" v="2"/>
<prop k="offset_map_unit_scale" v="0,0,0,0,0,0"/>
<prop k="offset_unit" v="MM"/>
<prop k="outline_width_map_unit_scale" v="0,0,0,0,0,0"/>
<prop k="outline_width_unit" v="MM"/>
<symbol alpha="1" clip_to_extent="1" type="line" name="@0@2">
<layer pass="0" class="SimpleLine" locked="0">
<prop k="capstyle" v="square"/>
<prop k="color_dd_active" v="1"/>
<prop k="color_dd_expression" v="CASE &#xd;&#xa;&#x9;WHEN &quot;v1&quot; = (104/4)*2 THEN  '#f93131'&#xd;&#xa;&#x9;WHEN &quot;v3&quot; = (104/4)*3 THEN  '#277c3e' &#xd;&#xa;&#x9;ELSE '#ffe523'&#xd;&#xa;END"/>
<prop k="color_dd_field" v=""/>
<prop k="color_dd_useexpr" v="1"/>
<prop k="customdash" v="5;2"/>
<prop k="customdash_map_unit_scale" v="0,0,0,0,0,0"/>
<prop k="customdash_unit" v="MM"/>
<prop k="draw_inside_polygon" v="0"/>
<prop k="joinstyle" v="bevel"/>
<prop k="line_color" v="176,178,179,255"/>
<prop k="line_style" v="solid"/>
<prop k="line_width" v="1"/>
<prop k="line_width_unit" v="MM"/>
<prop k="offset" v="0"/>
<prop k="offset_map_unit_scale" v="0,0,0,0,0,0"/>
<prop k="offset_unit" v="MM"/>
<prop k="use_custom_dash" v="0"/>
<prop k="width_dd_active" v="0"/>
<prop k="width_dd_expression" v="&quot;v3&quot; /104*4"/>
<prop k="width_dd_field" v=""/>
<prop k="width_dd_useexpr" v="1"/>
<prop k="width_map_unit_scale" v="0,0,0,0,0,0"/>
</layer>
</symbol>
</layer>
<layer pass="0" class="LinePatternFill" locked="0">
<prop k="angle" v="0"/>
<prop k="color" v="0,0,255,255"/>
<prop k="distance" v="4"/>
<prop k="distance_map_unit_scale" v="0,0,0,0,0,0"/>
<prop k="distance_unit" v="MM"/>
<prop k="line_width" v="0.26"/>
<prop k="line_width_map_unit_scale" v="0,0,0,0,0,0"/>
<prop k="line_width_unit" v="MM"/>
<prop k="offset" v="1"/>
<prop k="offset_map_unit_scale" v="0,0,0,0,0,0"/>
<prop k="offset_unit" v="MM"/>
<prop k="outline_width_map_unit_scale" v="0,0,0,0,0,0"/>
<prop k="outline_width_unit" v="MM"/>
<symbol alpha="1" clip_to_extent="1" type="line" name="@0@3">
<layer pass="0" class="SimpleLine" locked="0">
<prop k="capstyle" v="square"/>
<prop k="color_dd_active" v="1"/>
<prop k="color_dd_expression" v="CASE &#xd;&#xa;&#x9;WHEN &quot;v1&quot; = (104/4)*2 THEN  '#ffe523'&#xd;&#xa;&#x9;WHEN &quot;v1&quot; = (104/4)*1 THEN  '#277c3e' &#xd;&#xa;END"/>
<prop k="color_dd_field" v=""/>
<prop k="color_dd_useexpr" v="1"/>
<prop k="customdash" v="5;2"/>
<prop k="customdash_map_unit_scale" v="0,0,0,0,0,0"/>
<prop k="customdash_unit" v="MM"/>
<prop k="draw_inside_polygon" v="0"/>
<prop k="joinstyle" v="bevel"/>
<prop k="line_color" v="176,178,179,255"/>
<prop k="line_style" v="solid"/>
<prop k="line_width" v="1"/>
<prop k="line_width_unit" v="MM"/>
<prop k="offset" v="0"/>
<prop k="offset_map_unit_scale" v="0,0,0,0,0,0"/>
<prop k="offset_unit" v="MM"/>
<prop k="use_custom_dash" v="0"/>
<prop k="width_dd_active" v="0"/>
<prop k="width_dd_expression" v="&quot;v3&quot; /104*4"/>
<prop k="width_dd_field" v=""/>
<prop k="width_dd_useexpr" v="1"/>
<prop k="width_map_unit_scale" v="0,0,0,0,0,0"/>
</layer>
</symbol>
</layer>
<layer pass="0" class="LinePatternFill" locked="0">
<prop k="angle" v="0"/>
<prop k="color" v="0,0,255,255"/>
<prop k="distance" v="4"/>
<prop k="distance_map_unit_scale" v="0,0,0,0,0,0"/>
<prop k="distance_unit" v="MM"/>
<prop k="line_width" v="0.26"/>
<prop k="line_width_map_unit_scale" v="0,0,0,0,0,0"/>
<prop k="line_width_unit" v="MM"/>
<prop k="offset" v="0"/>
<prop k="offset_map_unit_scale" v="0,0,0,0,0,0"/>
<prop k="offset_unit" v="MM"/>
<prop k="outline_width_map_unit_scale" v="0,0,0,0,0,0"/>
<prop k="outline_width_unit" v="MM"/>
<symbol alpha="1" clip_to_extent="1" type="line" name="@0@4">
<layer pass="0" class="SimpleLine" locked="0">
<prop k="capstyle" v="square"/>
<prop k="color_dd_active" v="1"/>
<prop k="color_dd_expression" v="CASE &#xd;&#xa;&#x9;WHEN &quot;v2&quot; = (104/4)*2 THEN  '#ffe523'&#xd;&#xa;&#x9;ELSE  '#277c3e' &#xd;&#xa;END"/>
<prop k="color_dd_field" v=""/>
<prop k="color_dd_useexpr" v="1"/>
<prop k="customdash" v="5;2"/>
<prop k="customdash_map_unit_scale" v="0,0,0,0,0,0"/>
<prop k="customdash_unit" v="MM"/>
<prop k="draw_inside_polygon" v="0"/>
<prop k="joinstyle" v="bevel"/>
<prop k="line_color" v="176,178,179,255"/>
<prop k="line_style" v="solid"/>
<prop k="line_width" v="1"/>
<prop k="line_width_unit" v="MM"/>
<prop k="offset" v="0"/>
<prop k="offset_map_unit_scale" v="0,0,0,0,0,0"/>
<prop k="offset_unit" v="MM"/>
<prop k="use_custom_dash" v="0"/>
<prop k="width_dd_active" v="0"/>
<prop k="width_dd_expression" v="&quot;v3&quot; /104*4"/>
<prop k="width_dd_field" v=""/>
<prop k="width_dd_useexpr" v="1"/>
<prop k="width_map_unit_scale" v="0,0,0,0,0,0"/>
</layer>
</symbol>
</layer>
</symbol>
</symbols>
<rotation/>
<sizescale scalemethod="diameter"/>
<orderby>
<orderByClause asc="0" nullsFirst="0">v1</orderByClause>
</orderby>
</renderer-v2>
<labeling type="simple"/>
<customproperties>
<property key="labeling" value="pal"/>
<property key="labeling/angleOffset" value="0"/>
<property key="labeling/blendMode" value="0"/>
<property key="labeling/bufferBlendMode" value="0"/>
<property key="labeling/bufferColorA" value="255"/>
<property key="labeling/bufferColorB" value="255"/>
<property key="labeling/bufferColorG" value="255"/>
<property key="labeling/bufferColorR" value="255"/>
<property key="labeling/bufferDraw" value="true"/>
<property key="labeling/bufferJoinStyle" value="64"/>
<property key="labeling/bufferNoFill" value="false"/>
<property key="labeling/bufferSize" value="1"/>
<property key="labeling/bufferSizeInMapUnits" value="false"/>
<property key="labeling/bufferSizeMapUnitMaxScale" value="0"/>
<property key="labeling/bufferSizeMapUnitMinScale" value="0"/>
<property key="labeling/bufferTransp" value="0"/>
<property key="labeling/centroidInside" value="true"/>
<property key="labeling/centroidWhole" value="false"/>
<property key="labeling/decimals" value="3"/>
<property key="labeling/displayAll" value="false"/>
<property key="labeling/dist" value="0"/>
<property key="labeling/distInMapUnits" value="false"/>
<property key="labeling/distMapUnitMaxScale" value="0"/>
<property key="labeling/distMapUnitMinScale" value="0"/>
<property key="labeling/drawLabels" value="true"/>
<property key="labeling/enabled" value="true"/>
<property key="labeling/fieldName" value="'COMMUNE: '||Type"/>
<property key="labeling/fitInPolygonOnly" value="false"/>
<property key="labeling/fontCapitals" value="0"/>
<property key="labeling/fontFamily" value="MS Shell Dlg 2"/>
<property key="labeling/fontItalic" value="false"/>
<property key="labeling/fontLetterSpacing" value="0"/>
<property key="labeling/fontLimitPixelSize" value="false"/>
<property key="labeling/fontMaxPixelSize" value="10000"/>
<property key="labeling/fontMinPixelSize" value="3"/>
<property key="labeling/fontSize" value="8"/>
<property key="labeling/fontSizeInMapUnits" value="false"/>
<property key="labeling/fontSizeMapUnitMaxScale" value="0"/>
<property key="labeling/fontSizeMapUnitMinScale" value="0"/>
<property key="labeling/fontStrikeout" value="false"/>
<property key="labeling/fontUnderline" value="false"/>
<property key="labeling/fontWeight" value="50"/>
<property key="labeling/fontWordSpacing" value="0"/>
<property key="labeling/formatNumbers" value="false"/>
<property key="labeling/isExpression" value="true"/>
<property key="labeling/labelOffsetInMapUnits" value="true"/>
<property key="labeling/labelOffsetMapUnitMaxScale" value="0"/>
<property key="labeling/labelOffsetMapUnitMinScale" value="0"/>
<property key="labeling/labelPerPart" value="false"/>
<property key="labeling/leftDirectionSymbol" value="&lt;"/>
<property key="labeling/limitNumLabels" value="false"/>
<property key="labeling/maxCurvedCharAngleIn" value="20"/>
<property key="labeling/maxCurvedCharAngleOut" value="-20"/>
<property key="labeling/maxNumLabels" value="2000"/>
<property key="labeling/mergeLines" value="false"/>
<property key="labeling/minFeatureSize" value="0"/>
<property key="labeling/multilineAlign" value="0"/>
<property key="labeling/multilineHeight" value="1"/>
<property key="labeling/namedStyle" value="Normal"/>
<property key="labeling/obstacle" value="true"/>
<property key="labeling/obstacleFactor" value="1"/>
<property key="labeling/obstacleType" value="0"/>
<property key="labeling/offsetType" value="0"/>
<property key="labeling/placeDirectionSymbol" value="0"/>
<property key="labeling/placement" value="1"/>
<property key="labeling/placementFlags" value="10"/>
<property key="labeling/plussign" value="false"/>
<property key="labeling/predefinedPositionOrder" value="TR,TL,BR,BL,R,L,TSR,BSR"/>
<property key="labeling/preserveRotation" value="true"/>
<property key="labeling/previewBkgrdColor" value="#ffffff"/>
<property key="labeling/priority" value="5"/>
<property key="labeling/repeatDistance" value="0"/>
<property key="labeling/repeatDistanceMapUnitMaxScale" value="0"/>
<property key="labeling/repeatDistanceMapUnitMinScale" value="0"/>
<property key="labeling/repeatDistanceUnit" value="1"/>
<property key="labeling/reverseDirectionSymbol" value="false"/>
<property key="labeling/rightDirectionSymbol" value=">"/>
<property key="labeling/scaleMax" value="10000000"/>
<property key="labeling/scaleMin" value="1"/>
<property key="labeling/scaleVisibility" value="false"/>
<property key="labeling/shapeBlendMode" value="0"/>
<property key="labeling/shapeBorderColorA" value="255"/>
<property key="labeling/shapeBorderColorB" value="128"/>
<property key="labeling/shapeBorderColorG" value="128"/>
<property key="labeling/shapeBorderColorR" value="128"/>
<property key="labeling/shapeBorderWidth" value="0"/>
<property key="labeling/shapeBorderWidthMapUnitMaxScale" value="0"/>
<property key="labeling/shapeBorderWidthMapUnitMinScale" value="0"/>
<property key="labeling/shapeBorderWidthUnits" value="1"/>
<property key="labeling/shapeDraw" value="false"/>
<property key="labeling/shapeFillColorA" value="255"/>
<property key="labeling/shapeFillColorB" value="255"/>
<property key="labeling/shapeFillColorG" value="255"/>
<property key="labeling/shapeFillColorR" value="255"/>
<property key="labeling/shapeJoinStyle" value="64"/>
<property key="labeling/shapeOffsetMapUnitMaxScale" value="0"/>
<property key="labeling/shapeOffsetMapUnitMinScale" value="0"/>
<property key="labeling/shapeOffsetUnits" value="1"/>
<property key="labeling/shapeOffsetX" value="0"/>
<property key="labeling/shapeOffsetY" value="0"/>
<property key="labeling/shapeRotation" value="0"/>
<property key="labeling/shapeRotationType" value="0"/>
<property key="labeling/shapeSVGFile" value=""/>
<property key="labeling/shapeSizeMapUnitMaxScale" value="0"/>
<property key="labeling/shapeSizeMapUnitMinScale" value="0"/>
<property key="labeling/shapeSizeType" value="0"/>
<property key="labeling/shapeSizeUnits" value="1"/>
<property key="labeling/shapeSizeX" value="0"/>
<property key="labeling/shapeSizeY" value="0"/>
<property key="labeling/shapeTransparency" value="0"/>
<property key="labeling/shapeType" value="0"/>
<property key="labeling/textColorA" value="255"/>
<property key="labeling/textColorB" value="0"/>
<property key="labeling/textColorG" value="0"/>
<property key="labeling/textColorR" value="0"/>
<property key="labeling/textTransp" value="0"/>
<property key="labeling/upsidedownLabels" value="0"/>
<property key="labeling/wrapChar" value=""/>
<property key="labeling/xOffset" value="0"/>
<property key="labeling/yOffset" value="0"/>
<property key="labeling/zIndex" value="0"/>
<property key="variableNames" value="_fields_"/>
<property key="variableValues" value=""/>
</customproperties>
<blendMode>0</blendMode>
<featureBlendMode>0</featureBlendMode>
<layerTransparency>0</layerTransparency>
<displayfield>Type</displayfield>
<label>0</label>
<labelattributes>
<label fieldname="" text="Label"/>
<family fieldname="" name="MS Shell Dlg 2"/>
<size fieldname="" units="pt" value="12"/>
<bold fieldname="" on="0"/>
<italic fieldname="" on="0"/>
<underline fieldname="" on="0"/>
<strikeout fieldname="" on="0"/>
<color fieldname="" red="0" blue="0" green="0"/>
<x fieldname=""/>
<y fieldname=""/>
<offset x="0" y="0" units="pt" yfieldname="" xfieldname=""/>
<angle fieldname="" value="0" auto="0"/>
<alignment fieldname="" value="center"/>
<buffercolor fieldname="" red="255" blue="255" green="255"/>
<buffersize fieldname="" units="pt" value="1"/>
<bufferenabled fieldname="" on=""/>
<multilineenabled fieldname="" on=""/>
<selectedonly on=""/>
</labelattributes>
<SingleCategoryDiagramRenderer diagramType="Pie">
<DiagramCategory penColor="#000000" labelPlacementMethod="XHeight" penWidth="0" diagramOrientation="Up" minimumSize="0" barWidth="5" penAlpha="255" maxScaleDenominator="1e+08" backgroundColor="#ffffff" transparency="0" width="15" scaleDependency="Area" backgroundAlpha="255" angleOffset="1440" scaleBasedVisibility="0" enabled="0" height="15" sizeType="MM" minScaleDenominator="-4.65661e-10">
<fontProperties description="MS Shell Dlg 2,8.25,-1,5,50,0,0,0,0,0" style=""/>
</DiagramCategory>
</SingleCategoryDiagramRenderer>
<DiagramLayerSettings yPosColumn="-1" linePlacementFlags="10" placement="0" dist="0" xPosColumn="-1" priority="0" obstacle="0" zIndex="0" showAll="1"/>
<annotationform></annotationform>
<excludeAttributesWMS/>
<excludeAttributesWFS/>
<attributeactions/>
<editform></editform>
<editforminit/>
<editforminitcodesource>0</editforminitcodesource>
<editforminitfilepath></editforminitfilepath>
<editforminitcode><![CDATA[# -*- coding: utf-8 -*-
"""
QGIS forms can have a Python function that is called when the form is
opened.

Enter the name of the function in the "Python Init function"
field.
An example follows:
"""
from PyQt4.QtGui import QWidget

def my_form_open(dialog, layer, feature):
geom = feature.geometry()
control = dialog.findChild(QWidget, "MyLineEdit")
]]></editforminitcode>
<featformsuppress>0</featformsuppress>
<editorlayout>generatedlayout</editorlayout>
<widgets/>
<conditionalstyles>
<rowstyles/>
<fieldstyles/>
</conditionalstyles>
<layerGeometryType>2</layerGeometryType>
</qgis>
``````