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

doc work + type hints + refactor – artifacts.py -- dangles #125

Merged
merged 4 commits into from
Dec 2, 2024
Merged
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
52 changes: 34 additions & 18 deletions sgeop/artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,27 @@ def reconnect(conts_groups, new_connections, artifact, split_points, eps):
return new_connections


def remove_dangles(new_connections, artifact, eps=1e-4):
# the drop above could've introduced a dangling edges. Remove those.
def remove_dangles(
new_connections: np.ndarray,
artifact: gpd.GeoDataFrame,
eps: float = 1e-4,
) -> np.ndarray:
"""Dropping lines can introduce dangling edges. Remove those.

Parameters
----------
new_connections : np.ndarray
New linestring for reconnections.
artifact : geopandas.GeoDataFrame
The polygonal representation of the artifact.
eps : float = 1e-4
Small tolerance epsilon.

Returns
-------
np.ndarray
``new_connections`` without dangling edges.
"""

new_connections = shapely.line_merge(new_connections)
pts0 = shapely.get_point(new_connections, 0)
Expand Down Expand Up @@ -1408,22 +1427,19 @@ def nx_gx_cluster(
to_add.extend(lines_to_add)


def is_dangle(edgelines):
first = shapely.get_point(edgelines, 0)
last = shapely.get_point(edgelines, -1)
first_ix, edge_ix1 = edgelines.sindex.query(first, predicate="intersects")
first_sum = sparse.coo_array(
([True] * len(first_ix), (first_ix, edge_ix1)),
shape=(len(edgelines), len(edgelines)),
dtype=np.bool_,
).sum(axis=1)

last_ix, edge_ix1 = edgelines.sindex.query(last, predicate="intersects")
last_sum = sparse.coo_array(
([True] * len(last_ix), (last_ix, edge_ix1)),
shape=(len(edgelines), len(edgelines)),
dtype=np.bool_,
).sum(axis=1)
def is_dangle(edgelines: gpd.GeoSeries) -> bool:
"""Determine if an edge is dangling or not."""

def _sum_intersects(loc: int) -> int:
"""Sum the number of places linestrings intersect each other."""
point = shapely.get_point(edgelines, loc)
ix, edge_ix1 = edgelines.sindex.query(point, predicate="intersects")
data = ([True] * len(ix), (ix, edge_ix1))
return sparse.coo_array(data, shape=shape, dtype=np.bool_).sum(axis=1)

shape = (len(edgelines), len(edgelines))
first_sum = _sum_intersects(0)
last_sum = _sum_intersects(-1)

return (first_sum == 1) | (last_sum == 1)

Expand Down
Loading