Skip to content

Commit

Permalink
feat: added option to change plotting dpi
Browse files Browse the repository at this point in the history
  • Loading branch information
RaczeQ committed Oct 23, 2024
1 parent 309f798 commit 3d7e3af
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 35 deletions.
19 changes: 10 additions & 9 deletions pixel_map/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,7 @@
app = typer.Typer(context_settings={"help_option_names": ["-h", "--help"]}, rich_markup_mode="rich")

# TODO:
# - add option to select colours (pass list - must match number of files)
# - add option to select colours per type (polygon, linestring, point)
# (pass list(s) - must match number of files)
# - define default colour schemes with option to select
# --light (positron + blue)
# or --dark (darkmatter + orange?) [default]
# or --street (voyager + ???)
# - add option to select tileset (or no tileset) by name
# - add option to pass map height and width
# - add option to remove the panel (border)


def _version_callback(value: bool) -> None:
Expand Down Expand Up @@ -218,6 +209,15 @@ def plot(
show_default=False,
),
] = None,
plotting_dpi: Annotated[
int,
typer.Option(
"--dpi",
metavar="INT",
help=("DPI used to get better quality matplotlib plot before rendering to terminal."),
show_default=True,
),
] = 10,
example_files: Annotated[
bool,
typer.Option(
Expand Down Expand Up @@ -316,6 +316,7 @@ def plot(
no_border=no_border,
console_width=console_width,
console_height=console_height,
plotting_dpi=plotting_dpi,
)


Expand Down
49 changes: 23 additions & 26 deletions pixel_map/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from rich import get_console
from rich.box import HEAVY
from rich.color import Color
from rich.console import Console
from rich.panel import Panel
from rich.progress import Progress, SpinnerColumn, TextColumn
from rich.style import Style
Expand All @@ -41,6 +42,7 @@ def plot_geo_data(
no_border: bool = False,
console_width: Optional[int] = None,
console_height: Optional[int] = None,
plotting_dpi: int = 10,
) -> None:
"""
Plot the geo data into a terminal.
Expand All @@ -67,6 +69,8 @@ def plot_geo_data(
Defaults to None.
console_height (int, optional): Console height. Can be used to set arbitrary value.
Defaults to None.
plotting_dpi (int, optional): Quality of matplotlib figure. It's used to multiply terminal
size by some value to get better quality plot. Defaults to 10.
"""
console = get_console()

Expand All @@ -88,36 +92,26 @@ def plot_geo_data(

map_ratio = map_width / map_height

with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True,
console=console,
) as progress:
with _get_progress_object(console) as progress:
progress.add_task("Calculating bounding box", total=None)
bbox_axes_bounds = None
if bbox:
bbox, bbox_axes_bounds = _expand_bbox_to_match_ratio(bbox, ratio=map_ratio)

with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True,
console=console,
) as progress:
with _get_progress_object(console) as progress:
progress.add_task("Loading Geo data", total=None)
gdfs = _load_geo_data(files, bbox=bbox)
if bbox:
gdfs = [gdf.clip_by_rect(*bbox) for gdf in gdfs]

with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True,
console=console,
) as progress:
with _get_progress_object(console) as progress:
progress.add_task("Plotting geo data", total=None)
f, ax = plt.subplots(figsize=(map_width, map_height), dpi=10)
f, ax = plt.subplots(figsize=(map_width, map_height), dpi=plotting_dpi)

ax.set_axis_off()
ax.set_xticks([])
ax.set_yticks([])

f.patch.set_facecolor(background_color)
canvas = f.canvas
# gdf.to_crs(3857).plot(ax=ax, alpha=0.4)
Expand All @@ -129,7 +123,6 @@ def plot_geo_data(
plot_color = color[idx % len(color)]
plot_alpha = alpha[idx % len(alpha)]
gdf.to_crs(3857).plot(ax=ax, color=plot_color, alpha=plot_alpha)
ax.axis("off")

if bbox_axes_bounds:
left, bottom, right, top = bbox_axes_bounds
Expand Down Expand Up @@ -161,12 +154,7 @@ def plot_geo_data(
image_flat = np.frombuffer(canvas.tostring_rgb(), dtype="uint8") # (H * W * 3,)
image = image_flat.reshape(*reversed(canvas.get_width_height()), 3)

with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True,
console=console,
) as progress:
with _get_progress_object(console) as progress:
progress.add_task("Rendering geo data", total=None)
renderer_object = AVAILABLE_RENDERERS[renderer](
terminal_width=terminal_width, terminal_height=terminal_height
Expand Down Expand Up @@ -354,3 +342,12 @@ def _generate_panel_subtitle(
return bbox_str
else:
return ""


def _get_progress_object(console: Console) -> Progress:
return Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True,
console=console,
)
3 changes: 3 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ def test_basic_run() -> None:
@P.case("Console width", ["--width", "10"]) # type: ignore
@P.case("Console height", ["--height", "10"]) # type: ignore
@P.case("Console width and height", ["--width", "10", "--height", "10"]) # type: ignore
@P.case("Plotting dpi small", ["--dpi", "1"]) # type: ignore
@P.case("Plotting dpi big", ["--dpi", "100"]) # type: ignore
def test_proper_args(args: list[str]) -> None:
"""Test if runs properly with options."""
result = runner.invoke(app, ["--example", "monaco_buildings", *args])
Expand Down Expand Up @@ -104,6 +106,7 @@ def test_renderers(renderer: str) -> None:
@P.case("Wrong opacity", ["-a", random_str()]) # type: ignore
@P.case("Wrong background color", ["--bg-color", random_str()]) # type: ignore
@P.case("Wrong number of colors", ["london_buildings", "-c", "C0,C1,C2"]) # type: ignore
@P.case("Wrong plotting dpi", ["--dpi", random_str()]) # type: ignore
def test_wrong_args(args: list[str], capsys: pytest.CaptureFixture) -> None:
"""Test if doesn't run properly with options."""
# Fix for the I/O error from the Click repository
Expand Down

0 comments on commit 3d7e3af

Please sign in to comment.