From 04a9c8c0c6322c9ac95dc7591a104a357ea6f0bf Mon Sep 17 00:00:00 2001 From: TAnas0 Date: Sat, 28 Sep 2024 01:09:44 +0100 Subject: [PATCH 1/3] docs(README): add `curl` commands to create ServiceArea and check a point --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 6934f5c..5ce68ec 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,18 @@ uvicorn src.main:app --reload ## Testing +To test using `curl` commands, you can try creating a Service Area, and check if a latitude/longitude pair is contained in the registered Service Areas: + +```bash +curl -X 'POST' 'http://127.0.0.1:8000/api/v1/serviceareas/' -H 'Content-Type: application/json' -d '{ + "name": "Downtown Area 2", + "price": 25.50, + "geojson": "{\"type\": \"Polygon\", \"coordinates\": [[[30.0, 10.0], [40.0, 40.0], [20.0, 40.0], [10.0, 20.0], [30.0, 10.0]]]}" +}' + +curl -X GET "http://localhost:8000/api/v1/serviceareas/check?lat=30&lng=20" +``` + To run the unit tests: ```bash pytest From 62637164ead81e5344ad05195bd46c1346258b8e Mon Sep 17 00:00:00 2001 From: TAnas0 Date: Sat, 28 Sep 2024 01:10:11 +0100 Subject: [PATCH 2/3] chore(routes): remove trailing slash from the check endpoint --- src/routes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes.py b/src/routes.py index 169cf46..f6017bc 100644 --- a/src/routes.py +++ b/src/routes.py @@ -122,7 +122,7 @@ def delete_service_area(service_area_id: int, db: Session = Depends(get_db)): return service_area_to_dict(db_service_area) -@router.get("/serviceareas/check/", response_model=List[ServiceAreaSchema]) +@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})" From cffa292bd4f75e8bf711dcd861e66dabf39e1733 Mon Sep 17 00:00:00 2001 From: TAnas0 Date: Sat, 28 Sep 2024 01:10:18 +0100 Subject: [PATCH 3/3] refactor(models): use GeoAlchemy to_shape in geojson validation --- src/models.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/models.py b/src/models.py index 3f95eb9..5c44e66 100644 --- a/src/models.py +++ b/src/models.py @@ -1,6 +1,6 @@ from sqlalchemy import Column, Integer, String, Float, CheckConstraint from .database import Base -from geoalchemy2 import Geometry +from geoalchemy2 import Geometry, shape from sqlalchemy.orm import validates from shapely.geometry import shape @@ -81,8 +81,8 @@ def validate_price(self, key, price): def validate_geojson(self, key, geojson): try: # Parse and validate the GeoJSON structure - geojson_dict = json.loads(geojson) - geometry = shape(geojson_dict["geometry"]) + from geoalchemy2.shape import to_shape + geometry = to_shape(geojson) if geometry.geom_type != "Polygon": raise ValueError("GeoJSON must represent a POLYGON")