Explorar el Código

Fixed #30552 -- Fixed loss of SRID when calling reverse() on LineString/Point.

Thanks Mariusz Felisiak for contributing the Point part.
Claude Paroz hace 5 años
padre
commit
3c6d32e0b2

+ 3 - 0
django/contrib/gis/geos/linestring.py

@@ -105,6 +105,7 @@ class LineString(LinearGeometryMixin, GEOSGeometry):
     def _set_list(self, length, items):
         ndim = self._cs.dims
         hasz = self._cs.hasz  # I don't understand why these are different
+        srid = self.srid
 
         # create a new coordinate sequence and populate accordingly
         cs = GEOSCoordSeq(capi.create_cs(length, ndim), z=hasz)
@@ -115,6 +116,8 @@ class LineString(LinearGeometryMixin, GEOSGeometry):
         if ptr:
             capi.destroy_geom(self.ptr)
             self.ptr = ptr
+            if srid is not None:
+                self.srid = srid
             self._post_init()
         else:
             # can this happen?

+ 3 - 0
django/contrib/gis/geos/point.py

@@ -76,8 +76,11 @@ class Point(GEOSGeometry):
     def _set_list(self, length, items):
         ptr = self._create_point(length, items)
         if ptr:
+            srid = self.srid
             capi.destroy_geom(self.ptr)
             self._ptr = ptr
+            if srid is not None:
+                self.srid = srid
             self._post_init()
         else:
             # can this happen?

+ 12 - 0
tests/gis_tests/geos_tests/test_geos.py

@@ -280,6 +280,12 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
 
             prev = pnt  # setting the previous geometry
 
+    def test_point_reverse(self):
+        point = GEOSGeometry('POINT(144.963 -37.8143)', 4326)
+        self.assertEqual(point.srid, 4326)
+        point.reverse()
+        self.assertEqual(point.ewkt, 'SRID=4326;POINT (-37.8143 144.963)')
+
     def test_multipoints(self):
         "Testing MultiPoint objects."
         for mp in self.geometries.multipoints:
@@ -348,6 +354,12 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
         # Test __iter__().
         self.assertEqual(list(LineString((0, 0), (1, 1), (2, 2))), [(0, 0), (1, 1), (2, 2)])
 
+    def test_linestring_reverse(self):
+        line = GEOSGeometry('LINESTRING(144.963 -37.8143,151.2607 -33.887)', 4326)
+        self.assertEqual(line.srid, 4326)
+        line.reverse()
+        self.assertEqual(line.ewkt, 'SRID=4326;LINESTRING (151.2607 -33.887, 144.963 -37.8143)')
+
     def test_multilinestring(self):
         "Testing MultiLineString objects."
         prev = fromstr('POINT(0 0)')