Skip to content

Commit

Permalink
Raise ValueError for non-finite distance to buffer/offset_curve (#1522)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwtoews committed Oct 29, 2022
1 parent ab0da3f commit da27cdc
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions shapely/geometry/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ def buffer(

if mitre_limit == 0.0:
raise ValueError("Cannot compute offset from zero-length line segment")
elif not np.isfinite(distance):
raise ValueError("buffer distance must be finite")

return shapely.buffer(
self,
Expand Down
3 changes: 3 additions & 0 deletions shapely/geometry/linestring.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Line strings and related utilities
"""
import numpy as np

import shapely
from shapely.geometry.base import BaseGeometry, JOIN_STYLE
Expand Down Expand Up @@ -146,6 +147,8 @@ def offset_curve(
"""
if mitre_limit == 0.0:
raise ValueError("Cannot compute offset from zero-length line segment")
elif not np.isfinite(distance):
raise ValueError("offset_curve distance must be finite")
return shapely.offset_curve(self, distance, quad_segs, join_style, mitre_limit)

def parallel_offset(
Expand Down
7 changes: 7 additions & 0 deletions tests/test_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
from shapely.constructive import BufferCapStyle, BufferJoinStyle


@pytest.mark.parametrize("distance", [float("nan"), float("inf")])
def test_non_finite_distance(distance):
g = geometry.Point(0, 0)
with pytest.raises(ValueError, match="distance must be finite"):
g.buffer(distance)


class BufferTests(unittest.TestCase):
"""Test Buffer Point/Line/Polygon with and without single_sided params"""

Expand Down
9 changes: 9 additions & 0 deletions tests/test_parallel_offset.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import pytest

from . import unittest
from shapely.geometry import LineString, LinearRing
from shapely.testing import assert_geometries_equal


@pytest.mark.parametrize("distance", [float("nan"), float("inf")])
def test_non_finite_distance(distance):
g = LineString([(0, 0), (10, 0)])
with pytest.raises(ValueError, match="distance must be finite"):
g.parallel_offset(distance)


class OperationsTestCase(unittest.TestCase):

def test_parallel_offset_linestring(self):
Expand Down

0 comments on commit da27cdc

Please sign in to comment.