/django/contrib/gis/geos/base.py

https://code.google.com/p/mango-py/ · Python · 52 lines · 26 code · 7 blank · 19 comment · 8 complexity · 9e53e5e2bd24ab4e84900a94319444a6 MD5 · raw file

  1. from ctypes import c_void_p
  2. from types import NoneType
  3. from django.contrib.gis.geos.error import GEOSException, GEOSIndexError
  4. # Trying to import GDAL libraries, if available. Have to place in
  5. # try/except since this package may be used outside GeoDjango.
  6. try:
  7. from django.contrib.gis import gdal
  8. except ImportError:
  9. # A 'dummy' gdal module.
  10. class GDALInfo(object):
  11. HAS_GDAL = False
  12. GEOJSON = False
  13. gdal = GDALInfo()
  14. # NumPy supported?
  15. try:
  16. import numpy
  17. except ImportError:
  18. numpy = False
  19. class GEOSBase(object):
  20. """
  21. Base object for GEOS objects that has a pointer access property
  22. that controls access to the underlying C pointer.
  23. """
  24. # Initially the pointer is NULL.
  25. _ptr = None
  26. # Default allowed pointer type.
  27. ptr_type = c_void_p
  28. # Pointer access property.
  29. def _get_ptr(self):
  30. # Raise an exception if the pointer isn't valid don't
  31. # want to be passing NULL pointers to routines --
  32. # that's very bad.
  33. if self._ptr: return self._ptr
  34. else: 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 isinstance(ptr, (self.ptr_type, NoneType)):
  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)