Open void-rooster opened 8 months ago
Maybe related to https://github.com/pydantic/pydantic/issues/8984 🤷
add the same time you could do something like
from pydantic import field_serializer
from geojson_pydantic import Point
from geojson_pydantic.types import Position
class MyPoint(Point):
@field_serializer("coordinates")
def six_digits_precision(self, coords: Position) -> Position:
rounded_coords = [round(coord, 6) for coord in coords]
return tuple(rounded_coords)
MyPoint(type="Point", coordinates=(0.000000000001,0)).model_dump(exclude_none=True)
>> {'type': 'Point', 'coordinates': (0.0, 0.0)}
We have to provide coordinates with 6 digits of precision and use a field serializer to do so. Curiously, we can use the serializer on fields allowed to be either a Point or a Polygon, but not with fields allowed to be only a Point.
This produces a
SchemaError
:traceback:
without the field serializer, or with the field serializer and different treatment for coordinate extraction based on Point vs Polygon, it succeeds.