forked from cnheider/jord
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
183 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,4 @@ | |
from .transformation import * | ||
from .rings import * | ||
from .selection import * | ||
from .grouping import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# -*- coding: utf-8 -*- | ||
from typing import Mapping, Sequence, Union | ||
|
||
import shapely | ||
from shapely import unary_union | ||
|
||
from jord.shapely_utilities.geometry_types import is_multi | ||
|
||
__all__ = ["overlap_groups"] | ||
|
||
|
||
def overlap_groups(to_be_grouped: Union[Sequence, Mapping]) -> 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())) | ||
groups = [] | ||
already_grouped = [] | ||
|
||
if not is_multi(union): | ||
groups.append(to_be_grouped) | ||
else: | ||
for union_part in union.geoms: | ||
intersectors = {} | ||
for k, v in to_be_grouped.items(): | ||
if shapely.intersects(v, union_part): | ||
assert k not in already_grouped | ||
intersectors[k] = v | ||
already_grouped.append(k) | ||
groups.append(intersectors) | ||
|
||
return groups | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
def demo(): | ||
from shapely.geometry import box | ||
from pprint import pprint | ||
|
||
data = [ | ||
box(1, 1, 3, 3), | ||
box(2, 2, 3, 3), | ||
box(4, 4, 6, 6), | ||
box(4, 4, 5, 5), | ||
box(5, 5, 6, 6), | ||
box(7, 7, 8, 8), | ||
box(1, 1, 2, 2), | ||
box(4, 4, 6, 6), | ||
] | ||
|
||
pprint(overlap_groups(data)) | ||
|
||
demo() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# -*- coding: utf-8 -*- | ||
import shapely | ||
|
||
from jord.shapely_utilities import is_multi | ||
|
||
|
||
def test_line_string_is_multi(): | ||
assert is_multi(shapely.LineString([[0, 0], [1, 0], [1, 1]])) == False | ||
|
||
|
||
def test_point_is_multi(): | ||
assert is_multi(shapely.Point([[0, 0]])) == False | ||
|
||
|
||
def test_linear_ring_is_multi(): | ||
assert is_multi(shapely.LinearRing(((0, 0), (0, 1), (1, 1), (1, 0)))) == False | ||
|
||
|
||
def test_polygon_is_multi(): | ||
assert ( | ||
is_multi( | ||
shapely.Polygon( | ||
((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)) | ||
) | ||
) | ||
== False | ||
) | ||
|
||
|
||
def test_multi_point_is_multi(): | ||
assert is_multi(shapely.MultiPoint([[0.0, 0.0], [1.0, 2.0]])) | ||
|
||
|
||
def test_multi_line_string_is_multi(): | ||
assert is_multi(shapely.MultiLineString([[[0, 0], [1, 2]], [[4, 4], [5, 6]]])) | ||
|
||
|
||
def test_multi_polygon_is_multi(): | ||
assert is_multi( | ||
shapely.MultiPolygon( | ||
[ | ||
( | ||
((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)), | ||
[((0.1, 0.1), (0.1, 0.2), (0.2, 0.2), (0.2, 0.1))], | ||
) | ||
] | ||
) | ||
) | ||
|
||
|
||
def test_geometry_collection_is_multi(): | ||
assert is_multi( | ||
shapely.GeometryCollection( | ||
[shapely.Point(51, -1), shapely.LineString([(52, -1), (49, 2)])] | ||
) | ||
) |