Skip to content

Commit

Permalink
bump
Browse files Browse the repository at this point in the history
  • Loading branch information
cnheider committed Dec 7, 2023
1 parent 59c8de6 commit b57ca26
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion jord/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

__project__ = "Jord"
__author__ = "Christian Heider Lindbjerg"
__version__ = "0.2.8"
__version__ = "0.2.9"
__doc__ = r"""
.. module:: jord
:platform: Unix, Windows
Expand Down
26 changes: 23 additions & 3 deletions jord/shapely_utilities/grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,26 @@
import shapely
from shapely import unary_union

from jord.shapely_utilities import closing
from jord.shapely_utilities.geometry_types import is_multi

__all__ = ["overlap_groups"]


def overlap_groups(to_be_grouped: Union[Sequence, Mapping]) -> Sequence[Mapping]:
def overlap_groups(
to_be_grouped: Union[Sequence, Mapping], must_be_unique: bool = False
) -> Sequence[Mapping]:
if isinstance(to_be_grouped, Mapping):
...
else:
to_be_grouped = dict(zip((i for i in range(len(to_be_grouped))), to_be_grouped))

union = unary_union(list(to_be_grouped.values()))
if must_be_unique:
assert not any(is_multi(p) for p in to_be_grouped.values()), to_be_grouped

s = list(unary_union(v) for v in to_be_grouped.values())

union = closing(unary_union(s)).buffer(0)
groups = []
already_grouped = []

Expand All @@ -26,7 +34,8 @@ def overlap_groups(to_be_grouped: Union[Sequence, Mapping]) -> Sequence[Mapping]
intersectors = {}
for k, v in to_be_grouped.items():
if shapely.intersects(v, union_part):
assert k not in already_grouped
if must_be_unique:
assert k not in already_grouped, f"{k, already_grouped, v}"
intersectors[k] = v
already_grouped.append(k)
groups.append(intersectors)
Expand All @@ -53,4 +62,15 @@ def demo():

pprint(overlap_groups(data))

data = [
box(1, 1, 3, 3),
unary_union([box(2, 2, 3, 3), box(4, 4, 5, 5)]),
box(4, 4, 6, 6),
box(4, 4, 5, 5),
]

pprint(overlap_groups(data))

# pprint(overlap_groups(data, must_be_unique=True)) # FAILS!

demo()

0 comments on commit b57ca26

Please sign in to comment.