Skip to content

Commit

Permalink
Report FSL conversion warnings to usage API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 27, 2024
1 parent d806d10 commit 5c95243
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 14 deletions.
26 changes: 17 additions & 9 deletions felt/core/layer_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,23 +206,23 @@ def merge_dicts(tgt: Dict, enhancer: Dict) -> Dict:

@staticmethod
def representative_layer_style(
layer: QgsMapLayer) -> LayerStyle:
layer: QgsMapLayer,
conversion_context: ConversionContext) -> LayerStyle:
"""
Returns a decent representative style for a layer
"""
if not layer.isSpatial() or not layer.renderer():
return LayerStyle()

context = ConversionContext()
fsl = None
if isinstance(layer, QgsVectorLayer):
fsl = FslConverter.vector_layer_to_fsl(
layer, context
layer, conversion_context
)
if layer.labelsEnabled():
label_def = FslConverter.label_settings_to_fsl(
layer.labeling().settings(),
context
conversion_context
)
if label_def:
if fsl:
Expand All @@ -232,7 +232,7 @@ def representative_layer_style(

elif isinstance(layer, QgsRasterLayer):
fsl = FslConverter.raster_layer_to_fsl(
layer, context
layer, conversion_context
)

if fsl:
Expand Down Expand Up @@ -279,6 +279,7 @@ def generate_file_name(self, suffix: str) -> str:
def export_layer_for_felt(
self,
layer: QgsMapLayer,
conversion_context: ConversionContext,
feedback: Optional[QgsFeedback] = None,
force_upload_raster_as_styled: bool = False
) -> ZippedExportResult:
Expand All @@ -287,10 +288,13 @@ def export_layer_for_felt(
:raises LayerPackagingException
"""
if isinstance(layer, QgsVectorLayer):
res = self.export_vector_layer(layer, feedback)
res = self.export_vector_layer(layer,
conversion_context,
feedback)
elif isinstance(layer, QgsRasterLayer):
res = self.export_raster_layer(
layer, feedback, force_upload_raster_as_styled)
layer, conversion_context,
feedback, force_upload_raster_as_styled)
else:
assert False

Expand Down Expand Up @@ -326,6 +330,7 @@ def _get_original_style_xml(layer: QgsMapLayer) -> str:
def export_vector_layer(
self,
layer: QgsVectorLayer,
conversion_context: ConversionContext,
feedback: Optional[QgsFeedback] = None) -> LayerExportDetails:
"""
Exports a vector layer into a format acceptable for Felt
Expand Down Expand Up @@ -425,7 +430,8 @@ def export_vector_layer(
result=layer_export_result,
error_message=error_message,
qgis_style_xml=self._get_original_style_xml(layer),
style=self.representative_layer_style(layer)
style=self.representative_layer_style(layer,
conversion_context)
)

def run_raster_writer(self,
Expand Down Expand Up @@ -518,14 +524,16 @@ def run_raster_writer(self,
def export_raster_layer(
self,
layer: QgsRasterLayer,
conversion_context: ConversionContext,
feedback: Optional[QgsFeedback] = None,
force_upload_raster_as_styled: bool = False) -> LayerExportDetails:
"""
Exports a raster layer into a format acceptable for Felt
"""
dest_file = self.generate_file_name('.tif')

converted_style = self.representative_layer_style(layer)
converted_style = self.representative_layer_style(layer,
conversion_context)
upload_raster_as_styled = (force_upload_raster_as_styled or
not converted_style.fsl)
layer_export_result, error_message = self.run_raster_writer(
Expand Down
1 change: 1 addition & 0 deletions felt/core/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Logger(QObject):

PACKAGING_RASTER = 'packaging_raster'
PACKAGING_VECTOR = 'packaging_vector'
FSL_CONVERSION = 'fsl_conversion'
MAP_EXPORT = 'map_export'
S3_UPLOAD = 's3_upload'
UNSUPPORTED_LAYER = "unsupported_layer"
Expand Down
12 changes: 12 additions & 0 deletions felt/core/map_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from .map import Map
from .map_utils import MapUtils
from .multi_step_feedback import MultiStepFeedback
from .fsl_converter import ConversionContext
from .s3_upload_parameters import S3UploadParameters


Expand Down Expand Up @@ -285,6 +286,8 @@ def run(self):
message
)

conversion_context = ConversionContext()

if not self.associated_map:
self.status_changed.emit(self.tr('Creating map'))
reply = API_CLIENT.create_map(
Expand Down Expand Up @@ -355,6 +358,7 @@ def run(self):
try:
result = exporter.export_layer_for_felt(
layer,
conversion_context,
multi_step_feedback
)
except LayerPackagingException as e:
Expand All @@ -373,6 +377,14 @@ def run(self):

multi_step_feedback.step_finished()

if conversion_context.warnings:
Logger.instance().log_message_json(
{
'type': Logger.FSL_CONVERSION,
'warnings': conversion_context.warnings
}
)

if self.isCanceled():
return False

Expand Down
4 changes: 3 additions & 1 deletion felt/test/test_fsl_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2322,7 +2322,9 @@ def test_layer_to_fsl_with_labels_no_renderer(self):
layer.setLabelsEnabled(True)
layer.setLabeling(QgsVectorLayerSimpleLabeling(label_settings))

style = LayerExporter.representative_layer_style(layer)
conversion_context = ConversionContext()
style = LayerExporter.representative_layer_style(layer,
conversion_context)

self.assertEqual(
style.fsl,
Expand Down
17 changes: 13 additions & 4 deletions felt/test/test_layer_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
from ..core import (
LayerExporter,
LayerExportResult,
LayerSupport
LayerSupport,
ConversionContext
)

QGIS_APP = get_qgis_app()
Expand Down Expand Up @@ -164,7 +165,8 @@ def test_vector_conversion(self):
exporter = LayerExporter(
QgsCoordinateTransformContext()
)
result = exporter.export_layer_for_felt(layer)
conversion_context = ConversionContext()
result = exporter.export_layer_for_felt(layer, conversion_context)
self.assertEqual(result.result, LayerExportResult.Success)
self.assertEqual(result.filename[-4:], '.zip')
with zipfile.ZipFile(result.filename) as z:
Expand Down Expand Up @@ -200,7 +202,8 @@ def test_gml_conversion(self):
exporter = LayerExporter(
QgsCoordinateTransformContext()
)
result = exporter.export_layer_for_felt(layer)
conversion_context = ConversionContext()
result = exporter.export_layer_for_felt(layer, conversion_context)
self.assertEqual(result.result, LayerExportResult.Success)
self.assertTrue(result.filename)
self.assertEqual(result.filename[-4:], '.zip')
Expand All @@ -226,11 +229,13 @@ def test_raster_conversion_raw(self):
renderer = QgsPalettedRasterRenderer(layer.dataProvider(), 1, [])
layer.setRenderer(renderer)

conversion_context = ConversionContext()
exporter = LayerExporter(
QgsCoordinateTransformContext()
)
result = exporter.export_layer_for_felt(
layer
layer,
conversion_context
)
self.assertEqual(result.result, LayerExportResult.Success)
self.assertTrue(result.filename)
Expand Down Expand Up @@ -275,8 +280,10 @@ def test_raster_conversion_styled(self):
exporter = LayerExporter(
QgsCoordinateTransformContext()
)
conversion_context = ConversionContext()
result = exporter.export_layer_for_felt(
layer,
conversion_context,
force_upload_raster_as_styled=True
)
self.assertEqual(result.result, LayerExportResult.Success)
Expand Down Expand Up @@ -329,11 +336,13 @@ def test_raster_conversion_no_fsl_conversion(self):
renderer = QgsRasterContourRenderer(layer.dataProvider())
layer.setRenderer(renderer)

conversion_context = ConversionContext()
exporter = LayerExporter(
QgsCoordinateTransformContext()
)
result = exporter.export_layer_for_felt(
layer,
conversion_context,
force_upload_raster_as_styled=False
)
self.assertEqual(result.result, LayerExportResult.Success)
Expand Down

0 comments on commit 5c95243

Please sign in to comment.