Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tess simplification #595

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from shapely import coverage_simplify
tess.geometry = coverage_simplify(
tess.geometry = shapely.coverage_simplify(

We do import shapely on top

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