Skip to content

Commit

Permalink
feat(routes): check if a geo point is in a polygon
Browse files Browse the repository at this point in the history
  • Loading branch information
TAnas0 committed Sep 27, 2024
1 parent a4c432a commit 57a1b27
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
ServiceAreaUpdate,
)
from typing import List
from sqlalchemy import func

router = APIRouter()

Expand Down Expand Up @@ -119,3 +120,23 @@ def delete_service_area(service_area_id: int, db: Session = Depends(get_db)):
db.commit()

return service_area_to_dict(db_service_area)


@router.get("/serviceareas/check/", response_model=List[ServiceAreaSchema])
def check_point_in_service_area(lat: float, lng: float, db: Session = Depends(get_db)):
# Create a point geometry using the latitude and longitude
point = f"SRID=4326;POINT({lng} {lat})"

# Query the service areas to check if they contain the point
service_areas = db.query(ServiceArea).filter(
func.ST_Contains(
func.ST_Transform(ServiceArea.geojson, 4326),
func.ST_GeomFromText(point, 4326)
)
).all()

# Check if any service areas were found
if not service_areas:
raise HTTPException(status_code=404, detail="No service area found for the given coordinates")

return [service_area_to_dict(sa) for sa in service_areas]

0 comments on commit 57a1b27

Please sign in to comment.