Compare commits

..

24 Commits

Author SHA1 Message Date
Deeman
a47dfd5535 merge: dual-ring map markers with 5-tier color scale
All checks were successful
CI / test (push) Successful in 54s
CI / tag (push) Successful in 3s
2026-03-08 22:16:54 +01:00
Deeman
116a4272f1 feat(maps): dual-ring markers with 5-tier color scale
Replace flat circle markers with nested dual-ring structure encoding
two scores per marker (core = primary, ring = secondary). Upgrade
from 3-tier to 5-tier colorblind-safe scale. Add pulse animation for
high-opportunity markers (>=75). Extract shared PNMarkers module from
3 duplicated implementations.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-08 22:16:45 +01:00
Deeman
8ced3a986e merge: geo headers on city/region hubs (opportunity map pre-select, city highlight, color fix)
All checks were successful
CI / test (push) Successful in 57s
CI / tag (push) Successful in 3s
2026-03-08 20:50:43 +01:00
Deeman
291fb2abd9 feat(geo): use CF headers on opportunity map + country overview maps
Pre-select user's country on opportunity map dropdown (CF-IPCountry),
auto-load the map on page load. Highlight user's city on country
overview maps with a blue ring (CF-IPCity best-effort match). Unify
opportunity score color scale to red/amber/green (was using blue for
low scores). Inject window.__GEO global for client-side geo access.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-08 20:43:52 +01:00
Deeman
bfb0178615 merge: markets page improvements — score v6, bubble UX, geo-localization 2026-03-08 20:27:01 +01:00
Deeman
40d8c75b81 feat: stash CF-RegionCode and CF-IPCity headers in g for future use
All three Cloudflare geo headers now available:
- g.user_country (CF-IPCountry) — used by geo-sorted article listing
- g.user_region (CF-RegionCode) — available for within-country sorting
- g.user_city (CF-IPCity) — available for city-level proximity

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-08 20:26:50 +01:00
Deeman
d7bd053dc6 chore: update CHANGELOG for score v6, bubble UX, and geo-localization
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-08 20:26:28 +01:00
Deeman
d379dc7551 feat(markets): geo-localize article + map sorting via CF-IPCountry
- Read Cloudflare CF-IPCountry header into g.user_country (before_request)
- _filter_articles() sorts user's country first, nearby countries second,
  then rest — falls back to published_at DESC when header is absent
- map_countries sorted so user's country bubble renders on top (Leaflet z-order)
- Nearby-country mapping covers DACH, Iberia, Nordics, Benelux, UK/IE, Americas

Prerequisite: Nginx must forward CF-IPCountry header to Quart (same as Umami).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-08 20:25:43 +01:00
Deeman
814e8290a2 fix(markets): add map legend + unify bubble color scales
- Add legend below map: bubble size = venue count, color = Market Score
- Unify opportunity score color to use same green/orange/red scale
  (was using blue for low scores, inconsistent with market score)
- Add mkt_legend_size / mkt_legend_color i18n keys (EN + DE)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-08 20:24:21 +01:00
Deeman
67fbfde53d feat(scoring): Opportunity Score v5 → v6 — calibrate for saturated markets
- Lower density ceiling 8→5/100k (Spain at 6-16/100k now hits zero-gap)
- Increase supply deficit weight 35→40 pts (primary differentiator)
- Reduce addressable market 25→20 pts (less weight on population alone)
- Invert market validation → market headroom (high country maturity = less opportunity)

Target: Spain avg opportunity drops from ~78 to ~50-60 range.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-08 20:23:08 +01:00
Deeman
bf811444ba merge: Score v6 — World Bank global economic data for non-EU countries
All checks were successful
CI / test (push) Successful in 56s
CI / tag (push) Successful in 3s
2026-03-08 19:40:57 +01:00
Deeman
3c135051fd feat(scoring): Score v6 — World Bank global economic data for non-EU countries
Non-EU countries (AR, MX, AE, AU, etc.) previously got NULL for
median_income_pps and pli_construction, falling back to EU-calibrated
defaults (15K PPS, PLI=100) that produced wrong scores.

New World Bank WDI extractor fetches GNI per capita PPP and price level
ratio for 215 countries. dim_countries uses Germany as calibration anchor
to scale WB values into the Eurostat range (dynamic ratio, self-corrects
as both sources update). EU countries keep exact Eurostat values.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-08 18:17:33 +01:00
Deeman
c3847bb617 merge: Market Score v4 + Opportunity Score v5
All checks were successful
CI / test (push) Successful in 55s
CI / tag (push) Successful in 2s
2026-03-08 15:32:26 +01:00
Deeman
fcef47cb22 chore: update CHANGELOG + admin dependency graph for score v4/v5
- CHANGELOG.md: document Market Score v4 and Opportunity Score v5 changes
- pipeline_routes.py: add dim_countries to location_profiles dependency list

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-08 15:32:06 +01:00
Deeman
118c2c0fc7 feat(scoring): Opportunity Score v4 → v5 — fix correlated components
- Merge supply gap (30pts) + catchment gap (15pts) → supply deficit (35pts, GREATEST)
  Eliminates ~80% correlated double-count on a single signal.
- Add sports culture signal (10pts): tennis court density as racquet-sport adoption proxy.
  Ceiling 50 courts/25km. Harmless when tennis data is zero (contributes 0).
- Add construction affordability (5pts): income relative to PLI construction costs.
  Joins dim_countries.pli_construction. High income + low build cost = high score.
- Reduce economic power from 20 → 15pts to make room.

New weights: addressable market 25, economic power 15, supply deficit 35,
sports culture 10, construction affordability 5, market validation 10.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-08 15:30:04 +01:00
Deeman
cd6d950233 feat(scoring): Market Score v3 → v4 — fix Spain underscoring
- Lower count gate threshold: 5 → 3 venues (3 establishes a market pattern)
- Lower density ceiling: LN(21) → LN(11) (10/100k is reachable for mature markets)
- Better demand fallback: 0.4 → 0.65 multiplier + 0.3 floor (venues = demand evidence)
- Fix economic context: income/200 → income/25000 (actual discrimination vs free 10 pts)

Expected: Spain avg market score rises from ~54 to ~65-75.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-08 15:22:48 +01:00
Deeman
28e44384ef merge: opportunity map HTMX data islands + remove dead API endpoint
All checks were successful
CI / test (push) Successful in 1m0s
CI / tag (push) Successful in 3s
# Conflicts:
#	transform/sqlmesh_padelnomics/models/serving/location_opportunity_profile.sql
#	web/src/padelnomics/api.py
#	web/src/padelnomics/public/templates/opportunity_map.html
2026-03-07 21:05:52 +01:00
Deeman
b1e008a2a4 refactor(maps): opportunity map → HTMX data islands, remove dead API endpoint
- Delete opportunity() JSON endpoint from api.py (dead after this refactor)
- Add GET /opportunity-map/data route returning HTML partial with two JSON
  data islands (opp_points + ref_points from serving.location_profiles)
- Create partials/opportunity_map_data.html (2-line data island partial)
- Rewrite opportunity_map.html: HTMX attrs on <select>, invisible #map-data
  swap target, htmx:afterSwap listener replaces fetch()-based loadCountry()

city_venues endpoint stays public (article-maps.js calls it on public pages).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-07 20:56:45 +01:00
Deeman
d556ceecee fix(api): restore public access to country_cities + opportunity endpoints
opportunity_map.html (public page) still fetches these. Only countries.json
and city_venues.json are no longer called from any public page, so those two
keep @login_required.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-07 20:41:12 +01:00
Deeman
f215ea8e3a fix: supply gap inflation + inline map data + guard API endpoints
A. location_profiles.sql: supply gap now uses GREATEST(catchment_padel_courts,
   COALESCE(city_padel_venue_count, 0)) so Playtomic venues prevent cities like
   Murcia/Cordoba/Gijon from receiving a full 30-pt supply gap bonus when their
   OSM catchment count is zero. Expected ~10-15 pt drop for affected ES cities.

B. pseo_country_overview.sql: add population-weighted lat/lon centroid columns
   so the markets map can use accurate country positions from this table.

C/D. content/routes.py + markets.html: query pseo_country_overview in the route
   and pass as map_countries to the template, replacing the fetch('/api/...') call
   with inline JSON. Map scores now match pseo_country_overview (pop-weighted),
   and the page loads without an extra round-trip.

E. api.py: add @login_required to all 4 endpoints. Unauthenticated callers get
   a 302 redirect to login instead of data.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-07 20:33:31 +01:00
Deeman
b2ffad055b fix(supervisor): use file path for export_serving (not -m module syntax)
All checks were successful
CI / test (push) Successful in 1m0s
CI / tag (push) Successful in 3s
-m padelnomics.export_serving resolves to web package, not src/padelnomics.
src/padelnomics is not a uv workspace member so it's not importable by name.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-07 18:08:46 +01:00
Deeman
544891611f feat(transform): opportunity score v4 — market validation + population-weighted aggregation
All checks were successful
CI / test (push) Successful in 57s
CI / tag (push) Successful in 2s
Two targeted fixes for inflated country scores (ES 83, SE 77):

1. pseo_country_overview: replace AVG() with population-weighted averages
   for avg_opportunity_score and avg_market_score. Madrid/Barcelona now
   dominate Spain's average instead of hundreds of 30K-town white-space
   towns. Expected ES drop from ~83 to ~55-65.

2. location_profiles: replace dead sports culture component (10 pts,
   tennis data all zeros) with market validation signal.
   Split scored CTE into: market_scored → country_market → scored.
   country_market aggregates AVG(market_score) per country from cities
   with padel courts (market_score > 0), so zero-court locations don't
   dilute the signal. ES (~60/100) → ~6 pts. SE (~35/100) → ~3.5 pts.
   NULL → 0.5 neutral → 5 pts (untested market, not penalised).

Score budget unchanged: 25+20+30+15+10 = 100 pts.
No new models, no new data sources, no cycles.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-07 17:23:11 +01:00
Deeman
c30a7943aa feat(transform): opportunity score v4 — market validation + population-weighted aggregation
Two targeted fixes for inflated country scores (ES 83, SE 77):

