|
1 |
| -from typing import Annotated, Any, Literal |
| 1 | +from typing import Annotated, Any, Literal, Tuple, Union |
2 | 2 |
|
3 | 3 | from pydantic import BeforeValidator, PlainSerializer
|
4 | 4 | from pydantic_extra_types.coordinate import Coordinate
|
|
8 | 8 |
|
9 | 9 |
|
10 | 10 | class GeoFilter:
|
| 11 | + """ |
| 12 | + A geographic filter for searching within a radius of a coordinate point. |
| 13 | + |
| 14 | + This filter is used with GEO fields to find models within a specified |
| 15 | + distance from a given location. |
| 16 | + |
| 17 | + Args: |
| 18 | + longitude: The longitude of the center point (-180 to 180) |
| 19 | + latitude: The latitude of the center point (-90 to 90) |
| 20 | + radius: The search radius (must be positive) |
| 21 | + unit: The unit of measurement ('m', 'km', 'mi', or 'ft') |
| 22 | + |
| 23 | + Example: |
| 24 | + >>> # Find all locations within 10 miles of Portland, OR |
| 25 | + >>> filter = GeoFilter( |
| 26 | + ... longitude=-122.6765, |
| 27 | + ... latitude=45.5231, |
| 28 | + ... radius=10, |
| 29 | + ... unit="mi" |
| 30 | + ... ) |
| 31 | + >>> results = await Location.find( |
| 32 | + ... Location.coordinates == filter |
| 33 | + ... ).all() |
| 34 | + """ |
| 35 | + |
11 | 36 | def __init__(self, longitude: float, latitude: float, radius: float, unit: RadiusUnit):
|
| 37 | + # Validate coordinates |
| 38 | + if not -180 <= longitude <= 180: |
| 39 | + raise ValueError(f"Longitude must be between -180 and 180, got {longitude}") |
| 40 | + if not -90 <= latitude <= 90: |
| 41 | + raise ValueError(f"Latitude must be between -90 and 90, got {latitude}") |
| 42 | + if radius <= 0: |
| 43 | + raise ValueError(f"Radius must be positive, got {radius}") |
| 44 | + |
12 | 45 | self.longitude = longitude
|
13 | 46 | self.latitude = latitude
|
14 | 47 | self.radius = radius
|
15 | 48 | self.unit = unit
|
16 | 49 |
|
17 |
| - def __str__(self): |
| 50 | + def __str__(self) -> str: |
18 | 51 | return f"{self.longitude} {self.latitude} {self.radius} {self.unit}"
|
| 52 | + |
| 53 | + @classmethod |
| 54 | + def from_coordinates(cls, coords: Coordinate, radius: float, unit: RadiusUnit) -> "GeoFilter": |
| 55 | + """ |
| 56 | + Create a GeoFilter from a Coordinates object. |
| 57 | + |
| 58 | + Args: |
| 59 | + coords: A Coordinate object with latitude and longitude |
| 60 | + radius: The search radius |
| 61 | + unit: The unit of measurement |
| 62 | + |
| 63 | + Returns: |
| 64 | + A new GeoFilter instance |
| 65 | + """ |
| 66 | + return cls(coords.longitude, coords.latitude, radius, unit) |
19 | 67 |
|
20 | 68 |
|
21 | 69 | CoordinateType = Coordinate
|
22 | 70 |
|
23 | 71 |
|
24 |
| -def parse_redis(v: Any): |
| 72 | +def parse_redis(v: Any) -> Union[Tuple[str, str], Any]: |
25 | 73 | """
|
| 74 | + Transform Redis coordinate format to Pydantic coordinate format. |
| 75 | + |
26 | 76 | The pydantic coordinate type expects a string in the format 'latitude,longitude'.
|
27 |
| - Redis expects a string in the format 'longitude,latitude'. |
28 |
| -
|
| 77 | + Redis stores coordinates in the format 'longitude,latitude'. |
| 78 | + |
29 | 79 | This validator transforms the input from Redis into the expected format for pydantic.
|
| 80 | + |
| 81 | + Args: |
| 82 | + v: The value from Redis (typically a string like "longitude,latitude") |
| 83 | + |
| 84 | + Returns: |
| 85 | + A tuple of (latitude, longitude) strings if input is a coordinate string, |
| 86 | + otherwise returns the input unchanged. |
| 87 | + |
| 88 | + Raises: |
| 89 | + ValueError: If the coordinate string format is invalid |
30 | 90 | """
|
31 | 91 | if isinstance(v, str):
|
32 | 92 | parts = v.split(",")
|
33 | 93 |
|
34 | 94 | if len(parts) != 2:
|
35 |
| - raise ValueError("Invalid coordinate format") |
| 95 | + raise ValueError(f"Invalid coordinate format. Expected 'longitude,latitude' but got: {v}") |
36 | 96 |
|
37 |
| - return (parts[1], parts[0]) |
| 97 | + return (parts[1], parts[0]) # Swap to (latitude, longitude) |
38 | 98 |
|
39 | 99 | return v
|
40 | 100 |
|
|
0 commit comments