2009-03-08 07:02:48 +08:00
|
|
|
from ctypes import c_void_p
|
2012-05-23 18:20:03 +08:00
|
|
|
|
2009-03-08 07:02:48 +08:00
|
|
|
from django.contrib.gis.gdal.error import GDALException
|
2012-07-20 18:45:19 +08:00
|
|
|
from django.utils import six
|
2009-03-08 07:02:48 +08:00
|
|
|
|
|
|
|
class GDALBase(object):
|
|
|
|
"""
|
|
|
|
Base object for GDAL objects that has a pointer access property
|
|
|
|
that controls access to the underlying C pointer.
|
|
|
|
"""
|
|
|
|
# Initially the pointer is NULL.
|
|
|
|
_ptr = None
|
|
|
|
|
|
|
|
# Default allowed pointer type.
|
|
|
|
ptr_type = c_void_p
|
|
|
|
|
|
|
|
# Pointer access property.
|
|
|
|
def _get_ptr(self):
|
|
|
|
# Raise an exception if the pointer isn't valid don't
|
|
|
|
# want to be passing NULL pointers to routines --
|
|
|
|
# that's very bad.
|
|
|
|
if self._ptr: return self._ptr
|
|
|
|
else: raise GDALException('GDAL %s pointer no longer valid.' % self.__class__.__name__)
|
|
|
|
|
|
|
|
def _set_ptr(self, ptr):
|
|
|
|
# Only allow the pointer to be set with pointers of the
|
|
|
|
# compatible type or None (NULL).
|
2012-07-20 18:45:19 +08:00
|
|
|
if isinstance(ptr, six.integer_types):
|
2009-03-08 07:02:48 +08:00
|
|
|
self._ptr = self.ptr_type(ptr)
|
2012-05-23 18:20:03 +08:00
|
|
|
elif ptr is None or isinstance(ptr, self.ptr_type):
|
2009-03-08 07:02:48 +08:00
|
|
|
self._ptr = ptr
|
|
|
|
else:
|
|
|
|
raise TypeError('Incompatible pointer type')
|
|
|
|
|
|
|
|
ptr = property(_get_ptr, _set_ptr)
|
|
|
|
|