Skip to content

Commit

Permalink
warn, don't raise
Browse files Browse the repository at this point in the history
  • Loading branch information
jGaboardi committed Oct 5, 2024
1 parent a29aeb3 commit 19335f7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
16 changes: 14 additions & 2 deletions sgeop/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,16 @@ def angle_between_two_lines(
Based on ``momepy.coins`` but adapted to shapely lines.
"""

return_bad = 0.0

lines_distinct = line1 != line2
if not lines_distinct:
raise ValueError("Input lines are identical - must be distinct.")
warnings.warn(
f"Input lines are identical - must be distinct. Returning {return_bad}",
UserWarning,
stacklevel=2,
)
return return_bad

# extract points
a, b, c, d = shapely.get_coordinates([line1, line2]).tolist()
Expand All @@ -63,7 +70,12 @@ def angle_between_two_lines(

lines_share_vertex = max(points.values()) > 1
if not lines_share_vertex:
raise ValueError("Input lines do not share a vertex.")
warnings.warn(
"Input lines do not share a vertex. Returning {return_bad}",
UserWarning,
stacklevel=2,
)
return return_bad

# points where line touch = "origin" (for vector-based angle calculation)
origin = [k for k, v in points.items() if v == 2][0]
Expand Down
14 changes: 9 additions & 5 deletions sgeop/tests/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,15 @@ def test_q4(self):
assert observed == known

def test_indistinct(self):
with pytest.raises(
ValueError, match="Input lines are identical - must be distinct."
known = 0.0
with pytest.warns(
UserWarning, match="Input lines are identical - must be distinct."
):
sgeop.geometry.angle_between_two_lines(self.line1, self.line1)
observed = sgeop.geometry.angle_between_two_lines(self.line1, self.line1)
assert observed == known

def test_not_adjacent(self):
with pytest.raises(ValueError, match="Input lines do not share a vertex."):
sgeop.geometry.angle_between_two_lines(self.line1, self.line4)
known = 0.0
with pytest.warns(UserWarning, match="Input lines do not share a vertex."):
observed = sgeop.geometry.angle_between_two_lines(self.line1, self.line4)
assert observed == known

0 comments on commit 19335f7

Please sign in to comment.