Skip to content

Commit

Permalink
Handle mixed dash/solid lines for rules (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson authored Jun 9, 2024
1 parent babe00f commit e9b1fb8
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
12 changes: 12 additions & 0 deletions felt/core/fsl_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def format_warnings_for_reporting(self) -> Dict[str, object]:
class FslConverter:
NULL_COLOR = "rgba(0, 0, 0, 0)"
COLOR_RAMP_INTERPOLATION_STEPS = 30
SOLID_LINE_DASH_HACK = [1, 0]

@staticmethod
def create_symbol_dict() -> Dict[str, object]:
Expand Down Expand Up @@ -655,13 +656,22 @@ def create_varying_style_from_list(
if len(styles) < 1:
return []

any_has_dash_array = False
for symbol in styles:
for layer in symbol:
if 'dashArray' in layer:
any_has_dash_array = True

result = []
# upgrade all properties in first symbol to lists
first_symbol = styles[0]
for layer in first_symbol:
list_dict = {}
for key, value in layer.items():
list_dict[key] = [value]

if any_has_dash_array and 'dashArray' not in list_dict:
list_dict['dashArray'] = [FslConverter.SOLID_LINE_DASH_HACK]
result.append(list_dict)

for symbol in styles[1:]:
Expand All @@ -676,6 +686,8 @@ def create_varying_style_from_list(
# symbol
if source_layer and key in source_layer:
value.append(source_layer[key])
elif key == 'dashArray':
value.append(FslConverter.SOLID_LINE_DASH_HACK)
else:
value.append(value[0])

Expand Down
65 changes: 65 additions & 0 deletions felt/test/test_fsl_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -1891,6 +1891,71 @@ def test_categorized_no_stroke(self):
'type': 'categorical'}
)

def test_categorized_dash_array_for_one(self):
"""
Test categorized renderer with dashes on one category only
"""
conversion_context = ConversionContext()

line = QgsSimpleLineSymbolLayer(color=QColor(255, 0, 0))
line.setPenStyle(Qt.DashLine)
line_symbol = QgsLineSymbol()
line_symbol.changeSymbolLayer(0, line.clone())

line_symbol2 = QgsLineSymbol()
line.setColor(QColor(255, 0, 255))
line.setPenStyle(Qt.SolidLine)
line_symbol2.changeSymbolLayer(0, line.clone())

categories = [
QgsRendererCategory(1, line_symbol.clone(), 'first cat'),
QgsRendererCategory(2, line_symbol2.clone(), 'second cat')
]

renderer = QgsCategorizedSymbolRenderer('my_field',
categories)
self.assertEqual(
FslConverter.vector_renderer_to_fsl(renderer,
conversion_context),
{'config': {'categoricalAttribute': 'my_field',
'categories': ['1', '2'],
'showOther': False},
'legend': {'displayName': {'1': 'first cat', '2': 'second cat'}},
'style': [{'color': ['rgb(255, 0, 0)', 'rgb(255, 0, 255)'],
'dashArray': [[2.5, 2], [1, 0]],
'isClickable': False,
'isHoverable': False,
'lineCap': 'square',
'lineJoin': 'bevel',
'size': 1}],
'type': 'categorical'}
)

# flip category order and re-test
categories = [
QgsRendererCategory(1, line_symbol2.clone(), 'first cat'),
QgsRendererCategory(2, line_symbol.clone(), 'second cat')
]

renderer = QgsCategorizedSymbolRenderer('my_field',
categories)
self.assertEqual(
FslConverter.vector_renderer_to_fsl(renderer,
conversion_context),
{'config': {'categoricalAttribute': 'my_field',
'categories': ['1', '2'],
'showOther': False},
'legend': {'displayName': {'1': 'first cat', '2': 'second cat'}},
'style': [{'color': ['rgb(255, 0, 255)', 'rgb(255, 0, 0)'],
'dashArray': [[1, 0], [2.5, 2]],
'isClickable': False,
'isHoverable': False,
'lineCap': 'square',
'lineJoin': 'bevel',
'size': 1}],
'type': 'categorical'}
)

def test_graduated_renderer(self):
"""
Test converting graduated renderers
Expand Down

0 comments on commit e9b1fb8

Please sign in to comment.