Skip to content

Commit

Permalink
feat(ui/flask): enable aruco markers via query param
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasschaub committed Dec 14, 2024
1 parent b999910 commit c4679e6
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 28 deletions.
10 changes: 5 additions & 5 deletions sketch_map_tool/map_generation/generate_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def generate_pdf(
format_: PaperFormat,
scale: float,
layer: Layer,
aruco_markers: bool = False,
aruco: bool = False,
) -> Tuple[BytesIO, BytesIO]:
"""
Generate a sketch map pdf, i.e. a PDF containing the given map image
Expand Down Expand Up @@ -89,7 +89,7 @@ def generate_pdf(
portrait,
m_per_px,
img_format,
aruco_markers,
aruco,
)

map_pdf = BytesIO()
Expand Down Expand Up @@ -251,7 +251,7 @@ def create_map_frame(
portrait: bool,
m_per_px: float,
img_format: str,
aruco_markers: bool = False,
aruco: bool = False,
) -> BytesIO:
map_frame = BytesIO()
canvas = Canvas(map_frame)
Expand All @@ -269,7 +269,7 @@ def create_map_frame(
height=height,
)
canvas.rotate(-90)
if aruco_markers:
if aruco:
draw_markers(canvas, globe_size, height=width, width=height)
else:
draw_globes(canvas, globe_size, height=width, width=height)
Expand All @@ -285,7 +285,7 @@ def create_map_frame(
width=width,
height=height,
)
if aruco_markers:
if aruco:
draw_markers(canvas, globe_size, height, width)
else:
draw_globes(canvas, globe_size, height, width)
Expand Down
15 changes: 14 additions & 1 deletion sketch_map_tool/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,16 @@ def case_study_timor_leste_pdf(lang="en") -> Response: # pyright: ignore
@app.get("/<lang>/create")
def create(lang="en") -> str:
"""Serve forms for creating a sketch map"""
# feature flag for enabling aruco markers
if request.args.get("aruco") is None:
aruco = False
else:
aruco = True

return render_template(
"create.html",
lang=lang,
aruco=aruco,
esri_api_key=config.get_config_value("esri-api-key"),
)

Expand All @@ -118,12 +125,18 @@ def create_results_post(lang="en") -> Response:
scale = float(request.form["scale"])
layer = Layer(request.form["layer"].replace(":", "-").replace("_", "-").lower())

# feature flag for enabling aruco markers
if request.args.get("aruco") is None:
aruco = False
else:
aruco = True

# Unique id for current request
uuid = str(uuid4())

# Tasks
task_sketch_map = tasks.generate_sketch_map.apply_async(
args=(uuid, bbox, format_, orientation, size, scale, layer)
args=(uuid, bbox, format_, orientation, size, scale, layer, aruco)
)
task_quality_report = tasks.generate_quality_report.apply_async(
args=tuple([bbox_wgs84])
Expand Down
2 changes: 2 additions & 0 deletions sketch_map_tool/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def generate_sketch_map(
size: Size,
scale: float,
layer: Layer,
aruco: bool,
) -> BytesIO | AsyncResult:
"""Generate and returns a sketch map as PDF and stores the map frame in DB."""
map_image = wms_client.get_map_image(bbox, size, layer)
Expand All @@ -84,6 +85,7 @@ def generate_sketch_map(
format_,
scale,
layer,
aruco,
)
db_client_celery.insert_map_frame(map_img, uuid, bbox, format_, orientation, layer)
return map_pdf
Expand Down
4 changes: 4 additions & 0 deletions sketch_map_tool/templates/create.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@
</article>
</div>

{% if aruco == True: %}
<form id="page-setup-form" action="/{{ lang }}/create/results?aruco=true" method="post">
{% else %}
<form id="page-setup-form" action="/{{ lang }}/create/results" method="post">
{% endif %}
<label for="format">{{ _('Paper Format') }}</label>
<select id="format" name="format"></select>

Expand Down
5 changes: 5 additions & 0 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ def layer(request):
return Layer(request.param)


@pytest.fixture(params=[True, False])
def aruco(request):
return request.param


@pytest.fixture
def uuid():
return "654dd0d3-7bb0-4a05-8a68-517f0d9fc98e"
Expand Down
20 changes: 6 additions & 14 deletions tests/unit/test_map_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,43 +54,41 @@ def qr_code_approval(uuid, bbox):

@pytest.mark.parametrize("paper_format", [A0, A1, A2, A3, A4, LETTER, TABLOID])
@pytest.mark.parametrize("orientation", ["landscape", "portrait"])
@pytest.mark.parametrize("aruco_markers", [True, False])
def test_generate_pdf(
map_image,
qr_code,
paper_format: PaperFormat,
orientation, # pyright: ignore reportUnusedVariable
layer,
aruco_markers,
aruco,
) -> None:
sketch_map, sketch_map_template = generate_pdf(
map_image,
qr_code,
paper_format,
1283.129,
layer,
aruco_markers,
aruco,
)
assert isinstance(sketch_map, BytesIO)
assert isinstance(sketch_map_template, BytesIO)


# NOTE: To reduce number of approvals, parameter numbers are kept low.
@pytest.mark.parametrize("orientation", ["landscape"])
@pytest.mark.parametrize("aruco_markers", [True, False])
def test_generate_pdf_sketch_map_approval(
map_image,
qr_code_approval,
orientation, # pyright: ignore reportUnusedVariable
aruco_markers,
aruco,
) -> None:
sketch_map, _ = generate_pdf(
map_image,
qr_code_approval,
A4,
1283.129,
Layer("osm"),
aruco_markers,
aruco,
)
# NOTE: The resulting PDFs across multiple test runs have slight non-visual
# differences leading to a failure when using `verify_binary` on the PDFs.
Expand All @@ -117,21 +115,15 @@ def test_generate_pdf_sketch_map_approval(
# NOTE: To reduce number of approvals, parameter numbers are kept low.
@pytest.mark.parametrize("paper_format", [A4])
@pytest.mark.parametrize("orientation", ["landscape"])
@pytest.mark.parametrize("aruco_markers", [True, False])
def test_generate_pdf_sketch_map_template_approval(
map_image,
qr_code_approval,
paper_format: PaperFormat,
orientation, # pyright: ignore reportUnusedVariable
aruco_markers,
aruco,
) -> None:
_, sketch_map_template = generate_pdf(
map_image,
qr_code_approval,
paper_format,
1283.129,
Layer("osm"),
aruco_markers,
map_image, qr_code_approval, paper_format, 1283.129, Layer("osm"), aruco
)
# fmt: off
options = (
Expand Down
19 changes: 11 additions & 8 deletions tests/unit/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,22 @@ def mock_get_task_id(monkeypatch, uuid):


@vcr.use_cassette
def test_generate_sketch_map(monkeypatch, uuid, bbox, format_, size, scale, layer):
def test_generate_sketch_map(
monkeypatch,
uuid,
bbox,
format_,
size,
scale,
layer,
aruco,
):
monkeypatch.setattr(
"sketch_map_tool.tasks.db_client_celery.insert_map_frame",
lambda *_: uuid,
)
map_pdf = tasks.generate_sketch_map(
uuid,
bbox,
format_,
"landscape",
size,
scale,
layer,
uuid, bbox, format_, "landscape", size, scale, layer, aruco
)
assert isinstance(map_pdf, BytesIO)

Expand Down

0 comments on commit c4679e6

Please sign in to comment.