2008-08-06 02:13:06 +08:00
|
|
|
"""
|
|
|
|
This module houses the ctypes initialization procedures, as well
|
|
|
|
as the notice and error handler function callbacks (get called
|
|
|
|
when an error occurs in GEOS).
|
|
|
|
|
|
|
|
This module also houses GEOS Pointer utilities, including
|
|
|
|
get_pointer_arr(), and GEOM_PTR.
|
|
|
|
"""
|
2012-10-05 04:41:03 +08:00
|
|
|
import logging
|
2011-07-13 17:35:51 +08:00
|
|
|
import os
|
|
|
|
import re
|
2015-01-28 20:35:27 +08:00
|
|
|
from ctypes import CDLL, CFUNCTYPE, POINTER, Structure, c_char_p
|
2008-08-06 02:13:06 +08:00
|
|
|
from ctypes.util import find_library
|
2012-10-05 04:41:03 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
from django.contrib.gis.geos.error import GEOSException
|
2012-10-06 05:38:01 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2016-01-08 07:50:06 +08:00
|
|
|
from django.utils.functional import SimpleLazyObject
|
2017-02-16 00:14:21 +08:00
|
|
|
from django.utils.version import get_version_tuple
|
2012-10-05 04:41:03 +08:00
|
|
|
|
|
|
|
logger = logging.getLogger('django.contrib.gis')
|
|
|
|
|
2015-04-24 21:45:36 +08:00
|
|
|
|
|
|
|
def load_geos():
|
|
|
|
# Custom library path set?
|
|
|
|
try:
|
|
|
|
from django.conf import settings
|
|
|
|
lib_path = settings.GEOS_LIBRARY_PATH
|
|
|
|
except (AttributeError, EnvironmentError,
|
|
|
|
ImportError, ImproperlyConfigured):
|
|
|
|
lib_path = None
|
|
|
|
|
|
|
|
# Setting the appropriate names for the GEOS-C library.
|
|
|
|
if lib_path:
|
|
|
|
lib_names = None
|
|
|
|
elif os.name == 'nt':
|
|
|
|
# Windows NT libraries
|
|
|
|
lib_names = ['geos_c', 'libgeos_c-1']
|
|
|
|
elif os.name == 'posix':
|
|
|
|
# *NIX libraries
|
|
|
|
lib_names = ['geos_c', 'GEOS']
|
|
|
|
else:
|
|
|
|
raise ImportError('Unsupported OS "%s"' % os.name)
|
|
|
|
|
|
|
|
# Using the ctypes `find_library` utility to find the path to the GEOS
|
|
|
|
# shared library. This is better than manually specifying each library name
|
|
|
|
# and extension (e.g., libgeos_c.[so|so.1|dylib].).
|
|
|
|
if lib_names:
|
|
|
|
for lib_name in lib_names:
|
|
|
|
lib_path = find_library(lib_name)
|
|
|
|
if lib_path is not None:
|
|
|
|
break
|
|
|
|
|
|
|
|
# No GEOS library could be found.
|
|
|
|
if lib_path is None:
|
|
|
|
raise ImportError(
|
|
|
|
'Could not find the GEOS library (tried "%s"). '
|
|
|
|
'Try setting GEOS_LIBRARY_PATH in your settings.' %
|
|
|
|
'", "'.join(lib_names)
|
|
|
|
)
|
|
|
|
# Getting the GEOS C library. The C interface (CDLL) is used for
|
|
|
|
# both *NIX and Windows.
|
|
|
|
# See the GEOS C API source code for more details on the library function calls:
|
|
|
|
# http://geos.refractions.net/ro/doxygen_docs/html/geos__c_8h-source.html
|
|
|
|
_lgeos = CDLL(lib_path)
|
|
|
|
# Here we set up the prototypes for the initGEOS_r and finishGEOS_r
|
|
|
|
# routines. These functions aren't actually called until they are
|
2016-01-08 07:50:06 +08:00
|
|
|
# attached to a GEOS context handle -- this actually occurs in
|
|
|
|
# geos/prototypes/threadsafe.py.
|
2015-04-24 21:45:36 +08:00
|
|
|
_lgeos.initGEOS_r.restype = CONTEXT_PTR
|
|
|
|
_lgeos.finishGEOS_r.argtypes = [CONTEXT_PTR]
|
|
|
|
return _lgeos
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# The notice and error handler C function callback definitions.
|
2010-01-13 02:40:54 +08:00
|
|
|
# Supposed to mimic the GEOS message handler (C below):
|
|
|
|
# typedef void (*GEOSMessageHandler)(const char *fmt, ...);
|
2008-08-06 02:13:06 +08:00
|
|
|
NOTICEFUNC = CFUNCTYPE(None, c_char_p, c_char_p)
|
2013-11-03 04:12:09 +08:00
|
|
|
|
|
|
|
|
2012-10-05 04:41:03 +08:00
|
|
|
def notice_h(fmt, lst):
|
2012-09-24 01:59:27 +08:00
|
|
|
fmt, lst = fmt.decode(), lst.decode()
|
2008-08-06 02:13:06 +08:00
|
|
|
try:
|
|
|
|
warn_msg = fmt % lst
|
2013-09-30 23:55:14 +08:00
|
|
|
except TypeError:
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
warn_msg = fmt
|
2015-12-27 04:49:52 +08:00
|
|
|
logger.warning('GEOS_NOTICE: %s\n', warn_msg)
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
notice_h = NOTICEFUNC(notice_h)
|
|
|
|
|
|
|
|
ERRORFUNC = CFUNCTYPE(None, c_char_p, c_char_p)
|
2013-11-03 04:12:09 +08:00
|
|
|
|
|
|
|
|
2012-10-05 04:41:03 +08:00
|
|
|
def error_h(fmt, lst):
|
2012-09-24 01:59:27 +08:00
|
|
|
fmt, lst = fmt.decode(), lst.decode()
|
2008-08-06 02:13:06 +08:00
|
|
|
try:
|
|
|
|
err_msg = fmt % lst
|
2013-09-30 23:55:14 +08:00
|
|
|
except TypeError:
|
2008-08-06 02:13:06 +08:00
|
|
|
err_msg = fmt
|
2015-12-27 04:49:52 +08:00
|
|
|
logger.error('GEOS_ERROR: %s\n', err_msg)
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
error_h = ERRORFUNC(error_h)
|
|
|
|
|
2015-02-06 02:25:34 +08:00
|
|
|
# #### GEOS Geometry C data structures, and utility functions. ####
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Opaque GEOS geometry structures, used for GEOM_PTR and CS_PTR
|
2013-10-17 16:17:41 +08:00
|
|
|
class GEOSGeom_t(Structure):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2013-10-17 16:17:41 +08:00
|
|
|
class GEOSPrepGeom_t(Structure):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2013-10-17 16:17:41 +08:00
|
|
|
class GEOSCoordSeq_t(Structure):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2013-10-17 16:17:41 +08:00
|
|
|
class GEOSContextHandle_t(Structure):
|
|
|
|
pass
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Pointers to opaque GEOS geometry structures.
|
|
|
|
GEOM_PTR = POINTER(GEOSGeom_t)
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
PREPGEOM_PTR = POINTER(GEOSPrepGeom_t)
|
2008-08-06 02:13:06 +08:00
|
|
|
CS_PTR = POINTER(GEOSCoordSeq_t)
|
2013-10-22 21:31:43 +08:00
|
|
|
CONTEXT_PTR = POINTER(GEOSContextHandle_t)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
Refactored the GEOS interface. Improvements include:
* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists. Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property. Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries. Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT. Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.
Fixed #9557, #9877, #10222
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-24 08:12:21 +08:00
|
|
|
# Used specifically by the GEOSGeom_createPolygon and GEOSGeom_createCollection
|
2008-08-06 02:13:06 +08:00
|
|
|
# GEOS routines
|
|
|
|
def get_pointer_arr(n):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Get a ctypes pointer array (of length `n`) for GEOSGeom_t opaque pointer."
|
2008-08-06 02:13:06 +08:00
|
|
|
GeomArr = GEOM_PTR * n
|
|
|
|
return GeomArr()
|
|
|
|
|
2015-04-24 21:45:36 +08:00
|
|
|
|
|
|
|
lgeos = SimpleLazyObject(load_geos)
|
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class GEOSFuncFactory:
|
2015-04-25 00:09:20 +08:00
|
|
|
"""
|
|
|
|
Lazy loading of GEOS functions.
|
|
|
|
"""
|
2015-04-24 21:45:36 +08:00
|
|
|
argtypes = None
|
|
|
|
restype = None
|
|
|
|
errcheck = None
|
|
|
|
|
2017-02-02 00:41:56 +08:00
|
|
|
def __init__(self, func_name, *args, restype=None, errcheck=None, argtypes=None, **kwargs):
|
2015-04-24 21:45:36 +08:00
|
|
|
self.func_name = func_name
|
2017-02-02 00:41:56 +08:00
|
|
|
if restype is not None:
|
|
|
|
self.restype = restype
|
|
|
|
if errcheck is not None:
|
|
|
|
self.errcheck = errcheck
|
|
|
|
if argtypes is not None:
|
|
|
|
self.argtypes = argtypes
|
2015-04-24 21:45:36 +08:00
|
|
|
self.args = args
|
|
|
|
self.kwargs = kwargs
|
|
|
|
self.func = None
|
|
|
|
|
|
|
|
def __call__(self, *args, **kwargs):
|
|
|
|
if self.func is None:
|
|
|
|
self.func = self.get_func(*self.args, **self.kwargs)
|
2016-01-08 07:50:06 +08:00
|
|
|
return self.func(*args, **kwargs)
|
2015-04-24 21:45:36 +08:00
|
|
|
|
|
|
|
def get_func(self, *args, **kwargs):
|
2016-01-08 07:50:06 +08:00
|
|
|
from django.contrib.gis.geos.prototypes.threadsafe import GEOSFunc
|
|
|
|
func = GEOSFunc(self.func_name)
|
|
|
|
func.argtypes = self.argtypes or []
|
2015-04-24 21:45:36 +08:00
|
|
|
func.restype = self.restype
|
|
|
|
if self.errcheck:
|
|
|
|
func.errcheck = self.errcheck
|
|
|
|
return func
|
|
|
|
|
|
|
|
|
2017-01-25 04:31:57 +08:00
|
|
|
# Return the string version of the GEOS library. Have to set the restype
|
2016-01-08 07:50:06 +08:00
|
|
|
# explicitly to c_char_p to ensure compatibility across 32 and 64-bit platforms.
|
|
|
|
geos_version = GEOSFuncFactory('GEOSversion', restype=c_char_p)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Regular expression should be able to parse version strings such as
|
2013-03-13 16:50:45 +08:00
|
|
|
# '3.0.0rc4-CAPI-1.3.3', '3.0.0-CAPI-1.4.1', '3.4.0dev-CAPI-1.8.0' or '3.4.0dev-CAPI-1.8.0 r0'
|
|
|
|
version_regex = re.compile(
|
|
|
|
r'^(?P<version>(?P<major>\d+)\.(?P<minor>\d+)\.(?P<subminor>\d+))'
|
|
|
|
r'((rc(?P<release_candidate>\d+))|dev)?-CAPI-(?P<capi_version>\d+\.\d+\.\d+)( r\d+)?$'
|
|
|
|
)
|
2013-11-03 04:12:09 +08:00
|
|
|
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def geos_version_info():
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return a dictionary containing the various version metadata parsed from
|
2008-08-06 02:13:06 +08:00
|
|
|
the GEOS version string, including the version number, whether the version
|
|
|
|
is a release candidate (and what number release candidate), and the C API
|
|
|
|
version.
|
|
|
|
"""
|
2012-09-12 20:50:16 +08:00
|
|
|
ver = geos_version().decode()
|
2008-08-06 02:13:06 +08:00
|
|
|
m = version_regex.match(ver)
|
2013-03-13 16:50:45 +08:00
|
|
|
if not m:
|
|
|
|
raise GEOSException('Could not parse version info string "%s"' % ver)
|
2014-12-07 05:00:09 +08:00
|
|
|
return {key: m.group(key) for key in (
|
|
|
|
'version', 'release_candidate', 'capi_version', 'major', 'minor', 'subminor')}
|
2017-02-16 00:14:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
def geos_version_tuple():
|
|
|
|
"""Return the GEOS version as a tuple (major, minor, subminor)."""
|
|
|
|
return get_version_tuple(geos_version_info()['version'])
|