summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Fedoseev <fedoseev.sergey@gmail.com>2015-11-05 13:25:44 +0500
committerTim Graham <timograham@gmail.com>2015-11-18 18:36:10 -0500
commit1e35dd1a053b620f065101c02069c8e142ebf8ec (patch)
treea54b454c66f20d3f52bbf292748f65571f5005f2
parent7a452c5ce295679307bd81dd9b5f37b3cf762acf (diff)
Fixed #25663 -- Added checking of the number of points for LinearRing and LineString.
-rw-r--r--django/contrib/gis/geos/linestring.py26
-rw-r--r--tests/gis_tests/geos_tests/test_geos.py17
2 files changed, 31 insertions, 12 deletions
diff --git a/django/contrib/gis/geos/linestring.py b/django/contrib/gis/geos/linestring.py
index df2a8145c7..7b74f7700a 100644
--- a/django/contrib/gis/geos/linestring.py
+++ b/django/contrib/gis/geos/linestring.py
@@ -36,16 +36,24 @@ class LineString(ProjectInterpolateMixin, GEOSGeometry):
# Getting the number of coords and the number of dimensions -- which
# must stay the same, e.g., no LineString((1, 2), (1, 2, 3)).
ncoords = len(coords)
- if coords:
- ndim = len(coords[0])
- else:
- raise TypeError('Cannot initialize on empty sequence.')
- self._checkdim(ndim)
+ if ncoords < self._minlength:
+ raise TypeError(
+ '%s requires at least %d points, got %s.' % (
+ self.__class__.__name__,
+ self._minlength,
+ ncoords,
+ )
+ )
+ ndim = None
# Incrementing through each of the coordinates and verifying
- for i in range(1, ncoords):
- if not isinstance(coords[i], (tuple, list, Point)):
- raise TypeError('each coordinate should be a sequence (list or tuple)')
- if len(coords[i]) != ndim:
+ for coord in coords:
+ if not isinstance(coord, (tuple, list, Point)):
+ raise TypeError('Each coordinate should be a sequence (list or tuple)')
+
+ if ndim is None:
+ ndim = len(coord)
+ self._checkdim(ndim)
+ elif len(coord) != ndim:
raise TypeError('Dimension mismatch.')
numpy_coords = False
elif numpy and isinstance(coords, numpy.ndarray):
diff --git a/tests/gis_tests/geos_tests/test_geos.py b/tests/gis_tests/geos_tests/test_geos.py
index 683d00c89f..d983af98ea 100644
--- a/tests/gis_tests/geos_tests/test_geos.py
+++ b/tests/gis_tests/geos_tests/test_geos.py
@@ -3,7 +3,6 @@ from __future__ import unicode_literals
import ctypes
import json
import random
-import unittest
from binascii import a2b_hex, b2a_hex
from io import BytesIO
from unittest import skipUnless
@@ -19,7 +18,7 @@ from django.contrib.gis.geos.base import GEOSBase
from django.contrib.gis.shortcuts import numpy
from django.template import Context
from django.template.engine import Engine
-from django.test import ignore_warnings, mock
+from django.test import SimpleTestCase, ignore_warnings, mock
from django.utils import six
from django.utils.deprecation import RemovedInDjango20Warning
from django.utils.encoding import force_bytes
@@ -29,7 +28,7 @@ from ..test_data import TestDataMixin
@skipUnless(HAS_GEOS, "Geos is required.")
-class GEOSTest(unittest.TestCase, TestDataMixin):
+class GEOSTest(SimpleTestCase, TestDataMixin):
def test_base(self):
"Tests out the GEOSBase class."
@@ -326,6 +325,12 @@ class GEOSTest(unittest.TestCase, TestDataMixin):
if numpy:
self.assertEqual(ls, LineString(numpy.array(ls.tuple))) # as numpy array
+ with self.assertRaisesMessage(TypeError, 'Each coordinate should be a sequence (list or tuple)'):
+ LineString((0, 0))
+
+ with self.assertRaisesMessage(TypeError, 'LineString requires at least 2 points, got 1.'):
+ LineString([(0, 0)])
+
def test_multilinestring(self):
"Testing MultiLineString objects."
prev = fromstr('POINT(0 0)')
@@ -369,6 +374,12 @@ class GEOSTest(unittest.TestCase, TestDataMixin):
if numpy:
self.assertEqual(lr, LinearRing(numpy.array(lr.tuple)))
+ with self.assertRaisesMessage(TypeError, 'LinearRing requires at least 4 points, got 3.'):
+ LinearRing((0, 0), (1, 1), (0, 0))
+
+ with self.assertRaisesMessage(TypeError, 'LinearRing requires at least 4 points, got 1.'):
+ LinearRing([(0, 0)])
+
def test_polygons_from_bbox(self):
"Testing `from_bbox` class method."
bbox = (-180, -90, 180, 90)