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

BUG: refactor computation of angles in COINS #632

Merged
merged 20 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
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
144 changes: 68 additions & 76 deletions docs/user_guide/graph/coins.ipynb

Large diffs are not rendered by default.

158 changes: 47 additions & 111 deletions momepy/coins.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@
----------
edge_gdf : GeoDataFrame
A GeoDataFrame containing edge geometry of a street network.
``edge_gdf`` cannot contain identical or overlapping LineStrings.
``edge_gdf`` should ideally not contain MultiLineStrings.
angle_threshold : int, float (default 0)
The angle threshold for the COINS algorithm. Segments will only be considered
a part of the same street if the deflection angle is above the threshold.
angle_threshold : int, float (default 0), units: degrees
The threshold for the interior angle within the COINS algorithm.
Possible values: ``0 <= angle_threshold < 180``, in degrees.
Segments will only be considered part of the same stroke group
if the interior angle between them is above the threshold.

Examples
--------
Expand Down Expand Up @@ -126,7 +129,7 @@
out_line.append(
[
part,
_compute_orientation(part),
[],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

since the COINS algorithm operates with position-based (index-based) attributes of lines, the very first attribute being "orientation": this is now obsolete, so this list will always be empty (we're not computing orientation anymore), but keeping it there nevertheless for the moment not to mess up with the index-based accessing of attributes in the rest of the algorithm. (would probably require major refactoring if we wanted to get rid of this issue, for now i guess it's best just not to touch it)

[],
[],
[],
Expand Down Expand Up @@ -378,118 +381,51 @@
return [list(pair) for pair in zip(tmp_list[:-1], tmp_list[1:], strict=True)]


def _compute_angle(point1, point2):
"""Calculates the angle between two points in space."""
height = abs(point2[1] - point1[1])
base = abs(point2[0] - point1[0])
angle = round(math.degrees(math.atan(height / base)), 3)
return angle


def _compute_orientation(line):
"""Calculates the orientation of a line segment. Point1 is
the lower one on the y-axes and vice versa for Point2.
def _angle_between_two_lines(line1, line2):
"""
Computes interior angle between 2 lines.
input: line<x> ... list of 2 tuples (x,y);
line1 and line2 by definition share one unique tuple
(overlap in 1 point)
returns: interior angle in degrees 0<alpha<=180
(we assume that line1!=line2, so alpha=0 not possible)
"""
point1 = line[1]
point2 = line[0]

# If the latutide of a point is less and the longitude is more, or
# If the latitude of a point is more and the longitude is less, then
# the point is oriented leftward and wil have negative orientation.
if ((point2[0] > point1[0]) and (point2[1] < point1[1])) or (
(point2[0] < point1[0]) and (point2[1] > point1[1])
):
return -_compute_angle(point1, point2)

# if the latitudes are same, the line is horizontal
elif point2[1] == point1[1]:
return 0

# if the longitudes are same, the line is vertical
elif point2[0] == point1[0]:
return 90

return _compute_angle(point1, point2)


def _points_set_angle(line1, line2):
"""Calculate the acute joining angle between two given set of points."""
l1orien = _compute_orientation(line1)
l2orien = _compute_orientation(line2)

if ((l1orien > 0) and (l2orien < 0)) or ((l1orien < 0) and (l2orien > 0)):
return abs(l1orien) + abs(l2orien)

elif ((l1orien > 0) and (l2orien > 0)) or ((l1orien < 0) and (l2orien < 0)):
theta1 = abs(l1orien) + 180 - abs(l2orien)
theta2 = abs(l2orien) + 180 - abs(l1orien)
if theta1 < theta2:
return theta1
else:
return theta2

elif (l1orien == 0) or (l2orien == 0):
if l1orien < 0:
return 180 - abs(l1orien)
elif l2orien < 0:
return 180 - abs(l2orien)
else:
return 180 - (
abs(_compute_orientation(line1)) + abs(_compute_orientation(line2))
)

elif l1orien == l2orien:
return 180

# extract points
a, b = tuple(line1[0]), tuple(line1[1])
c, d = tuple(line2[0]), tuple(line2[1])

def _angle_between_two_lines(line1, line2):
"""Calculate the joining angle between two line segments."""
l1p1, l1p2 = line1
l2p1, l2p2 = line2
l1orien = _compute_orientation(line1)
l2orien = _compute_orientation(line2)

# If both lines have same orientation, return 180 If one of the
# lines is zero, exception for that If both the lines are on same side
# of the horizontal plane, calculate 180-(sumOfOrientation) If both the
# lines are on same side of the vertical plane, calculate pointSetAngle.
if l1orien == l2orien:
angle = 180

elif (l1orien == 0) or (l2orien == 0):
angle = _points_set_angle(line1, line2)

elif l1p1 == l2p1:
if ((l1p1[1] > l1p2[1]) and (l1p1[1] > l2p2[1])) or (
(l1p1[1] < l1p2[1]) and (l1p1[1] < l2p2[1])
):
angle = 180 - (abs(l1orien) + abs(l2orien))
else:
angle = _points_set_angle([l1p1, l1p2], [l2p1, l2p2])
# assertion: we expect exactly 2 of the 4 points to be identical
# (lines touch in this point)
points = collections.Counter([a, b, c, d])

elif l1p1 == l2p2:
if ((l1p1[1] > l2p1[1]) and (l1p1[1] > l1p2[1])) or (
(l1p1[1] < l2p1[1]) and (l1p1[1] < l1p2[1])
):
angle = 180 - (abs(l1orien) + abs(l2orien))
else:
angle = _points_set_angle([l1p1, l1p2], [l2p2, l2p1])

elif l1p2 == l2p1:
if ((l1p2[1] > l1p1[1]) and (l1p2[1] > l2p2[1])) or (
(l1p2[1] < l1p1[1]) and (l1p2[1] < l2p2[1])
):
angle = 180 - (abs(l1orien) + abs(l2orien))
else:
angle = _points_set_angle([l1p2, l1p1], [l2p1, l2p2])
# make sure lines are not identical
if len(points) == 2:
raise ValueError(

Check warning on line 404 in momepy/coins.py

View check run for this annotation

Codecov / codecov/patch

momepy/coins.py#L404

Added line #L404 was not covered by tests
"Lines are identical. Please revise input data "
"to ensure no lines are identical or overlapping. "
"You can check for duplicates using `gdf.geometry.duplicated()`."
)

elif l1p2 == l2p2:
if ((l1p2[1] > l1p1[1]) and (l1p2[1] > l2p1[1])) or (
(l1p2[1] < l1p1[1]) and (l1p2[1] < l2p1[1])
):
angle = 180 - (abs(l1orien) + abs(l2orien))
else:
angle = _points_set_angle([l1p2, l1p1], [l2p2, l2p1])
# make sure lines do touch
if len(points) == 4:
raise ValueError("Lines do not touch.")

Check warning on line 412 in momepy/coins.py

View check run for this annotation

Codecov / codecov/patch

momepy/coins.py#L412

Added line #L412 was not covered by tests

# points where line touch = "origin" (for vector-based angle calculation)
origin = [k for k, v in points.items() if v == 2][0]
# other 2 unique points (one on each line)
point1, point2 = (k for k, v in points.items() if v == 1)

# translate lines into vectors (numpy arrays)
v1 = [point1[0] - origin[0], point1[1] - origin[1]]
v2 = [point2[0] - origin[0], point2[1] - origin[1]]

# compute angle between 2 vectors in degrees
dot_product = v1[0] * v2[0] + v1[1] * v2[1]
norm_v1 = math.sqrt(v1[0] ** 2 + v1[1] ** 2)
norm_v2 = math.sqrt(v2[0] ** 2 + v2[1] ** 2)
cos_theta = dot_product / (norm_v1 * norm_v2)
angle = math.degrees(math.acos(cos_theta))

return angle

Expand Down
107 changes: 106 additions & 1 deletion momepy/tests/test_coins.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pandas as pd
import pytest
from pandas.testing import assert_index_equal, assert_series_equal
from shapely.geometry import LineString

import momepy as mm

Expand Down Expand Up @@ -111,4 +112,108 @@ def test_premerge(self):
)
assert_index_equal(result.columns, expected_columns)

assert not result.isna().any().any()
assert not result.drop(columns="orientation").isna().any().any()

def test_sharp_angles(self):
# test case 1
a = (0, 0)
b = (2, 0)
c = (1, 1)
d = (2, 1)
e = (-1, 1)

line1 = [a, b]
line2 = [a, c]
line3 = [a, d]
line4 = [a, e]

gdf = gpd.GeoDataFrame(
{
"geometry": [
LineString(line1),
LineString(line2),
LineString(line3),
LineString(line4),
]
}
)

coins = mm.COINS(gdf, angle_threshold=0)
stroke_attr = coins.stroke_attribute()

expected_groups = [
bool(stroke_attr[0] == 0),
bool(stroke_attr[1] == 1),
bool(stroke_attr[2] == 2),
bool(stroke_attr[3] == 0),
bool(stroke_attr[0] != stroke_attr[2]),
]

assert all(expected_groups)

# test case 2

a = (0, 0)
b = (-1, 1)
c = (-4, 1)
d = (-2, -1)
e = (-1, -2)

line1 = [a, b]
line2 = [a, c]
line3 = [a, d]
line4 = [a, e]

gdf = gpd.GeoDataFrame(
{
"geometry": [
LineString(line1),
LineString(line2),
LineString(line3),
LineString(line4),
]
}
)

coins = mm.COINS(gdf, angle_threshold=0)
stroke_attr = coins.stroke_attribute()

expected_groups = [
bool(stroke_attr[0] == 0),
bool(stroke_attr[1] == 1),
bool(stroke_attr[2] == 2),
bool(stroke_attr[3] == 0),
bool(stroke_attr[0] != stroke_attr[2]),
]

assert all(expected_groups)

# test case 3
a = (0, 0)
b = (1, 1)
c = (0, 1)

line1 = [a, b]
line2 = [a, c]

gdf = gpd.GeoDataFrame(
{
"geometry": [
LineString(line1),
LineString(line2),
]
}
)

# interior angle is 45deg, angle_threshold is 0:
# expecting both lines to be part of same group
coins = mm.COINS(gdf, angle_threshold=0)
stroke_attr = coins.stroke_attribute()
assert bool(stroke_attr[0] == stroke_attr[1] == 0)

# interior angle is 45deg, angle_threshold is 46:
# expecting lines to in different groups
coins = mm.COINS(gdf, angle_threshold=46)
stroke_attr = coins.stroke_attribute()
# expecting both lines to be part of same group
assert stroke_attr[0] != stroke_attr[1]
Loading