forked from venetoarpa/arpav-cline-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 #67 from ricardogsilva/66-return-stations-list-as-…
…a-geojson-feature-collection- Return GeoJSON FeatureCollection from stations list API endpoint
- Loading branch information
Showing
9 changed files
with
209 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import json | ||
from typing import Annotated | ||
|
||
import geoalchemy2 | ||
import shapely.io | ||
from pydantic.functional_serializers import PlainSerializer | ||
|
||
def serialize_wkbelement(wkbelement: geoalchemy2.WKBElement): | ||
geom = shapely.io.from_wkb(bytes(wkbelement.data)) | ||
return json.loads(shapely.io.to_geojson(geom)) | ||
|
||
|
||
WkbElement = Annotated[ | ||
geoalchemy2.WKBElement, | ||
PlainSerializer(serialize_wkbelement, return_type=dict, when_used="json") | ||
] |
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
Empty file.
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,85 @@ | ||
import typing | ||
|
||
import geojson_pydantic | ||
import pydantic | ||
from fastapi.requests import Request | ||
|
||
from ..base import ( | ||
ListLinks, | ||
get_pagination_urls, | ||
) | ||
|
||
F = typing.TypeVar("F", bound="ApiReadableModelAsFeature") | ||
|
||
|
||
@typing.runtime_checkable | ||
class ApiReadableModelAsFeature(typing.Protocol): | ||
"""Protocol to be used by all schema models that represent API resources. | ||
It includes the `from_db_instance()` class method, which is to be used for | ||
constructing instances. | ||
""" | ||
|
||
@classmethod | ||
def from_db_instance( # noqa: D102 | ||
cls: typing.Type[F], db_instance: pydantic.BaseModel, request: Request | ||
) -> F: | ||
... | ||
|
||
|
||
class ArpavFeatureCollection(geojson_pydantic.FeatureCollection): | ||
list_item_type: typing.ClassVar[typing.Type[ApiReadableModelAsFeature]] | ||
path_operation_name: typing.ClassVar[str] | ||
|
||
type: str = "FeatureCollection" | ||
links: ListLinks | ||
number_matched: int | ||
number_total: int | ||
number_returned: int | ||
|
||
@classmethod | ||
def from_items( | ||
cls, | ||
items: typing.Sequence[pydantic.BaseModel], | ||
request: Request, | ||
*, | ||
limit: int, | ||
offset: int, | ||
filtered_total: int, | ||
unfiltered_total: int | ||
) -> "cls": | ||
return cls( | ||
features=[ | ||
cls.list_item_type.from_db_instance(i, request) | ||
for i in items | ||
], | ||
links=cls._get_list_links( | ||
request, limit, offset, filtered_total, len(items)), | ||
number_matched=filtered_total, | ||
number_total=unfiltered_total, | ||
number_returned=len(items), | ||
) | ||
|
||
@classmethod | ||
def _get_list_links( | ||
cls, | ||
request: Request, | ||
limit: int, | ||
offset: int, | ||
filtered_total: int, | ||
num_returned_records: int | ||
) -> ListLinks: | ||
filters = dict(request.query_params) | ||
if "limit" in filters.keys(): | ||
del filters["limit"] | ||
if "offset" in filters.keys(): | ||
del filters["offset"] | ||
pagination_urls = get_pagination_urls( | ||
request.url_for(cls.path_operation_name), | ||
num_returned_records, | ||
filtered_total, | ||
limit, | ||
offset, | ||
**filters, | ||
) | ||
return ListLinks(**pagination_urls) |
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,42 @@ | ||
from fastapi import Request | ||
import geojson_pydantic | ||
import pydantic | ||
|
||
from .....schemas import ( | ||
models as app_models, | ||
fields, | ||
) | ||
from .base import ArpavFeatureCollection | ||
|
||
|
||
class StationFeatureCollectionItem(geojson_pydantic.Feature): | ||
model_config = pydantic.ConfigDict(arbitrary_types_allowed=True) | ||
|
||
type: str = "Feature" | ||
id: pydantic.UUID4 | ||
geometry: fields.WkbElement | ||
links: list[str] | ||
|
||
@classmethod | ||
def from_db_instance( | ||
cls, | ||
instance: app_models.Station, | ||
request: Request, | ||
) -> "StationFeatureCollectionItem": | ||
url = request.url_for("get_station", **{"station_id": instance.id}) | ||
return cls( | ||
id=instance.id, | ||
geometry=instance.geom, | ||
properties=instance.model_dump( | ||
exclude={ | ||
"id", | ||
"geom", | ||
} | ||
), | ||
links=[str(url)] | ||
) | ||
|
||
|
||
class StationFeatureCollection(ArpavFeatureCollection): | ||
path_operation_name = "list_stations" | ||
list_item_type = StationFeatureCollectionItem |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import logging | ||
|
||
import pydantic | ||
from fastapi import Request | ||
|
||
|
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