PageRenderTime 235ms CodeModel.GetById 230ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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

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