Add POIs and journey times to map

This commit is contained in:
Andras Schmelczer 2026-01-28 22:10:41 +00:00
parent 7bfb1729bf
commit 500b9ef2aa
11 changed files with 914 additions and 177 deletions

View file

@ -77,14 +77,28 @@ def query_hexagons_cached(
# Filter by year range
df = df.filter((pl.col("year") >= min_year) & (pl.col("year") <= max_year))
# Check which journey time columns exist
journey_cols = [
"median_journey_minutes",
"median_pt_easy_minutes",
"median_pt_quick_minutes",
"median_cycling_minutes",
]
available_journey_cols = [c for c in journey_cols if c in df.columns]
# Aggregate across years (weighted by count)
df = df.group_by("h3").agg(
agg_exprs = [
pl.col("count").sum().alias("count"),
(pl.col("avg_price") * pl.col("count")).sum().alias("weighted_price_sum"),
pl.col("median_price").median().alias("median_price"),
pl.col("min_price").min().alias("min_price"),
pl.col("max_price").max().alias("max_price"),
)
]
for jc in available_journey_cols:
# Journey time is same across years, just take first non-null
agg_exprs.append(pl.col(jc).first())
df = df.group_by("h3").agg(agg_exprs)
# Calculate weighted average price
df = df.with_columns(
@ -97,16 +111,18 @@ def query_hexagons_cached(
)
# Build response efficiently using Polars
df = df.select(
[
pl.col("h3"),
pl.col("count"),
pl.col("avg_price").round(2),
pl.col("median_price").round(2),
pl.col("min_price"),
pl.col("max_price"),
]
)
select_cols = [
pl.col("h3"),
pl.col("count"),
pl.col("avg_price").round(2),
pl.col("median_price").round(2),
pl.col("min_price"),
pl.col("max_price"),
]
for jc in available_journey_cols:
select_cols.append(pl.col(jc).round(0))
df = df.select(select_cols)
return df.to_dicts()