mirror of
https://github.com/django/django.git
synced 2024-12-23 01:25:58 +00:00
Refs #25663 -- Fixed checking of the number of points for LineString if initialized from numpy.array.
This commit is contained in:
parent
97e1d24330
commit
229fc793a0
@ -32,18 +32,22 @@ class LineString(ProjectInterpolateMixin, GEOSGeometry):
|
|||||||
else:
|
else:
|
||||||
coords = args
|
coords = args
|
||||||
|
|
||||||
|
if not (isinstance(coords, (tuple, list)) or numpy and isinstance(coords, numpy.ndarray)):
|
||||||
|
raise TypeError('Invalid initialization input for LineStrings.')
|
||||||
|
|
||||||
|
ncoords = len(coords)
|
||||||
|
if ncoords < self._minlength:
|
||||||
|
raise ValueError(
|
||||||
|
'%s requires at least %d points, got %s.' % (
|
||||||
|
self.__class__.__name__,
|
||||||
|
self._minlength,
|
||||||
|
ncoords,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
if isinstance(coords, (tuple, list)):
|
if isinstance(coords, (tuple, list)):
|
||||||
# Getting the number of coords and the number of dimensions -- which
|
# Getting the number of coords and the number of dimensions -- which
|
||||||
# must stay the same, e.g., no LineString((1, 2), (1, 2, 3)).
|
# must stay the same, e.g., no LineString((1, 2), (1, 2, 3)).
|
||||||
ncoords = len(coords)
|
|
||||||
if ncoords < self._minlength:
|
|
||||||
raise TypeError(
|
|
||||||
'%s requires at least %d points, got %s.' % (
|
|
||||||
self.__class__.__name__,
|
|
||||||
self._minlength,
|
|
||||||
ncoords,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
ndim = None
|
ndim = None
|
||||||
# Incrementing through each of the coordinates and verifying
|
# Incrementing through each of the coordinates and verifying
|
||||||
for coord in coords:
|
for coord in coords:
|
||||||
@ -56,16 +60,13 @@ class LineString(ProjectInterpolateMixin, GEOSGeometry):
|
|||||||
elif len(coord) != ndim:
|
elif len(coord) != ndim:
|
||||||
raise TypeError('Dimension mismatch.')
|
raise TypeError('Dimension mismatch.')
|
||||||
numpy_coords = False
|
numpy_coords = False
|
||||||
elif numpy and isinstance(coords, numpy.ndarray):
|
else:
|
||||||
shape = coords.shape # Using numpy's shape.
|
shape = coords.shape # Using numpy's shape.
|
||||||
if len(shape) != 2:
|
if len(shape) != 2:
|
||||||
raise TypeError('Too many dimensions.')
|
raise TypeError('Too many dimensions.')
|
||||||
self._checkdim(shape[1])
|
self._checkdim(shape[1])
|
||||||
ncoords = shape[0]
|
|
||||||
ndim = shape[1]
|
ndim = shape[1]
|
||||||
numpy_coords = True
|
numpy_coords = True
|
||||||
else:
|
|
||||||
raise TypeError('Invalid initialization input for LineStrings.')
|
|
||||||
|
|
||||||
# Creating a coordinate sequence object because it is easier to
|
# Creating a coordinate sequence object because it is easier to
|
||||||
# set the points using GEOSCoordSeq.__setitem__().
|
# set the points using GEOSCoordSeq.__setitem__().
|
||||||
|
@ -325,11 +325,19 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
|
|||||||
if numpy:
|
if numpy:
|
||||||
self.assertEqual(ls, LineString(numpy.array(ls.tuple))) # as numpy array
|
self.assertEqual(ls, LineString(numpy.array(ls.tuple))) # as numpy array
|
||||||
|
|
||||||
with self.assertRaisesMessage(TypeError, 'Each coordinate should be a sequence (list or tuple)'):
|
with self.assertRaisesMessage(TypeError, 'Each coordinate should be a sequence (list or tuple)'):
|
||||||
LineString((0, 0))
|
LineString((0, 0))
|
||||||
|
|
||||||
with self.assertRaisesMessage(TypeError, 'LineString requires at least 2 points, got 1.'):
|
with self.assertRaisesMessage(ValueError, 'LineString requires at least 2 points, got 1.'):
|
||||||
LineString([(0, 0)])
|
LineString([(0, 0)])
|
||||||
|
|
||||||
|
if numpy:
|
||||||
|
with self.assertRaisesMessage(ValueError, 'LineString requires at least 2 points, got 1.'):
|
||||||
|
LineString(numpy.array([(0, 0)]))
|
||||||
|
|
||||||
|
with mock.patch('django.contrib.gis.geos.linestring.numpy', False):
|
||||||
|
with self.assertRaisesMessage(TypeError, 'Invalid initialization input for LineStrings.'):
|
||||||
|
LineString('wrong input')
|
||||||
|
|
||||||
def test_multilinestring(self):
|
def test_multilinestring(self):
|
||||||
"Testing MultiLineString objects."
|
"Testing MultiLineString objects."
|
||||||
@ -374,12 +382,16 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
|
|||||||
if numpy:
|
if numpy:
|
||||||
self.assertEqual(lr, LinearRing(numpy.array(lr.tuple)))
|
self.assertEqual(lr, LinearRing(numpy.array(lr.tuple)))
|
||||||
|
|
||||||
with self.assertRaisesMessage(TypeError, 'LinearRing requires at least 4 points, got 3.'):
|
with self.assertRaisesMessage(ValueError, 'LinearRing requires at least 4 points, got 3.'):
|
||||||
LinearRing((0, 0), (1, 1), (0, 0))
|
LinearRing((0, 0), (1, 1), (0, 0))
|
||||||
|
|
||||||
with self.assertRaisesMessage(TypeError, 'LinearRing requires at least 4 points, got 1.'):
|
with self.assertRaisesMessage(ValueError, 'LinearRing requires at least 4 points, got 1.'):
|
||||||
LinearRing([(0, 0)])
|
LinearRing([(0, 0)])
|
||||||
|
|
||||||
|
if numpy:
|
||||||
|
with self.assertRaisesMessage(ValueError, 'LinearRing requires at least 4 points, got 1.'):
|
||||||
|
LinearRing(numpy.array([(0, 0)]))
|
||||||
|
|
||||||
def test_polygons_from_bbox(self):
|
def test_polygons_from_bbox(self):
|
||||||
"Testing `from_bbox` class method."
|
"Testing `from_bbox` class method."
|
||||||
bbox = (-180, -90, 180, 90)
|
bbox = (-180, -90, 180, 90)
|
||||||
|
Loading…
Reference in New Issue
Block a user