|
| 1 | +import json |
| 2 | + |
| 3 | +from typing import List, Dict, Any, Optional |
| 4 | + |
| 5 | +from fastapi import Depends, APIRouter, HTTPException, status |
| 6 | +from geojson import Feature, FeatureCollection |
| 7 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 8 | + |
| 9 | +from ..schemas.police import ( |
| 10 | + PoliceGeometryResponse, |
| 11 | + PoliceResponse |
| 12 | +) |
| 13 | +from ..dependencies import get_session |
| 14 | +from ..services.police import ( |
| 15 | + get_police_station_by_id, |
| 16 | + get_police_station_geometries_by_bbox, |
| 17 | + get_police_station_geometries_by_lat_lng |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +route_police = APIRouter(prefix='/police/v1') |
| 22 | + |
| 23 | + |
| 24 | +def create_geojson_from_rows(rows: List[Dict[str, Any]]) -> FeatureCollection: |
| 25 | + features = [ |
| 26 | + Feature( |
| 27 | + id=row['id'], |
| 28 | + geometry=json.loads(row['geojson']), |
| 29 | + properties={'label': row['label']} |
| 30 | + ) |
| 31 | + for row in rows |
| 32 | + ] |
| 33 | + |
| 34 | + crs = {'type': 'name', 'properties': { |
| 35 | + 'name': 'urn:ogc:def:crs:OGC:1.3:CRS84'}} |
| 36 | + |
| 37 | + return FeatureCollection(features, crs=crs) |
| 38 | + |
| 39 | + |
| 40 | +@route_police.get( |
| 41 | + '/details', |
| 42 | + response_model=PoliceResponse, |
| 43 | + tags=['Polizeidienststellen'], |
| 44 | + description=( |
| 45 | + 'Retrieves police station details based on the provided station id.' |
| 46 | + ) |
| 47 | +) |
| 48 | +async def fetch_police_station_by_id( |
| 49 | + station_id: int, |
| 50 | + session: AsyncSession = Depends(get_session) |
| 51 | +) -> List[PoliceResponse]: |
| 52 | + rows = await get_police_station_by_id(session, station_id) |
| 53 | + identifier = f'station_id {station_id}' |
| 54 | + |
| 55 | + if not rows: |
| 56 | + raise HTTPException( |
| 57 | + status_code=status.HTTP_404_NOT_FOUND, |
| 58 | + detail=f'No matches found for {identifier}' |
| 59 | + ) |
| 60 | + |
| 61 | + return rows |
| 62 | + |
| 63 | + |
| 64 | +@route_police.get( |
| 65 | + '/bounds', |
| 66 | + response_model=PoliceGeometryResponse, |
| 67 | + tags=['Polizeidienststellen'], |
| 68 | + description=( |
| 69 | + 'Retrieves police geometries based on the provided bounding box. ' |
| 70 | + 'The coordinates must be in the order: xmin, ymin, xmax, ymax.' |
| 71 | + ) |
| 72 | +) |
| 73 | +async def fetch_police_station_geometries_by_bbox( |
| 74 | + xmin: float, |
| 75 | + ymin: float, |
| 76 | + xmax: float, |
| 77 | + ymax: float, |
| 78 | + session: AsyncSession = Depends(get_session) |
| 79 | +) -> FeatureCollection: |
| 80 | + rows = await get_police_station_geometries_by_bbox( |
| 81 | + session, xmin, ymin, xmax, ymax |
| 82 | + ) |
| 83 | + |
| 84 | + if not rows: |
| 85 | + raise HTTPException( |
| 86 | + status_code=status.HTTP_404_NOT_FOUND, |
| 87 | + detail='No matches found for the given bounding box' |
| 88 | + ) |
| 89 | + |
| 90 | + return create_geojson_from_rows(rows) |
| 91 | + |
| 92 | + |
| 93 | +@route_police.get( |
| 94 | + '/radius', |
| 95 | + response_model=PoliceGeometryResponse, |
| 96 | + responses={ |
| 97 | + 400: { |
| 98 | + 'description': 'Bad Request', |
| 99 | + 'content': { |
| 100 | + 'application/json': { |
| 101 | + 'example': { |
| 102 | + 'detail': 'string' |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + }, |
| 107 | + 404: { |
| 108 | + 'description': 'Not Found', |
| 109 | + 'content': { |
| 110 | + 'application/json': { |
| 111 | + 'example': { |
| 112 | + 'detail': 'string' |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + }, |
| 117 | + 422: { |
| 118 | + 'description': 'Unprocessable Entity', |
| 119 | + 'content': { |
| 120 | + 'application/json': { |
| 121 | + 'example': { |
| 122 | + 'detail': [ |
| 123 | + { |
| 124 | + 'loc': ['query', 'station_id'], |
| 125 | + 'msg': 'value is not a valid integer', |
| 126 | + 'type': 'type_error.integer' |
| 127 | + } |
| 128 | + ] |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + }, |
| 134 | + tags=['Polizeidienststellen'], |
| 135 | + description=( |
| 136 | + 'Retrieves police geometries based on the provided latitude and longitude. ' |
| 137 | + 'The coordinates must be in the order: lat, lng.' |
| 138 | + ) |
| 139 | +) |
| 140 | +async def fetch_police_station_geometries_by_lat_lng( |
| 141 | + lat: float, |
| 142 | + lng: float, |
| 143 | + session: AsyncSession = Depends(get_session) |
| 144 | +) -> FeatureCollection: |
| 145 | + """ |
| 146 | + Retrieve polices near a specific lat/lng point. |
| 147 | +
|
| 148 | + Args: |
| 149 | + lat: Latitude |
| 150 | + lng: Longitude |
| 151 | + session: Database session |
| 152 | +
|
| 153 | + Returns: |
| 154 | + GeoJSON FeatureCollection of polices |
| 155 | +
|
| 156 | + Raises: |
| 157 | + HTTPException: If no polices are found near the coordinates |
| 158 | + """ |
| 159 | + rows = await get_police_station_geometries_by_lat_lng(session, lat, lng) |
| 160 | + |
| 161 | + if not rows: |
| 162 | + raise HTTPException( |
| 163 | + status_code=status.HTTP_404_NOT_FOUND, |
| 164 | + detail=f'No matches found for coordinates ({lat}, {lng})' |
| 165 | + ) |
| 166 | + |
| 167 | + return create_geojson_from_rows(rows) |
0 commit comments