1. pseo_country_overview: replace AVG() with population-weighted averages
   for avg_opportunity_score and avg_market_score. Madrid/Barcelona now
   dominate Spain's average instead of hundreds of 30K-town white-space
   towns. Expected ES drop from ~83 to ~55-65.

2. location_opportunity_profile: replace dead sports culture component
   (10 pts, tennis data all zeros) with market validation signal.
   New country_market CTE aggregates city_market_profile per country_code.
   ES (~60/100) → ~6 pts (proven demand). SE (~35/100) → ~3.5 pts
   (struggling market). NULL → 0.5 neutral → 5 pts (untested market).

Score budget unchanged: 25+20+30+15+10 = 100 pts.
New dependency: location_opportunity_profile → serving.city_market_profile (no cycle).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-07 15:36:51 +01:00
Deeman
b071199895 fix(docker): copy content/ directory into image
All checks were successful
CI / test (push) Successful in 54s
CI / tag (push) Successful in 2s
content/articles/ holds the cornerstone .md source files which
_sync_static_articles() reads on every /admin/articles load.
Without this COPY they were absent from the container.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-07 15:03:44 +01:00
28 changed files with 767 additions and 209 deletions

View File

@@ -7,6 +7,22 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
## [Unreleased]
### Changed
- **Dual-ring map markers** — map markers now encode two scores visually: inner core = primary score, outer ring = secondary score. Markets hub and country overview show Market Score (core) + Opportunity Score (ring). Opportunity map shows Opportunity Score (core) + Market Score (ring). City venue maps unchanged (navy dots). Color scale upgraded from 3-tier (green/amber/red) to 5-tier (deep green ≥80, teal ≥60, amber ≥40, orange-red ≥20, red <20) with distinct luminance at each tier for colorblind safety. Markers < 18px fall back to single-layer (no ring). Muted markers (cities without articles) show dashed ring outline. Highlighted markers (user's geo city) get blue outer glow. Opportunity map markers with score ≥75 pulse gently to highlight top investment targets. Tooltip lines now have inline color dots matching marker layers. All map scripts share a single `map-markers.js` module (`PNMarkers.scoreColor` + `PNMarkers.makeIcon`), replacing 3 duplicated implementations.
### Added
- **Geo headers on city/region hubs** — Cloudflare geo headers (`CF-IPCountry`, `CF-IPCity`) now used across location-based pages. Opportunity map pre-selects and auto-loads the user's country. Country overview maps highlight the user's city with a blue ring (best-effort CF-IPCity name match). `window.__GEO` JS global injected via `base.html` for client-side map code.
### Fixed
- **Opportunity map color scale** — low-score bubbles used blue (`#3B82F6`) instead of red (`#DC2626`), inconsistent with the unified `scoreColor()` scale used everywhere else. Fixed in `oppColor()`, legend, and `article-maps.js` tooltip colors. Thresholds aligned to ≥60/30/\<30.
### Changed
- **Opportunity Score v5 → v6** — calibrates for saturated markets (Spain avg dropped from ~78 to ~50-60 range). Density ceiling lowered from 8 → 5/100k (Spain at 6-16/100k now hits zero-gap). Supply deficit weight increased from 35 → 40 pts. Addressable market reduced from 25 → 20 pts. Market validation inverted → "market headroom": high country avg maturity now reduces opportunity (saturated market = less room for new entrants).
- **Markets page map legend** — bubble map now has a visual legend explaining size = venue count, color = Market Score. Opportunity score tooltip color unified to same green/amber/red scale (was using blue for low scores, inconsistent).
- **Geo-localized article sorting** — `/markets` page sorts articles by user proximity using Cloudflare CF-IPCountry header. User's country first, nearby countries second (DACH, Iberia, Nordics, etc.), rest by published_at. Map bubbles re-ordered so user's country renders on top. Falls back to chronological order when header is absent (local dev).
- **Score v6: Global economic data** — `dim_countries.median_income_pps` and `pli_construction` now cover all target markets, not just EU. World Bank WDI indicators (GNI per capita PPP + price level ratio) fill gaps for non-EU countries (AR, MX, AE, AU, etc.) with values calibrated to the Eurostat scale using Germany as anchor. EU countries keep exact Eurostat values. New extractor (`worldbank.py`), staging model (`stg_worldbank_income`), and `dim_countries` fallback CTEs. No changes to scoring formulas — the fix is upstream in the data layer.
- **Market Score v3 → v4** — fixes Spain averaging 54 (should be 65-80). Four calibration changes: count gate threshold lowered from 5 → 3 venues (3 establishes a market pattern), density ceiling lowered from LN(21) → LN(11) (10/100k is reachable for mature markets), demand evidence fallback raised from 0.4 → 0.65 multiplier with 0.3 floor (existence of venues IS evidence of demand), economic context ceiling changed from income/200 → income/25000 (actual discrimination instead of free 10 pts for everyone).
- **Opportunity Score v4 → v5** — fixes structural flaws: supply gap (30pts) + catchment gap (15pts) merged into single supply deficit (35pts, GREATEST of density gap and distance gap) eliminating ~80% correlated double-count. New sports culture signal (10pts) using tennis court density as racquet-sport adoption proxy. New construction affordability signal (5pts) using income relative to PLI construction costs from `dim_countries`. Economic power reduced from 20 → 15pts. New dependency on `foundation.dim_countries` for `pli_construction`.
- **Unified `location_profiles` serving model** — merged `city_market_profile` and `location_opportunity_profile` into a single `serving.location_profiles` table at `(country_code, geoname_id)` grain. Both Marktreife-Score (Market Score) and Marktpotenzial-Score (Opportunity Score) are now computed per location. City data enriched via LEFT JOIN `dim_cities` on `geoname_id`. Downstream models (`planner_defaults`, `pseo_city_costs_de`, `pseo_city_pricing`) updated to query `location_profiles` directly. `city_padel_venue_count` (exact from dim_cities) distinguished from `padel_venue_count` (spatial 5km from dim_locations).
- **Both scores on all map tooltips** — country map shows avg Market Score + avg Opportunity Score; city map shows Market Score + Opportunity Score per city; opportunity map shows Opportunity Score + Market Score per location. All score labels use the trademarked "Padelnomics Market Score" / "Padelnomics Opportunity Score" names.
- **API endpoints** — `/api/markets/countries.json` adds `avg_opportunity_score`; `/api/markets/<country>/cities.json` adds `opportunity_score`; `/api/opportunity/<country>.json` adds `market_score`.

View File

@@ -26,6 +26,7 @@ RUN mkdir -p /app/data && chown -R appuser:appuser /app
COPY --from=build --chown=appuser:appuser /app .
COPY --from=css-build /app/web/src/padelnomics/static/css/output.css ./web/src/padelnomics/static/css/output.css
COPY --chown=appuser:appuser infra/supervisor/workflows.toml ./infra/supervisor/workflows.toml
COPY --chown=appuser:appuser content/ ./content/
USER appuser
ENV PYTHONUNBUFFERED=1
ENV DATABASE_PATH=/app/data/app.db

View File

@@ -22,6 +22,7 @@ extract-census-usa-income = "padelnomics_extract.census_usa_income:main"
extract-ons-uk = "padelnomics_extract.ons_uk:main"
extract-geonames = "padelnomics_extract.geonames:main"
extract-gisco = "padelnomics_extract.gisco:main"
extract-worldbank = "padelnomics_extract.worldbank:main"
[build-system]
requires = ["hatchling"]

View File

@@ -7,7 +7,7 @@ A graphlib.TopologicalSorter schedules them: tasks with no unmet dependencies
run immediately in parallel; each completion may unlock new tasks.
Current dependency graph:
- All 9 non-availability extractors have no dependencies (run in parallel)
- All 10 non-availability extractors have no dependencies (run in parallel)
- playtomic_availability depends on playtomic_tenants (starts as soon as
tenants finishes, even if other extractors are still running)
"""
@@ -38,6 +38,8 @@ from .playtomic_availability import EXTRACTOR_NAME as AVAILABILITY_NAME
from .playtomic_availability import extract as extract_availability
from .playtomic_tenants import EXTRACTOR_NAME as TENANTS_NAME
from .playtomic_tenants import extract as extract_tenants
from .worldbank import EXTRACTOR_NAME as WORLDBANK_NAME
from .worldbank import extract as extract_worldbank
logger = setup_logging("padelnomics.extract")
@@ -54,6 +56,7 @@ EXTRACTORS: dict[str, tuple] = {
GEONAMES_NAME: (extract_geonames, []),
GISCO_NAME: (extract_gisco, []),
TENANTS_NAME: (extract_tenants, []),
WORLDBANK_NAME: (extract_worldbank, []),
AVAILABILITY_NAME: (extract_availability, [TENANTS_NAME]),
}

View File

@@ -0,0 +1,153 @@
"""World Bank WDI extractor — GNI per capita PPP and price level ratio.
Fetches two indicators (one API call each, no key required):
- NY.GNP.PCAP.PP.CD — GNI per capita, PPP (international $)
- PA.NUS.PPPC.RF — Price level ratio (PPP conversion factor / exchange rate)
These provide global fallbacks behind Eurostat for dim_countries.median_income_pps
and dim_countries.pli_construction (see dim_countries.sql for calibration logic).
API: World Bank API v2 — https://api.worldbank.org/v2/
No API key required. No env vars.
Landing: {LANDING_DIR}/worldbank/{year}/{month}/wdi_indicators.json.gz
Output: {"rows": [{"country_code": "DE", "indicator": "NY.GNP.PCAP.PP.CD",
"ref_year": 2023, "value": 74200.0}, ...], "count": N}
"""
import json
import sqlite3
from pathlib import Path
import niquests
from ._shared import HTTP_TIMEOUT_SECONDS, run_extractor, setup_logging
from .utils import get_last_cursor, landing_path, write_gzip_atomic
logger = setup_logging("padelnomics.extract.worldbank")
EXTRACTOR_NAME = "worldbank"
INDICATORS = ["NY.GNP.PCAP.PP.CD", "PA.NUS.PPPC.RF"]
# 6 years of data — we take the latest non-null per country in staging
DATE_RANGE = "2019:2025"
MAX_PER_PAGE = 5000
MAX_PAGES = 3
WDI_BASE_URL = "https://api.worldbank.org/v2/country/all/indicator"
# WB aggregate codes that look like real 2-letter country codes.
# These are regional/income-group aggregates, not actual countries.
_WB_AGGREGATE_CODES = frozenset({
"EU", "OE",
"XC", "XD", "XE", "XF", "XG", "XH", "XI", "XJ", "XL", "XM",
"XN", "XO", "XP", "XQ", "XR", "XS", "XT", "XU", "XV", "XY",
"ZF", "ZG", "ZH", "ZI", "ZJ", "ZQ", "ZT",
"V1", "V2", "V3", "V4",
})
def _normalize_country_code(wb_code: str) -> str | None:
"""Normalize WB country code to ISO alpha-2. Returns None for aggregates."""
code = wb_code.strip().upper()
if len(code) != 2:
return None
# Reject codes starting with a digit (e.g. "1W" for World)
if code[0].isdigit():
return None
if code in _WB_AGGREGATE_CODES:
return None
return code
def _fetch_indicator(
session: niquests.Session,
indicator: str,
) -> list[dict]:
"""Fetch all records for one indicator. Returns list of row dicts."""
rows: list[dict] = []
page = 1
while page <= MAX_PAGES:
url = (
f"{WDI_BASE_URL}/{indicator}"
f"?format=json&date={DATE_RANGE}&per_page={MAX_PER_PAGE}&page={page}"
)
logger.info("GET %s page %d", indicator, page)
resp = session.get(url, timeout=HTTP_TIMEOUT_SECONDS * 2)
resp.raise_for_status()
data = resp.json()
assert isinstance(data, list) and len(data) == 2, (
f"unexpected WB response shape for {indicator}: {type(data)}, len={len(data)}"
)
meta, records = data
total_pages = meta.get("pages", 1)
if records is None:
logger.warning("WB returned null data for %s page %d", indicator, page)
break
for record in records:
value = record.get("value")
if value is None:
continue
country_code = _normalize_country_code(record["country"]["id"])
if country_code is None:
continue
rows.append({
"country_code": country_code,
"indicator": indicator,
"ref_year": int(record["date"]),
"value": float(value),
})
if page >= total_pages:
break
page += 1
return rows
def extract(
landing_dir: Path,
year_month: str,
conn: sqlite3.Connection,
session: niquests.Session,
) -> dict:
"""Fetch WDI indicators. Skips if already run this month."""
last_cursor = get_last_cursor(conn, EXTRACTOR_NAME)
if last_cursor == year_month:
logger.info("already have data for %s — skipping", year_month)
return {"files_written": 0, "files_skipped": 1, "bytes_written": 0}
rows: list[dict] = []
for indicator in INDICATORS:
indicator_rows = _fetch_indicator(session, indicator)
logger.info("%s: %d records", indicator, len(indicator_rows))
rows.extend(indicator_rows)
assert len(rows) >= 200, f"expected ≥200 WB records, got {len(rows)} — API may have changed"
logger.info("total: %d WDI records", len(rows))
year, month = year_month.split("/")
dest_dir = landing_path(landing_dir, "worldbank", year, month)
dest = dest_dir / "wdi_indicators.json.gz"
payload = json.dumps({"rows": rows, "count": len(rows)}).encode()
bytes_written = write_gzip_atomic(dest, payload)
logger.info("written %s bytes compressed", f"{bytes_written:,}")
return {
"files_written": 1,
"files_skipped": 0,
"bytes_written": bytes_written,
"cursor_value": year_month,
}
def main() -> None:
run_extractor(EXTRACTOR_NAME, extract)
if __name__ == "__main__":
main()

View File

@@ -42,7 +42,7 @@ do
# The web app detects the inode change on next query — no restart needed.
DUCKDB_PATH="${DUCKDB_PATH:-/data/padelnomics/lakehouse.duckdb}" \
SERVING_DUCKDB_PATH="${SERVING_DUCKDB_PATH:-/data/padelnomics/analytics.duckdb}" \
uv run python -m padelnomics.export_serving
uv run python src/padelnomics/export_serving.py
) || {
if [ -n "${ALERT_WEBHOOK_URL:-}" ]; then

View File

@@ -72,3 +72,8 @@ description = "UK local authority population estimates from ONS"
module = "padelnomics_extract.gisco"
schedule = "0 0 1 1 *"
description = "EU geographic boundaries (NUTS2 polygons) from Eurostat GISCO"
[worldbank]
module = "padelnomics_extract.worldbank"
schedule = "monthly"
description = "GNI per capita PPP + price level ratio from World Bank WDI"

View File

@@ -2,10 +2,14 @@
--
-- Consolidates data previously duplicated across dim_cities and dim_locations:
-- - country_name_en / country_slug (was: ~50-line CASE blocks in both models)
-- - median_income_pps (was: country_income CTE in both models)
-- - energy prices, labour costs, PLI indices (new — from Eurostat datasets)
-- - median_income_pps (Eurostat PPS preferred, World Bank GNI PPP fallback)
-- - energy prices, labour costs, PLI indices (Eurostat, WB price level ratio fallback)
-- - cost override columns for the financial calculator
--
-- World Bank fallback: for non-EU countries (AR, MX, AE, AU, etc.), income and PLI
-- are derived from WB WDI indicators calibrated to the Eurostat scale using Germany
-- as anchor. See de_calibration CTE. EU countries keep exact Eurostat values.
--
-- Used by: dim_cities, dim_locations, pseo_city_costs_de, planner_defaults.
-- Grain: country_code (one row per ISO 3166-1 alpha-2 country code).
-- Kind: FULL — small table (~40 rows), full refresh daily.
@@ -82,6 +86,26 @@ de_elec AS (
de_gas AS (
SELECT gas_eur_gj FROM latest_gas WHERE country_code = 'DE'
),
-- Latest World Bank WDI per country (GNI PPP + price level ratio)
latest_wb AS (
SELECT country_code, gni_ppp, price_level_ratio, ref_year AS wb_year
FROM staging.stg_worldbank_income
WHERE gni_ppp IS NOT NULL OR price_level_ratio IS NOT NULL
QUALIFY ROW_NUMBER() OVER (PARTITION BY country_code ORDER BY ref_year DESC) = 1
),
-- Germany calibration anchor: Eurostat PPS + WB GNI PPP + WB price ratio + Eurostat PLI construction.
-- Used to scale World Bank values into Eurostat-comparable ranges.
-- Single row; if DE is missing from any source, that ratio produces NULL (safe fallthrough).
de_calibration AS (
SELECT
i.median_income_pps AS de_eurostat_pps,
wb.gni_ppp AS de_gni_ppp,
wb.price_level_ratio AS de_price_level_ratio,
p.construction AS de_pli_construction
FROM (SELECT median_income_pps FROM latest_income WHERE country_code = 'DE') i
CROSS JOIN (SELECT gni_ppp, price_level_ratio FROM latest_wb WHERE country_code = 'DE') wb
CROSS JOIN (SELECT construction FROM pli_pivoted WHERE country_code = 'DE') p
),
-- All distinct country codes from any source
all_countries AS (
SELECT country_code FROM latest_income
@@ -93,6 +117,8 @@ all_countries AS (
SELECT country_code FROM latest_labour
UNION
SELECT country_code FROM pli_pivoted
UNION
SELECT country_code FROM latest_wb
-- Ensure known padel markets appear even if Eurostat doesn't cover them yet
UNION ALL
SELECT unnest(['DE','ES','GB','FR','IT','PT','AT','CH','NL','BE','SE','NO','DK','FI',
@@ -149,15 +175,21 @@ SELECT
ELSE ac.country_code
END, '[^a-zA-Z0-9]+', '-'
)) AS country_slug,
-- Income data
-- Income: Eurostat PPS preferred, World Bank GNI PPP scaled to PPS as fallback
COALESCE(
i.median_income_pps,
i.income_year,
ROUND(wb.gni_ppp * (de_cal.de_eurostat_pps / NULLIF(de_cal.de_gni_ppp, 0)), 0)
) AS median_income_pps,
COALESCE(i.income_year, wb.wb_year) AS income_year,
-- Raw energy and labour data (for reference / future staffed-scenario use)
e.electricity_eur_kwh,
g.gas_eur_gj,
la.labour_cost_eur_hour,
-- PLI indices per category (EU27=100)
p.construction AS pli_construction,
-- PLI construction: Eurostat preferred, World Bank price level ratio scaled to PLI as fallback
COALESCE(
p.construction,
ROUND(wb.price_level_ratio / NULLIF(de_cal.de_price_level_ratio, 0) * de_cal.de_pli_construction, 1)
) AS pli_construction,
p.housing AS pli_housing,
p.services AS pli_services,
p.misc AS pli_misc,
@@ -278,8 +310,10 @@ LEFT JOIN latest_electricity e ON ac.country_code = e.country_code
LEFT JOIN latest_gas g ON ac.country_code = g.country_code
LEFT JOIN latest_labour la ON ac.country_code = la.country_code
LEFT JOIN pli_pivoted p ON ac.country_code = p.country_code
LEFT JOIN latest_wb wb ON ac.country_code = wb.country_code
CROSS JOIN de_pli de_p
CROSS JOIN de_elec de_e
CROSS JOIN de_gas de_g
CROSS JOIN de_calibration de_cal
-- Enforce grain
QUALIFY ROW_NUMBER() OVER (PARTITION BY ac.country_code ORDER BY ac.country_code) = 1

View File

@@ -5,28 +5,36 @@
--
-- Two scores per location:
--
-- Padelnomics Market Score (Marktreife-Score v3, 0100):
-- Padelnomics Market Score (Marktreife-Score v4, 0100):
-- "How mature/established is this padel market?"
-- Only meaningful for locations matched to a dim_cities row (city_slug IS NOT NULL)
-- with padel venues. 0 for all other locations.
--
-- 40 pts supply development — log-scaled density (LN ceiling 20/100k) × count gate
-- 25 pts demand evidence — occupancy when available; 40% density proxy otherwise
-- v4 changes: lower count gate (5→3), lower density ceiling (LN(21)→LN(11)),
-- better demand fallback (0.4→0.65 with 0.3 floor), economic context discrimination (200→25K).
--
-- 40 pts supply development — log-scaled density (LN ceiling 10/100k) × count gate (3)
-- 25 pts demand evidence — occupancy when available; 65% density proxy + 0.3 floor otherwise
-- 15 pts addressable market — log-scaled population, ceiling 1M
-- 10 pts economic context — income PPS normalised to 200 ceiling
-- 10 pts economic context — income PPS normalised to 25,000 ceiling
-- 10 pts data quality — completeness discount
--
-- Padelnomics Opportunity Score (Marktpotenzial-Score v3, 0100):
-- Padelnomics Opportunity Score (Marktpotenzial-Score v6, 0100):
-- "Where should I build a padel court?"
-- Computed for ALL locations — zero-court locations score highest on supply gap.
-- H3 catchment methodology: addressable market and supply gap use a regional
-- Computed for ALL locations — zero-court locations score highest on supply deficit.
-- H3 catchment methodology: addressable market and supply deficit use a regional
-- H3 catchment (res-5 cell + 6 neighbours, ~24km radius).
--
-- 25 pts addressable market — log-scaled catchment population, ceiling 500K
-- 20 pts economic power — income PPS, normalised to 35,000
-- 30 pts supply gap — inverted catchment venue density; 0 courts = full marks
-- 15 pts catchment gap — distance to nearest padel court
-- 10 pts sports culturetennis courts within 25km
-- v6 changes: lower density ceiling 8→5/100k (saturated markets hit zero-gap sooner),
-- increase supply deficit weight 35→40 pts, reduce addressable market 25→20 pts,
-- invert market validation (high country maturity = LESS opportunity).
--
-- 20 pts addressable market log-scaled catchment population, ceiling 500K
-- 15 pts economic power — income PPS, normalised to 35,000
-- 40 pts supply deficit — max(density gap, distance gap); eliminates double-count
-- 10 pts sports culture — tennis court density as racquet-sport adoption proxy
-- 5 pts construction affordability — income relative to construction costs (PLI)
-- 10 pts market headroom — inverse country-level avg market maturity
--
-- Consumers query directly with WHERE filters:
-- cities API: WHERE country_slug = ? AND city_slug IS NOT NULL
@@ -105,7 +113,7 @@ city_match AS (
ORDER BY c.padel_venue_count DESC
) = 1
),
-- Pricing / occupancy from Playtomic (via city_slug) + H3 catchment
-- Pricing / occupancy from Playtomic (via city_slug) + H3 catchment + country PLI
with_pricing AS (
SELECT
b.*,
@@ -118,6 +126,7 @@ with_pricing AS (
vpb.median_occupancy_rate,
vpb.median_daily_revenue_per_venue,
vpb.price_currency,
dc.pli_construction,
COALESCE(ct.catchment_population, b.population)::BIGINT AS catchment_population,
COALESCE(ct.catchment_padel_courts, b.padel_venue_count)::INTEGER AS catchment_padel_courts
FROM base b
@@ -129,9 +138,11 @@ with_pricing AS (
AND cm.city_slug = vpb.city_slug
LEFT JOIN catchment ct
ON b.geoname_id = ct.geoname_id
LEFT JOIN foundation.dim_countries dc
ON b.country_code = dc.country_code
),
-- Both scores computed from the enriched base
scored AS (
-- Step 1: market score only — needed first so we can aggregate country averages.
market_scored AS (
SELECT *,
-- City-level venue density (from dim_cities exact count, not dim_locations spatial 5km)
CASE WHEN population > 0
@@ -144,34 +155,38 @@ scored AS (
WHEN population > 0 OR COALESCE(city_padel_venue_count, 0) > 0 THEN 0.5
ELSE 0.0
END AS data_confidence,
-- ── Market Score (Marktreife-Score v3) ──────────────────────────────────
-- ── Market Score (Marktreife-Score v4) ──────────────────────────────────
-- 0 when no city match or no venues (city_padel_venue_count NULL or 0)
CASE WHEN COALESCE(city_padel_venue_count, 0) > 0 THEN
ROUND(
-- Supply development (40 pts)
-- density ceiling 10/100k (LN(11)), count gate 3 venues
40.0 * LEAST(1.0, LN(
COALESCE(
CASE WHEN population > 0
THEN COALESCE(city_padel_venue_count, 0)::DOUBLE / population * 100000
ELSE 0 END
, 0) + 1) / LN(21))
* LEAST(1.0, COALESCE(city_padel_venue_count, 0) / 5.0)
, 0) + 1) / LN(11))
* LEAST(1.0, COALESCE(city_padel_venue_count, 0) / 3.0)
-- Demand evidence (25 pts)
-- with occupancy: scale to 65% target. Without: 65% of supply proxy + 0.3 floor
-- (existence of venues IS evidence of demand)
+ 25.0 * CASE
WHEN median_occupancy_rate IS NOT NULL
THEN LEAST(1.0, median_occupancy_rate / 0.65)
ELSE 0.4 * LEAST(1.0, LN(
ELSE GREATEST(0.3, 0.65 * LEAST(1.0, LN(
COALESCE(
CASE WHEN population > 0
THEN COALESCE(city_padel_venue_count, 0)::DOUBLE / population * 100000
ELSE 0 END
, 0) + 1) / LN(21))
* LEAST(1.0, COALESCE(city_padel_venue_count, 0) / 5.0)
, 0) + 1) / LN(11))
* LEAST(1.0, COALESCE(city_padel_venue_count, 0) / 3.0))
END
-- Addressable market (15 pts)
+ 15.0 * LEAST(1.0, LN(GREATEST(population, 1)) / LN(1000000))
-- Economic context (10 pts)
+ 10.0 * LEAST(1.0, COALESCE(median_income_pps, 100) / 200.0)
-- ceiling 25,000 PPS discriminates between wealthy and poorer markets
+ 10.0 * LEAST(1.0, COALESCE(median_income_pps, 15000) / 25000.0)
-- Data quality (10 pts)
+ 10.0 * CASE
WHEN population > 0 AND COALESCE(city_padel_venue_count, 0) > 0 THEN 1.0
@@ -180,25 +195,58 @@ scored AS (
END
, 1)
ELSE 0
END AS market_score,
-- ── Opportunity Score (Marktpotenzial-Score v3, H3 catchment) ──────────
ROUND(
-- Addressable market (25 pts): log-scaled catchment population, ceiling 500K
25.0 * LEAST(1.0, LN(GREATEST(catchment_population, 1)) / LN(500000))
-- Economic power (20 pts): income PPS normalised to 35,000
+ 20.0 * LEAST(1.0, COALESCE(median_income_pps, 15000) / 35000.0)
-- Supply gap (30 pts): inverted catchment venue density
+ 30.0 * GREATEST(0.0, 1.0 - COALESCE(
CASE WHEN catchment_population > 0
THEN catchment_padel_courts::DOUBLE / catchment_population * 100000
ELSE 0.0
END, 0.0) / 8.0)
-- Catchment gap (15 pts): distance to nearest court
+ 15.0 * COALESCE(LEAST(1.0, nearest_padel_court_km / 30.0), 0.5)
-- Sports culture (10 pts): tennis courts within 25km
+ 10.0 * LEAST(1.0, tennis_courts_within_25km / 10.0)
, 1) AS opportunity_score
END AS market_score
FROM with_pricing
),
-- Step 2: country-level avg market maturity — used as market headroom signal (10 pts).
-- Filter to market_score > 0 (cities with padel courts only) so zero-court locations
-- don't dilute the country signal. Higher avg = more saturated = less headroom.
country_market AS (
SELECT
country_code,
ROUND(AVG(market_score), 1) AS country_avg_market_score
FROM market_scored
WHERE market_score > 0
GROUP BY country_code
),
-- Step 3: add opportunity_score using country market validation signal.
scored AS (
SELECT ms.*,
-- ── Opportunity Score (Marktpotenzial-Score v6, H3 catchment) ──────────
ROUND(
-- Addressable market (20 pts): log-scaled catchment population, ceiling 500K
20.0 * LEAST(1.0, LN(GREATEST(catchment_population, 1)) / LN(500000))
-- Economic power (15 pts): income PPS normalised to 35,000
+ 15.0 * LEAST(1.0, COALESCE(median_income_pps, 15000) / 35000.0)
-- Supply deficit (40 pts): max of density gap and distance gap.
-- Ceiling 5/100k (down from 8): Spain at 6-16/100k now hits zero-gap.
+ 40.0 * GREATEST(
-- density-based gap (H3 catchment): 0 courts = 1.0, 5/100k = 0.0
GREATEST(0.0, 1.0 - COALESCE(
CASE WHEN catchment_population > 0
THEN GREATEST(catchment_padel_courts, COALESCE(city_padel_venue_count, 0))::DOUBLE / catchment_population * 100000
ELSE 0.0
END, 0.0) / 5.0),
-- distance-based gap: 30km+ = 1.0, 0km = 0.0; NULL = 0.5
COALESCE(LEAST(1.0, nearest_padel_court_km / 30.0), 0.5)
)
-- Sports culture (10 pts): tennis density as racquet-sport adoption proxy.
-- Ceiling 50 courts within 25km. Harmless when tennis data is zero (contributes 0).
+ 10.0 * LEAST(1.0, COALESCE(tennis_courts_within_25km, 0) / 50.0)
-- Construction affordability (5 pts): income purchasing power relative to build costs.
-- PLI construction is EU27=100 index. High income + low construction cost = high score.
+ 5.0 * LEAST(1.0,
COALESCE(median_income_pps, 15000) / 35000.0
/ GREATEST(0.5, COALESCE(pli_construction, 100.0) / 100.0)
)
-- Market headroom (10 pts): INVERSE country-level avg market maturity.
-- High avg market score = saturated market = LESS opportunity for new entrants.
-- ES (~46/100): proven demand, less headroom → ~5.4 pts.
-- SE (~40/100): emerging → ~6 pts. NULL: 0.5 neutral → 5 pts.
+ 10.0 * (1.0 - COALESCE(cm.country_avg_market_score / 100.0, 0.5))
, 1) AS opportunity_score
FROM market_scored ms
LEFT JOIN country_market cm ON ms.country_code = cm.country_code
)
SELECT
s.geoname_id,

View File

@@ -18,13 +18,14 @@ SELECT
country_slug,
COUNT(*) AS city_count,
SUM(padel_venue_count) AS total_venues,
ROUND(AVG(market_score), 1) AS avg_market_score,
-- Population-weighted: large cities (Madrid, Barcelona) dominate, not hundreds of small towns
ROUND(SUM(market_score * population) / NULLIF(SUM(population), 0), 1) AS avg_market_score,
MAX(market_score) AS top_city_market_score,
-- Top 5 cities by venue count (prominence), then score for internal linking
LIST(city_slug ORDER BY padel_venue_count DESC, market_score DESC NULLS LAST)[1:5] AS top_city_slugs,
LIST(city_name ORDER BY padel_venue_count DESC, market_score DESC NULLS LAST)[1:5] AS top_city_names,
-- Opportunity score aggregates (NULL-safe: cities without geoname_id match excluded from AVG)
ROUND(AVG(opportunity_score), 1) AS avg_opportunity_score,
-- Opportunity score aggregates (population-weighted: saturated megacities dominate, not hundreds of small towns)
ROUND(SUM(opportunity_score * population) / NULLIF(SUM(population), 0), 1) AS avg_opportunity_score,
MAX(opportunity_score) AS top_opportunity_score,
-- Top 5 opportunity cities by population (prominence), then opportunity score
LIST(city_slug ORDER BY population DESC, opportunity_score DESC NULLS LAST)[1:5] AS top_opportunity_slugs,
@@ -36,6 +37,8 @@ SELECT
-- Use the most common currency in the country (MIN is deterministic for single-currency countries)
MIN(price_currency) AS price_currency,
SUM(population) AS total_population,
ROUND(SUM(lat * population) / NULLIF(SUM(population), 0), 4) AS lat,
ROUND(SUM(lon * population) / NULLIF(SUM(population), 0), 4) AS lon,
CURRENT_DATE AS refreshed_date
FROM serving.pseo_city_costs_de
GROUP BY country_code, country_name_en, country_slug

View File

@@ -0,0 +1,41 @@
-- World Bank WDI indicators: GNI per capita PPP and price level ratio.
-- Pivoted to one row per (country_code, ref_year) with both indicators as columns.
--
-- Source: data/landing/worldbank/{year}/{month}/wdi_indicators.json.gz
-- Extracted by: worldbank.py
-- Used by: dim_countries (fallback behind Eurostat for non-EU countries)
MODEL (
name staging.stg_worldbank_income,
kind FULL,
cron '@daily',
grain (country_code, ref_year)
);
WITH parsed AS (
SELECT
row ->> 'country_code' AS country_code,
TRY_CAST(row ->> 'ref_year' AS INTEGER) AS ref_year,
row ->> 'indicator' AS indicator,
TRY_CAST(row ->> 'value' AS DOUBLE) AS value,
CURRENT_DATE AS extracted_date
FROM (
SELECT UNNEST(rows) AS row
FROM read_json(
@LANDING_DIR || '/worldbank/*/*/wdi_indicators.json.gz',
auto_detect = true
)
)
WHERE (row ->> 'country_code') IS NOT NULL
)
SELECT
country_code,
ref_year,
MAX(value) FILTER (WHERE indicator = 'NY.GNP.PCAP.PP.CD') AS gni_ppp,
MAX(value) FILTER (WHERE indicator = 'PA.NUS.PPPC.RF') AS price_level_ratio,
MAX(extracted_date) AS extracted_date
FROM parsed
WHERE value IS NOT NULL
AND value > 0
AND LENGTH(country_code) = 2
GROUP BY country_code, ref_year

View File

@@ -111,7 +111,7 @@ _DAG: dict[str, list[str]] = {
"fct_daily_availability": ["fct_availability_slot", "dim_venue_capacity"],
# Serving
"venue_pricing_benchmarks": ["fct_daily_availability"],
"location_profiles": ["dim_locations", "dim_cities", "venue_pricing_benchmarks"],
"location_profiles": ["dim_locations", "dim_cities", "dim_countries", "venue_pricing_benchmarks"],
"planner_defaults": ["venue_pricing_benchmarks", "location_profiles"],
"pseo_city_costs_de": [
"location_profiles", "planner_defaults",

View File

@@ -10,6 +10,7 @@
<body>
<div class="article-body">{{ body_html | safe }}</div>
<script>window.LEAFLET_JS_URL = '{{ url_for("static", filename="vendor/leaflet/leaflet.min.js") }}';</script>
<script src="{{ url_for('static', filename='js/map-markers.js') }}"></script>
<script src="{{ url_for('static', filename='js/article-maps.js') }}"></script>
</body>
</html>

View File

@@ -34,5 +34,6 @@
</div>
<script>window.LEAFLET_JS_URL = '{{ url_for("static", filename="vendor/leaflet/leaflet.min.js") }}';</script>
<script src="{{ url_for('static', filename='js/map-markers.js') }}"></script>
<script src="{{ url_for('static', filename='js/article-maps.js') }}"></script>
{% endblock %}

View File

@@ -8,6 +8,7 @@ daily when the pipeline runs).
from quart import Blueprint, abort, jsonify
from .analytics import fetch_analytics
from .auth.routes import login_required
from .core import fetch_all, is_flag_enabled
bp = Blueprint("api", __name__)
@@ -26,6 +27,7 @@ async def _require_maps_flag() -> None:
@bp.route("/markets/countries.json")
@login_required
async def countries():
"""Country-level aggregates for the markets hub map."""
await _require_maps_flag()
@@ -96,23 +98,3 @@ async def city_venues(country_slug: str, city_slug: str):
)
return jsonify(rows), 200, _CACHE_HEADERS
@bp.route("/opportunity/<country_slug>.json")
async def opportunity(country_slug: str):
"""Location-level opportunity scores for the opportunity map."""
await _require_maps_flag()
assert country_slug, "country_slug required"
rows = await fetch_analytics(
"""
SELECT location_name, location_slug, lat, lon,
opportunity_score, market_score,
nearest_padel_court_km,
padel_venue_count, population
FROM serving.location_profiles
WHERE country_slug = ? AND opportunity_score > 0
ORDER BY opportunity_score DESC
LIMIT 500
""",
[country_slug],
)
return jsonify(rows), 200, _CACHE_HEADERS

View File

@@ -148,6 +148,18 @@ def create_app() -> Quart:
# Per-request hooks
# -------------------------------------------------------------------------
@app.before_request
async def set_user_geo():
"""Stash Cloudflare geo headers in g for proximity sorting.
Requires nginx: proxy_set_header CF-IPCountry $http_cf_ipcountry;
proxy_set_header CF-RegionCode $http_cf_regioncode;
proxy_set_header CF-IPCity $http_cf_ipcity;
"""
g.user_country = request.headers.get("CF-IPCountry", "").upper() or ""
g.user_region = request.headers.get("CF-RegionCode", "") or ""
g.user_city = request.headers.get("CF-IPCity", "") or ""
@app.before_request
async def validate_lang():
"""404 unsupported language prefixes (e.g. /fr/terms)."""
@@ -234,6 +246,8 @@ def create_app() -> Quart:
"csrf_token": get_csrf_token,
"ab_variant": getattr(g, "ab_variant", None),
"ab_tag": getattr(g, "ab_tag", None),
"user_country": g.get("user_country", ""),
"user_city": g.get("user_city", ""),
"lang": effective_lang,
"t": get_translations(effective_lang),
"v": _ASSET_VERSION,

View File

@@ -9,6 +9,7 @@ from jinja2 import Environment, FileSystemLoader
from markupsafe import Markup
from quart import Blueprint, abort, g, redirect, render_template, request
from ..analytics import fetch_analytics
from ..core import (
REPO_ROOT,
capture_waitlist_email,
@@ -203,6 +204,21 @@ async def markets():
)
articles = await _filter_articles(q, country, region)
map_countries = await fetch_analytics("""
SELECT country_code, country_name_en, country_slug,
city_count, total_venues,
avg_market_score, avg_opportunity_score,
lat, lon
FROM serving.pseo_country_overview
ORDER BY total_venues DESC
""")
# Sort so user's country renders last (on top in Leaflet z-order)
user_country = g.get("user_country", "")
if user_country and map_countries:
map_countries = sorted(
map_countries,
key=lambda c: 1 if c["country_code"] == user_country else 0,
)
return await render_template(
"markets.html",
@@ -212,6 +228,7 @@ async def markets():
current_q=q,
current_country=country,
current_region=region,
map_countries=map_countries,
)
@@ -227,9 +244,46 @@ async def market_results():
return await render_template("partials/market_results.html", articles=articles)
_NEARBY_COUNTRIES: dict[str, tuple[str, ...]] = {
"DE": ("AT", "CH"), "AT": ("DE", "CH"), "CH": ("DE", "AT"),
"ES": ("PT",), "PT": ("ES",),
"GB": ("IE",), "IE": ("GB",),
"US": ("CA",), "CA": ("US",),
"IT": ("CH",), "FR": ("BE", "CH"), "BE": ("FR", "NL", "DE"),
"NL": ("BE", "DE"), "SE": ("NO", "DK", "FI"), "NO": ("SE", "DK"),
"DK": ("SE", "NO", "DE"), "FI": ("SE",),
"MX": ("US",), "BR": ("AR",), "AR": ("BR",),
}
def _geo_order_clause(user_country: str) -> tuple[str, list]:
"""Build ORDER BY clause that sorts user's country first, nearby second.
Returns (order_sql, params) where order_sql starts with the geo CASE
followed by published_at DESC. Caller prepends 'ORDER BY'.
"""
if not user_country:
return "published_at DESC", []
nearby = _NEARBY_COUNTRIES.get(user_country, ())
if nearby:
placeholders = ",".join("?" * len(nearby))
geo_case = f"""CASE WHEN country = ? THEN 0
WHEN country IN ({placeholders}) THEN 1
ELSE 2 END,
published_at DESC"""
return geo_case, [user_country, *nearby]
return """CASE WHEN country = ? THEN 0 ELSE 1 END,
published_at DESC""", [user_country]
async def _filter_articles(q: str, country: str, region: str) -> list[dict]:
"""Query published articles for the current language."""
"""Query published articles for the current language, geo-sorted."""
lang = g.get("lang", "en")
user_country = g.get("user_country", "")
geo_order, geo_params = _geo_order_clause(user_country)
if q:
# FTS query
wheres = ["articles_fts MATCH ?"]
@@ -243,14 +297,16 @@ async def _filter_articles(q: str, country: str, region: str) -> list[dict]:
wheres.append("a.region = ?")
params.append(region)
where = " AND ".join(wheres)
# Geo-sort references a.country
order = geo_order.replace("country", "a.country")
return await fetch_all(
f"""SELECT a.* FROM articles a
JOIN articles_fts ON articles_fts.rowid = a.id
WHERE {where}
AND a.status = 'published' AND a.published_at <= datetime('now')
ORDER BY a.published_at DESC
ORDER BY {order}
LIMIT 100""",
tuple(params),
tuple(params + geo_params),
)
else:
wheres = ["status = 'published'", "published_at <= datetime('now')", "language = ?"]
@@ -264,8 +320,8 @@ async def _filter_articles(q: str, country: str, region: str) -> list[dict]:
where = " AND ".join(wheres)
return await fetch_all(
f"""SELECT * FROM articles WHERE {where}
ORDER BY published_at DESC LIMIT 100""",
tuple(params),
ORDER BY {geo_order} LIMIT 100""",
tuple(params + geo_params),
)

