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

keep only a single dangle in a loop #140

Merged
merged 1 commit into from
Aug 5, 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
78 changes: 32 additions & 46 deletions core/algorithms/simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,57 +346,43 @@ def loop(
)
split_points.extend(splitters)

possible_dangle = possible_dangle[shapely.disjoint(possible_dangle, dropped)]
n_comps = graph.Graph.build_contiguity(
gpd.GeoSeries(possible_dangle), rook=False
).n_components
if n_comps == 1:
logger.debug("LOOP components 1")
dangle_coins = momepy.COINS(
gpd.GeoSeries(shapely.line_merge(possible_dangle)).explode(),
flow_mode=True,
).stroke_gdf()
candidate = dangle_coins.loc[dangle_coins.length.idxmax()].geometry
if candidate.intersects(snap_to.union_all().buffer(eps)) and (
candidate.length > min_dangle_length
):
logger.debug("LOOP intersects and length > min_dangle_length")
if not primes.empty:
points = [
shapely.get_point(candidate, 0),
shapely.get_point(candidate, -1),
]
distances = shapely.distance(points, highest_hierarchy.union_all())
if distances.max() > min_dangle_length:
logger.debug("LOOP prime check passed")
to_add.append(candidate)
else:
to_add.append(candidate)
elif n_comps > 1:
possible_dangle = gpd.GeoDataFrame(
geometry=possible_dangle[shapely.disjoint(possible_dangle, dropped)]
)
comps = graph.Graph.build_contiguity(
possible_dangle.difference(snap_to.union_all().buffer(eps)), rook=False
)
if comps.n_components > 1:
# NOTE: it is unclear to me what exactly should happen here. I believe that
# there will be cases when we may want to keep multiple dangles. Now keeping
# only one.
logger.debug("LOOP components many")
dangle_coins = momepy.COINS(
gpd.GeoSeries(shapely.line_merge(possible_dangle)).explode(),
flow_mode=True,
).stroke_gdf()
candidate = dangle_coins.loc[dangle_coins.length.idxmax()].geometry
if candidate.intersects(snap_to.union_all().buffer(eps)) and (
candidate.length > min_dangle_length
):
logger.debug("LOOP intersects and length > min_dangle_length")
if not primes.empty:
points = [
shapely.get_point(candidate, 0),
shapely.get_point(candidate, -1),
]
distances = shapely.distance(points, highest_hierarchy.union_all())
if distances.max() > min_dangle_length:
logger.debug("LOOP prime check passed")
to_add.append(candidate)
else:
comp_labels = comps.component_labels.values
longest_component = possible_dangle.dissolve(
comps.component_labels
).length.idxmax()
possible_dangle = possible_dangle[comp_labels == longest_component]

dangle_coins = momepy.COINS(
possible_dangle,
flow_mode=True,
).stroke_gdf()
candidate = dangle_coins.loc[dangle_coins.length.idxmax()].geometry
if candidate.intersects(snap_to.union_all().buffer(eps)) and (
candidate.length > min_dangle_length
):
logger.debug("LOOP intersects and length > min_dangle_length")
if not primes.empty:
points = [
shapely.get_point(candidate, 0),
shapely.get_point(candidate, -1),
]
distances = shapely.distance(points, highest_hierarchy.union_all())
if distances.max() > min_dangle_length:
logger.debug("LOOP prime check passed")
to_add.append(candidate)
else:
to_add.append(candidate)

return to_add

Expand Down
Loading