Skip to content

Commit

Permalink
Convert label zoom range
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 3, 2024
1 parent 8c4307f commit f3aea19
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
10 changes: 7 additions & 3 deletions felt/core/fsl_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
QgsStringUtils
)

from .map_utils import MapUtils


class LogLevel(Enum):
"""
Expand Down Expand Up @@ -1192,6 +1194,11 @@ def label_settings_to_fsl(settings: QgsPalLayerSettings,
)
if settings.autoWrapLength > 0:
converted_format['maxLineChars'] = settings.autoWrapLength
if settings.scaleVisibility:
converted_format['minZoom'] = MapUtils.map_scale_to_leaflet_tile_zoom(
settings.minimumScale)
converted_format['maxZoom'] = MapUtils.map_scale_to_leaflet_tile_zoom(
settings.maximumScale)

res = {
'config': {
Expand All @@ -1200,9 +1207,6 @@ def label_settings_to_fsl(settings: QgsPalLayerSettings,
'label': converted_format
}

# maxZoom
# minZoom

# For now, we don't convert these and leave them to the Felt
# defaults -- there's too many other unsupported placement
# related configuration settings in QGIS which impact on the
Expand Down
34 changes: 18 additions & 16 deletions felt/core/map_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""Map utilities
.. note:: This program is free software; you can redistribute it and/or modify
Expand All @@ -7,12 +6,6 @@
(at your option) any later version.
"""

__author__ = '(C) 2018 by Nyall Dawson'
__date__ = '20/04/2018'
__copyright__ = 'Copyright 2018, North Road'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

from qgis.PyQt.QtCore import (
QSize
)
Expand Down Expand Up @@ -54,6 +47,23 @@ class MapUtils:
70.5310735
]

@staticmethod
def map_scale_to_leaflet_tile_zoom(
scale: float
) -> int:
"""
Returns the leaflet tile zoom level roughly
corresponding to a QGIS map scale
"""
for level, min_scale in enumerate(MapUtils.ZOOM_LEVEL_SCALE_BREAKS):
if min_scale < scale:
# we play it safe and zoom out a step -- this is because
# we don't know the screen size or DPI on which the map
# will actually be viewed, so we err on the conservative side
return level - 1

return len(MapUtils.ZOOM_LEVEL_SCALE_BREAKS) - 1

@staticmethod
def calculate_leaflet_tile_zoom_for_extent(
extent: QgsReferencedRectangle,
Expand All @@ -74,12 +84,4 @@ def calculate_leaflet_tile_zoom_for_extent(
map_settings.setOutputSize(target_map_size)

scale = map_settings.scale()

for level, min_scale in enumerate(MapUtils.ZOOM_LEVEL_SCALE_BREAKS):
if min_scale < scale:
# we play it safe and zoom out a step -- this is because
# we don't know the screen size or DPI on which the map
# will actually be viewed, so we err on the conservative side
return level - 1

return len(MapUtils.ZOOM_LEVEL_SCALE_BREAKS) - 1
return MapUtils.map_scale_to_leaflet_tile_zoom(scale)
19 changes: 19 additions & 0 deletions felt/test/test_fsl_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -1655,6 +1655,25 @@ def test_label_settings(self):
)
label_settings.autoWrapLength = 0

# zoom ranges
label_settings.scaleVisibility = True
label_settings.minimumScale = 5677474
label_settings.maximumScale = 34512
self.assertEqual(
FslConverter.label_settings_to_fsl(label_settings, context),
{'config': {'labelAttribute': 'my_field'},
'label': {'color': 'rgba(255, 0, 0, 0.3)',
'fontSize': 13,
'fontStyle': 'normal',
'fontWeight': 400,
'haloColor': 'rgba(0, 0, 0, 0)',
'haloWidth': 4,
'letterSpacing': 0.0,
'lineHeight': 1.0,
'maxZoom': 14,
'minZoom': 6}}
)


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

0 comments on commit f3aea19

Please sign in to comment.