2008-08-06 02:13:06 +08:00
|
|
|
"""
|
|
|
|
This module houses the GEOSCoordSeq object, which is used internally
|
|
|
|
by GEOSGeometry to house the actual coordinates of the Point,
|
|
|
|
LineString, and LinearRing geometries.
|
|
|
|
"""
|
|
|
|
from ctypes import c_double, c_uint, byref
|
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.geos.base import GEOSBase, numpy
|
2008-08-06 02:13:06 +08:00
|
|
|
from django.contrib.gis.geos.error import GEOSException, GEOSIndexError
|
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.geos.libgeos import CS_PTR
|
|
|
|
from django.contrib.gis.geos import prototypes as capi
|
2012-07-21 00:53:11 +08:00
|
|
|
from django.utils.six.moves import xrange
|
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
|
|
|
class GEOSCoordSeq(GEOSBase):
|
2008-08-06 02:13:06 +08:00
|
|
|
"The internal representation of a list of coordinates inside a Geometry."
|
|
|
|
|
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
|
|
|
ptr_type = CS_PTR
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
#### Python 'magic' routines ####
|
|
|
|
def __init__(self, ptr, z=False):
|
|
|
|
"Initializes from a GEOS pointer."
|
|
|
|
if not isinstance(ptr, CS_PTR):
|
|
|
|
raise TypeError('Coordinate sequence should initialize with a CS_PTR.')
|
|
|
|
self._ptr = ptr
|
|
|
|
self._z = z
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
"Iterates over each point in the coordinate sequence."
|
|
|
|
for i in xrange(self.size):
|
|
|
|
yield self[i]
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
"Returns the number of points in the coordinate sequence."
|
|
|
|
return int(self.size)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
"Returns the string representation of the coordinate sequence."
|
|
|
|
return str(self.tuple)
|
|
|
|
|
|
|
|
def __getitem__(self, index):
|
|
|
|
"Returns the coordinate sequence value at the given index."
|
|
|
|
coords = [self.getX(index), self.getY(index)]
|
|
|
|
if self.dims == 3 and self._z:
|
|
|
|
coords.append(self.getZ(index))
|
|
|
|
return tuple(coords)
|
|
|
|
|
|
|
|
def __setitem__(self, index, value):
|
|
|
|
"Sets the coordinate sequence value at the given index."
|
|
|
|
# Checking the input value
|
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
|
|
|
if isinstance(value, (list, tuple)):
|
2008-08-06 02:13:06 +08:00
|
|
|
pass
|
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
|
|
|
elif numpy and isinstance(value, numpy.ndarray):
|
2008-08-06 02:13:06 +08:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise TypeError('Must set coordinate with a sequence (list, tuple, or numpy array).')
|
|
|
|
# Checking the dims of the input
|
|
|
|
if self.dims == 3 and self._z:
|
|
|
|
n_args = 3
|
|
|
|
set_3d = True
|
|
|
|
else:
|
|
|
|
n_args = 2
|
|
|
|
set_3d = False
|
|
|
|
if len(value) != n_args:
|
|
|
|
raise TypeError('Dimension of value does not match.')
|
|
|
|
# Setting the X, Y, Z
|
|
|
|
self.setX(index, value[0])
|
|
|
|
self.setY(index, value[1])
|
2013-10-17 16:17:41 +08:00
|
|
|
if set_3d:
|
|
|
|
self.setZ(index, value[2])
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
#### Internal Routines ####
|
|
|
|
def _checkindex(self, index):
|
|
|
|
"Checks the given index."
|
|
|
|
sz = self.size
|
|
|
|
if (sz < 1) or (index < 0) or (index >= sz):
|
|
|
|
raise GEOSIndexError('invalid GEOS Geometry index: %s' % str(index))
|
|
|
|
|
|
|
|
def _checkdim(self, dim):
|
|
|
|
"Checks the given dimension."
|
|
|
|
if dim < 0 or dim > 2:
|
|
|
|
raise GEOSException('invalid ordinate dimension "%d"' % dim)
|
|
|
|
|
|
|
|
#### Ordinate getting and setting routines ####
|
|
|
|
def getOrdinate(self, dimension, index):
|
|
|
|
"Returns the value for the given dimension and index."
|
|
|
|
self._checkindex(index)
|
|
|
|
self._checkdim(dimension)
|
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
|
|
|
return capi.cs_getordinate(self.ptr, index, dimension, byref(c_double()))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def setOrdinate(self, dimension, index, value):
|
|
|
|
"Sets the value for the given dimension and index."
|
|
|
|
self._checkindex(index)
|
|
|
|
self._checkdim(dimension)
|
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
|
|
|
capi.cs_setordinate(self.ptr, index, dimension, value)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def getX(self, index):
|
|
|
|
"Get the X value at the index."
|
|
|
|
return self.getOrdinate(0, index)
|
|
|
|
|
|
|
|
def setX(self, index, value):
|
|
|
|
"Set X with the value at the given index."
|
|
|
|
self.setOrdinate(0, index, value)
|
|
|
|
|
|
|
|
def getY(self, index):
|
|
|
|
"Get the Y value at the given index."
|
|
|
|
return self.getOrdinate(1, index)
|
|
|
|
|
|
|
|
def setY(self, index, value):
|
|
|
|
"Set Y with the value at the given index."
|
|
|
|
self.setOrdinate(1, index, value)
|
|
|
|
|
|
|
|
def getZ(self, index):
|
|
|
|
"Get Z with the value at the given index."
|
|
|
|
return self.getOrdinate(2, index)
|
|
|
|
|
|
|
|
def setZ(self, index, value):
|
|
|
|
"Set Z with the value at the given index."
|
|
|
|
self.setOrdinate(2, index, value)
|
|
|
|
|
|
|
|
### Dimensions ###
|
|
|
|
@property
|
|
|
|
def size(self):
|
|
|
|
"Returns the size of this coordinate sequence."
|
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
|
|
|
return capi.cs_getsize(self.ptr, byref(c_uint()))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def dims(self):
|
|
|
|
"Returns the dimensions of this coordinate sequence."
|
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
|
|
|
return capi.cs_getdims(self.ptr, byref(c_uint()))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def hasz(self):
|
|
|
|
"""
|
|
|
|
Returns whether this coordinate sequence is 3D. This property value is
|
|
|
|
inherited from the parent Geometry.
|
|
|
|
"""
|
|
|
|
return self._z
|
|
|
|
|
|
|
|
### Other Methods ###
|
|
|
|
def clone(self):
|
|
|
|
"Clones this coordinate sequence."
|
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
|
|
|
return GEOSCoordSeq(capi.cs_clone(self.ptr), self.hasz)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def kml(self):
|
|
|
|
"Returns the KML representation for the coordinates."
|
|
|
|
# Getting the substitution string depending on whether the coordinates have
|
|
|
|
# a Z dimension.
|
2013-10-17 16:17:41 +08:00
|
|
|
if self.hasz:
|
|
|
|
substr = '%s,%s,%s '
|
|
|
|
else:
|
|
|
|
substr = '%s,%s,0 '
|
2008-08-06 02:13:06 +08:00
|
|
|
return '<coordinates>%s</coordinates>' % \
|
2013-08-30 07:20:00 +08:00
|
|
|
''.join(substr % self[i] for i in xrange(len(self))).strip()
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def tuple(self):
|
|
|
|
"Returns a tuple version of this coordinate sequence."
|
|
|
|
n = self.size
|
2013-10-17 16:17:41 +08:00
|
|
|
if n == 1:
|
|
|
|
return self[0]
|
|
|
|
else:
|
|
|
|
return tuple(self[i] for i in xrange(n))
|