Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement support for heatmap renderer conversion #72

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions felt/core/fsl_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
QgsSingleBandGrayRenderer,
QgsRasterPipe,
QgsRasterDataProvider,
QgsColorRampShader
QgsColorRampShader,
QgsHeatmapRenderer
)

from .logger import Logger
Expand Down Expand Up @@ -336,9 +337,7 @@ def vector_renderer_to_fsl(renderer: QgsFeatureRenderer,
QgsRuleBasedRenderer:
FslConverter.rule_based_renderer_to_fsl,
QgsNullSymbolRenderer: FslConverter.null_renderer_to_fsl,

# Could potentially be supported:
# QgsHeatmapRenderer
QgsHeatmapRenderer: FslConverter.heatmap_renderer_to_fsl,

# No meaningful conversions for these types:
# Qgs25DRenderer
Expand Down Expand Up @@ -605,6 +604,45 @@ def rule_based_renderer_to_fsl(
"type": "categorical"
}

@staticmethod
def heatmap_renderer_to_fsl(renderer: QgsHeatmapRenderer,
context: ConversionContext,
layer_opacity: float = 1) \
-> Optional[Dict[str, object]]:
"""
Converts a QGIS heatmap renderer to an FSL definition
"""
colors = []
ramp = renderer.colorRamp()
for i in range(FslConverter.COLOR_RAMP_INTERPOLATION_STEPS):
val = i / FslConverter.COLOR_RAMP_INTERPOLATION_STEPS
color = ramp.color(val)
colors.append(color.name())

if renderer.weightExpression():
context.push_warning(
'Heatmap point weighting cannot be converted',
LogLevel.Warning,
detail={
'object': 'renderer',
'renderer': 'heatmap',
'cause': 'heatmap_point_weight',
'summary': 'heatmap with point weighting'
})

return {
"style": {
"color": colors,
"opacity": layer_opacity,
"size": FslConverter.convert_to_pixels(
renderer.radius(), renderer.radiusUnit(),
context),
"intensity": 1
},
"legend": {"displayName": {"0": "Low", "1": "High"}},
"type": "heatmap"
}

@staticmethod
def null_renderer_to_fsl(renderer: QgsNullSymbolRenderer,
context: ConversionContext,
Expand Down
89 changes: 88 additions & 1 deletion felt/test/test_fsl_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
QgsSingleBandGrayRenderer,
QgsContrastEnhancement,
QgsInvertedPolygonRenderer,
QgsVectorLayerSimpleLabeling
QgsVectorLayerSimpleLabeling,
QgsHeatmapRenderer
)

from .utilities import get_qgis_app
Expand Down Expand Up @@ -1950,6 +1951,92 @@ def test_graduated_renderer(self):
'type': 'numeric'}
)

def test_heatmap_renderer(self):
"""
Test converting heatmap renderers
"""
conversion_context = ConversionContext()

renderer = QgsHeatmapRenderer()
self.assertEqual(
FslConverter.vector_renderer_to_fsl(renderer, conversion_context),
{'legend': {'displayName': {'0': 'Low', '1': 'High'}},
'style': {'color': ['#ffffff',
'#f7f7f7',
'#eeeeee',
'#e6e6e6',
'#dddddd',
'#d5d5d5',
'#cccccc',
'#c3c3c3',
'#bbbbbb',
'#b3b3b3',
'#aaaaaa',
'#a2a2a2',
'#999999',
'#919191',
'#888888',
'#808080',
'#777777',
'#6f6f6f',
'#666666',
'#5e5e5e',
'#555555',
'#4d4d4d',
'#444444',
'#3b3b3b',
'#333333',
'#2a2a2a',
'#222222',
'#191919',
'#111111',
'#080808'],
'intensity': 1,
'opacity': 1,
'size': 38},
'type': 'heatmap'}
)

self.assertEqual(
FslConverter.vector_renderer_to_fsl(renderer, conversion_context,
layer_opacity=0.5),
{'legend': {'displayName': {'0': 'Low', '1': 'High'}},
'style': {'color': ['#ffffff',
'#f7f7f7',
'#eeeeee',
'#e6e6e6',
'#dddddd',
'#d5d5d5',
'#cccccc',
'#c3c3c3',
'#bbbbbb',
'#b3b3b3',
'#aaaaaa',
'#a2a2a2',
'#999999',
'#919191',
'#888888',
'#808080',
'#777777',
'#6f6f6f',
'#666666',
'#5e5e5e',
'#555555',
'#4d4d4d',
'#444444',
'#3b3b3b',
'#333333',
'#2a2a2a',
'#222222',
'#191919',
'#111111',
'#080808'],
'intensity': 1,
'opacity': 0.5,
'size': 38},
'type': 'heatmap'}
)

@unittest.skipIf(Qgis.QGIS_VERSION_INT < 32400, 'QGIS too old')
def test_text_format_conversion(self):
"""
Expand Down
Loading