Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bbox behavior different from spec #125

Closed
bordax opened this issue Jun 1, 2023 · 9 comments · Fixed by #143
Closed

Bbox behavior different from spec #125

bordax opened this issue Jun 1, 2023 · 9 comments · Fixed by #143

Comments

@bordax
Copy link

bordax commented Jun 1, 2023

Current Behavior

When we initialize any GeoJson type without passing a bbox, it's setted to null.

from geojson_pydantic import FeatureCollection
from pprint import pprint

fc = {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": "",
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [-37.33175659179676, -9.424103736877383],
                        [-37.31844711303711, -9.420578002929688],
                        [-37.26972198486328, -9.45348930358881],
                        [-37.3108024597168, -9.467284202575684],
                        [-37.33392715454096, -9.487695693969727],
                        [-37.37298583984375, -9.524782180786133],
                        [-37.4452285766601, -9.476631164550724],
                        [-37.44975662231445, -9.453315734863281],
                        [-37.33175659179676, -9.424103736877383],
                    ]
                ],
            },
        }
    ],
}
fc = FeatureCollection(**fc)
pprint(fc.dict())

Will print

{'bbox': None,
 'features': [{'bbox': None,
               'geometry': {'bbox': None,
                            'coordinates': [[(-37.33175659179676,
                                              -9.424103736877383),
                                             (-37.31844711303711,
                                              -9.420578002929688),
                                             (-37.26972198486328,
                                              -9.45348930358881),
                                             (-37.3108024597168,
                                              -9.467284202575684),
                                             (-37.33392715454096,
                                              -9.487695693969727),
                                             (-37.37298583984375,
                                              -9.524782180786133),
                                             (-37.4452285766601,
                                              -9.476631164550724),
                                             (-37.44975662231445,
                                              -9.453315734863281),
                                             (-37.33175659179676,
                                              -9.424103736877383)]],
                            'type': 'Polygon'},
               'id': None,
               'properties': {},
               'type': 'Feature'}],
 'type': 'FeatureCollection'}

Expected Behavior

From the standard, at the definition of a GeoJSON Object:

GeoJSON Object

A GeoJSON object represents a Geometry, Feature, or collection of Features.
A GeoJSON object is a JSON object.
A GeoJSON object has a member with the name "type". The value of the member MUST be one of the GeoJSON types.
A GeoJSON object MAY have a "bbox" member, the value of which MUST be a bounding box array (see Section 5).

Then, from the section on bounding boxes:

  1. Bounding Box

    A GeoJSON object MAY have a member named "bbox" to include
    information on the coordinate range for its Geometries, Features, or
    FeatureCollections. The value of the bbox member MUST be an array of
    length 2*n where n is the number of dimensions represented in the
    contained geometries, with all axes of the most southwesterly point
    followed by all axes of the more northeasterly point. The axes order
    of a bbox follows the axes order of geometries.

In other words, when passing no bbox, the parsed geometry should contain no bbox fields.

{
 'features': [{
               'geometry': {
                            'coordinates': [[(-37.33175659179676,
                                              -9.424103736877383),
                                             (-37.31844711303711,
                                              -9.420578002929688),
                                             (-37.26972198486328,
                                              -9.45348930358881),
                                             (-37.3108024597168,
                                              -9.467284202575684),
                                             (-37.33392715454096,
                                              -9.487695693969727),
                                             (-37.37298583984375,
                                              -9.524782180786133),
                                             (-37.4452285766601,
                                              -9.476631164550724),
                                             (-37.44975662231445,
                                              -9.453315734863281),
                                             (-37.33175659179676,
                                              -9.424103736877383)]],
                            'type': 'Polygon'},
               'id': None,
               'properties': {},
               'type': 'Feature'}],
 'type': 'FeatureCollection'
}
@vincentsarago
Copy link
Member

For .dict() I don't think the behaviour is wrong, but it might for .json()

fc.json()
'{"type": "FeatureCollection", "features": [{"type": "Feature", "geometry": {"type": "Polygon", "coordinates": [[[-37.33175659179676, -9.424103736877383], [-37.31844711303711, -9.420578002929688], [-37.26972198486328, -9.45348930358881], [-37.3108024597168, -9.467284202575684], [-37.33392715454096, -9.487695693969727], [-37.37298583984375, -9.524782180786133], [-37.4452285766601, -9.476631164550724], [-37.44975662231445, -9.453315734863281], [-37.33175659179676, -9.424103736877383]]], "bbox": null}, "properties": {}, "id": null, "bbox": null}], "bbox": null}'

you can avoid this by using pydantic option exclude_none=True

fc.json(exclude_none=True)
'{"type": "FeatureCollection", "features": [{"type": "Feature", "geometry": {"type": "Polygon", "coordinates": [[[-37.33175659179676, -9.424103736877383], [-37.31844711303711, -9.420578002929688], [-37.26972198486328, -9.45348930358881], [-37.3108024597168, -9.467284202575684], [-37.33392715454096, -9.487695693969727], [-37.37298583984375, -9.524782180786133], [-37.4452285766601, -9.476631164550724], [-37.44975662231445, -9.453315734863281], [-37.33175659179676, -9.424103736877383]]]}, "properties": {}}]}

@bordax
Copy link
Author

bordax commented Jun 1, 2023

Yeah, it's ok that .dict() returns None values, but I think that the correct behavior on .json() shouldn't depends on an extra parameter. What do you think?

@vincentsarago
Copy link
Member

Sadly I don't think there is an easy way to fix this on geojson-pydantic side 🤔

Note: this is also true for Feature id

@bordax
Copy link
Author

bordax commented Jun 1, 2023

Would be too aggressive to override .json() on _GeometryBase defaulting exclude_none or exclude_unset to True? hehe

@vincentsarago
Copy link
Member

@eseglem do you think we should update the model_dump in geojson-pydantic 1.0 to take care of this?

@eseglem
Copy link
Collaborator

eseglem commented Jul 11, 2023

@vincentsarago Probably worth taking a look. Optional keywords that are not actually nullable are still not great in Pydantic.

I don't think I like messing with exclude_none since there are nullable required fields but exclude_unset might be reasonable. Not really a fan of messing with the default Pydantic behavior, since it would also affect things like Properties.

I think the best bet might be @model_serializer which we should be able to go in and say when serializing out remove the specific keywords that shouldn't be null.

@SamuelWillis
Copy link

Are there any plans to make these changes to 0.6.3 as well? Unfortunately I am unable to bump to pydantic 2 at this time and would like to avoid the null bbox values as well.

@vincentsarago
Copy link
Member

@SamuelWillis we don't have any plan to backport this feature to 0.6.x but I'm happy to review any PR 🙏

@SamuelWillis
Copy link

@vincentsarago I'll see if I can find time here, I'm fairly new to python but I assume the solutions will be similar/the same?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants