| 1 |
""" |
| 2 |
This module houses the ctypes initialization procedures, as well |
| 3 |
as the notice and error handler function callbacks (get called |
| 4 |
when an error occurs in GEOS). |
| 5 |
|
| 6 |
This module also houses GEOS Pointer utilities, including |
| 7 |
get_pointer_arr(), and GEOM_PTR. |
| 8 |
""" |
| 9 |
|
| 10 |
import logging |
| 11 |
import os |
| 12 |
from ctypes import CDLL, CFUNCTYPE, POINTER, Structure, c_char_p |
| 13 |
from ctypes.util import find_library |
| 14 |
|
| 15 |
from django.core.exceptions import ImproperlyConfigured |
| 16 |
from django.utils.functional import SimpleLazyObject, cached_property |
| 17 |
from django.utils.version import get_version_tuple |
| 18 |
|
| 19 |
logger = logging.getLogger("django.contrib.gis") |
| 20 |
|
| 21 |
|
| 22 |
def load_geos(): |
| 23 |
# Custom library path set? |
| 24 |
try: |
| 25 |
from django.conf import settings |
| 26 |
|
| 27 |
lib_path = settings.GEOS_LIBRARY_PATH |
| 28 |
except (AttributeError, ImportError, ImproperlyConfigured, OSError): |
| 29 |
lib_path = None |
| 30 |
|
| 31 |
# Setting the appropriate names for the GEOS-C library. |
| 32 |
if lib_path: |
| 33 |
lib_names = None |
| 34 |
elif os.name == "nt": |
| 35 |
# Windows NT libraries |
| 36 |
lib_names = ["geos_c", "libgeos_c-1"] |
| 37 |
elif os.name == "posix": |
| 38 |
# *NIX libraries |
| 39 |
lib_names = ["geos_c", "GEOS"] |
| 40 |
else: |
| 41 |
raise ImportError('Unsupported OS "%s"' % os.name) |
| 42 |
|
| 43 |
# Using the ctypes `find_library` utility to find the path to the GEOS |
| 44 |
# shared library. This is better than manually specifying each library name |
| 45 |
# and extension (e.g., libgeos_c.[so|so.1|dylib].). |
| 46 |
if lib_names: |
| 47 |
for lib_name in lib_names: |
| 48 |
lib_path = find_library(lib_name) |
| 49 |
if lib_path is not None: |
| 50 |
break |
| 51 |
|
| 52 |
# No GEOS library could be found. |
| 53 |
if lib_path is None: |
| 54 |
raise ImportError( |
| 55 |
'Could not find the GEOS library (tried "%s"). ' |
| 56 |
"Try setting GEOS_LIBRARY_PATH in your settings." % '", "'.join(lib_names) |
| 57 |
) |
| 58 |
# Getting the GEOS C library. The C interface (CDLL) is used for |
| 59 |
# both *NIX and Windows. |
| 60 |
# See the GEOS C API source code for more details on the library function calls: |
| 61 |
# https://libgeos.org/doxygen/geos__c_8h_source.html |
| 62 |
_lgeos = CDLL(lib_path) |
| 63 |
# Here we set up the prototypes for the initGEOS_r and finishGEOS_r |
| 64 |
# routines. These functions aren't actually called until they are |
| 65 |
# attached to a GEOS context handle -- this actually occurs in |
| 66 |
# geos/prototypes/threadsafe.py. |
| 67 |
_lgeos.initGEOS_r.restype = CONTEXT_PTR |
| 68 |
_lgeos.finishGEOS_r.argtypes = [CONTEXT_PTR] |
| 69 |
# Set restype for compatibility across 32 and 64-bit platforms. |
| 70 |
_lgeos.GEOSversion.restype = c_char_p |
| 71 |
return _lgeos |
| 72 |
|
| 73 |
|
| 74 |
# The notice and error handler C function callback definitions. |
| 75 |
# Supposed to mimic the GEOS message handler (C below): |
| 76 |
# typedef void (*GEOSMessageHandler)(const char *fmt, ...); |
| 77 |
NOTICEFUNC = CFUNCTYPE(None, c_char_p, c_char_p) |
| 78 |
|
| 79 |
|
| 80 |
def notice_h(fmt, lst): |
| 81 |
fmt, lst = fmt.decode(), lst.decode() |
| 82 |
try: |
| 83 |
warn_msg = fmt % lst |
| 84 |
except TypeError: |
| 85 |
warn_msg = fmt |
| 86 |
logger.warning("GEOS_NOTICE: %s\n", warn_msg) |
| 87 |
|
| 88 |
|
| 89 |
notice_h = NOTICEFUNC(notice_h) |
| 90 |
|
| 91 |
ERRORFUNC = CFUNCTYPE(None, c_char_p, c_char_p) |
| 92 |
|
| 93 |
|
| 94 |
def error_h(fmt, lst): |
| 95 |
fmt, lst = fmt.decode(), lst.decode() |
| 96 |
try: |
| 97 |
err_msg = fmt % lst |
| 98 |
except TypeError: |
| 99 |
err_msg = fmt |
| 100 |
logger.error("GEOS_ERROR: %s\n", err_msg) |
| 101 |
|
| 102 |
|
| 103 |
error_h = ERRORFUNC(error_h) |
| 104 |
|
| 105 |
# #### GEOS Geometry C data structures, and utility functions. #### |
| 106 |
|
| 107 |
|
| 108 |
# Opaque GEOS geometry structures, used for GEOM_PTR and CS_PTR |
| 109 |
class GEOSGeom_t(Structure): |
| 110 |
pass |
| 111 |
|
| 112 |
|
| 113 |
class GEOSPrepGeom_t(Structure): |
| 114 |
pass |
| 115 |
|
| 116 |
|
| 117 |
class GEOSCoordSeq_t(Structure): |
| 118 |
pass |
| 119 |
|
| 120 |
|
| 121 |
class GEOSContextHandle_t(Structure): |
| 122 |
pass |
| 123 |
|
| 124 |
|
| 125 |
# Pointers to opaque GEOS geometry structures. |
| 126 |
GEOM_PTR = POINTER(GEOSGeom_t) |
| 127 |
PREPGEOM_PTR = POINTER(GEOSPrepGeom_t) |
| 128 |
CS_PTR = POINTER(GEOSCoordSeq_t) |
| 129 |
CONTEXT_PTR = POINTER(GEOSContextHandle_t) |
| 130 |
|
| 131 |
|
| 132 |
lgeos = SimpleLazyObject(load_geos) |
| 133 |
|
| 134 |
|
| 135 |
class GEOSFuncFactory: |
| 136 |
""" |
| 137 |
Lazy loading of GEOS functions. |
| 138 |
""" |
| 139 |
|
| 140 |
argtypes = None |
| 141 |
restype = None |
| 142 |
errcheck = None |
| 143 |
|
| 144 |
def __init__(self, func_name, *, restype=None, errcheck=None, argtypes=None): |
| 145 |
self.func_name = func_name |
| 146 |
if restype is not None: |
| 147 |
self.restype = restype |
| 148 |
if errcheck is not None: |
| 149 |
self.errcheck = errcheck |
| 150 |
if argtypes is not None: |
| 151 |
self.argtypes = argtypes |
| 152 |
|
| 153 |
def __call__(self, *args): |
| 154 |
return self.func(*args) |
| 155 |
|
| 156 |
@cached_property |
| 157 |
def func(self): |
| 158 |
from django.contrib.gis.geos.prototypes.threadsafe import GEOSFunc |
| 159 |
|
| 160 |
func = GEOSFunc(self.func_name) |
| 161 |
func.argtypes = self.argtypes or [] |
| 162 |
func.restype = self.restype |
| 163 |
if self.errcheck: |
| 164 |
func.errcheck = self.errcheck |
| 165 |
return func |
| 166 |
|
| 167 |
|
| 168 |
def geos_version(): |
| 169 |
"""Return the string version of the GEOS library.""" |
| 170 |
return lgeos.GEOSversion() |
| 171 |
|
| 172 |
|
| 173 |
def geos_version_tuple(): |
| 174 |
"""Return the GEOS version as a tuple (major, minor, subminor).""" |
| 175 |
return get_version_tuple(geos_version().decode()) |