1 | from ctypes import c_void_p
|
---|
2 | from types import NoneType
|
---|
3 | from django.contrib.gis.gdal.error import GDALException
|
---|
4 |
|
---|
5 | class GDALBase(object):
|
---|
6 | """
|
---|
7 | Base object for GDAL objects that has a pointer access property
|
---|
8 | that controls access to the underlying C pointer.
|
---|
9 | """
|
---|
10 | # Initially the pointer is NULL.
|
---|
11 | _ptr = None
|
---|
12 |
|
---|
13 | # Default allowed pointer type.
|
---|
14 | ptr_type = c_void_p
|
---|
15 |
|
---|
16 | # Pointer access property.
|
---|
17 | def _get_ptr(self):
|
---|
18 | # Raise an exception if the pointer isn't valid don't
|
---|
19 | # want to be passing NULL pointers to routines --
|
---|
20 | # that's very bad.
|
---|
21 | if self._ptr: return self._ptr
|
---|
22 | else: raise GDALException('GDAL %s pointer no longer valid.' % self.__class__.__name__)
|
---|
23 |
|
---|
24 | def _set_ptr(self, ptr):
|
---|
25 | # Only allow the pointer to be set with pointers of the
|
---|
26 | # compatible type or None (NULL).
|
---|
27 | if isinstance(ptr, int) or isinstance(ptr, long):
|
---|
28 | self._ptr = self.ptr_type(ptr)
|
---|
29 | elif isinstance(ptr, (self.ptr_type, NoneType)):
|
---|
30 | self._ptr = ptr
|
---|
31 | else:
|
---|
32 | raise TypeError('Incompatible pointer type')
|
---|
33 |
|
---|
34 | ptr = property(_get_ptr, _set_ptr)
|
---|
35 |
|
---|