|
1 from ctypes import c_void_p |
|
2 from types import NoneType |
|
3 from 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. |
|
7 try: |
|
8 from django.contrib.gis import gdal |
|
9 except ImportError: |
|
10 # A 'dummy' gdal module. |
|
11 class GDALInfo(object): |
|
12 HAS_GDAL = False |
|
13 GEOJSON = False |
|
14 gdal = GDALInfo() |
|
15 |
|
16 # NumPy supported? |
|
17 try: |
|
18 import numpy |
|
19 except ImportError: |
|
20 numpy = False |
|
21 |
|
22 class 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) |