Skip to content

Commit

Permalink
feat(raster-result): add attributions file to zip
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasschaub authored Mar 5, 2024
2 parents 9346b35 + 12cc882 commit 502dea4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
3 changes: 2 additions & 1 deletion sketch_map_tool/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,13 @@ def digitize_results_post(lang="en") -> Response:
]
uuids = [args_["uuid"] for args_ in args]
bboxes = [args_["bbox"] for args_ in args]
layer = [args_["layer"] for args_ in args]
map_frames = dict()
for uuid in set(uuids): # Only retrieve map_frame once per uuid to save memory
map_frame_buffer = BytesIO(db_client_flask.select_map_frame(UUID(uuid)))
map_frames[uuid] = to_array(map_frame_buffer.read())
result_id_1 = (
georeference_sketch_maps.s(ids, file_names, uuids, map_frames, bboxes)
georeference_sketch_maps.s(ids, file_names, uuids, map_frames, bboxes, layer)
.apply_async()
.id
)
Expand Down
21 changes: 19 additions & 2 deletions sketch_map_tool/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from sketch_map_tool import celery_app as celery
from sketch_map_tool import get_config_value, map_generation
from sketch_map_tool.database import client_celery as db_client_celery
from sketch_map_tool.definitions import get_attribution
from sketch_map_tool.helpers import to_array
from sketch_map_tool.models import Bbox, Layer, PaperFormat, Size
from sketch_map_tool.oqt_analyses import generate_pdf as generate_report_pdf
Expand Down Expand Up @@ -92,16 +93,29 @@ def georeference_sketch_maps(
uuids: list[str],
map_frames: dict[str, NDArray],
bboxes: list[Bbox],
layers: list[Layer],
) -> AsyncResult | BytesIO:
def process(sketch_map_id: int, uuid: str, bbox: Bbox) -> BytesIO:
"""Process a Sketch Map."""
def process(
sketch_map_id: int,
uuid: str,
bbox: Bbox,
) -> BytesIO:
"""Process a Sketch Map and its attribution."""
# r = interim result
r = db_client_celery.select_file(sketch_map_id)
r = to_array(r)
r = clip(r, map_frames[uuid])
r = georeference(r, bbox)
return r

def get_attribution_file(layers: list[Layer]) -> BytesIO:
attributions = []
for index, layer in enumerate(layers):
attribution = get_attribution(layer)
attribution = attribution.replace("<br />", "\n")
attributions.append(attribution)
return BytesIO("\n".join(attributions).encode())

def zip_(file: BytesIO, file_name: str):
with ZipFile(buffer, "a") as zip_file:
name = ".".join(file_name.split(".")[:-1])
Expand All @@ -110,6 +124,9 @@ def zip_(file: BytesIO, file_name: str):
buffer = BytesIO()
for file_id, uuid, bbox, file_name in zip(file_ids, uuids, bboxes, file_names):
zip_(process(file_id, uuid, bbox), file_name)
with ZipFile(buffer, "a") as zip_file:
zip_file.writestr("attributions.txt", get_attribution_file(layers).read())

buffer.seek(0)
return buffer

Expand Down
4 changes: 2 additions & 2 deletions sketch_map_tool/upload_processing/qr_code_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ def _decode_data(data) -> MappingProxyType:
*[float(coordinate) for coordinate in contents[2:-1]]
) # Raises ValueError for non-float values
try:
layer = getattr(Layer, (contents[7]))
layer = Layer(contents[6])
except IndexError:
# backward compatibility
layer = getattr(Layer, "OSM")
layer = Layer("osm")
except ValueError as error:
raise QRCodeError(N_("QR-Code does not have expected content.")) from error
return MappingProxyType(
Expand Down

0 comments on commit 502dea4

Please sign in to comment.