You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

base.py 1.2 KiB

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