Skip to content

Commit

Permalink
Make geometry collections hashable
Browse files Browse the repository at this point in the history
  • Loading branch information
cleder committed Oct 28, 2023
1 parent 4ba9aab commit 0b98057
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/HISTORY.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog
------------------

- remove Python 3.7 support
- Geometries are now immutable and hashable

1.1.1 (2023/10/27)
------------------
Expand Down
4 changes: 4 additions & 0 deletions pygeoif/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,10 @@ def __eq__(self, other: object) -> bool:
second=other.__geo_interface__, # type: ignore [attr-defined]
)

def __hash__(self) -> int:
"""Return the hash of the collection."""
return hash(self.wkt)

def __len__(self) -> int:
"""
Length of the collection.
Expand Down
18 changes: 13 additions & 5 deletions tests/test_geometrycollection.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Test Baseclass."""

import pytest

from pygeoif import geometry


Expand Down Expand Up @@ -429,7 +427,6 @@ def test_nested_geometry_collection_repr_eval() -> None:
)


@pytest.mark.xfail(reason="GeometryCollection is not hashable")
def test_nested_geometry_collection_hash() -> None:
multipoint = geometry.MultiPoint([(0, 0), (1, 1), (1, 2), (2, 2)])
gc1 = geometry.GeometryCollection([geometry.Point(0, 0), multipoint])
Expand All @@ -445,10 +442,21 @@ def test_nested_geometry_collection_hash() -> None:
line = geometry.LineString([(0, 0), (1, 1)])
gc = geometry.GeometryCollection([gc2, poly1, poly2, p0, p1, ring, line])

assert hash(gc) == 0
assert hash(gc) == hash(
geometry.GeometryCollection(
[
gc2,
poly1,
poly2,
p0,
p1,
ring,
line,
],
),
)


@pytest.mark.xfail(reason="GeometryCollection is not hashable")
def test_hash_empty() -> None:
gc = geometry.GeometryCollection([])

Expand Down

0 comments on commit 0b98057

Please sign in to comment.