View File

@@ -61,5 +61,6 @@
{% block scripts %}
<script>window.LEAFLET_JS_URL = '{{ url_for("static", filename="vendor/leaflet/leaflet.min.js") }}';</script>
<script src="{{ url_for('static', filename='js/map-markers.js') }}"></script>
<script src="{{ url_for('static', filename='js/article-maps.js') }}"></script>
{% endblock %}

View File

@@ -16,7 +16,27 @@
<p class="text-slate">{{ t.mkt_subheading }}</p>
</header>
<div id="markets-map" style="height:420px; border-radius:12px;" class="mb-6"></div>
<div id="markets-map" style="height:420px; border-radius:12px;" class="mb-4"></div>
<!-- Map legend -->
<div class="mb-6" style="display:flex; flex-wrap:wrap; gap:1rem 1.5rem; align-items:center; font-size:0.82rem; color:#64748B;">
<span style="display:flex; align-items:center; gap:0.35rem;">
<span style="font-weight:600;"></span> inner = Market Score
&nbsp;<span style="font-weight:600;"></span> ring = Opportunity Score
</span>
<span style="display:flex; align-items:center; gap:0.3rem;">
<span style="display:inline-block;width:10px;height:10px;border-radius:50%;background:#15803D;"></span>≥80
<span style="display:inline-block;width:10px;height:10px;border-radius:50%;background:#0D9488;margin-left:4px;"></span>≥60
<span style="display:inline-block;width:10px;height:10px;border-radius:50%;background:#D97706;margin-left:4px;"></span>≥40
<span style="display:inline-block;width:10px;height:10px;border-radius:50%;background:#EA580C;margin-left:4px;"></span>≥20
<span style="display:inline-block;width:10px;height:10px;border-radius:50%;background:#DC2626;margin-left:4px;"></span>&lt;20
</span>
<span style="display:flex; align-items:center; gap:0.35rem;">
<span style="display:inline-block; width:12px; height:12px; border-radius:50%; background:#64748B; border:2px solid white; box-shadow:0 1px 3px rgba(0,0,0,0.2);"></span>
<span style="display:inline-block; width:18px; height:18px; border-radius:50%; background:#64748B; border:2px solid white; box-shadow:0 1px 3px rgba(0,0,0,0.2);"></span>
{{ t.mkt_legend_size }}
</span>
</div>
<!-- Filters -->
<div class="card mb-8">
@@ -68,51 +88,41 @@
{% block scripts %}
<script src="{{ url_for('static', filename='vendor/leaflet/leaflet.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/map-markers.js') }}"></script>
<script>
(function() {
var sc = PNMarkers.scoreColor;
var map = L.map('markets-map', {scrollWheelZoom: false}).setView([48.5, 10], 4);
L.tileLayer('https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png', {
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OSM</a> &copy; <a href="https://carto.com/">CARTO</a>',
maxZoom: 18
}).addTo(map);
function scoreColor(score) {
if (score >= 60) return '#16A34A';
if (score >= 30) return '#D97706';
return '#DC2626';
function dot(hex, filled) {
return filled
? '<span style="display:inline-block;width:8px;height:8px;border-radius:50%;background:' + hex + ';vertical-align:middle;margin-right:4px;"></span>'
: '<span style="display:inline-block;width:8px;height:8px;border-radius:50%;border:2px solid ' + hex + ';vertical-align:middle;margin-right:4px;"></span>';
}
function makeIcon(size, color) {
var s = Math.round(size);
return L.divIcon({
className: '',
html: '<div class="pn-marker" style="width:' + s + 'px;height:' + s + 'px;background:' + color + ';opacity:0.82;"></div>',
iconSize: [s, s],
iconAnchor: [s / 2, s / 2],
});
}
fetch('/api/markets/countries.json')
.then(function(r) { return r.json(); })
.then(function(data) {
if (!data.length) return;
var data = {{ map_countries | tojson }};
if (data.length) {
var maxV = Math.max.apply(null, data.map(function(d) { return d.total_venues; }));
var lang = document.documentElement.lang || 'en';
data.forEach(function(c) {
if (!c.lat || !c.lon) return;
var size = 12 + 44 * Math.sqrt(c.total_venues / maxV);
var color = scoreColor(c.avg_market_score);
var oppColor = c.avg_opportunity_score >= 60 ? '#16A34A' : (c.avg_opportunity_score >= 30 ? '#D97706' : '#3B82F6');
var coreHex = sc(c.avg_market_score);
var ringHex = sc(c.avg_opportunity_score || 0);
var tip = '<strong>' + c.country_name_en + '</strong><br>'
+ c.total_venues + ' venues · ' + c.city_count + ' cities<br>'
+ '<span style="color:' + color + ';font-weight:600;">Padelnomics Market Score: ' + c.avg_market_score + '/100</span><br>'
+ '<span style="color:' + oppColor + ';font-weight:600;">Padelnomics Opportunity Score: ' + (c.avg_opportunity_score || 0) + '/100</span>';
L.marker([c.lat, c.lon], { icon: makeIcon(size, color) })
+ dot(coreHex, true) + '<span style="color:' + coreHex + ';font-weight:600;">Market Score: ' + c.avg_market_score + '/100</span><br>'
+ dot(ringHex, false) + '<span style="color:' + ringHex + ';font-weight:600;">Opportunity Score: ' + (c.avg_opportunity_score || 0) + '/100</span><br>'
+ '<span style="color:#94A3B8;font-size:0.75rem;">' + c.total_venues + ' venues · ' + c.city_count + ' cities</span>';
L.marker([c.lat, c.lon], { icon: PNMarkers.makeIcon({ size: size, coreColor: coreHex, ringColor: ringHex }) })
.bindTooltip(tip, { className: 'map-tooltip', direction: 'top', offset: [0, -Math.round(size / 2)] })
.on('click', function() { window.location = '/' + lang + '/markets/' + c.country_slug; })
.addTo(map);
});
});
}
})();
</script>
{% endblock %}

View File

@@ -606,6 +606,8 @@
"mkt_all_countries": "Alle Länder",
"mkt_all_regions": "Alle Regionen",
"mkt_no_results": "Keine Märkte gefunden. Passe Deine Filter an.",
"mkt_legend_size": "Kreisgröße = Anzahl Anlagen",
"mkt_legend_color": "Farbe = Market Score",
"waitlist_markets_title": "Marktdaten — Demnächst verfügbar",
"waitlist_markets_sub": "Detaillierte Marktberichte für Padel-Investoren: Baukosten, Umsatz-Benchmarks, Auslastungsdaten und ROI-Analysen nach Stadt und Region.",
"waitlist_markets_feature1": "Echte Kostendaten aus laufenden Anlagen in über 30 Ländern",

View File

@@ -606,6 +606,8 @@
"mkt_all_countries": "All Countries",
"mkt_all_regions": "All Regions",
"mkt_no_results": "No markets found. Try adjusting your filters.",
"mkt_legend_size": "Bubble size = venue count",
"mkt_legend_color": "Color = Market Score",
"waitlist_markets_title": "Markets Intelligence — Coming Soon",
"waitlist_markets_sub": "Deep-dive market reports for padel investors: construction costs, revenue benchmarks, occupancy data, and ROI analysis by city and region.",
"waitlist_markets_feature1": "Real cost data from operating venues across 30+ countries",

View File

@@ -79,12 +79,63 @@ async def opportunity_map():
if not await is_flag_enabled("maps", default=True):
abort(404)
countries = await fetch_analytics("""
SELECT DISTINCT country_slug, country_name_en
SELECT DISTINCT country_slug, country_name_en, country_code
FROM serving.location_profiles
WHERE city_slug IS NOT NULL
ORDER BY country_name_en
""")
return await render_template("opportunity_map.html", countries=countries)
user_cc = g.get("user_country", "")
selected_slug = ""
if user_cc:
for c in countries:
if c["country_code"] == user_cc:
selected_slug = c["country_slug"]
break
countries = sorted(
countries,
key=lambda c: (0 if c["country_code"] == user_cc else 1, c["country_name_en"]),
)
return await render_template("opportunity_map.html", countries=countries, selected_slug=selected_slug)
@bp.route("/opportunity-map/data")
async def opportunity_map_data():
"""HTMX partial: opportunity + reference data islands for Leaflet map."""
from ..core import is_flag_enabled
if not await is_flag_enabled("maps", default=True):
abort(404)
country_slug = request.args.get("country", "")
if not country_slug:
return ""
opp_points = await fetch_analytics(
"""
SELECT location_name, location_slug, lat, lon,
opportunity_score, market_score,
nearest_padel_court_km, padel_venue_count, population
FROM serving.location_profiles
WHERE country_slug = ? AND opportunity_score > 0
ORDER BY opportunity_score DESC
LIMIT 500
""",
[country_slug],
)
ref_points = await fetch_analytics(
"""
SELECT city_name, city_slug, lat, lon,
city_padel_venue_count AS padel_venue_count,
market_score, population
FROM serving.location_profiles
WHERE country_slug = ? AND city_slug IS NOT NULL
ORDER BY city_padel_venue_count DESC
LIMIT 200
""",
[country_slug],
)
return await render_template(
"partials/opportunity_map_data.html",
opp_points=opp_points,
ref_points=ref_points,
)
@bp.route("/imprint")

View File

@@ -24,30 +24,40 @@
<div class="card mb-4" style="padding: 1rem 1.25rem;">
<label class="form-label" for="opp-country-select" style="margin-bottom: 0.5rem; display:block;">Select a country</label>
<select id="opp-country-select" class="form-input" style="max-width: 280px;">
<select id="opp-country-select" name="country" class="form-input" style="max-width:280px;"
hx-get="{{ url_for('public.opportunity_map_data') }}"
hx-target="#map-data"
hx-trigger="change">
<option value="">— choose country —</option>
{% for c in countries %}
<option value="{{ c.country_slug }}">{{ c.country_name_en }}</option>
<option value="{{ c.country_slug }}" {% if c.country_slug == selected_slug %}selected{% endif %}>{{ c.country_name_en }}</option>
{% endfor %}
</select>
</div>
<div id="opportunity-map"></div>
<div id="map-data" style="display:none;"></div>
<div class="mt-4 text-sm text-slate">
<strong>Circle size:</strong> population &nbsp;|&nbsp;
<strong>Color:</strong>
<span style="display:inline-block;width:10px;height:10px;border-radius:50%;background:#16A34A;vertical-align:middle;margin:0 4px"></span>High (≥70) &nbsp;
<span style="display:inline-block;width:10px;height:10px;border-radius:50%;background:#D97706;vertical-align:middle;margin:0 4px"></span>Mid (4070) &nbsp;
<span style="display:inline-block;width:10px;height:10px;border-radius:50%;background:#3B82F6;vertical-align:middle;margin:0 4px"></span>Low (&lt;40)
<div class="mt-4 text-sm text-slate" style="display:flex; flex-wrap:wrap; gap:0.5rem 1.5rem; align-items:center;">
<span><strong></strong> inner = Opportunity Score &nbsp;<strong></strong> ring = Market Score</span>
<span style="display:flex; align-items:center; gap:0.3rem;">
<span style="display:inline-block;width:10px;height:10px;border-radius:50%;background:#15803D;"></span>≥80
<span style="display:inline-block;width:10px;height:10px;border-radius:50%;background:#0D9488;margin-left:4px;"></span>≥60
<span style="display:inline-block;width:10px;height:10px;border-radius:50%;background:#D97706;margin-left:4px;"></span>40
<span style="display:inline-block;width:10px;height:10px;border-radius:50%;background:#EA580C;margin-left:4px;"></span>≥20
<span style="display:inline-block;width:10px;height:10px;border-radius:50%;background:#DC2626;margin-left:4px;"></span>&lt;20
</span>
<span><strong>Size:</strong> population</span>
</div>
</main>
{% endblock %}
{% block scripts %}
<script src="{{ url_for('static', filename='vendor/leaflet/leaflet.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/map-markers.js') }}"></script>
<script>
(function() {
var sc = PNMarkers.scoreColor;
var TILES = 'https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png';
var TILES_ATTR = '&copy; <a href="https://www.openstreetmap.org/copyright">OSM</a> &copy; <a href="https://carto.com/">CARTO</a>';
@@ -57,22 +67,6 @@
var oppLayer = L.layerGroup().addTo(map);
var refLayer = L.layerGroup().addTo(map);
function oppColor(score) {
if (score >= 70) return '#16A34A';
if (score >= 40) return '#D97706';
return '#3B82F6';
}
function makeIcon(size, color) {
var s = Math.round(size);
return L.divIcon({
className: '',
html: '<div class="pn-marker" style="width:' + s + 'px;height:' + s + 'px;background:' + color + ';opacity:0.8;"></div>',
iconSize: [s, s],
iconAnchor: [s / 2, s / 2],
});
}
var REF_ICON = L.divIcon({
className: '',
html: '<div class="pn-venue" style="background:#94A3B8;border-color:white;opacity:0.7;"></div>',
@@ -86,54 +80,65 @@
: (p || '');
}
function loadCountry(slug) {
function dot(hex, filled) {
return filled
? '<span style="display:inline-block;width:8px;height:8px;border-radius:50%;background:' + hex + ';vertical-align:middle;margin-right:4px;"></span>'
: '<span style="display:inline-block;width:8px;height:8px;border-radius:50%;border:2px solid ' + hex + ';vertical-align:middle;margin-right:4px;"></span>';
}
function renderMap() {
oppLayer.clearLayers();
refLayer.clearLayers();
if (!slug) return;
var oppEl = document.getElementById('opp-data');
var refEl = document.getElementById('ref-data');
if (!oppEl) return;
var oppData = JSON.parse(oppEl.textContent);
var refData = JSON.parse(refEl.textContent);
fetch('/api/opportunity/' + slug + '.json')
.then(function(r) { return r.json(); })
.then(function(data) {
if (!data.length) return;
var maxPop = Math.max.apply(null, data.map(function(d) { return d.population || 1; }));
var bounds = [];
data.forEach(function(loc) {
if (!loc.lat || !loc.lon) return;
var size = 8 + 40 * Math.sqrt((loc.population || 1) / maxPop);
var color = oppColor(loc.opportunity_score);
var dist = loc.nearest_padel_court_km != null
? loc.nearest_padel_court_km.toFixed(1) + ' km to nearest court'
: 'No nearby courts';
var mktColor = loc.market_score >= 60 ? '#16A34A' : (loc.market_score >= 30 ? '#D97706' : '#DC2626');
var tip = '<strong>' + loc.location_name + '</strong><br>'
+ '<span style="color:' + color + ';font-weight:600;">Padelnomics Opportunity Score: ' + loc.opportunity_score + '/100</span><br>'
+ '<span style="color:' + mktColor + ';font-weight:600;">Padelnomics Market Score: ' + (loc.market_score || 0) + '/100</span><br>'
+ dist + ' · Pop. ' + fmtPop(loc.population);
L.marker([loc.lat, loc.lon], { icon: makeIcon(size, color) })
.bindTooltip(tip, { className: 'map-tooltip', direction: 'top', offset: [0, -Math.round(size / 2)] })
.addTo(oppLayer);
bounds.push([loc.lat, loc.lon]);
});
if (bounds.length) map.fitBounds(bounds, { padding: [30, 30] });
});
// Existing venues as small gray reference dots (drawn first = behind opp dots)
fetch('/api/markets/' + slug + '/cities.json')
.then(function(r) { return r.json(); })
.then(function(data) {
data.forEach(function(c) {
refData.forEach(function(c) {
if (!c.lat || !c.lon || !c.padel_venue_count) return;
L.marker([c.lat, c.lon], { icon: REF_ICON })
.bindTooltip(c.city_name + ' — ' + c.padel_venue_count + ' existing venues',
{ className: 'map-tooltip', direction: 'top', offset: [0, -7] })
.addTo(refLayer);
});
if (!oppData.length) return;
var maxPop = Math.max.apply(null, oppData.map(function(d) { return d.population || 1; }));
var bounds = [];
oppData.forEach(function(loc) {
if (!loc.lat || !loc.lon) return;
var size = 8 + 40 * Math.sqrt((loc.population || 1) / maxPop);
var coreHex = sc(loc.opportunity_score);
var ringHex = sc(loc.market_score || 0);
var dist = loc.nearest_padel_court_km != null
? loc.nearest_padel_court_km.toFixed(1) + ' km to nearest court'
: 'No nearby courts';
var tip = '<strong>' + loc.location_name + '</strong><br>'
+ dot(coreHex, true) + '<span style="color:' + coreHex + ';font-weight:600;">Opportunity Score: ' + loc.opportunity_score + '/100</span><br>'
+ dot(ringHex, false) + '<span style="color:' + ringHex + ';font-weight:600;">Market Score: ' + (loc.market_score || 0) + '/100</span><br>'
+ '<span style="color:#94A3B8;font-size:0.75rem;">' + dist + ' · Pop. ' + fmtPop(loc.population) + '</span>';
var icon = PNMarkers.makeIcon({
size: size,
coreColor: coreHex,
ringColor: ringHex,
pulse: loc.opportunity_score >= 75,
});
L.marker([loc.lat, loc.lon], { icon: icon })
.bindTooltip(tip, { className: 'map-tooltip', direction: 'top', offset: [0, -Math.round(size / 2)] })
.addTo(oppLayer);
bounds.push([loc.lat, loc.lon]);
});
if (bounds.length) map.fitBounds(bounds, { padding: [30, 30] });
}
document.getElementById('opp-country-select').addEventListener('change', function() {
loadCountry(this.value);
document.body.addEventListener('htmx:afterSwap', function(e) {
if (e.detail.target.id === 'map-data') renderMap();
});
// Auto-load if country pre-selected via geo header
var sel = document.getElementById('opp-country-select');
if (sel.value) htmx.trigger(sel, 'change');
})();
</script>
{% endblock %}

View File

@@ -0,0 +1,2 @@
<script id="opp-data" type="application/json">{{ opp_points | tojson }}</script>
<script id="ref-data" type="application/json">{{ ref_points | tojson }}</script>

View File

@@ -879,8 +879,10 @@
.leaflet-tooltip.map-tooltip::before { display: none; }
.leaflet-tooltip.map-tooltip strong { color: white; }
/* Polished variable-size circle — white border + drop shadow */
/* ── Dual-ring map markers ── */
/* Container: sets size, white border, shadow, hover */
.pn-marker {
position: relative;
border-radius: 50%;
border: 2.5px solid white;
box-shadow: 0 2px 8px rgba(0,0,0,0.28);
@@ -891,19 +893,57 @@
box-shadow: 0 3px 12px rgba(0,0,0,0.38);
transform: scale(1.1);
}
/* Outer ring: secondary score color */
.pn-marker__ring {
position: absolute;
inset: 0;
border-radius: 50%;
opacity: 0.65;
}
/* Inner core: primary score color, inset 5px from container edges */
.pn-marker__core {
position: absolute;
inset: 5px;
border-radius: 50%;
border: 1.5px solid rgba(255,255,255,0.5);
}
/* Compact fallback: marker < 18px, no ring — core fills container */
.pn-marker--compact .pn-marker__core {
inset: 0;
border: none;
}
/* Non-article city markers: faded + dashed border, no click affordance */
/* User's city highlight — blue outer glow */
.pn-marker--highlight {
border: 2.5px solid #3B82F6;
box-shadow: 0 0 0 3px rgba(59,130,246,0.25), 0 2px 8px rgba(0,0,0,0.28);
}
/* Non-article city markers: faded, dashed ring outline, no click */
.pn-marker--muted {
opacity: 0.45;
border: 2px dashed rgba(255,255,255,0.6);
opacity: 0.4;
cursor: default;
filter: saturate(0.7);
filter: saturate(0.6) brightness(1.1);
}
.pn-marker--muted .pn-marker__ring {
background: transparent !important;
border: 1.5px dashed rgba(255,255,255,0.6);
opacity: 1;
}
.pn-marker--muted:hover {
transform: none;
box-shadow: 0 2px 8px rgba(0,0,0,0.28);
}
/* Pulse animation — opportunity map only, score >= 75 */
.pn-marker--pulse .pn-marker__ring {
animation: marker-pulse 2.5s ease-in-out infinite;
}
@keyframes marker-pulse {
0%, 100% { transform: scale(1); opacity: 0.65; }
50% { transform: scale(1.35); opacity: 0.25; }
}
/* Small fixed venue dot */
.pn-venue {
width: 10px;

View File

@@ -4,6 +4,8 @@
* Looks for #country-map and #city-map elements. If neither exists, does nothing.
* Expects data-* attributes on the map elements and a global LEAFLET_JS_URL
* variable pointing to the Leaflet JS bundle.
*
* Depends on map-markers.js (window.PNMarkers) being loaded first.
*/
(function() {
var countryMapEl = document.getElementById('country-map');
@@ -12,22 +14,13 @@
var TILES = 'https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png';
var TILES_ATTR = '&copy; <a href="https://www.openstreetmap.org/copyright">OSM</a> &copy; <a href="https://carto.com/">CARTO</a>';
var sc = PNMarkers.scoreColor;
function scoreColor(score) {
if (score >= 60) return '#16A34A';
if (score >= 30) return '#D97706';
return '#DC2626';
}
function makeIcon(size, color, muted) {
var s = Math.round(size);
var cls = 'pn-marker' + (muted ? ' pn-marker--muted' : '');
return L.divIcon({
className: '',
html: '<div class="' + cls + '" style="width:' + s + 'px;height:' + s + 'px;background:' + color + ';"></div>',
iconSize: [s, s],
iconAnchor: [s / 2, s / 2],
});
function tooltipDot(hex, filled) {
var style = filled
? 'display:inline-block;width:8px;height:8px;border-radius:50%;background:' + hex + ';vertical-align:middle;margin-right:4px;'
: 'display:inline-block;width:8px;height:8px;border-radius:50%;border:2px solid ' + hex + ';vertical-align:middle;margin-right:4px;';
return '<span style="' + style + '"></span>';
}
function initCountryMap(el) {
@@ -45,22 +38,29 @@
if (!c.lat || !c.lon) return;
var size = 10 + 36 * Math.sqrt((c.padel_venue_count || 1) / maxV);
var hasArticle = c.has_article !== false;
var color = scoreColor(c.market_score);
var coreHex = sc(c.market_score);
var ringHex = sc(c.opportunity_score || 0);
var pop = c.population >= 1000000
? (c.population / 1000000).toFixed(1) + 'M'
: (c.population >= 1000 ? Math.round(c.population / 1000) + 'K' : (c.population || ''));
var oppColor = c.opportunity_score >= 60 ? '#16A34A' : (c.opportunity_score >= 30 ? '#D97706' : '#3B82F6');
var tip = '<strong>' + c.city_name + '</strong><br>'
+ tooltipDot(coreHex, true) + '<span style="color:' + coreHex + ';font-weight:600;">Market Score: ' + Math.round(c.market_score) + '/100</span><br>'
+ tooltipDot(ringHex, false) + '<span style="color:' + ringHex + ';font-weight:600;">Opportunity Score: ' + Math.round(c.opportunity_score || 0) + '/100</span><br>'
+ '<span style="color:#94A3B8;font-size:0.75rem;">'
+ (c.padel_venue_count || 0) + ' venues'
+ (pop ? ' · ' + pop : '')
+ '<br><span style="color:' + color + ';font-weight:600;">Padelnomics Market Score: ' + Math.round(c.market_score) + '/100</span>'
+ '<br><span style="color:' + oppColor + ';font-weight:600;">Padelnomics Opportunity Score: ' + Math.round(c.opportunity_score || 0) + '/100</span>';
+ (pop ? ' · ' + pop : '') + '</span>';
if (hasArticle) {
tip += '<br><span style="color:#94A3B8;font-size:0.75rem;">Click to explore →</span>';
} else {
tip += '<br><span style="color:#94A3B8;font-size:0.75rem;">Coming soon</span>';
}
var marker = L.marker([c.lat, c.lon], { icon: makeIcon(size, color, !hasArticle) })
var icon = PNMarkers.makeIcon({
size: size,
coreColor: coreHex,
ringColor: ringHex,
muted: !hasArticle,
});
var marker = L.marker([c.lat, c.lon], { icon: icon })
.bindTooltip(tip, { className: 'map-tooltip', direction: 'top', offset: [0, -Math.round(size / 2)] })
.addTo(map);
if (hasArticle) {
@@ -69,6 +69,24 @@
bounds.push([c.lat, c.lon]);
});
if (bounds.length) map.fitBounds(bounds, { padding: [24, 24] });
// Highlight user's city (best-effort name match via CF-IPCity)
var uc = (window.__GEO || {}).city || '';
if (uc) {
var match = data.find(function(c) {
return c.city_name && c.city_name.toLowerCase() === uc.toLowerCase();
});
if (match && match.lat && match.lon) {
var hSize = 10 + 36 * Math.sqrt((match.padel_venue_count || 1) / maxV);
var hIcon = PNMarkers.makeIcon({
size: hSize,
coreColor: sc(match.market_score),
ringColor: sc(match.opportunity_score || 0),
highlight: true,
});
L.marker([match.lat, match.lon], { icon: hIcon }).addTo(map);
}
}
})
.catch(function(err) { console.error('Country map fetch failed:', err); });
}

View File

@@ -0,0 +1,67 @@
/**
* Shared map marker utilities — dual-ring design with 5-tier color scale.
*
* Exposes window.PNMarkers = { scoreColor, makeIcon }
*
* scoreColor(score) → hex color string (5 tiers, colorblind-safe luminance steps)
* makeIcon(opts) → L.divIcon with dual-ring HTML
*
* opts = {
* size: number, // marker diameter in px
* coreColor: string, // hex for inner core (primary score)
* ringColor: string, // hex for outer ring (secondary score)
* muted: boolean, // dashed ring, no click affordance
* highlight: boolean, // blue outer glow (user's geo city)
* pulse: boolean, // gentle ring pulse (high opportunity)
* }
*/
(function() {
'use strict';
// 5-tier color scale — distinct luminance at each tier for deuteranopia/protanopia
function scoreColor(score) {
if (score >= 80) return '#15803D'; // deep green — excellent
if (score >= 60) return '#0D9488'; // teal — good
if (score >= 40) return '#D97706'; // amber — moderate
if (score >= 20) return '#EA580C'; // orange-red — below avg
return '#DC2626'; // red — poor
}
var COMPACT_THRESHOLD_PX = 18;
function makeIcon(opts) {
var s = Math.round(opts.size);
var compact = s < COMPACT_THRESHOLD_PX;
var cls = 'pn-marker';
if (compact) cls += ' pn-marker--compact';
if (opts.muted) cls += ' pn-marker--muted';
if (opts.highlight) cls += ' pn-marker--highlight';
if (opts.pulse && !opts.muted) cls += ' pn-marker--pulse';
var html;
if (compact || !opts.ringColor) {
// Single-layer fallback: core fills entire marker
html = '<div class="' + cls + '" style="width:' + s + 'px;height:' + s + 'px;">'
+ '<div class="pn-marker__core" style="background:' + opts.coreColor + ';"></div>'
+ '</div>';
} else {
html = '<div class="' + cls + '" style="width:' + s + 'px;height:' + s + 'px;">'
+ '<div class="pn-marker__ring" style="background:' + opts.ringColor + ';"></div>'
+ '<div class="pn-marker__core" style="background:' + opts.coreColor + ';"></div>'
+ '</div>';
}
return L.divIcon({
className: '',
html: html,
iconSize: [s, s],
iconAnchor: [s / 2, s / 2],
});
}
window.PNMarkers = {
scoreColor: scoreColor,
makeIcon: makeIcon,
};
})();

View File

@@ -36,6 +36,7 @@
<meta property="og:image" content="{{ url_for('static', filename='images/logo.png', _external=True) }}">
<meta name="twitter:card" content="summary_large_image">
<script>window.__GEO = {country: "{{ user_country }}", city: "{{ user_city }}"};</script>
{% block head %}{% endblock %}
</head>
<body>