location search beta 1
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 1m3s
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 1m3s
This commit is contained in:
@@ -5,6 +5,7 @@ Uses real data from UK Government Compare School Performance downloads.
|
||||
"""
|
||||
|
||||
from contextlib import asynccontextmanager
|
||||
import pandas as pd
|
||||
from fastapi import FastAPI, HTTPException, Query
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.responses import FileResponse
|
||||
@@ -13,7 +14,7 @@ from typing import Optional
|
||||
|
||||
from .config import settings
|
||||
from .schemas import METRIC_DEFINITIONS, RANKING_COLUMNS, SCHOOL_COLUMNS
|
||||
from .data_loader import load_school_data, clear_cache
|
||||
from .data_loader import load_school_data, clear_cache, geocode_single_postcode, geocode_postcodes_bulk, haversine_distance
|
||||
from .utils import clean_for_json
|
||||
|
||||
|
||||
@@ -54,11 +55,25 @@ async def root():
|
||||
return FileResponse(settings.frontend_dir / "index.html")
|
||||
|
||||
|
||||
@app.get("/compare")
|
||||
async def serve_compare():
|
||||
"""Serve the frontend for /compare route (SPA routing)."""
|
||||
return FileResponse(settings.frontend_dir / "index.html")
|
||||
|
||||
|
||||
@app.get("/rankings")
|
||||
async def serve_rankings():
|
||||
"""Serve the frontend for /rankings route (SPA routing)."""
|
||||
return FileResponse(settings.frontend_dir / "index.html")
|
||||
|
||||
|
||||
@app.get("/api/schools")
|
||||
async def get_schools(
|
||||
search: Optional[str] = Query(None, description="Search by school name"),
|
||||
local_authority: Optional[str] = Query(None, description="Filter by local authority"),
|
||||
school_type: Optional[str] = Query(None, description="Filter by school type"),
|
||||
postcode: Optional[str] = Query(None, description="Search near postcode"),
|
||||
radius: float = Query(5.0, ge=0.1, le=50, description="Search radius in miles"),
|
||||
page: int = Query(1, ge=1, description="Page number"),
|
||||
page_size: int = Query(None, ge=1, le=100, description="Results per page"),
|
||||
):
|
||||
@@ -66,6 +81,7 @@ async def get_schools(
|
||||
Get list of unique primary schools with pagination.
|
||||
|
||||
Returns paginated results with total count for efficient loading.
|
||||
Supports location-based search using postcode.
|
||||
"""
|
||||
df = load_school_data()
|
||||
|
||||
@@ -80,9 +96,45 @@ async def get_schools(
|
||||
latest_year = df.groupby('urn')['year'].max().reset_index()
|
||||
df_latest = df.merge(latest_year, on=['urn', 'year'])
|
||||
|
||||
available_cols = [c for c in SCHOOL_COLUMNS if c in df_latest.columns]
|
||||
# Include lat/long in columns for location search
|
||||
location_cols = ['latitude', 'longitude']
|
||||
available_cols = [c for c in SCHOOL_COLUMNS + location_cols if c in df_latest.columns]
|
||||
schools_df = df_latest[available_cols].drop_duplicates(subset=['urn'])
|
||||
|
||||
# Location-based search
|
||||
search_coords = None
|
||||
if postcode:
|
||||
coords = geocode_single_postcode(postcode)
|
||||
if coords:
|
||||
search_coords = coords
|
||||
schools_df = schools_df.copy()
|
||||
|
||||
# Geocode school postcodes on-demand if not already cached
|
||||
if 'postcode' in schools_df.columns:
|
||||
unique_postcodes = schools_df['postcode'].dropna().unique().tolist()
|
||||
geocoded = geocode_postcodes_bulk(unique_postcodes)
|
||||
|
||||
# Add lat/long from geocoded data
|
||||
schools_df['latitude'] = schools_df['postcode'].apply(
|
||||
lambda pc: geocoded.get(str(pc).strip().upper(), (None, None))[0] if pd.notna(pc) else None
|
||||
)
|
||||
schools_df['longitude'] = schools_df['postcode'].apply(
|
||||
lambda pc: geocoded.get(str(pc).strip().upper(), (None, None))[1] if pd.notna(pc) else None
|
||||
)
|
||||
|
||||
# Filter by distance
|
||||
def calc_distance(row):
|
||||
if pd.isna(row.get('latitude')) or pd.isna(row.get('longitude')):
|
||||
return float('inf')
|
||||
return haversine_distance(
|
||||
search_coords[0], search_coords[1],
|
||||
row['latitude'], row['longitude']
|
||||
)
|
||||
|
||||
schools_df['distance'] = schools_df.apply(calc_distance, axis=1)
|
||||
schools_df = schools_df[schools_df['distance'] <= radius]
|
||||
schools_df = schools_df.sort_values('distance')
|
||||
|
||||
# Apply filters
|
||||
if search:
|
||||
search_lower = search.lower()
|
||||
@@ -103,12 +155,18 @@ async def get_schools(
|
||||
end_idx = start_idx + page_size
|
||||
schools_df = schools_df.iloc[start_idx:end_idx]
|
||||
|
||||
# Remove internal columns before sending
|
||||
output_cols = [c for c in schools_df.columns if c not in ['latitude', 'longitude']]
|
||||
if 'distance' in schools_df.columns:
|
||||
output_cols.append('distance')
|
||||
|
||||
return {
|
||||
"schools": clean_for_json(schools_df),
|
||||
"schools": clean_for_json(schools_df[output_cols]),
|
||||
"total": total,
|
||||
"page": page,
|
||||
"page_size": page_size,
|
||||
"total_pages": (total + page_size - 1) // page_size if page_size > 0 else 0,
|
||||
"search_location": {"postcode": postcode, "radius": radius} if search_coords else None,
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user