-
Notifications
You must be signed in to change notification settings - Fork 35
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 #144 from eseglem/CustomSerializer
Create a base model and generic serialization.
- Loading branch information
Showing
7 changed files
with
179 additions
and
141 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,68 @@ | ||
"""pydantic BaseModel for GeoJSON objects.""" | ||
from __future__ import annotations | ||
|
||
from typing import Any, Dict, List, Optional, Set | ||
|
||
from pydantic import BaseModel, SerializationInfo, field_validator, model_serializer | ||
|
||
from geojson_pydantic.types import BBox | ||
|
||
|
||
class _GeoJsonBase(BaseModel): | ||
bbox: Optional[BBox] = None | ||
|
||
# These fields will not be included when serializing in json mode | ||
# `.model_dump_json()` or `.model_dump(mode="json")` | ||
__geojson_exclude_if_none__: Set[str] = {"bbox"} | ||
|
||
@property | ||
def __geo_interface__(self) -> Dict[str, Any]: | ||
"""GeoJSON-like protocol for geo-spatial (GIS) vector data. | ||
ref: https://gist.github.com/sgillies/2217756#__geo_interface | ||
""" | ||
return self.model_dump(mode="json") | ||
|
||
@field_validator("bbox") | ||
def validate_bbox(cls, bbox: Optional[BBox]) -> Optional[BBox]: | ||
"""Validate BBox values are ordered correctly.""" | ||
# If bbox is None, there is nothing to validate. | ||
if bbox is None: | ||
return None | ||
|
||
# A list to store any errors found so we can raise them all at once. | ||
errors: List[str] = [] | ||
|
||
# Determine where the second position starts. 2 for 2D, 3 for 3D. | ||
offset = len(bbox) // 2 | ||
|
||
# Check X | ||
if bbox[0] > bbox[offset]: | ||
errors.append(f"Min X ({bbox[0]}) must be <= Max X ({bbox[offset]}).") | ||
# Check Y | ||
if bbox[1] > bbox[1 + offset]: | ||
errors.append(f"Min Y ({bbox[1]}) must be <= Max Y ({bbox[1 + offset]}).") | ||
# If 3D, check Z values. | ||
if offset > 2 and bbox[2] > bbox[2 + offset]: | ||
errors.append(f"Min Z ({bbox[2]}) must be <= Max Z ({bbox[2 + offset]}).") | ||
|
||
# Raise any errors found. | ||
if errors: | ||
raise ValueError("Invalid BBox. Error(s): " + " ".join(errors)) | ||
|
||
return bbox | ||
|
||
@model_serializer(when_used="json", mode="wrap") | ||
def clean_model(self, serializer: Any, _info: SerializationInfo) -> Dict[str, Any]: | ||
"""Custom Model serializer to match the GeoJSON specification. | ||
Used to remove fields which are optional but cannot be null values. | ||
""" | ||
# This seems like the best way to have the least amount of unexpected consequences. | ||
# We want to avoid forcing values in `exclude_none` or `exclude_unset` which could | ||
# cause issues or unexpected behavior for downstream users. | ||
data: Dict[str, Any] = serializer(self) | ||
for field in self.__geojson_exclude_if_none__: | ||
if field in data and data[field] is None: | ||
del data[field] | ||
return data |
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.