-
The JSON validator of Pydantic expect a string json-formatted. I understand why it's a good thing to validate python dict object with a valid geojson structure, but what if I already have a geojson string and I want to validate this string with the geojson-pydantic types? Sure I can |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 13 replies
-
@Vikka I'm not sure what you are trying to do, could you write down some pseudo code please? pydantic comes with https://pydantic-docs.helpmanual.io/usage/models/#helper-functions which is maybe what you are looking for from geojson_pydantic import Feature
f = '{"type": "Feature", "geometry": {"type": "Polygon", "coordinates": [[[13.38272, 52.46385], [13.42786, 52.46385], [13.42786, 52.48445], [13.38272, 52.48445], [13.38272, 52.46385]]]}}'
Feature.parse_raw(f)
Feature(type='Feature', geometry=Polygon(coordinates=[[(13.38272, 52.46385), (13.42786, 52.46385), (13.42786, 52.48445), (13.38272, 52.48445), (13.38272, 52.46385)]], type='Polygon'), properties=None, id=None, bbox=None) |
Beta Was this translation helpful? Give feedback.
-
FYI I'm going to move this over the |
Beta Was this translation helpful? Give feedback.
-
Hi, I use Pydantic with FastAPI for data validation, and the geojson came from db return. This is kind of thing I want do to: @router.get("/endpoint/path")
def route_fct() -> list[geojson_pydantic.Polygon]:
with get_session() as session:
geojson_result = session.query(
func.ST_AsGeoJSON(Model.geometry, options=2).label("geojson")
).first()
return geojson_result[0].geojson Comparison of from geojson_pydantic import Polygon
from pydantic import BaseModel, Json
class A(BaseModel):
json_data: Json
geojson: Polygon
json_str = '{"type": "Polygon","coordinates": [[[13.38272, 52.46385],[13.42786, 52.46385],[13.42786, 52.48445],[13.38272, 52.48445],[13.38272, 52.46385]]]}'
polygon = {
"type": "Polygon",
"coordinates": [
[
[13.38272, 52.46385],
[13.42786, 52.46385],
[13.42786, 52.48445],
[13.38272, 52.48445],
[13.38272, 52.46385],
]
],
}
print(A(json_data=json_str, geojson=polygon))
# json_data={'type': 'Polygon', 'coordinates': [[[13.38272, 52.46385], [13.42786, 52.46385], [13.42786, 52.48445], [13.38272, 52.48445], [13.38272, 52.46385]]]}
# geojson=Polygon(coordinates=[[(13.38272, 52.46385), (13.42786, 52.46385), (13.42786, 52.48445), (13.38272, 52.48445), (13.38272, 52.46385)]], type='Polygon') The result with fastAPI is the same, but the object we can validate isn't. |
Beta Was this translation helpful? Give feedback.
-
@Vikka should we also implement this for Feature and FeatureCollection ? |
Beta Was this translation helpful? Give feedback.
-
I have been reviewing a bunch of stuff in this package and came across the validate function which confused me. I don't understand why any changes were necessary. The If you just want to parse a string into an actual object class A(BaseModel):
geojson: Json[Polygon]
print(A(geojson=json_str))
# geojson=Polygon(coordinates=[[(13.38272, 52.46385), (13.42786, 52.46385), (13.42786, 52.48445), (13.38272, 52.48445), (13.38272, 52.46385)]], type='Polygon') Does that not accomplish the same thing? |
Beta Was this translation helpful? Give feedback.
-
Just use See discussion here: #66 (comment) |
Beta Was this translation helpful? Give feedback.
Just use
.parse_raw()
orJson[Polygon]
return type if it's ok to return a string.See discussion here: #66 (comment)