From b0d218e9e26febb35fcb91ad1c748b95674f1e59 Mon Sep 17 00:00:00 2001 From: Justin Bronn Date: Wed, 27 Jan 2010 03:47:37 +0000 Subject: [PATCH] `OGRGeometry` objects may now be pickled. git-svn-id: http://code.djangoproject.com/svn/django/trunk@12303 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/gis/gdal/geometries.py | 24 ++++++++++++++++++---- django/contrib/gis/gdal/tests/test_geom.py | 10 +++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/django/contrib/gis/gdal/geometries.py b/django/contrib/gis/gdal/geometries.py index df5ea70547..92d2d1ea4b 100644 --- a/django/contrib/gis/gdal/geometries.py +++ b/django/contrib/gis/gdal/geometries.py @@ -130,6 +130,26 @@ class OGRGeometry(GDALBase): # Setting the class depending upon the OGR Geometry Type self.__class__ = GEO_CLASSES[self.geom_type.num] + def __del__(self): + "Deletes this Geometry." + if self._ptr: capi.destroy_geom(self._ptr) + + # Pickle routines + def __getstate__(self): + srs = self.srs + if srs: + srs = srs.wkt + else: + srs = None + return str(self.wkb), srs + + def __setstate__(self, state): + wkb, srs = state + ptr = capi.from_wkb(wkb, None, byref(c_void_p()), len(wkb)) + if not ptr: raise OGRException('Invalid OGRGeometry loaded from pickled state.') + self.ptr = ptr + self.srs = srs + @classmethod def from_bbox(cls, bbox): "Constructs a Polygon from a bounding box (4-tuple)." @@ -137,10 +157,6 @@ class OGRGeometry(GDALBase): return OGRGeometry( 'POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s))' % ( x0, y0, x0, y1, x1, y1, x1, y0, x0, y0) ) - def __del__(self): - "Deletes this Geometry." - if self._ptr: capi.destroy_geom(self._ptr) - ### Geometry set-like operations ### # g = g1 | g2 def __or__(self, other): diff --git a/django/contrib/gis/gdal/tests/test_geom.py b/django/contrib/gis/gdal/tests/test_geom.py index 93f08504a1..1590d3a23b 100644 --- a/django/contrib/gis/gdal/tests/test_geom.py +++ b/django/contrib/gis/gdal/tests/test_geom.py @@ -447,6 +447,16 @@ class OGRGeomTest(unittest.TestCase): self.assertEqual([1.0, 2.0, 3.0], ls_25d.z) self.assertEqual(3, ls_25d.coord_dim) + def test17_pickle(self): + "Testing pickle support." + import cPickle + g1 = OGRGeometry('LINESTRING(1 1 1,2 2 2,3 3 3)', 'WGS84') + g2 = cPickle.loads(cPickle.dumps(g1)) + self.assertEqual(g1, g2) + self.assertEqual(4326, g2.srs.srid) + self.assertEqual(g1.srs.wkt, g2.srs.wkt) + + def suite(): s = unittest.TestSuite() s.addTest(unittest.makeSuite(OGRGeomTest))