Skip to content

Commit

Permalink
Avoid in-place multiplication of affinity paramters (#794)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwtoews authored and sgillies committed Nov 22, 2019
1 parent bd588dd commit 97ab1eb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
6 changes: 3 additions & 3 deletions shapely/affinity.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def rotate(geom, angle, origin='center', use_radians=False):
if geom.is_empty:
return geom
if not use_radians: # convert from degrees
angle *= pi/180.0
angle = angle * pi/180.0
cosp = cos(angle)
sinp = sin(angle)
if abs(cosp) < 2.5e-16:
Expand Down Expand Up @@ -227,8 +227,8 @@ def skew(geom, xs=0.0, ys=0.0, origin='center', use_radians=False):
if geom.is_empty:
return geom
if not use_radians: # convert from degrees
xs *= pi/180.0
ys *= pi/180.0
xs = xs * pi/180.0
ys = ys * pi/180.0
tanx = tan(xs)
tany = tan(ys)
if abs(tanx) < 2.5e-16:
Expand Down
39 changes: 39 additions & 0 deletions tests/test_affinity.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
from shapely.wkt import loads as load_wkt
from shapely.geometry import Point

try:
import numpy
except ImportError:
numpy = False


class AffineTestCase(unittest.TestCase):

Expand Down Expand Up @@ -161,6 +166,21 @@ def test_rotate_empty(self):
els = load_wkt('LINESTRING EMPTY')
self.assertTrue(rls.equals(els))

@unittest.skipIf(not numpy, 'numpy not installed')
def test_rotate_angle_array(self):
ls = load_wkt('LINESTRING(240 400, 240 300, 300 300)')
els = load_wkt('LINESTRING(220 320, 320 320, 320 380)')
# check with degrees
theta = numpy.array([90.0])
rls = affinity.rotate(ls, theta)
self.assertEqual(theta[0], 90.0)
self.assertTrue(rls.equals(els))
# check with radians
theta = numpy.array([pi/2])
rls = affinity.rotate(ls, theta, use_radians=True)
self.assertEqual(theta[0], pi/2)
self.assertTrue(rls.equals(els))

def test_scale(self):
ls = load_wkt('LINESTRING(240 400 10, 240 300 30, 300 300 20)')
# test defaults of 1.0
Expand Down Expand Up @@ -243,6 +263,25 @@ def test_skew_empty(self):
els = load_wkt('LINESTRING EMPTY')
self.assertTrue(sls.equals(els))

@unittest.skipIf(not numpy, 'numpy not installed')
def test_skew_xs_ys_array(self):
ls = load_wkt('LINESTRING(240 400 10, 240 300 30, 300 300 20)')
els = load_wkt('LINESTRING (253.39745962155615 417.3205080756888, '
'226.60254037844385 317.3205080756888, '
'286.60254037844385 282.67949192431126)')
# check with degrees
xs_ys = numpy.array([15.0, -30.0])
sls = affinity.skew(ls, xs_ys[0:1], xs_ys[1:2])
self.assertEqual(xs_ys[0], 15.0)
self.assertEqual(xs_ys[1], -30.0)
self.assertTrue(sls.almost_equals(els))
# check with radians
xs_ys = numpy.array([pi/12, -pi/6])
sls = affinity.skew(ls, xs_ys[0:1], xs_ys[1:2], use_radians=True)
self.assertEqual(xs_ys[0], pi/12)
self.assertEqual(xs_ys[1], -pi/6)
self.assertTrue(sls.almost_equals(els))

def test_translate(self):
ls = load_wkt('LINESTRING(240 400 10, 240 300 30, 300 300 20)')
# test default offset of 0.0
Expand Down

0 comments on commit 97ab1eb

Please sign in to comment.