From d453dfb1daea58e441fee0bb36b57d8b200f785c Mon Sep 17 00:00:00 2001 From: Sergey Fedoseev Date: Wed, 5 Apr 2017 13:05:03 +0500 Subject: [PATCH] Refs #28024 -- Optimized GEOSCoordSeq.tuple by avoiding superfluous index and dimension checks. --- django/contrib/gis/geos/coordseq.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/django/contrib/gis/geos/coordseq.py b/django/contrib/gis/geos/coordseq.py index d8a9a43b24a..1a97e71f726 100644 --- a/django/contrib/gis/geos/coordseq.py +++ b/django/contrib/gis/geos/coordseq.py @@ -80,6 +80,25 @@ class GEOSCoordSeq(GEOSBase): if dim < 0 or dim > 2: raise GEOSException('invalid ordinate dimension "%d"' % dim) + def _get_x(self, index): + return capi.cs_getx(self.ptr, index, byref(c_double())) + + def _get_y(self, index): + return capi.cs_gety(self.ptr, index, byref(c_double())) + + def _get_z(self, index): + return capi.cs_getz(self.ptr, index, byref(c_double())) + + @property + def _point_getter(self): + return self._get_point_3d if self.dims == 3 and self._z else self._get_point_2d + + def _get_point_2d(self, index): + return (self._get_x(index), self._get_y(index)) + + def _get_point_3d(self, index): + return (self._get_x(index), self._get_y(index), self._get_z(index)) + # #### Ordinate getting and setting routines #### def getOrdinate(self, dimension, index): "Return the value for the given dimension and index." @@ -157,7 +176,7 @@ class GEOSCoordSeq(GEOSBase): def tuple(self): "Return a tuple version of this coordinate sequence." n = self.size + get_point = self._point_getter if n == 1: - return self[0] - else: - return tuple(self[i] for i in range(n)) + return get_point(0) + return tuple(get_point(i) for i in range(n))