Skip to content

Expectations of to_dict methods in returning JSON-typed values, regarding geometry #1047

@philvarner

Description

@philvarner

Currently, the to_dict method returns a "shallow" dict representation of the object. For example, if the Item has a geometry that has a coordinates value that is a nested tuple of numbers , the resulting dict's geometry key points to that same tuple. However, I think a more common assumption is that to_dict returns a JSON-typed representation of the Item, e.g., with geometry being nested arrays and numbers, regardless of what the internal representation is. Maybe there could be a flag on to_dict to determine this behavior, or another method like to_geojson_dict?

mypy checking doesn't catch this because geometry is typed as Optional[Dict[str,Any]], so it can't catch it. (Union types would help here, allowing the stricter Optional[Dict[str,str|list[list|float]])

The current docstring for to_dict is Generate a dictionary representing the JSON of this serialized object. , which is wrong wrt to the current behavior if non-JSON-compliant tuples are used.

The context behind this is we had an Item that (unintentionally) had tuple geometry coordinate values, and used .to_dict() to create a value to pass to stac-validator validation, expecting that we were getting a JSON-style dict. The error message was:

Exception: STAC Item validation failed. Error: {‘type’: ‘Polygon’, ‘coordinates’: (((-91.00013888888888, 43.00013888888889), (-91.00013888888888, 44.00013888888889), (-92.00013888888888, 44.00013888888889), (-92.00013888888888, 43.00013888888889), (-91.00013888888888, 43.00013888888889)),)} is not valid under any of the given schemas. Error is in geometry .

because the tuple geometry doesn't match the schema of nested arrays of numbers, and then gets str serialized to a value of nested tuples of numbers.

Relatedly, __geo_interface__ will return an invalid value if tuple coordinates are used.

Metadata

Metadata

Assignees

Labels

documentationIssues related to PySTAC documentation

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions