Skip to content

Commit

Permalink
display: preview roses link to full rose
Browse files Browse the repository at this point in the history
  • Loading branch information
dhimmel committed Nov 20, 2024
1 parent 4f903f1 commit f39f96c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ List of related webpages:
- Display table webpage background of falling snowflakes ([examples](https://freefrontend.com/css-snow-effects/))
- Max slope v difficulty by region
- Cleanup unused dependencies
- fix matplotlib super title spacing
- How many ski areas in the world, comparing to the Vanat report
- Total combined vert of ski areas by rank of ski area (how much do big resorts drive the aggregated metrics)

Expand Down
69 changes: 56 additions & 13 deletions openskistats/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
load_downhill_ski_areas_from_download_pl,
load_runs_from_download_pl,
)
from openskistats.plot import plot_orientation, subplot_orientations
from openskistats.plot import (
_generate_margin_text,
plot_orientation,
subplot_orientations,
)
from openskistats.utils import get_data_directory


Expand Down Expand Up @@ -361,23 +365,34 @@ def create_ski_area_roses(overwrite: bool = False) -> None:
"""
Export ski area roses to SVG for display.
"""
directory = get_data_directory().joinpath("ski_areas")
directory.mkdir(exist_ok=True)
partitions = (
load_bearing_distribution_pl(ski_area_filters=get_display_ski_area_filters())
.filter(pl.col("num_bins") == 8)
.partition_by("ski_area_id", as_dict=True)
directory = get_data_directory().joinpath("ski-areas")
directory_preview = directory.joinpath("roses-preview")
directory_full = directory.joinpath("roses-full")
for _directory in directory, directory_preview, directory_full:
_directory.mkdir(exist_ok=True)
ski_areas_pl = load_ski_areas_pl(
ski_area_filters=get_display_ski_area_filters()
).drop("bearings")
bearings_pl = load_bearing_distribution_pl(
ski_area_filters=get_display_ski_area_filters()
)
logging.info(
f"Creating ski area roses for {len(partitions)} ski areas in {directory}."
f"Creating ski area preview roses for {len(ski_areas_pl):,} ski areas in {directory} with {overwrite=}."
)
for (ski_area_id,), bearing_pl in partitions.items():
path = directory.joinpath(f"{ski_area_id}.svg")
for info in ski_areas_pl.iter_rows(named=True):
ski_area_id = info["ski_area_id"]
ski_area_name = info["ski_area_name"]
bearing_pl = bearings_pl.filter(pl.col("ski_area_id") == ski_area_id)

# plot and save preview rose
path = directory_preview.joinpath(f"{ski_area_id}.svg")
if not overwrite and path.exists():
continue

bearing_preview_pl = bearing_pl.filter(pl.col("num_bins") == 8)
fig, ax = plot_orientation(
bin_counts=bearing_pl.get_column("bin_count").to_numpy(),
bin_centers=bearing_pl.get_column("bin_center").to_numpy(),
bin_counts=bearing_preview_pl.get_column("bin_count").to_numpy(),
bin_centers=bearing_preview_pl.get_column("bin_center").to_numpy(),
margin_text={},
figsize=(1, 1),
alpha=1.0,
Expand All @@ -396,7 +411,35 @@ def create_ski_area_roses(overwrite: bool = False) -> None:
pad_inches=0.02,
transparent=True,
metadata={
"Title": "Ski Roses of the World: Downhill Ski Trail Orientations",
"Title": f"Preview Ski Rose for {ski_area_name}",
"Description": f"An 8-bin histogram of downhill ski trail orientations generated from <https://openskimap.org/?obj={ski_area_id}>.",
"Creator": "https://github.com/dhimmel/openskistats",
},
)
matplotlib.pyplot.close(fig)

# plot and save full rose
path = directory_full.joinpath(f"{ski_area_id}.svg")
bearing_full_pl = bearing_pl.filter(pl.col("num_bins") == 32)
fig, ax = plot_orientation(
bin_counts=bearing_full_pl.get_column("bin_count").to_numpy(),
bin_centers=bearing_full_pl.get_column("bin_center").to_numpy(),
title=ski_area_name,
margin_text=_generate_margin_text(info),
figsize=(4, 4),
alpha=1.0,
)
logging.info(f"Writing {path}")
fig.savefig(
path,
format="svg",
bbox_inches="tight",
# pad_inches=0.02,
facecolor="#FFFFFF",
transparent=False,
metadata={
"Title": f"Ski Rose for {ski_area_name}",
"Description": f"A 32-bin histogram of downhill ski trail orientations generated from <https://openskimap.org/?obj={ski_area_id}>.",
"Creator": "https://github.com/dhimmel/openskistats",
},
)
Expand Down
15 changes: 14 additions & 1 deletion openskistats/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ def get_ski_area_frontend_table() -> pl.DataFrame:
"poleward_affinity",
"eastward_affinity",
pl.selectors.starts_with("bin_proportion_"),
pl.format("""<img src="ski_areas/{}.svg">""", "ski_area_id").alias("rose"),
pl.col("ski_area_id").alias("rose"),
# pl.format("""<img src="ski-areas/roses-preview/{}.svg">""", "ski_area_id").alias("rose"),
)
.sort("ski_area_name")
)
Expand Down Expand Up @@ -274,6 +275,17 @@ def _format_header(ci: reactable.HeaderCellInfo) -> htmltools.Tag | str:
return column_name


def _rose_cell(ci: reactable.CellInfo) -> htmltools.Tag:
return htmltools.a(
htmltools.img(
src=f"ski-areas/roses-preview/{ci.value}.svg", alt="Preview Rose"
),
href=f"ski-areas/roses-full/{ci.value}.svg",
target="blank_",
class_="hover-preview",
)


def get_ski_area_reactable() -> reactable.Reactable:
data_pl = get_ski_area_frontend_table()

Expand Down Expand Up @@ -464,6 +476,7 @@ def _country_cell(ci: reactable.CellInfo) -> str:
name="Rose",
html=True,
sortable=False,
cell=_rose_cell,
# max_width=45,
),
],
Expand Down

0 comments on commit f39f96c

Please sign in to comment.