diff --git a/felt/core/fsl_converter.py b/felt/core/fsl_converter.py index 975f636..cfec26b 100644 --- a/felt/core/fsl_converter.py +++ b/felt/core/fsl_converter.py @@ -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]: @@ -655,6 +656,12 @@ 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] @@ -662,6 +669,9 @@ def create_varying_style_from_list( 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:]: @@ -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]) diff --git a/felt/test/test_fsl_conversion.py b/felt/test/test_fsl_conversion.py index 084402d..3379de7 100644 --- a/felt/test/test_fsl_conversion.py +++ b/felt/test/test_fsl_conversion.py @@ -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