Skip to content

Improve handling of bboxes and intervals #1268

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

Merged
merged 7 commits into from
Oct 19, 2023

Conversation

bmcandr
Copy link
Contributor

@bmcandr bmcandr commented Oct 18, 2023

Related Issue(s):

Description:

  • Allow SpatialExtent.bboxes to be a nested list of floats and/or integers to reflect the STAC spec typing of [[number]].
  • In SpatialExtent, wrap bboxes in a list if passed as list of floats OR integers.
  • In TemporalExtent, wrap intervals in a list if passed as a single interval.

While the linked issue discusses adding more robust validation/coercion of inputs, I opted to start simple because stricter checks on bboxes start to get a little gnarly:

        if all(isinstance(coord, (float, int)) for coord in bboxes):
            self.bboxes = [cast(list[float | int], bboxes)]
        elif all(
            all(
                isinstance(coord, (float, int)) for coord in bbox
            ) for bbox in bboxes
        ):
            self.bboxes = cast(Bboxes, bboxes)
        else:
            raise TypeError(f"unexpected type encountered in bboxes: {bboxes}")

If stricter checks are preferred, I can make that change. I can also add a version of test_spatial_allows_single_bbox to cover single bbox of ints if it's warranted.

PR Checklist:

  • pre-commit hooks pass locally
  • Tests pass (run scripts/test)
  • Documentation has been updated to reflect changes, if applicable
  • This PR maintains or improves overall codebase code coverage.
  • Changes are added to the CHANGELOG. See the docs for information about adding to the changelog.

@gadomski gadomski self-requested a review October 18, 2023 11:47
Copy link
Member

@gadomski gadomski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution, makes sense to me. Can you add unit tests demonstrating the fix behavior for temporal intervals and expecting the type error for bounding boxes?

@bmcandr
Copy link
Contributor Author

bmcandr commented Oct 19, 2023

I added the requested tests.

FWIW, Pylance suggests that the isinstance(bboxes|intervals, list) checks are unnecessary because they should always be lists per the type hints. OTOH explicit is better than implicit. Any preference?

@codecov
Copy link

codecov bot commented Oct 19, 2023

Codecov Report

All modified lines are covered by tests ✅

Files Coverage Δ
pystac/collection.py 97.00% <100.00%> (+0.82%) ⬆️

📢 Thoughts on this report? Let us know!.

@gadomski gadomski added this pull request to the merge queue Oct 19, 2023
Merged via the queue into stac-utils:main with commit 36e84da Oct 19, 2023
@bmcandr bmcandr deleted the fix/spatial-extent-bboxes branch October 19, 2023 14:14
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 this pull request may close these issues.

SpatialExtent value type checking allows creation of invalid Collections
2 participants