FastAPI service backed by PostgreSQL+PostGIS: cities/places/place_content schema (normalized per-language, per-length content for easy future re-ingestion), Alembic migration, an iterative-radius-expansion nearby search endpoint, a name-search fallback for denied-location flows, and a YAML seed loader CLI. Verified end-to-end against a live Docker Postgres+PostGIS instance (migration, city seed, place/content CRUD, and nearby radius expansion all confirmed working via curl). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
67 lines
2.3 KiB
Python
67 lines
2.3 KiB
Python
from fastapi import APIRouter, Depends, Query
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.deps import lang_param, length_param
|
|
from app.db.session import get_db
|
|
from app.schemas.common import LatLon
|
|
from app.schemas.nearby import NearbyPlace, NearbyResponse
|
|
from app.schemas.place import ContentOut
|
|
from app.services.nearby_search import find_nearby
|
|
|
|
router = APIRouter(tags=["nearby"])
|
|
|
|
|
|
@router.get("/nearby", response_model=NearbyResponse)
|
|
def nearby(
|
|
lat: float = Query(),
|
|
lon: float = Query(),
|
|
city_slug: str | None = Query(default=None),
|
|
lang: str = Depends(lang_param),
|
|
length: str = Depends(length_param),
|
|
min_results: int = Query(default=5, ge=1, le=50),
|
|
initial_radius_m: int = Query(default=300, ge=50, le=5000),
|
|
step_m: int = Query(default=200, ge=50, le=5000),
|
|
max_radius_m: int = Query(default=5000, ge=100, le=20000),
|
|
speed: float | None = Query(
|
|
default=None,
|
|
description="Reserved for v2: user's speed in m/s. Currently accepted but ignored.",
|
|
),
|
|
heading: float | None = Query(
|
|
default=None,
|
|
description="Reserved for v2: user's heading in degrees. Currently accepted but ignored.",
|
|
),
|
|
db: Session = Depends(get_db),
|
|
) -> NearbyResponse:
|
|
result = find_nearby(
|
|
db,
|
|
lat=lat,
|
|
lon=lon,
|
|
language=lang,
|
|
length=length,
|
|
city_slug=city_slug,
|
|
min_results=min_results,
|
|
initial_radius_m=initial_radius_m,
|
|
step_m=step_m,
|
|
max_radius_m=max_radius_m,
|
|
)
|
|
|
|
places = [
|
|
NearbyPlace(
|
|
id=place.id,
|
|
slug=place.slug,
|
|
category=place.category,
|
|
location=LatLon(lat=place_lat, lon=place_lon),
|
|
address=place.address,
|
|
district=place.district,
|
|
built_year=place.built_year,
|
|
architect_builder=place.architect_builder,
|
|
architectural_style=place.architectural_style,
|
|
content=ContentOut(
|
|
language=content.language, length=content.length, title=content.title, body=content.body
|
|
),
|
|
distance_m=round(distance_m, 1),
|
|
)
|
|
for place, content, distance_m, place_lon, place_lat in result.rows
|
|
]
|
|
|
|
return NearbyResponse(search_radius_m=result.search_radius_m, count=len(places), places=places)
|