forked from venetoarpa/Arpav-PPCV-backend
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #137 from ricardogsilva/132-add-API-endpoint-to-fi…
…nd-municipality-from-coordinates Find municipality by coordinates
- Loading branch information
Showing
6 changed files
with
175 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import logging | ||
from typing import Annotated | ||
|
||
import shapely.io | ||
from fastapi import ( | ||
APIRouter, | ||
Depends, | ||
HTTPException, | ||
Request, | ||
) | ||
from sqlmodel import Session | ||
|
||
from .... import database as db | ||
from ... import dependencies | ||
from ...responses import GeoJsonResponse | ||
from ..schemas.geojson import municipalities as municipalities_geojson | ||
|
||
logger = logging.getLogger(__name__) | ||
router = APIRouter() | ||
|
||
|
||
@router.get( | ||
"/", | ||
response_class=GeoJsonResponse, | ||
response_model=municipalities_geojson.MunicipalityFeatureCollection, | ||
) | ||
def list_municipalities( | ||
request: Request, | ||
db_session: Annotated[Session, Depends(dependencies.get_db_session)], | ||
list_params: Annotated[dependencies.CommonListFilterParameters, Depends()], | ||
coords: str | None = None, | ||
name: str | None = None, | ||
province: str | None = None, | ||
region: str | None = None, | ||
): | ||
"""List Italian municipalities.""" | ||
geom_filter_kwarg = {} | ||
if coords is not None: | ||
geom = shapely.io.from_wkt(coords) | ||
if geom.geom_type == "Point": | ||
geom_filter_kwarg = {"point_filter": geom} | ||
elif geom.geom_type == "Polygon": | ||
geom_filter_kwarg = {"polygon_intersection_filter": geom} | ||
else: | ||
raise HTTPException( | ||
status_code=400, detail="geometry be either point or polygon" | ||
) | ||
municipalities, filtered_total = db.list_municipalities( | ||
db_session, | ||
limit=list_params.limit, | ||
offset=list_params.offset, | ||
include_total=True, | ||
name_filter=name, | ||
province_name_filter=province, | ||
region_name_filter=region, | ||
**geom_filter_kwarg, | ||
) | ||
_, unfiltered_total = db.list_stations( | ||
db_session, limit=1, offset=0, include_total=True | ||
) | ||
return municipalities_geojson.MunicipalityFeatureCollection.from_items( | ||
municipalities, | ||
request, | ||
limit=list_params.limit, | ||
offset=list_params.offset, | ||
filtered_total=filtered_total, | ||
unfiltered_total=unfiltered_total, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
arpav_ppcv/webapp/api_v2/schemas/geojson/municipalities.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import geojson_pydantic | ||
import pydantic | ||
from fastapi import Request | ||
|
||
from .....schemas import ( | ||
fields, | ||
municipalities, | ||
) | ||
from .base import ArpavFeatureCollection | ||
|
||
|
||
class MunicipalityFeatureCollectionItem(geojson_pydantic.Feature): | ||
model_config = pydantic.ConfigDict(arbitrary_types_allowed=True) | ||
|
||
type: str = "Feature" | ||
id: pydantic.UUID4 | ||
geometry: fields.WkbElement | ||
|
||
@classmethod | ||
def from_db_instance( | ||
cls, | ||
instance: municipalities.Municipality, | ||
request: Request, | ||
) -> "MunicipalityFeatureCollectionItem": | ||
return cls( | ||
id=instance.id, | ||
geometry=instance.geom, | ||
properties={ | ||
**instance.model_dump( | ||
exclude={ | ||
"id", | ||
"geom", | ||
} | ||
), | ||
}, | ||
) | ||
|
||
|
||
class MunicipalityFeatureCollection(ArpavFeatureCollection): | ||
path_operation_name = "list_municipalities" | ||
list_item_type = MunicipalityFeatureCollectionItem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from fastapi.responses import JSONResponse | ||
|
||
|
||
class GeoJsonResponse(JSONResponse): | ||
media_type = "application/geo+json" |