Skip to content

Commit

Permalink
Refactor to allow multiple files to be included in zip per exported
Browse files Browse the repository at this point in the history
layer
  • Loading branch information
nyalldawson committed Nov 21, 2023
1 parent 7b853ab commit 01abd85
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
2 changes: 1 addition & 1 deletion felt/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from .map_uploader import MapUploaderTask # noqa
from .layer_exporter import ( # noqa
LayerExporter,
ExportResult
ZippedExportResult
)
from .multi_step_feedback import MultiStepFeedback # noqa
from .meta import PLUGIN_METADATA_PARSER # noqa
Expand Down
50 changes: 37 additions & 13 deletions felt/core/layer_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
import zipfile
from dataclasses import dataclass
from pathlib import Path
from typing import Optional
from typing import (
Optional,
List
)

from qgis.PyQt.QtCore import (
QVariant,
Expand Down Expand Up @@ -59,10 +62,23 @@


@dataclass
class ExportResult:
class LayerExportDetails:
"""
Export results
"""
representative_filename: str
filenames: List[str]
result: LayerExportResult
error_message: str
qgis_style_xml: str
style: Optional[LayerStyle] = None


@dataclass
class ZippedExportResult:
"""
A zipped export results
"""
filename: str
result: LayerExportResult
error_message: str
Expand Down Expand Up @@ -174,7 +190,7 @@ def export_layer_for_felt(
self,
layer: QgsMapLayer,
feedback: Optional[QgsFeedback] = None
) -> ExportResult:
) -> ZippedExportResult:
"""
Exports a layer into a format acceptable for Felt
:raises LayerPackagingException
Expand All @@ -189,15 +205,21 @@ def export_layer_for_felt(
# package into zip
zip_file_path = (
(Path(str(self.temp_dir.name)) /
(Path(res.filename).stem + '.zip')).as_posix())
(Path(res.representative_filename).stem + '.zip')).as_posix())
with zipfile.ZipFile(zip_file_path, "w", zipfile.ZIP_DEFLATED) as zipf:
zipf.write(res.filename, Path(res.filename).name)
for filename in res.filenames:
zipf.write(filename, Path(filename).name)

# add QGIS layer style xml also
zipf.writestr("qgis_style.xml", res.qgis_style_xml)

res.filename = zip_file_path
return res
return ZippedExportResult(
filename=zip_file_path,
result=res.result,
error_message=res.error_message,
qgis_style_xml=res.qgis_style_xml,
style=res.style
)

@staticmethod
def _get_original_style_xml(layer: QgsMapLayer) -> str:
Expand All @@ -212,7 +234,7 @@ def _get_original_style_xml(layer: QgsMapLayer) -> str:
def export_vector_layer(
self,
layer: QgsVectorLayer,
feedback: Optional[QgsFeedback] = None) -> ExportResult:
feedback: Optional[QgsFeedback] = None) -> LayerExportDetails:
"""
Exports a vector layer into a format acceptable for Felt
"""
Expand Down Expand Up @@ -305,8 +327,9 @@ def export_vector_layer(
res_layer.featureCount(), layer.featureCount())
)

return ExportResult(
filename=dest_file,
return LayerExportDetails(
representative_filename=dest_file,
filenames=[dest_file],
result=layer_export_result,
error_message=error_message,
qgis_style_xml=self._get_original_style_xml(layer),
Expand All @@ -316,7 +339,7 @@ def export_vector_layer(
def export_raster_layer(
self,
layer: QgsRasterLayer,
feedback: Optional[QgsFeedback] = None) -> ExportResult:
feedback: Optional[QgsFeedback] = None) -> LayerExportDetails:
"""
Exports a raster layer into a format acceptable for Felt
"""
Expand Down Expand Up @@ -404,8 +427,9 @@ def export_raster_layer(
LayerExportResult.Canceled,
}[res]

return ExportResult(
filename=dest_file,
return LayerExportDetails(
representative_filename=dest_file,
filenames=[dest_file],
result=layer_export_result,
error_message=error_message,
qgis_style_xml=self._get_original_style_xml(layer)
Expand Down

0 comments on commit 01abd85

Please sign in to comment.