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

ENH: reimplement tessellation using shapely #538

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
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
56 changes: 56 additions & 0 deletions momepy/functional/_elements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import geopandas as gpd
import shapely

Check warning on line 2 in momepy/functional/_elements.py

View check run for this annotation

Codecov / codecov/patch

momepy/functional/_elements.py#L1-L2

Added lines #L1 - L2 were not covered by tests


def tessellation(

Check warning on line 5 in momepy/functional/_elements.py

View check run for this annotation

Codecov / codecov/patch

momepy/functional/_elements.py#L5

Added line #L5 was not covered by tests
gdf,
limit=None,
shrink=0.4,
segment=0.5,
):
if gdf.crs and gdf.crs.is_geographic:
raise ValueError(

Check warning on line 12 in momepy/functional/_elements.py

View check run for this annotation

Codecov / codecov/patch

momepy/functional/_elements.py#L11-L12

Added lines #L11 - L12 were not covered by tests
"Geometry is in a geographic CRS. "
"Use 'GeoDataFrame.to_crs()' to re-project geometries to a "
"projected CRS before using Tessellation.",
)

objects = shapely.set_precision(gdf.geometry.copy(), 0.00001)
geom_types = objects.geom_type
mask_poly = geom_types.isin(["Polygon", "MultiPolygon"])
mask_line = objects.geom_type.isin(["LineString", "MultiLineString"])

Check warning on line 21 in momepy/functional/_elements.py

View check run for this annotation

Codecov / codecov/patch

momepy/functional/_elements.py#L18-L21

Added lines #L18 - L21 were not covered by tests

if shrink != 0 and mask_poly.any():
objects[mask_poly] = (

Check warning on line 24 in momepy/functional/_elements.py

View check run for this annotation

Codecov / codecov/patch

momepy/functional/_elements.py#L23-L24

Added lines #L23 - L24 were not covered by tests
objects[mask_poly]
.buffer(-shrink, cap_style=2, join_style=2)
.segmentize(segment)
)

# exclude conincident points
if mask_line.any():
objects[mask_line] = (

Check warning on line 32 in momepy/functional/_elements.py

View check run for this annotation

Codecov / codecov/patch

momepy/functional/_elements.py#L31-L32

Added lines #L31 - L32 were not covered by tests
objects.loc[mask_line]
.segmentize(segment)
.get_coordinates(index_parts=True)
.drop_duplicates(keep=False)
.groupby(level=0)
.apply(shapely.multipoints)
)

voronoi = shapely.voronoi_polygons(

Check warning on line 41 in momepy/functional/_elements.py

View check run for this annotation

Codecov / codecov/patch

momepy/functional/_elements.py#L41

Added line #L41 was not covered by tests
shapely.GeometryCollection(objects.values), extend_to=limit
)
geoms = gpd.GeoSeries(shapely.get_parts(voronoi), crs=gdf.crs)
ids_objects, ids_geoms = geoms.sindex.query(objects, predicate="intersects")
polygons = (

Check warning on line 46 in momepy/functional/_elements.py

View check run for this annotation

Codecov / codecov/patch

momepy/functional/_elements.py#L44-L46

Added lines #L44 - L46 were not covered by tests
geoms.iloc[ids_geoms]
.groupby(objects.index.take(ids_objects))
.agg(shapely.coverage_union_all)
).set_crs(gdf.crs)

if limit is not None:
to_be_clipped = polygons.sindex.query(limit.boundary, "intersects")
polygons.iloc[to_be_clipped] = polygons.iloc[to_be_clipped].intersection(limit)

Check warning on line 54 in momepy/functional/_elements.py

View check run for this annotation

Codecov / codecov/patch

momepy/functional/_elements.py#L52-L54

Added lines #L52 - L54 were not covered by tests

return polygons

Check warning on line 56 in momepy/functional/_elements.py

View check run for this annotation

Codecov / codecov/patch

momepy/functional/_elements.py#L56

Added line #L56 was not covered by tests