From b57ca26e50bbec5512dba7bd8151956d0757a3f3 Mon Sep 17 00:00:00 2001 From: Christian Heider Nielsen Date: Thu, 7 Dec 2023 15:06:12 +0100 Subject: [PATCH] bump --- jord/__init__.py | 2 +- jord/shapely_utilities/grouping.py | 26 +++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/jord/__init__.py b/jord/__init__.py index a73dcd5..58294b3 100755 --- a/jord/__init__.py +++ b/jord/__init__.py @@ -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 diff --git a/jord/shapely_utilities/grouping.py b/jord/shapely_utilities/grouping.py index 15a10d1..6a5da41 100644 --- a/jord/shapely_utilities/grouping.py +++ b/jord/shapely_utilities/grouping.py @@ -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 = [] @@ -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) @@ -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()