Skip to content

Commit

Permalink
Report data provider for unsupported layers
Browse files Browse the repository at this point in the history
And ensure we don't include empty layer details in usage reports
  • Loading branch information
nyalldawson committed Nov 21, 2023
1 parent 838ff4c commit 6b68ed0
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 21 deletions.
3 changes: 2 additions & 1 deletion felt/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
AuthState,
ObjectType,
LayerExportResult,
UsageType
UsageType,
LayerSupport
)
from .auth import OAuthWorkflow # noqa
from .map import Map # noqa
Expand Down
20 changes: 20 additions & 0 deletions felt/core/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ class LayerExportResult(Enum):
Canceled = auto()


class LayerSupport(Enum):
"""
Reasons why a layer is not supported
"""
Supported = auto()
NotImplementedProvider = auto()
NotImplementedLayerType = auto()
EmptyLayer = auto()

def should_report(self) -> bool:
"""
Returns True if the layer support should be reported to Felt
usage API
"""
return self not in (
LayerSupport.Supported,
LayerSupport.EmptyLayer
)


class UsageType(Enum):
"""
Usage types for reporting plugin usage
Expand Down
27 changes: 18 additions & 9 deletions felt/core/layer_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@
QgsRasterRange
)

from .enums import LayerExportResult
from .enums import (
LayerExportResult,
LayerSupport
)
from .exceptions import LayerPackagingException
from .layer_style import LayerStyle
from .logger import Logger
Expand Down Expand Up @@ -108,31 +111,37 @@ def __del__(self):
self.temp_dir.cleanup()

@staticmethod
def can_export_layer(layer: QgsMapLayer) -> Tuple[bool, str]:
def can_export_layer(layer: QgsMapLayer) \
-> Tuple[LayerSupport, str]:
"""
Returns True if a layer can be exported, and an explanatory
string if not
"""
if isinstance(layer, QgsVectorLayer):
# Vector layers must have some features
if layer.featureCount() == 0:
return False, 'Layer is empty'
return LayerSupport.EmptyLayer, 'Layer is empty'

return True, ''
return LayerSupport.Supported, ''

if isinstance(layer, QgsRasterLayer):
if layer.providerType() in (
'gdal',
'virtualraster'
):
return True, ''
return LayerSupport.Supported, ''

return False, '{} raster layers are not yet supported'.format(
layer.providerType()
return (
LayerSupport.NotImplementedProvider,
'{} raster layers are not yet supported'.format(
layer.providerType())
)

return False, '{} layers are not yet supported'.format(
layer.__class__.__name__
return (
LayerSupport.NotImplementedLayerType,
'{} layers are not yet supported'.format(
layer.__class__.__name__
)
)

@staticmethod
Expand Down
13 changes: 9 additions & 4 deletions felt/core/map_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
from .map_utils import MapUtils
from .multi_step_feedback import MultiStepFeedback
from .s3_upload_parameters import S3UploadParameters
from .enums import LayerSupport


class MapUploaderTask(QgsTask):
Expand Down Expand Up @@ -102,7 +103,8 @@ def __init__(self,

self.layers = [
layer.clone() for layer in visible_layers if
LayerExporter.can_export_layer(layer)[0]
LayerExporter.can_export_layer(layer)[
0] == LayerSupport.Supported
]

self._build_unsupported_layer_details(project, visible_layers)
Expand Down Expand Up @@ -152,16 +154,19 @@ def _build_unsupported_layer_details(self,
unsupported_layer_type_count = defaultdict(int)
unsupported_layer_names = set()
for layer in layers:
can_export, reason = LayerExporter.can_export_layer(layer)
if can_export:
support, reason = LayerExporter.can_export_layer(layer)
if not support.should_report():
continue

unsupported_layer_names.add(layer.name())
self.unsupported_layers.append((layer.name(), reason))
if layer.type() == QgsMapLayer.PluginLayer:
id_string = layer.pluginLayerType()
else:
id_string = str(layer.__class__.__name__)
id_string = '{}:{}'.format(
layer.__class__.__name__,
layer.providerType()
)

unsupported_layer_type_count[id_string] = (
unsupported_layer_type_count[id_string] + 1)
Expand Down
6 changes: 4 additions & 2 deletions felt/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@

from .core import (
AuthState,
LayerExporter
LayerExporter,
LayerSupport
)

from .gui import (
Expand Down Expand Up @@ -241,7 +242,8 @@ def _layer_tree_view_context_menu_about_to_show(self, menu: QMenu):
if layer is None:
return

if LayerExporter.can_export_layer(layer)[0]:
if (LayerExporter.can_export_layer(layer)[0] ==
LayerSupport.Supported):
menus = [action for action in menu.children() if
isinstance(action, QMenu) and
action.objectName() == 'exportMenu']
Expand Down
13 changes: 8 additions & 5 deletions felt/test/test_layer_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
from .utilities import get_qgis_app
from ..core import (
LayerExporter,
LayerExportResult
LayerExportResult,
LayerSupport
)

QGIS_APP = get_qgis_app()
Expand All @@ -48,20 +49,22 @@ def test_can_export_layer(self):
file = str(TEST_DATA_PATH / 'points.gpkg')
layer = QgsVectorLayer(file, 'test')
self.assertTrue(layer.isValid())
self.assertTrue(LayerExporter.can_export_layer(layer)[0])
self.assertEqual(
LayerExporter.can_export_layer(layer)[0], LayerSupport.Supported)

file = str(TEST_DATA_PATH / 'dem.tif')
layer = QgsRasterLayer(file, 'test')
self.assertTrue(layer.isValid())
self.assertTrue(LayerExporter.can_export_layer(layer)[0])
self.assertEqual(
LayerExporter.can_export_layer(layer)[0], LayerSupport.Supported)

layer = QgsRasterLayer(
'crs=EPSG:3857&format&type=xyz&url='
'https://tile.openstreetmap.org/%7Bz%7D/%7Bx%7D/%7By%7D.png'
'&zmax=19&zmin=0',
'test', 'wms')
can_export, reason = LayerExporter.can_export_layer(layer)
self.assertFalse(can_export)
support, reason = LayerExporter.can_export_layer(layer)
self.assertEqual(support, LayerSupport.NotImplementedProvider)
self.assertEqual(reason, 'wms raster layers are not yet supported')

def test_file_name(self):
Expand Down

0 comments on commit 6b68ed0

Please sign in to comment.