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
|
|
|
from ctypes import c_void_p
|
|
|
|
from types import NoneType
|
|
|
|
from django.contrib.gis.geos.error import GEOSException, GEOSIndexError
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Trying to import GDAL libraries, if available. Have to place in
|
|
|
|
# try/except since this package may be used outside GeoDjango.
|
|
|
|
try:
|
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
|
|
|
from django.contrib.gis import gdal
|
|
|
|
except ImportError:
|
|
|
|
# A 'dummy' gdal module.
|
|
|
|
class GDALInfo(object):
|
|
|
|
HAS_GDAL = False
|
|
|
|
GEOJSON = False
|
|
|
|
gdal = GDALInfo()
|
|
|
|
|
|
|
|
# NumPy supported?
|
|
|
|
try:
|
|
|
|
import numpy
|
|
|
|
except ImportError:
|
|
|
|
numpy = False
|
|
|
|
|
|
|
|
class GEOSBase(object):
|
|
|
|
"""
|
|
|
|
Base object for GEOS objects that has a pointer access property
|
|
|
|
that controls access to the underlying C pointer.
|
|
|
|
"""
|
|
|
|
# Initially the pointer is NULL.
|
2008-08-06 02:13:06 +08:00
|
|
|
_ptr = None
|
|
|
|
|
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
|
|
|
# Default allowed pointer type.
|
|
|
|
ptr_type = c_void_p
|
|
|
|
|
|
|
|
# Pointer access property.
|
|
|
|
def _get_ptr(self):
|
|
|
|
# Raise an exception if the pointer isn't valid don't
|
|
|
|
# want to be passing NULL pointers to routines --
|
|
|
|
# that's very bad.
|
|
|
|
if self._ptr: return self._ptr
|
|
|
|
else: raise GEOSException('NULL GEOS %s pointer encountered.' % self.__class__.__name__)
|
|
|
|
|
|
|
|
def _set_ptr(self, ptr):
|
|
|
|
# Only allow the pointer to be set with pointers of the
|
|
|
|
# compatible type or None (NULL).
|
|
|
|
if isinstance(ptr, int):
|
|
|
|
self._ptr = self.ptr_type(ptr)
|
|
|
|
elif isinstance(ptr, (self.ptr_type, NoneType)):
|
|
|
|
self._ptr = ptr
|
|
|
|
else:
|
|
|
|
raise TypeError('Incompatible pointer type')
|
|
|
|
|
|
|
|
# Property for controlling access to the GEOS object pointers. Using
|
|
|
|
# this raises an exception when the pointer is NULL, thus preventing
|
|
|
|
# the C library from attempting to access an invalid memory location.
|
|
|
|
ptr = property(_get_ptr, _set_ptr)
|