Skip to content

Commit

Permalink
feat: make aruco markers a feature flag
Browse files Browse the repository at this point in the history
- restore previous globe makers as default.
- aruco markers are only used if option flag is set
- add aruco markers flag to parameters of parametrized approval tests
  • Loading branch information
matthiasschaub committed Nov 12, 2024
1 parent 0ec25c9 commit fdd4059
Show file tree
Hide file tree
Showing 116 changed files with 63 additions and 2 deletions.
57 changes: 56 additions & 1 deletion sketch_map_tool/map_generation/generate_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import cv2
import fitz
from PIL import Image
from reportlab.graphics import renderPDF
from reportlab.graphics.shapes import Drawing
from reportlab.lib.pagesizes import landscape
from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
Expand All @@ -32,6 +33,7 @@ def generate_pdf(
format_: PaperFormat,
scale: float,
layer: Layer,
aruco_markers: bool = False,
) -> Tuple[BytesIO, BytesIO]:
"""
Generate a sketch map pdf, i.e. a PDF containing the given map image
Expand Down Expand Up @@ -89,6 +91,7 @@ def generate_pdf(
portrait,
m_per_px,
img_format,
aruco_markers,
)

map_pdf = BytesIO()
Expand Down Expand Up @@ -250,6 +253,7 @@ def create_map_frame(
portrait: bool,
m_per_px: float,
img_format: str,
aruco_markers: bool = False,
) -> BytesIO:
map_frame = BytesIO()
canvas = Canvas(map_frame)
Expand Down Expand Up @@ -280,14 +284,65 @@ def create_map_frame(
width=width,
height=height,
)
draw_markers(canvas, globe_size, height, width)
if aruco_markers:
draw_markers(canvas, globe_size, height, width)
else:
draw_globes(canvas, globe_size, height, width)
add_scalebar(canvas, width, height, m_per_px, format_)

canvas.save()
map_frame.seek(0)
return pdf_page_to_img(map_frame, img_format=img_format)


def draw_globes(canvas: Canvas, size: float, height: float, width: float):
globe_1, globe_2, globe_3, globe_4 = get_globes(size)

h = height - size
w = width - size

globes = [
# corner
globe_1,
globe_3,
globe_4,
globe_2,
# middle
globe_2,
globe_1,
globe_3,
globe_4,
]
positions = [
# corner globes
# bottom left
(0, 0),
# top left
(0, h),
# top right
(w, h),
# bottom right
(w, 0),
# middle globes
(0, h / 2),
(w / 2, h),
(w, h / 2),
(w / 2, 0),
]
for globe, (x, y) in zip(globes, positions):
renderPDF.draw(globe, canvas, x, y)


def get_globes(expected_size) -> Tuple[Drawing, ...]:
"""Read globe as SVG from disk, convert to RLG and scale it."""
globes = []
for i in range(1, 5):
globe = svg2rlg(PDF_RESOURCES_PATH / "globe_{0}.svg".format(i))
globe = resize_rlg_by_width(globe, expected_size)
globes.append(globe)
return tuple(globes)


def draw_markers(canvas: Canvas, size: float, height: float, width: float):
markers = get_aruco_markers(int(size))

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
8 changes: 7 additions & 1 deletion tests/unit/test_map_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,22 @@ def qr_code(uuid, bbox, layer, format_):

@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_sketch_map(
map_image,
qr_code,
paper_format: PaperFormat,
orientation, # pyright: ignore reportUnusedVariable
layer,
aruco_markers,
) -> None:
sketch_map, _ = generate_pdf(
map_image,
qr_code,
paper_format,
1283.129,
layer,
aruco_markers,
)
assert isinstance(sketch_map, BytesIO)
# NOTE: The resulting PDFs across multiple test runs have slight non-visual
Expand All @@ -88,19 +91,22 @@ def test_generate_pdf_sketch_map(

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

0 comments on commit fdd4059

Please sign in to comment.