Good changes

This commit is contained in:
Andras Schmelczer 2026-02-10 22:09:46 +00:00
parent d39d1b15fd
commit 1f68ca0512
23 changed files with 670 additions and 289 deletions

View file

@ -23,6 +23,7 @@ services:
POCKETBASE_URL: http://pocketbase:8090
SCREENSHOT_URL: http://screenshot:8002
OLLAMA_URL: http://host.docker.internal:11434
R5_URL: http://r5:8003
depends_on:
pocketbase:
condition: service_healthy
@ -84,12 +85,32 @@ services:
retries: 3
start_period: 5s
r5:
build: ./r5-service
ports:
- "8003:8003"
networks:
- dev-network
volumes:
- ./property-data/transit:/data/transit
- r5-cache:/root/.cache/r5py
environment:
TRANSIT_DATA_DIR: /data/transit
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8003/health"]
interval: 10s
timeout: 5s
retries: 3
start_period: 600s
init: true
volumes:
pb-data:
cargo-registry:
cargo-target:
frontend-node-modules:
screenshot-cache:
r5-cache:
networks:
dev-network:

View file

@ -1,7 +1,6 @@
import { useState, useEffect, useCallback, useMemo } from 'react';
import MapPage, { type ExportState } from './components/map/MapPage';
import DataSourcesPage from './components/data-sources/DataSourcesPage';
import FAQPage from './components/faq/FAQPage';
import LearnPage from './components/learn/LearnPage';
import PricingPage from './components/pricing/PricingPage';
import HomePage from './components/home/HomePage';
import SavedSearchesPage from './components/saved-searches/SavedSearchesPage';
@ -27,10 +26,8 @@ function pageToPath(page: Page): string {
switch (page) {
case 'dashboard':
return '/dashboard';
case 'data-sources':
return '/data-sources';
case 'faq':
return '/faq';
case 'learn':
return '/learn';
case 'saved-searches':
return '/saved';
case 'pricing':
@ -42,8 +39,7 @@ function pageToPath(page: Page): string {
function pathToPage(pathname: string): Page | null {
if (pathname === '/dashboard') return 'dashboard';
if (pathname === '/data-sources') return 'data-sources';
if (pathname === '/faq') return 'faq';
if (pathname === '/learn') return 'learn';
if (pathname === '/saved') return 'saved-searches';
if (pathname === '/pricing') return 'pricing';
if (pathname === '/') return 'home';
@ -85,7 +81,7 @@ export default function App() {
// Backward compat: dashboard params on unknown path
const params = new URLSearchParams(window.location.search);
if (params.has('v') || params.has('f') || params.has('poi') || params.has('tab')) {
if (params.has('lat') || params.has('filter') || params.has('poi') || params.has('tab') || params.has('v') || params.has('f')) {
// Rewrite URL to /dashboard keeping query params
window.history.replaceState({ page: 'dashboard' }, '', `/dashboard${window.location.search}`);
return 'dashboard';
@ -240,10 +236,8 @@ export default function App() {
/>
{activePage === 'home' ? (
<HomePage onOpenDashboard={() => navigateTo('dashboard')} onOpenPricing={() => navigateTo('pricing')} theme={theme} features={features} />
) : activePage === 'data-sources' ? (
<DataSourcesPage />
) : activePage === 'faq' ? (
<FAQPage />
) : activePage === 'learn' ? (
<LearnPage />
) : activePage === 'pricing' ? (
<PricingPage onOpenDashboard={() => navigateTo('dashboard')} />
) : activePage === 'saved-searches' ? (

View file

@ -1,6 +1,8 @@
import { useState, useCallback } from 'react';
import { useState, useCallback, useRef, useEffect } from 'react';
import type { PostcodeGeometry } from '../../types';
import { authHeaders } from '../../lib/api';
import { useIsMobile } from '../../hooks/useIsMobile';
import { SearchIcon } from '../ui/icons/SearchIcon';
export interface SearchedPostcode {
postcode: string;
@ -17,6 +19,29 @@ export default function PostcodeSearch({
const [query, setQuery] = useState('');
const [error, setError] = useState<string | null>(null);
const [loading, setLoading] = useState(false);
const [expanded, setExpanded] = useState(false);
const isMobile = useIsMobile();
const formRef = useRef<HTMLFormElement>(null);
const inputRef = useRef<HTMLInputElement>(null);
// Close on outside click (mobile only)
useEffect(() => {
if (!isMobile || !expanded) return;
const handler = (e: MouseEvent) => {
if (formRef.current && !formRef.current.contains(e.target as Node)) {
setExpanded(false);
}
};
document.addEventListener('mousedown', handler);
return () => document.removeEventListener('mousedown', handler);
}, [isMobile, expanded]);
// Focus input when expanding on mobile
useEffect(() => {
if (isMobile && expanded) {
inputRef.current?.focus();
}
}, [isMobile, expanded]);
const handleSubmit = useCallback(
async (e: React.FormEvent) => {
@ -41,19 +66,39 @@ export default function PostcodeSearch({
onFlyTo(json.latitude, json.longitude, 16);
onPostcodeSearched?.({ postcode: json.postcode, geometry: json.geometry });
setQuery('');
if (isMobile) setExpanded(false);
} catch {
setError('Lookup failed');
} finally {
setLoading(false);
}
},
[query, onFlyTo, onPostcodeSearched]
[query, onFlyTo, onPostcodeSearched, isMobile]
);
// Mobile collapsed state: just a search icon button
if (isMobile && !expanded) {
return (
<form onSubmit={handleSubmit} className="absolute top-3 left-3 z-10 flex flex-col gap-1">
<button
type="button"
onClick={() => setExpanded(true)}
className="absolute top-3 left-3 z-10 p-2 bg-white dark:bg-warm-800 rounded shadow-lg"
aria-label="Search postcode"
>
<SearchIcon className="w-5 h-5 text-warm-600 dark:text-warm-300" />
</button>
);
}
return (
<form
ref={formRef}
onSubmit={handleSubmit}
className="absolute top-3 left-3 z-10 flex flex-col gap-1"
>
<div className="flex shadow-lg rounded overflow-hidden">
<input
ref={inputRef}
type="text"
value={query}
onChange={(e) => {

View file

@ -32,11 +32,11 @@ export function FeatureActions({
active={isPinned}
size="md"
>
<EyeIcon filled={isPinned} />
<EyeIcon filled={isPinned} className="w-7 h-7 md:w-3.5 md:h-3.5" />
</IconButton>
{onAdd && (
<IconButton onClick={() => onAdd(feature.name)} title="Add filter" size="md">
<PlusIcon />
<PlusIcon className="w-7 h-7 md:w-3.5 md:h-3.5" />
</IconButton>
)}
{onRemove && (

View file

@ -1,5 +1,6 @@
import { useState, useCallback, useEffect } from 'react';
import type { AuthUser } from '../../hooks/useAuth';
import { shortenUrl } from '../../lib/api';
import { DownloadIcon } from './icons/DownloadIcon';
import { BookmarkIcon } from './icons/BookmarkIcon';
import { LogoIcon } from './icons/LogoIcon';
@ -12,7 +13,7 @@ import { SpinnerIcon } from './icons/SpinnerIcon';
import UserMenu from './UserMenu';
import MobileMenu from './MobileMenu';
export type Page = 'home' | 'dashboard' | 'data-sources' | 'faq' | 'saved-searches' | 'pricing';
export type Page = 'home' | 'dashboard' | 'saved-searches' | 'pricing';
export default function Header({
activePage,
@ -44,6 +45,7 @@ export default function Header({
isMobile: boolean;
}) {
const [copied, setCopied] = useState(false);
const [sharing, setSharing] = useState(false);
const [menuOpen, setMenuOpen] = useState(false);
// Close menu on Escape
@ -61,17 +63,16 @@ export default function Header({
if (!isMobile) setMenuOpen(false);
}, [isMobile]);
const handleShare = useCallback(() => {
const url = window.location.href;
const copyToClipboard = useCallback((text: string) => {
const onSuccess = () => {
setCopied(true);
setTimeout(() => setCopied(false), 2000);
};
if (navigator.clipboard?.writeText) {
navigator.clipboard.writeText(url).then(onSuccess);
navigator.clipboard.writeText(text).then(onSuccess);
} else {
const ta = document.createElement('textarea');
ta.value = url;
ta.value = text;
ta.style.position = 'fixed';
ta.style.opacity = '0';
document.body.appendChild(ta);
@ -82,6 +83,23 @@ export default function Header({
}
}, []);
const handleShare = useCallback(async () => {
const params = window.location.search.replace(/^\?/, '');
if (!params) {
copyToClipboard(window.location.href);
return;
}
setSharing(true);
try {
const shortUrl = await shortenUrl(params);
copyToClipboard(shortUrl);
} catch {
copyToClipboard(window.location.href);
} finally {
setSharing(false);
}
}, [copyToClipboard]);
const tabClass = (page: Page) =>
`px-3 py-1.5 rounded text-sm font-medium transition-colors ${
activePage === page
@ -98,7 +116,7 @@ export default function Header({
onClick={() => onPageChange('home')}
>
<LogoIcon className="w-5 h-5 text-teal-400" />
<span className="font-semibold text-lg">Perfect Postcodes</span>
<span className="font-semibold text-lg">Perfect Postcode</span>
</button>
{/* Desktop nav */}
@ -115,15 +133,6 @@ export default function Header({
Saved
</button>
)}
<button
className={tabClass('data-sources')}
onClick={() => onPageChange('data-sources')}
>
Data Sources
</button>
<button className={tabClass('faq')} onClick={() => onPageChange('faq')}>
FAQ
</button>
<button className={tabClass('pricing')} onClick={() => onPageChange('pricing')}>
Pricing
</button>
@ -152,9 +161,15 @@ export default function Header({
)}
<button
onClick={handleShare}
className="flex items-center gap-1.5 px-3 py-1.5 rounded bg-navy-800 hover:bg-navy-700 transition-colors text-sm"
disabled={sharing}
className="flex items-center gap-1.5 px-3 py-1.5 rounded bg-navy-800 hover:bg-navy-700 transition-colors text-sm disabled:opacity-50 disabled:cursor-wait"
>
{copied ? (
{sharing ? (
<>
<SpinnerIcon className="w-4 h-4 animate-spin" />
Sharing...
</>
) : copied ? (
<>
<CheckIcon className="w-4 h-4" />
Copied!
@ -255,6 +270,13 @@ export default function Header({
copied={copied}
/>
)}
{/* Mobile "Copied" toast */}
{isMobile && copied && (
<div className="fixed top-14 left-1/2 -translate-x-1/2 z-[60] flex items-center gap-2 px-4 py-2 rounded-lg bg-navy-900 text-white text-sm shadow-lg animate-fade-in">
<CheckIcon className="w-4 h-4 text-teal-400" />
Copied to clipboard
</div>
)}
</header>
);
}

View file

@ -78,10 +78,9 @@ export default function MobileMenu({
</button>
</div>
<nav className="flex-1 flex flex-col gap-1 p-3 overflow-y-auto">
{mobileNavItem('home', 'Home')}
{mobileNavItem('dashboard', 'Dashboard')}
{user && mobileNavItem('saved-searches', 'Saved')}
{mobileNavItem('data-sources', 'Data Sources')}
{mobileNavItem('faq', 'FAQ')}
{mobileNavItem('pricing', 'Pricing')}
{/* Dashboard actions */}

View file

@ -0,0 +1,16 @@
import type { ReactNode } from 'react';
interface PillGroupProps {
children: ReactNode;
className?: string;
}
export function PillGroup({ children, className = '' }: PillGroupProps) {
return (
<div
className={`flex flex-nowrap overflow-x-auto gap-1.5 md:flex-wrap md:overflow-x-visible scrollbar-hide ${className}`}
>
{children}
</div>
);
}

View file

@ -0,0 +1,34 @@
interface PillToggleProps {
label: string;
active: boolean;
onClick: () => void;
/** Visual hint for partial selection (e.g. some children selected) */
indeterminate?: boolean;
size?: 'sm' | 'xs';
}
export function PillToggle({
label,
active,
onClick,
indeterminate,
size = 'sm',
}: PillToggleProps) {
const sizeClasses = size === 'xs' ? 'px-2 py-0.5 text-xs' : 'px-2.5 py-1 text-sm';
const colorClasses = active
? 'bg-teal-600 text-white dark:bg-teal-500'
: indeterminate
? 'bg-teal-100 text-teal-700 dark:bg-teal-900/40 dark:text-teal-300'
: 'bg-warm-100 text-warm-600 hover:bg-warm-200 dark:bg-warm-700 dark:text-warm-300 dark:hover:bg-warm-600';
return (
<button
type="button"
onClick={onClick}
className={`${sizeClasses} ${colorClasses} rounded-full font-medium whitespace-nowrap cursor-pointer`}
>
{label}
</button>
);
}

View file

@ -2,10 +2,11 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Perfect Postcodes — Every neighbourhood in England & Wales</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="theme-color" content="#0f1528" />
<title>Perfect Postcode — Every neighbourhood in England & Wales</title>
<meta name="description" content="Explore property prices, energy ratings, crime stats, school ratings, and more across England & Wales on one interactive map." />
<meta name="x-og-placeholder" content="__PERFECT_POSTCODES_OG_TAGS__" />
<meta name="x-og-placeholder" content="__PERFECT_POSTCODE_OG_TAGS__" />
<script>
(function() {
if (localStorage.getItem('theme') === 'dark') {

View file

@ -0,0 +1,135 @@
"""Extract park and water body polygons from OSM PBF for postcode boundary trimming.
Uses pyosmium's FileProcessor with area assembly to convert OSM ways/relations
into Shapely polygons, reprojects to BNG (EPSG:27700), and saves as parquet.
Reuses the same great-britain-latest.osm.pbf as pois.py.
"""
import argparse
from pathlib import Path
import osmium
import polars as pl
from pyproj import Transformer
from shapely import wkb
from shapely.geometry import MultiPolygon, Polygon
from tqdm import tqdm
from .pois import download_pbf
MIN_AREA_SQM = 5_000 # ~70m x 70m — skip pocket parks and small ponds
# OSM tags that indicate non-residential green/water areas
GREENSPACE_TAGS = {
"leisure": {"park", "nature_reserve", "garden", "common"},
"landuse": {"forest"},
"natural": {"wood", "water", "wetland"},
"waterway": {"riverbank"},
}
_to_bng = Transformer.from_crs("EPSG:4326", "EPSG:27700", always_xy=True)
def _to_bng_polygon(geom):
"""Reproject a WGS84 Shapely geometry to BNG (EPSG:27700)."""
def transform_ring(coords):
xs, ys = zip(*coords)
bng_x, bng_y = _to_bng.transform(list(xs), list(ys))
return list(zip(bng_x, bng_y))
if geom.geom_type == "Polygon":
exterior = transform_ring(geom.exterior.coords)
holes = [transform_ring(h.coords) for h in geom.interiors]
return Polygon(exterior, holes)
elif geom.geom_type == "MultiPolygon":
parts = []
for p in geom.geoms:
exterior = transform_ring(p.exterior.coords)
holes = [transform_ring(h.coords) for h in p.interiors]
parts.append(Polygon(exterior, holes))
return MultiPolygon(parts)
return geom
def _matches_tags(tags):
"""Check if an OSM element's tags match our greenspace/water criteria."""
for key, values in GREENSPACE_TAGS.items():
if key in tags and tags[key] in values:
return True
return False
class GreenspaceHandler(osmium.SimpleHandler):
"""Collects area geometries matching greenspace/water tags."""
def __init__(self, progress):
super().__init__()
self._wkb_factory = osmium.geom.WKBFactory()
self._progress = progress
self.geometries = []
def area(self, a):
self._progress.update(1)
if not _matches_tags(a.tags):
return
try:
wkb_data = self._wkb_factory.create_multipolygon(a)
geom = wkb.loads(wkb_data, hex=True)
except Exception:
return
if geom.is_empty or not geom.is_valid:
return
# Reproject to BNG for area calculation
bng_geom = _to_bng_polygon(geom)
if bng_geom.area < MIN_AREA_SQM:
return
self.geometries.append(bng_geom.wkb)
def main():
parser = argparse.ArgumentParser(
description="Extract park/water polygons from OSM PBF"
)
parser.add_argument(
"--output", type=Path, required=True, help="Output parquet file path"
)
parser.add_argument(
"--pbf", type=Path, default=None, help="Path to existing PBF file"
)
args = parser.parse_args()
if args.pbf and args.pbf.exists():
pbf_file = args.pbf
print(f"Using existing PBF: {pbf_file}")
else:
pbf_file = Path("data/great-britain-latest.osm.pbf")
if not pbf_file.exists():
download_pbf(pbf_file)
else:
print(f"Using cached PBF: {pbf_file}")
print("Extracting greenspace/water areas from PBF (two-pass area assembly)...")
with tqdm(unit=" areas", unit_scale=True, desc="Processing", smoothing=0.05) as progress:
handler = GreenspaceHandler(progress)
handler.apply_file(str(pbf_file), locations=True)
print(f"Found {len(handler.geometries)} greenspace/water polygons >= {MIN_AREA_SQM} sqm")
# Merge overlapping geometries per 10km grid cell for efficiency
if handler.geometries:
# Save WKB geometries directly
df = pl.DataFrame({"geometry": handler.geometries})
args.output.parent.mkdir(parents=True, exist_ok=True)
df.write_parquet(args.output)
print(f"Saved to {args.output}")
else:
print("No geometries found — skipping output")
if __name__ == "__main__":
main()

View file

@ -74,6 +74,18 @@ def _build_wide(
iod = pl.scan_parquet(iod_path)
wide = wide.join(iod, left_on="lsoa21", right_on="LSOA code (2021)", how="left")
# Invert deprivation scores so that higher values = less deprived (better)
iod_score_cols = [
"Education, Skills and Training Score",
"Income Score (rate)",
"Employment Score (rate)",
"Health Deprivation and Disability Score",
"Living Environment Score",
"Indoors Sub-domain Score",
"Outdoors Sub-domain Score",
]
wide = wide.with_columns(*(pl.col(c).max() - pl.col(c) for c in iod_score_cols))
ethnicity = pl.scan_parquet(ethnicity_path)
wide = wide.join(
ethnicity,
@ -158,10 +170,20 @@ def _build_wide(
geosure = pl.scan_parquet(geosure_path)
wide = wide.join(geosure, on="postcode", how="left")
# Use built_form (Terraced, Semi-detached) when available, otherwise epc_property_type
# Derive property_type: prefer EPC data, fall back to price-paid.
# For Houses, use built_form (e.g. Semi-Detached, Mid-Terrace) for finer detail.
bad_built_form = pl.col("built_form").is_null() | pl.col("built_form").is_in(
["NO DATA!", "Not Recorded"]
)
has_epc = pl.col("epc_property_type").is_not_null()
is_house = pl.col("epc_property_type") == "House"
wide = wide.with_columns(
pl.when(pl.col("pp_property_type").is_in(["Terraced", "Semi-Detached"]))
pl.when(has_epc & is_house & ~bad_built_form)
.then(pl.col("built_form"))
.when(has_epc & is_house)
.then(pl.col("pp_property_type"))
.when(has_epc)
.then(pl.col("epc_property_type"))
.otherwise(pl.col("pp_property_type"))
.alias("property_type")
)

View file

@ -25,6 +25,7 @@ dependencies = [
"rasterio>=1.5.0",
"pyproj>=3.7.2",
"pyshp>=2.3.0",
"folium>=0.20.0",
]
[tool.uv]

View file

@ -1,5 +1,7 @@
FROM python:3.12-slim
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
# r5py needs a JVM to run the R5 routing engine
RUN apt-get update && \
apt-get install -y --no-install-recommends openjdk-21-jre-headless curl libexpat1 libgdal-dev && \
@ -7,8 +9,8 @@ RUN apt-get update && \
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY pyproject.toml .
RUN uv pip install --system --no-cache -r pyproject.toml
COPY main.py .

11
r5-service/pyproject.toml Normal file
View file

@ -0,0 +1,11 @@
[project]
name = "r5-service"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
"r5py>=0.8",
"fastapi>=0.115",
"uvicorn>=0.34",
"geopandas>=1.0",
"shapely>=2.0",
]

View file

@ -1,5 +0,0 @@
r5py>=0.8
fastapi>=0.115
uvicorn>=0.34
geopandas>=1.0
shapely>=2.0

View file

@ -17,39 +17,37 @@ export class ScreenshotCache {
/**
* Build a cache key by quantizing view params and hashing.
* - lat/lng quantized to 2 decimal places
* - lat/lon quantized to 2 decimal places
* - zoom quantized to integer
* - filters sorted alphabetically
* - filters and POI categories sorted alphabetically
*/
buildKey(params: Record<string, string>): string {
buildKey(params: URLSearchParams): string {
const normalized: Record<string, string> = {};
// Parse and quantize the view param (lat,lng,zoom)
if (params.v) {
const parts = params.v.split(',');
if (parts.length === 3) {
const lat = parseFloat(parts[0]).toFixed(2);
const lng = parseFloat(parts[1]).toFixed(2);
const zoom = Math.round(parseFloat(parts[2])).toString();
normalized.v = `${lat},${lng},${zoom}`;
} else {
normalized.v = params.v;
}
// Quantize lat/lon/zoom
const lat = params.get('lat');
const lon = params.get('lon');
const zoom = params.get('zoom');
if (lat && lon && zoom) {
normalized.lat = parseFloat(lat).toFixed(2);
normalized.lon = parseFloat(lon).toFixed(2);
normalized.zoom = Math.round(parseFloat(zoom)).toString();
}
// Sort filters
if (params.f) {
const segments = params.f.split(',').sort();
normalized.f = segments.join(',');
const filters = params.getAll('filter').sort();
if (filters.length > 0) {
normalized.filters = filters.join(',');
}
if (params.poi) {
const cats = params.poi.split(',').sort();
normalized.poi = cats.join(',');
// Sort POI categories
const pois = params.getAll('poi').sort();
if (pois.length > 0) {
normalized.poi = pois.join(',');
}
if (params.tab) {
normalized.tab = params.tab;
if (params.get('tab')) {
normalized.tab = params.get('tab')!;
}
const input = JSON.stringify(normalized);

View file

@ -3,8 +3,18 @@ import { ScreenshotCache } from './cache.js';
import { takeScreenshot, checkWebGL, closeBrowser } from './screenshot.js';
const PORT = parseInt(process.env.PORT || '8002', 10);
const APP_URL = process.env.APP_URL || 'http://localhost:8001';
const CACHE_DIR = process.env.CACHE_DIR || '/cache';
const APP_URL = process.env.APP_URL;
const CACHE_DIR = process.env.CACHE_DIR;
if (!APP_URL) {
console.error('Error: APP_URL environment variable is required');
process.exit(1);
}
if (!CACHE_DIR) {
console.error('Error: CACHE_DIR environment variable is required');
process.exit(1);
}
const cache = new ScreenshotCache(CACHE_DIR);
const app = express();
@ -24,15 +34,26 @@ app.get('/debug', async (_req, res) => {
app.get('/screenshot', async (req, res) => {
try {
const params: Record<string, string> = {};
for (const key of ['v', 'f', 'poi', 'tab', 'og']) {
const qs = new URLSearchParams();
for (const key of ['lat', 'lon', 'zoom', 'tab', 'og']) {
const val = req.query[key];
if (typeof val === 'string' && val) {
params[key] = val;
qs.set(key, val);
}
}
// Repeated params: filter, poi
for (const key of ['filter', 'poi']) {
const val = req.query[key];
if (typeof val === 'string' && val) {
qs.append(key, val);
} else if (Array.isArray(val)) {
for (const v of val) {
if (typeof v === 'string' && v) qs.append(key, v);
}
}
}
const cacheKey = cache.buildKey(params);
const cacheKey = cache.buildKey(qs);
// Check cache first
const cached = cache.get(cacheKey);
@ -45,7 +66,6 @@ app.get('/screenshot', async (req, res) => {
}
// Build the URL for the frontend in screenshot mode
const qs = new URLSearchParams(params);
qs.set('screenshot', '1');
const url = `${APP_URL}/?${qs}`;

1
server-rs/Cargo.lock generated
View file

@ -2374,6 +2374,7 @@ dependencies = [
"parking_lot",
"pmtiles",
"polars",
"rand 0.9.2",
"rayon",
"reqwest",
"rust_xlsxwriter",

View file

@ -25,6 +25,7 @@ reqwest = { version = "0.12", features = ["rustls-tls", "json"] }
urlencoding = "2"
rust_xlsxwriter = "0.79"
pmtiles = { version = "0.12", features = ["mmap-async-tokio"] }
rand = "0.9"
[lints.clippy]
min_ident_chars = "warn"

View file

@ -18,7 +18,7 @@ pub struct FeatureConfig {
pub description: &'static str,
/// Longer description explaining methodology, data source, and caveats
pub detail: &'static str,
/// Data source slug for linking to /data-sources#<slug>
/// Data source slug for linking to /learn#<slug>
pub source: &'static str,
/// Display prefix (e.g. "£")
pub prefix: &'static str,
@ -46,7 +46,7 @@ pub struct EnumFeatureConfig {
pub description: &'static str,
/// Longer description explaining methodology, data source, and caveats
pub detail: &'static str,
/// Data source slug for linking to /data-sources#<slug>
/// Data source slug for linking to /learn#<slug>
pub source: &'static str,
}
@ -114,6 +114,20 @@ pub static FEATURE_GROUPS: &[FeatureGroup] = &[
suffix: "",
raw: false,
},
FeatureConfig {
name: "Est. price per sqm",
bounds: Bounds::Percentile {
low: 0.0,
high: 98.0,
},
step: 100.0,
description: "Estimated current price divided by total floor area",
detail: "Calculated by dividing the inflation-adjusted estimated current price by the total floor area from the EPC certificate. Provides a more up-to-date price-per-area comparison than the historical sale price per sqm.",
source: "price-paid",
prefix: "£",
suffix: "",
raw: false,
},
FeatureConfig {
name: "Total floor area (sqm)",
bounds: Bounds::Percentile {
@ -257,8 +271,8 @@ pub static FEATURE_GROUPS: &[FeatureGroup] = &[
high: 98.0,
},
step: 0.1,
description: "IoD education deprivation score for the local area",
detail: "From the English Indices of Deprivation. Measures deprivation in education, skills and training in the local area (LSOA). Higher scores indicate greater deprivation. Combines children/young people sub-domain (school attainment, entry to higher education) and adult skills sub-domain (adult qualifications, English language proficiency).",
description: "IoD education score for the local area (higher = better)",
detail: "From the English Indices of Deprivation (inverted so higher = better). Measures education, skills and training quality in the local area (LSOA). Higher scores indicate less deprivation. Combines children/young people sub-domain (school attainment, entry to higher education) and adult skills sub-domain (adult qualifications, English language proficiency).",
source: "iod",
prefix: "",
suffix: "",
@ -301,8 +315,8 @@ pub static FEATURE_GROUPS: &[FeatureGroup] = &[
name: "Income Score (rate)",
bounds: Bounds::Fixed { min: 0.0, max: 0.6 },
step: 0.01,
description: "Proportion of the population experiencing income deprivation",
detail: "From the English Indices of Deprivation. The proportion of the local population experiencing deprivation relating to low income. Includes people on Income Support, income-based Jobseeker's Allowance, income-based Employment and Support Allowance, Pension Credit, Working Tax Credit and Child Tax Credit, Universal Credit, and asylum seekers.",
description: "Income deprivation rate, inverted (higher = less deprived)",
detail: "From the English Indices of Deprivation (inverted so higher = better). Higher values indicate less income deprivation. Based on Income Support, income-based Jobseeker's Allowance, income-based Employment and Support Allowance, Pension Credit, Working Tax Credit and Child Tax Credit, Universal Credit, and asylum seekers.",
source: "iod",
prefix: "",
suffix: "",
@ -312,8 +326,8 @@ pub static FEATURE_GROUPS: &[FeatureGroup] = &[
name: "Employment Score (rate)",
bounds: Bounds::Fixed { min: 0.0, max: 0.4 },
step: 0.01,
description: "Proportion of the working-age population involuntarily excluded from work",
detail: "From the English Indices of Deprivation. The proportion of the working-age population involuntarily excluded from the labour market. Includes claimants of Jobseeker's Allowance, Employment and Support Allowance, Incapacity Benefit, Severe Disablement Allowance, Carer's Allowance, and relevant Universal Credit claimants.",
description: "Employment deprivation rate, inverted (higher = less deprived)",
detail: "From the English Indices of Deprivation (inverted so higher = better). Higher values indicate less employment deprivation. Based on claimants of Jobseeker's Allowance, Employment and Support Allowance, Incapacity Benefit, Severe Disablement Allowance, Carer's Allowance, and relevant Universal Credit claimants.",
source: "iod",
prefix: "",
suffix: "",
@ -326,8 +340,8 @@ pub static FEATURE_GROUPS: &[FeatureGroup] = &[
high: 98.0,
},
step: 0.1,
description: "Risk of premature death and quality of life impairment",
detail: "From the English Indices of Deprivation. Measures the risk of premature death and impairment of quality of life through poor physical or mental health. Derived from years of potential life lost, comparative illness and disability ratio, acute morbidity, and mood and anxiety disorders.",
description: "Health and disability score (higher = better health outcomes)",
detail: "From the English Indices of Deprivation (inverted so higher = better). Higher scores indicate lower risk of premature death and better quality of life. Derived from years of potential life lost, comparative illness and disability ratio, acute morbidity, and mood and anxiety disorders.",
source: "iod",
prefix: "",
suffix: "",
@ -340,8 +354,8 @@ pub static FEATURE_GROUPS: &[FeatureGroup] = &[
high: 98.0,
},
step: 0.1,
description: "Quality of the local indoor and outdoor environment",
detail: "From the English Indices of Deprivation. Measures deprivation in the quality of the local environment. Combines the Indoors sub-domain (housing quality, central heating, housing conditions) and Outdoors sub-domain (air quality, road traffic accidents). Higher scores indicate poorer living environments.",
description: "Quality of the local indoor and outdoor environment (higher = better)",
detail: "From the English Indices of Deprivation (inverted so higher = better). Measures the quality of the local environment. Combines the Indoors sub-domain (housing quality, central heating, housing conditions) and Outdoors sub-domain (air quality, road traffic accidents). Higher scores indicate better living environments.",
source: "iod",
prefix: "",
suffix: "",
@ -354,8 +368,8 @@ pub static FEATURE_GROUPS: &[FeatureGroup] = &[
high: 98.0,
},
step: 0.1,
description: "Housing quality and conditions in the local area",
detail: "From the English Indices of Deprivation, Living Environment domain. Measures the quality of housing stock: houses without central heating, housing in poor condition, and houses failing Decent Homes standards. Higher scores indicate worse housing conditions.",
description: "Housing quality and conditions (higher = better)",
detail: "From the English Indices of Deprivation, Living Environment domain (inverted so higher = better). Measures the quality of housing stock: central heating availability, housing condition, and Decent Homes standards. Higher scores indicate better housing conditions.",
source: "iod",
prefix: "",
suffix: "",
@ -368,8 +382,8 @@ pub static FEATURE_GROUPS: &[FeatureGroup] = &[
high: 98.0,
},
step: 0.1,
description: "Air quality and road safety in the local area",
detail: "From the English Indices of Deprivation, Living Environment domain. Measures the outdoor living environment quality through air quality indicators and road traffic accident casualties involving pedestrians and cyclists. Higher scores indicate poorer outdoor environments.",
description: "Air quality and road safety (higher = better)",
detail: "From the English Indices of Deprivation, Living Environment domain (inverted so higher = better). Measures the outdoor living environment quality through air quality indicators and road traffic accident casualties involving pedestrians and cyclists. Higher scores indicate better outdoor environments.",
source: "iod",
prefix: "",
suffix: "",

View file

@ -8,7 +8,7 @@ use axum::response::Response;
use crate::state::AppState;
const OG_PLACEHOLDER: &str = r#"<meta name="x-og-placeholder" content="__PERFECT_POSTCODES_OG_TAGS__"/>"#;
const OG_PLACEHOLDER: &str = r#"<meta name="x-og-placeholder" content="__PERFECT_POSTCODE_OG_TAGS__"/>"#;
pub async fn og_middleware(request: Request, next: Next) -> Response {
// Capture the query string before passing the request through
@ -47,14 +47,14 @@ pub async fn og_middleware(request: Request, next: Next) -> Response {
};
let og_tags = format!(
r#"<meta property="og:title" content="Perfect Postcodes Every neighbourhood in England & Wales" />
r#"<meta property="og:title" content="Perfect Postcode Every neighbourhood in England & Wales" />
<meta property="og:description" content="Explore property prices, energy ratings, crime stats, school ratings, and more across England & Wales on one interactive map." />
<meta property="og:type" content="website" />
<meta property="og:image" content="{og_image_url}" />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:title" content="Perfect Postcodes — Every neighbourhood in England & Wales" />
<meta name="twitter:title" content="Perfect Postcode — Every neighbourhood in England & Wales" />
<meta name="twitter:description" content="Explore property prices, energy ratings, crime stats, school ratings, and more across England & Wales on one interactive map." />"#
);

View file

@ -93,21 +93,38 @@ fn extract_filter_feature_names(filters_str: Option<&str>) -> Vec<String> {
names
}
/// Build frontend-style query params for screenshot/dashboard URLs.
fn build_frontend_params(
center_lat: f64,
center_lon: f64,
zoom: f64,
filters_str: Option<&str>,
) -> String {
let mut parts = vec![
format!("lat={:.4}", center_lat),
format!("lon={:.4}", center_lon),
format!("zoom={:.1}", zoom),
];
if let Some(fs) = filters_str {
if !fs.is_empty() {
for entry in fs.split(',') {
if !entry.is_empty() {
parts.push(format!("filter={}", urlencoding::encode(entry.trim())));
}
}
}
}
parts.join("&")
}
/// Fetch a screenshot image from the screenshot service for Excel export.
async fn fetch_screenshot(
state: &AppState,
view_param: &str,
filters_str: Option<&str>,
frontend_params: &str,
) -> Option<Vec<u8>> {
let screenshot_base = &state.screenshot_url;
let mut params = vec![format!("v={}", urlencoding::encode(view_param))];
if let Some(fs) = filters_str {
if !fs.is_empty() {
params.push(format!("f={}", urlencoding::encode(fs)));
}
}
let url = format!("{}/screenshot?{}", screenshot_base, params.join("&"));
let url = format!("{}/screenshot?{}", screenshot_base, frontend_params);
match state.http_client.get(&url).send().await {
Ok(resp) if resp.status().is_success() => match resp.bytes().await {
@ -147,7 +164,7 @@ pub async fn get_export(
let public_url = state.public_url.clone();
// Compute view param for screenshot and dashboard URL
// Compute view center for screenshot and dashboard URL
let center_lat = (south + north) / 2.0;
let center_lon = (west + east) / 2.0;
let lat_span = north - south;
@ -156,10 +173,11 @@ pub async fn get_export(
} else {
12.0
};
let view_param = format!("{:.4},{:.4},{:.1}", center_lat, center_lon, zoom);
let frontend_params =
build_frontend_params(center_lat, center_lon, zoom, filters_str.as_deref());
// Fetch screenshot (async, before spawn_blocking)
let screenshot_bytes = fetch_screenshot(&state, &view_param, filters_str.as_deref()).await;
let screenshot_bytes = fetch_screenshot(&state, &frontend_params).await;
// Build feature name → description map from the precomputed features response
let feature_descriptions: FxHashMap<String, String> = state
@ -273,9 +291,50 @@ pub async fn get_export(
ordered
};
// Build Excel workbook
// Filter-only feature indices for the Selected sheet
let filter_feature_indices: Vec<usize> = filter_feature_names
.iter()
.filter_map(|name| state.feature_name_to_index.get(name.as_str()).copied())
.collect();
// Build feature unit map (feat_idx → (prefix, suffix)) for number formatting
let feature_units: FxHashMap<usize, (&str, &str)> = state
.features_response
.groups
.iter()
.flat_map(|group| &group.features)
.filter_map(|feat| match feat {
FeatureInfo::Numeric {
name,
prefix,
suffix,
..
} => {
let idx = state.feature_name_to_index.get(name.as_str())?;
Some((*idx, (*prefix, *suffix)))
}
_ => None,
})
.collect();
// Build Excel number formats per feature index for unit display
let mut feat_num_fmts: FxHashMap<usize, Format> = FxHashMap::default();
for &feat_idx in &all_feature_indices {
if let Some(&(prefix, suffix)) = feature_units.get(&feat_idx) {
if prefix.is_empty() && suffix.is_empty() {
continue;
}
let num_fmt_str = if !prefix.is_empty() {
format!("\"{}\"#,##0", prefix)
} else {
format!("#,##0.0\"{}\"", suffix)
};
feat_num_fmts.insert(feat_idx, Format::new().set_num_format(&num_fmt_str));
}
}
// Build Excel workbook with two sheets
let mut workbook = Workbook::new();
let sheet = workbook.add_worksheet();
// Formats
let header_fmt = Format::new()
@ -300,80 +359,92 @@ pub async fn get_export(
.set_font_color("#666666")
.set_align(FormatAlign::Left);
// Row 0: "View on Perfect Postcodes" link
let mut dashboard_url = format!("{}/", public_url);
let mut query_parts: Vec<String> = Vec::new();
query_parts.push(format!("v={}", view_param));
if let Some(ref fs) = filters_str {
if !fs.is_empty() {
query_parts.push(format!("f={}", urlencoding::encode(fs)));
}
}
if !query_parts.is_empty() {
dashboard_url.push('?');
dashboard_url.push_str(&query_parts.join("&"));
}
// Dashboard URL
let dashboard_url = format!("{}/?{}", public_url, frontend_params);
// Sheet 1: "Selected" (filter features only) with link + screenshot
// Sheet 2: "All Data" (all features)
let sheet_configs: [(&str, &[usize], bool); 2] = [
("Selected", &filter_feature_indices, true),
("All Data", &all_feature_indices, false),
];
for (sheet_name, feat_indices, include_header) in &sheet_configs {
let sheet = workbook.add_worksheet();
sheet
.write_url(0, 0, Url::new(&dashboard_url).set_text("View on Perfect Postcodes"))
.map_err(|err| format!("Failed to write URL: {err}"))?;
.set_name(*sheet_name)
.map_err(|e| format!("Failed to set sheet name: {e}"))?;
let mut current_row = 0u32;
if *include_header {
// URL row
sheet
.write_url(
0,
0,
Url::new(&dashboard_url).set_text("View on Perfect Postcode"),
)
.map_err(|e| format!("Failed to write URL: {e}"))?;
sheet
.set_row_format(0, &link_fmt)
.map_err(|err| format!("Failed to set row format: {err}"))?;
.map_err(|e| format!("Failed to set row format: {e}"))?;
current_row = 1;
// Row 1: screenshot (if available)
let mut current_row = 1u32;
// Screenshot
if let Some(ref img_bytes) = screenshot_bytes {
match Image::new_from_buffer(img_bytes) {
Ok(mut image) => {
// Scale image to fit: ~400px wide, auto height preserving aspect ratio
image = image.set_scale_to_size(400, 300, true);
sheet
.insert_image(current_row, 0, &image)
.map_err(|err| format!("Failed to insert screenshot: {err}"))?;
// Set row height to accommodate the image
.map_err(|e| format!("Failed to insert screenshot: {e}"))?;
sheet
.set_row_height(current_row, IMAGE_ROW_HEIGHT)
.map_err(|err| format!("Failed to set image row height: {err}"))?;
.map_err(|e| format!("Failed to set image row height: {e}"))?;
current_row += 1;
}
Err(err) => {
warn!("Failed to parse screenshot for export: {err}");
// Skip image row, don't leave a gap
}
}
}
// Leave a blank row between image and header
// Blank row between image and header
current_row += 1;
}
// Header row
let header_row = current_row;
sheet
.write_string_with_format(header_row, 0, "Postcode", &header_fmt)
.map_err(|err| format!("Failed to write header: {err}"))?;
.map_err(|e| format!("Failed to write header: {e}"))?;
sheet
.write_string_with_format(header_row, 1, "Properties", &header_fmt)
.map_err(|err| format!("Failed to write header: {err}"))?;
.map_err(|e| format!("Failed to write header: {e}"))?;
for (col_offset, &feat_idx) in all_feature_indices.iter().enumerate() {
for (col_offset, &feat_idx) in feat_indices.iter().enumerate() {
let col = (col_offset + 2) as u16;
sheet
.write_string_with_format(header_row, col, &feature_names[feat_idx], &header_fmt)
.map_err(|err| format!("Failed to write header: {err}"))?;
.write_string_with_format(
header_row,
col,
&feature_names[feat_idx],
&header_fmt,
)
.map_err(|e| format!("Failed to write header: {e}"))?;
}
// Description row (below header)
// Description row
let desc_row = header_row + 1;
// Empty descriptions for Postcode and Properties columns
sheet
.write_string_with_format(desc_row, 0, "", &desc_fmt)
.map_err(|err| format!("Failed to write desc: {err}"))?;
.map_err(|e| format!("Failed to write desc: {e}"))?;
sheet
.write_string_with_format(desc_row, 1, "Count of properties", &desc_fmt)
.map_err(|err| format!("Failed to write desc: {err}"))?;
.map_err(|e| format!("Failed to write desc: {e}"))?;
for (col_offset, &feat_idx) in all_feature_indices.iter().enumerate() {
for (col_offset, &feat_idx) in feat_indices.iter().enumerate() {
let col = (col_offset + 2) as u16;
let desc = feature_descriptions
.get(&feature_names[feat_idx])
@ -381,36 +452,39 @@ pub async fn get_export(
.unwrap_or("");
sheet
.write_string_with_format(desc_row, col, desc, &desc_fmt)
.map_err(|err| format!("Failed to write desc: {err}"))?;
.map_err(|e| format!("Failed to write desc: {e}"))?;
}
// Write data rows (starting after description row)
// Data rows
let data_start_row = desc_row + 1;
for (row_offset, (pc_idx, agg)) in postcode_aggs.iter().enumerate() {
let row = data_start_row + row_offset as u32;
sheet
.write_string(row, 0, &postcode_data.postcodes[*pc_idx])
.map_err(|err| format!("Failed to write postcode: {err}"))?;
.map_err(|e| format!("Failed to write postcode: {e}"))?;
sheet
.write_number(row, 1, agg.count as f64)
.map_err(|err| format!("Failed to write count: {err}"))?;
.map_err(|e| format!("Failed to write count: {e}"))?;
for (col_offset, &feat_idx) in all_feature_indices.iter().enumerate() {
for (col_offset, &feat_idx) in feat_indices.iter().enumerate() {
let col = (col_offset + 2) as u16;
if enum_indices.contains_key(&feat_idx) {
if let Some(freqs) = agg.enum_freqs.get(&feat_idx) {
if let Some((&mode_bits, _)) = freqs.iter().max_by_key(|(_, &count)| count)
if let Some((&mode_bits, _)) =
freqs.iter().max_by_key(|(_, &count)| count)
{
let mode_f32 = f32::from_bits(mode_bits);
let mode_idx = mode_f32 as usize;
if let Some(values) = enum_values.get(&feat_idx) {
if mode_idx < values.len() {
sheet.write_string(row, col, &values[mode_idx]).map_err(
|err| format!("Failed to write enum value: {err}"),
)?;
sheet
.write_string(row, col, &values[mode_idx])
.map_err(|e| {
format!("Failed to write enum value: {e}")
})?;
}
}
}
@ -418,19 +492,27 @@ pub async fn get_export(
} else {
let fc = agg.finite_counts[feat_idx];
if fc > 0 {
let mean = agg.sums[feat_idx] / fc as f64;
let mean = (agg.sums[feat_idx] / fc as f64 * 100.0).round() / 100.0;
if let Some(fmt) = feat_num_fmts.get(&feat_idx) {
sheet
.write_number(row, col, mean)
.map_err(|err| format!("Failed to write numeric value: {err}"))?;
.write_number_with_format(row, col, mean, fmt)
.map_err(|e| {
format!("Failed to write numeric value: {e}")
})?;
} else {
sheet.write_number(row, col, mean).map_err(|e| {
format!("Failed to write numeric value: {e}")
})?;
}
}
}
}
}
// If sampled, add a note at the bottom
// Sample note
if was_sampled {
let note_row = data_start_row + postcode_aggs.len() as u32 + 1;
let total_cols = (all_feature_indices.len() + 2) as u16;
let total_cols = (feat_indices.len() + 2) as u16;
sheet
.merge_range(
note_row,
@ -443,18 +525,19 @@ pub async fn get_export(
),
&note_fmt,
)
.map_err(|err| format!("Failed to write note: {err}"))?;
.map_err(|e| format!("Failed to write note: {e}"))?;
}
// Column widths
sheet.set_column_width(0, 12).ok();
sheet.set_column_width(1, 12).ok();
for col_offset in 0..all_feature_indices.len() {
for col_offset in 0..feat_indices.len() {
let col = (col_offset + 2) as u16;
let feat_name = &feature_names[all_feature_indices[col_offset]];
let feat_name = &feature_names[feat_indices[col_offset]];
let width = (feat_name.len() as f64 * 1.1).clamp(10.0, 30.0);
sheet.set_column_width(col, width).ok();
}
}
let buf = workbook
.save_to_buffer()
@ -485,7 +568,7 @@ pub async fn get_export(
),
(
header::CONTENT_DISPOSITION,
"attachment; filename=\"perfect-postcodes-export.xlsx\"",
"attachment; filename=\"perfect-postcode-export.xlsx\"",
),
],
bytes,

View file

@ -1,53 +1,19 @@
use std::sync::Arc;
use axum::extract::Query;
use axum::http::{header, StatusCode};
use axum::http::{header, StatusCode, Uri};
use axum::response::IntoResponse;
use tracing::{info, warn};
use crate::state::AppState;
#[derive(serde::Deserialize)]
pub struct ScreenshotQuery {
#[serde(rename = "v")]
view: Option<String>,
#[serde(rename = "f")]
filters: Option<String>,
poi: Option<String>,
tab: Option<String>,
/// When "1", renders the OG heading overlay on the screenshot
og: Option<String>,
}
pub async fn get_screenshot(
state: Arc<AppState>,
Query(query): Query<ScreenshotQuery>,
) -> impl IntoResponse {
pub async fn get_screenshot(state: Arc<AppState>, uri: Uri) -> impl IntoResponse {
let screenshot_base = &state.screenshot_url;
let mut params = Vec::new();
if query.og.as_deref() == Some("1") {
params.push("og=1".to_string());
}
if let Some(ref val) = query.view {
params.push(format!("v={}", urlencoding::encode(val)));
}
if let Some(ref val) = query.filters {
params.push(format!("f={}", urlencoding::encode(val)));
}
if let Some(ref val) = query.poi {
params.push(format!("poi={}", urlencoding::encode(val)));
}
if let Some(ref val) = query.tab {
params.push(format!("tab={}", urlencoding::encode(val)));
}
let qs = if params.is_empty() {
String::new()
} else {
format!("?{}", params.join("&"))
};
let url = format!("{}/screenshot{}", screenshot_base, qs);
let qs = uri
.query()
.map(|q| format!("?{q}"))
.unwrap_or_default();
let url = format!("{screenshot_base}/screenshot{qs}");
info!("Proxying screenshot request to: {}", url);
match state.http_client.get(&url).send().await {