Skip to content

Commit

Permalink
fix: dashed line conversion
Browse files Browse the repository at this point in the history
QGIS only honours line.customdash if line.use_custom_dash is 1.
Otherwise it honours line.style (same values as fill.outlineStyle)
Make sure we read the right one during reading, and make sure we
write line.use_custom_dash=1 when writing custom dashes.
  • Loading branch information
olsen232 committed Dec 12, 2024
1 parent 76b991e commit bde63ec
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 28 deletions.
1 change: 1 addition & 0 deletions data/qmls/line_simple.qml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<Option name="line_width" value="3" type="QString"/>
<Option name="line_width_unit" value="Pixel" type="QString"/>
<Option name="customdash" value="12;12" type="QString"/>
<Option name="use_custom_dash" value="1" type="QString"/>
</Option>
</layer>
</symbol>
Expand Down
1 change: 1 addition & 0 deletions data/qmls_old/line_simple.qml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<prop k="line_width" v="3"/>
<prop k="line_width_unit" v="Pixel"/>
<prop k="customdash" v="12;12"/>
<prop k="use_custom_dash" v="1"/>
</layer>
</symbol>
</symbols>
Expand Down
52 changes: 24 additions & 28 deletions src/QGISStyleParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,15 @@ type QmlRange = {
};
};

export const outlineStyleDashArrays = {
dot: [2, 2],
dash: [10, 2]
const dot = [2, 2];
const dash = [10, 2];

const lineStyleDashArrays: { [key: string]: number[] | undefined } = {
solid: undefined,
dot: dot,
dash: dash,
'dash dot': [...dash, ...dot],
'dash dot dot': [...dash, ...dot, ...dot]
};

const AnchorMap = {
Expand Down Expand Up @@ -609,8 +615,16 @@ export class QGISStyleParser implements StyleParser {
if (qmlMarkerProps.joinstyle) {
lineSymbolizer.join = qmlMarkerProps.joinstyle;
}
if (qmlMarkerProps.customdash) {
lineSymbolizer.dasharray = qmlMarkerProps.customdash.split(';').map(parseFloat);
if (!qmlMarkerProps.use_custom_dash || qmlMarkerProps.use_custom_dash === '0') {
const lineStyle = qmlMarkerProps?.line_style || 'solid';
const lineDashArray = lineStyleDashArrays[lineStyle as string];
if (lineDashArray) {
lineSymbolizer.dasharray = lineDashArray;
}
} else {
if (qmlMarkerProps.customdash) {
lineSymbolizer.dasharray = qmlMarkerProps.customdash.split(';').map(parseFloat);
}
}
if (qmlMarkerProps.offset) {
lineSymbolizer.perpendicularOffset = parseFloat(qmlMarkerProps.offset);
Expand Down Expand Up @@ -640,7 +654,7 @@ export class QGISStyleParser implements StyleParser {

const qmlMarkerProps: any = qmlSymbolizerLayerPropsToObject(symbolizerLayer);

let outlineStyle = qmlMarkerProps?.outline_style || 'solid';
const outlineStyle = qmlMarkerProps?.outline_style || 'solid';
if (qmlMarkerProps.outline_color && 'no' !== outlineStyle) {
fillSymbolizer.outlineColor = this.qmlColorToHex(qmlMarkerProps.outline_color);
}
Expand All @@ -651,28 +665,9 @@ export class QGISStyleParser implements StyleParser {
fillSymbolizer.color = this.qmlColorToHex(qmlMarkerProps.color);
}

switch (outlineStyle) {
case 'dot':
fillSymbolizer.outlineDasharray = outlineStyleDashArrays.dot;
break;
case 'dash':
fillSymbolizer.outlineDasharray = outlineStyleDashArrays.dash;
break;
case 'dash dot':
fillSymbolizer.outlineDasharray = [
...outlineStyleDashArrays.dash,
...outlineStyleDashArrays.dot
];
break;
case 'dash dot dot':
fillSymbolizer.outlineDasharray = [
...outlineStyleDashArrays.dash,
...outlineStyleDashArrays.dot,
...outlineStyleDashArrays.dot
];
break;
default:
break;
const outlineDashArray = lineStyleDashArrays[outlineStyle];
if (outlineDashArray) {
fillSymbolizer.outlineDasharray = outlineDashArray;
}

if (qmlMarkerProps.outline_width && 'no' !== outlineStyle) {
Expand Down Expand Up @@ -854,6 +849,7 @@ export class QGISStyleParser implements StyleParser {
};
if (symbolizer.dasharray) {
qmlProps.customdash = symbolizer.dasharray.join(';');
qmlProps.use_custom_dash = '1';
}

return {
Expand Down

0 comments on commit bde63ec

Please sign in to comment.