Skip to content
Merged
Changes from 7 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
24 changes: 21 additions & 3 deletions momepy/functional/_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
GPD_GE_013 = Version(gpd.__version__) >= Version("0.13.0")
GPD_GE_10 = Version(gpd.__version__) >= Version("1.0dev")
LPS_GE_411 = Version(libpysal.__version__) >= Version("4.11.dev")
SHPLY_GE_210 = Version(shapely.__version__) >= Version("2.1")

__all__ = [
"morphological_tessellation",
Expand Down Expand Up @@ -101,6 +102,7 @@ def enclosed_tessellation(
shrink: float = 0.4,
segment: float = 0.5,
threshold: float = 0.05,
simplify: bool = False,
n_jobs: int = -1,
) -> GeoDataFrame:
"""Generate enclosed tessellation
Expand Down Expand Up @@ -148,6 +150,9 @@ def enclosed_tessellation(
inlude it in the tessellation of that enclosure. Resolves sliver geometry
issues. If None, the check is skipped and all intersecting buildings are
considered. By default 0.05
simplify: bool, optional
Whether to attempt to simplify the resulting tesselation boundaries with
``shapely.coverage_simplify``. By default False.
n_jobs : int, optional
The number of jobs to run in parallel. -1 means using all available cores.
By default -1
Expand All @@ -166,6 +171,9 @@ def enclosed_tessellation(
momepy.verify_tessellation
"""

if simplify and not SHPLY_GE_210:
raise ImportError("Coverage simplification requires shapely 2.0.5 or higher.")

# convert to GeoDataFrame and add position (we will need it later)
enclosures = enclosures.geometry.to_frame()
enclosures["position"] = range(len(enclosures))
Expand Down Expand Up @@ -202,7 +210,8 @@ def enclosed_tessellation(

# generate tessellation in parallel
new = Parallel(n_jobs=n_jobs)(
delayed(_tess)(*t, threshold, shrink, segment, index_name) for t in tuples
delayed(_tess)(*t, threshold, shrink, segment, index_name, simplify)
for t in tuples
)

new_df = pd.concat(new, axis=0)
Expand Down Expand Up @@ -234,7 +243,7 @@ def enclosed_tessellation(
return pd.concat([new_df, singles.drop(columns="position"), clean_blocks])


def _tess(ix, poly, blg, threshold, shrink, segment, enclosure_id):
def _tess(ix, poly, blg, threshold, shrink, segment, enclosure_id, to_simplify):
"""Generate tessellation for a single enclosure. Helper for enclosed_tessellation"""
# check if threshold is set and filter buildings based on the threshold
if threshold:
Expand All @@ -252,13 +261,22 @@ def _tess(ix, poly, blg, threshold, shrink, segment, enclosure_id):
return_input=False,
as_gdf=True,
)
if to_simplify:
from shapely import coverage_simplify

tess.geometry = coverage_simplify(
tess.geometry, tolerance=1e-1, simplify_boundary=False
)
tess[enclosure_id] = ix
return tess

## in case a single building is left in blg
assigned_ix = ix if len(blg) == 1 else -1

return GeoDataFrame(
{enclosure_id: ix},
geometry=[poly],
index=[-1],
index=[assigned_ix],
crs=blg.crs,
)

Expand Down