Skip to content

Commit

Permalink
Raise ValueError for non-finite distance to buffer/offset_curve
Browse files Browse the repository at this point in the history
  • Loading branch information
mwtoews committed Sep 5, 2022
1 parent 73ff48c commit 3faf926
Show file tree
Hide file tree
Showing 4 changed files with 23 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 @@ -444,6 +444,8 @@ def buffer(

if mitre_limit == 0.0:
raise ValueError("Cannot compute offset from zero-length line segment")
elif not math.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 math

import shapely
from shapely.geometry.base import BaseGeometry, JOIN_STYLE
Expand Down Expand Up @@ -141,6 +142,8 @@ def offset_curve(
"""
if mitre_limit == 0.0:
raise ValueError("Cannot compute offset from zero-length line segment")
elif not math.isfinite(distance):
raise ValueError("offset_curve distance must be finite")
if side == "right":
distance *= -1
return shapely.offset_curve(self, distance, resolution, join_style, mitre_limit)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_buffer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import pytest

from . import unittest
from shapely import geometry
import pytest


@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 BufferSingleSidedCase(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 3faf926

Please sign in to comment.