Skip to content

Fix __geo_interface__ #1049

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 1 commit into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- Use [ruff](https://github.com/charliermarsh/ruff) instead of **isort** and **flake8** ([#1034](https://github.com/stac-utils/pystac/pull/1034))

### Fixed

- Item `__geo_interface__` now correctly returns a Feature, rather than only the Geometry ([#1049](https://github.com/stac-utils/pystac/pull/1049))

## [v1.7.0]

### Added
Expand Down
10 changes: 6 additions & 4 deletions pystac/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,12 +531,14 @@ def matches_object_type(cls, d: Dict[str, Any]) -> bool:
return identify_stac_object_type(d) == STACObjectType.ITEM

@property
def __geo_interface__(self) -> Optional[Dict[str, Any]]:
"""Returns this item's geometry.
def __geo_interface__(self) -> Dict[str, Any]:
"""Returns this item as a dictionary.

This just calls `to_dict` without self links or transforming any hrefs.

https://gist.github.com/sgillies/2217756

Returns:
Dict[str, Any]: The Item's geometry
Dict[str, Any]: This item as a dictionary.
"""
return deepcopy(self.geometry)
return self.to_dict(include_self_link=False, transform_hrefs=False)
14 changes: 8 additions & 6 deletions tests/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,6 @@ def test_from_invalid_dict_raises_exception(self) -> None:
with self.assertRaises(pystac.STACTypeError):
_ = pystac.Item.from_dict(catalog_dict)

def test_geo_interface(self) -> None:
item = pystac.Item.from_file(
TestCases.get_path("data-files/item/sample-item.json")
)
self.assertEqual(item.geometry, item.__geo_interface__)

def test_relative_extension_path(self) -> None:
item = pystac.Item.from_file(
TestCases.get_path(
Expand Down Expand Up @@ -466,3 +460,11 @@ def test_remove_hierarchical_links(
for link in item.links:
assert not link.is_hierarchical()
assert bool(item.get_single_link("canonical")) == add_canonical


def test_geo_interface() -> None:
item = pystac.Item.from_file(TestCases.get_path("data-files/item/sample-item.json"))
assert (
item.to_dict(include_self_link=False, transform_hrefs=False)
== item.__geo_interface__
)