base.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from ctypes import c_void_p
  2. from django.contrib.gis.geos.error import GEOSException
  3. # Trying to import GDAL libraries, if available. Have to place in
  4. # try/except since this package may be used outside GeoDjango.
  5. try:
  6. from django.contrib.gis import gdal
  7. except ImportError:
  8. # A 'dummy' gdal module.
  9. class GDALInfo(object):
  10. HAS_GDAL = False
  11. gdal = GDALInfo()
  12. # NumPy supported?
  13. try:
  14. import numpy
  15. except ImportError:
  16. numpy = False
  17. class GEOSBase(object):
  18. """
  19. Base object for GEOS objects that has a pointer access property
  20. that controls access to the underlying C pointer.
  21. """
  22. # Initially the pointer is NULL.
  23. _ptr = None
  24. # Default allowed pointer type.
  25. ptr_type = c_void_p
  26. # Pointer access property.
  27. def _get_ptr(self):
  28. # Raise an exception if the pointer isn't valid don't
  29. # want to be passing NULL pointers to routines --
  30. # that's very bad.
  31. if self._ptr:
  32. return self._ptr
  33. else:
  34. raise GEOSException('NULL GEOS %s pointer encountered.' % self.__class__.__name__)
  35. def _set_ptr(self, ptr):
  36. # Only allow the pointer to be set with pointers of the
  37. # compatible type or None (NULL).
  38. if ptr is None or isinstance(ptr, self.ptr_type):
  39. self._ptr = ptr
  40. else:
  41. raise TypeError('Incompatible pointer type')
  42. # Property for controlling access to the GEOS object pointers. Using
  43. # this raises an exception when the pointer is NULL, thus preventing
  44. # the C library from attempting to access an invalid memory location.
  45. ptr = property(_get_ptr, _set_ptr)