Skip to content

Commit

Permalink
OGRGeometry objects may now be pickled.
Browse files Browse the repository at this point in the history
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12303 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jbronn committed Jan 27, 2010
1 parent 25f47bb commit b0d218e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
24 changes: 20 additions & 4 deletions django/contrib/gis/gdal/geometries.py
Expand Up @@ -130,17 +130,33 @@ def __init__(self, geom_input, srs=None):
# 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)."
x0, y0, x1, y1 = bbox
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):
Expand Down
10 changes: 10 additions & 0 deletions django/contrib/gis/gdal/tests/test_geom.py
Expand Up @@ -447,6 +447,16 @@ def test16_25D(self):
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))
Expand Down

0 comments on commit b0d218e

Please sign in to comment.