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

Raise ValueError for non-finite distance to buffer/parallel_offset (maint-1.8) #1516

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions shapely/geometry/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,8 @@ def buffer(self, distance, resolution=16, quadsegs=None,
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")

if 'buffer_with_params' in self.impl:
params = self._lgeos.GEOSBufferParams_create()
Expand Down
3 changes: 3 additions & 0 deletions shapely/geometry/linestring.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""

from ctypes import c_double
import math
import warnings

from shapely.coords import CoordinateSequence
Expand Down Expand Up @@ -171,6 +172,8 @@ def parallel_offset(
if mitre_limit == 0.0:
raise ValueError(
'Cannot compute offset from zero-length line segment')
elif not math.isfinite(distance):
raise ValueError("parallel_offset distance must be finite")
try:
return geom_factory(self.impl['parallel_offset'](
self, distance, resolution, join_style, mitre_limit, side))
Expand Down
9 changes: 9 additions & 0 deletions tests/test_buffer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import pytest

from . import unittest
from shapely import geometry


@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
11 changes: 10 additions & 1 deletion tests/test_parallel_offset.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import pytest

from . import unittest
from shapely.geometry import LineString, LinearRing
from shapely.wkt import loads


@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):

Expand Down