Skip to content

Commit

Permalink
Convert scale based visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 9, 2024
1 parent 3a9e6db commit 1d369c6
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 4 deletions.
26 changes: 25 additions & 1 deletion felt/core/fsl_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from qgis.core import (
NULL,
QgsVectorLayer,
QgsSymbol,
QgsSymbolLayer,
QgsSimpleFillSymbolLayer,
Expand Down Expand Up @@ -71,9 +72,32 @@ def push_warning(self, warning: str, level: LogLevel = LogLevel.Warning):


class FslConverter:

NULL_COLOR = "rgba(0, 0, 0, 0)"

@staticmethod
def vector_layer_to_fsl(
layer: QgsVectorLayer,
context: ConversionContext
) -> Optional[Dict[str, object]]:
"""
Converts a vector layer to FSL
"""
fsl = FslConverter.vector_renderer_to_fsl(
layer.renderer(), context, layer.opacity()
)
if not fsl:
return None

if layer.hasScaleBasedVisibility():
if layer.minimumScale():
fsl['style']['minZoom'] = MapUtils.map_scale_to_leaflet_tile_zoom(
layer.minimumScale())
if layer.maximumScale():
fsl['style']['maxZoom'] = MapUtils.map_scale_to_leaflet_tile_zoom(
layer.maximumScale())

return fsl

@staticmethod
def vector_renderer_to_fsl(renderer: QgsFeatureRenderer,
context: ConversionContext,
Expand Down
4 changes: 2 additions & 2 deletions felt/core/layer_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ def representative_layer_style(layer: QgsVectorLayer) -> LayerStyle:
return LayerStyle()

context = ConversionContext()
fsl = FslConverter.vector_renderer_to_fsl(
layer.renderer(), context, layer.opacity()
fsl = FslConverter.vector_layer_to_fsl(
layer, context
)
if fsl:
fsl['version'] = '2.1'
Expand Down
87 changes: 86 additions & 1 deletion felt/test/test_fsl_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
QgsRendererRange,
QgsTextFormat,
QgsStringUtils,
QgsPalLayerSettings
QgsPalLayerSettings,
QgsVectorLayer
)

from .utilities import get_qgis_app
Expand Down Expand Up @@ -1693,6 +1694,90 @@ def test_label_settings(self):
'minZoom': 6}}
)

def test_layer_to_fsl(self):
"""
Test converting whole layer to FSL
"""
conversion_context = ConversionContext()

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

renderer = QgsSingleSymbolRenderer(line_symbol.clone())
self.assertEqual(
FslConverter.vector_renderer_to_fsl(renderer, conversion_context),
{'legend': {},
'style': {'color': 'rgb(255, 0, 0)',
'lineCap': 'square',
'lineJoin': 'bevel',
'size': 1},
'type': 'simple'}
)
layer = QgsVectorLayer('x', '', 'memory')
layer.setRenderer(renderer)

self.assertEqual(
FslConverter.vector_layer_to_fsl(layer, conversion_context),
{'legend': {},
'style': {'color': 'rgb(255, 0, 0)',
'lineCap': 'square',
'lineJoin': 'bevel',
'size': 1},
'type': 'simple'}
)

# layer opacity
layer.setOpacity(0.5)
self.assertEqual(
FslConverter.vector_layer_to_fsl(layer, conversion_context),
{'legend': {},
'style': {'color': 'rgb(255, 0, 0)',
'lineCap': 'square',
'lineJoin': 'bevel',
'opacity': 0.5,
'size': 1},
'type': 'simple'}
)
layer.setOpacity(1)

# zoom range
layer.setScaleBasedVisibility(True)
layer.setMinimumScale(10000)
self.assertEqual(
FslConverter.vector_layer_to_fsl(layer, conversion_context),
{'legend': {},
'style': {'color': 'rgb(255, 0, 0)',
'lineCap': 'square',
'lineJoin': 'bevel',
'minZoom': 15,
'size': 1},
'type': 'simple'}
)
layer.setMaximumScale(1000)
self.assertEqual(
FslConverter.vector_layer_to_fsl(layer, conversion_context),
{'legend': {},
'style': {'color': 'rgb(255, 0, 0)',
'lineCap': 'square',
'lineJoin': 'bevel',
'minZoom': 15,
'maxZoom': 19,
'size': 1},
'type': 'simple'}
)
layer.setMinimumScale(0)
self.assertEqual(
FslConverter.vector_layer_to_fsl(layer, conversion_context),
{'legend': {},
'style': {'color': 'rgb(255, 0, 0)',
'lineCap': 'square',
'lineJoin': 'bevel',
'maxZoom': 19,
'size': 1},
'type': 'simple'}
)


if __name__ == "__main__":
suite = unittest.makeSuite(FslConversionTest)
Expand Down

0 comments on commit 1d369c6

Please sign in to comment.