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.
|
|
|
|
"""
|
2019-10-17 16:17:42 +08:00
|
|
|
from ctypes import byref, c_byte, c_double, c_uint
|
2015-01-28 20:35:27 +08:00
|
|
|
|
|
|
|
from django.contrib.gis.geos import prototypes as capi
|
2015-02-09 18:14:48 +08:00
|
|
|
from django.contrib.gis.geos.base import GEOSBase
|
2015-08-12 20:42:01 +08:00
|
|
|
from django.contrib.gis.geos.error import GEOSException
|
2019-10-17 16:17:42 +08:00
|
|
|
from django.contrib.gis.geos.libgeos import CS_PTR, geos_version_tuple
|
2015-02-09 18:14:48 +08:00
|
|
|
from django.contrib.gis.shortcuts import numpy
|
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
|
|
|
def __init__(self, ptr, z=False):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Initialize from a GEOS pointer."
|
2008-08-06 02:13:06 +08:00
|
|
|
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):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Iterate over each point in the coordinate sequence."
|
2014-12-13 21:04:36 +08:00
|
|
|
for i in range(self.size):
|
2008-08-06 02:13:06 +08:00
|
|
|
yield self[i]
|
|
|
|
|
|
|
|
def __len__(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the number of points in the coordinate sequence."
|
2008-08-06 02:13:06 +08:00
|
|
|
return int(self.size)
|
|
|
|
|
|
|
|
def __str__(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the string representation of the coordinate sequence."
|
2008-08-06 02:13:06 +08:00
|
|
|
return str(self.tuple)
|
|
|
|
|
|
|
|
def __getitem__(self, index):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the coordinate sequence value at the given index."
|
2017-04-05 21:37:04 +08:00
|
|
|
self._checkindex(index)
|
|
|
|
return self._point_getter(index)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def __setitem__(self, index, value):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Set the coordinate sequence value at the given index."
|
2008-08-06 02:13:06 +08:00
|
|
|
# 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
|
2017-04-05 22:06:37 +08:00
|
|
|
point_setter = self._set_point_3d
|
2008-08-06 02:13:06 +08:00
|
|
|
else:
|
|
|
|
n_args = 2
|
2017-04-05 22:06:37 +08:00
|
|
|
point_setter = self._set_point_2d
|
2008-08-06 02:13:06 +08:00
|
|
|
if len(value) != n_args:
|
|
|
|
raise TypeError('Dimension of value does not match.')
|
2017-04-05 22:06:37 +08:00
|
|
|
self._checkindex(index)
|
|
|
|
point_setter(index, value)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2015-02-06 02:25:34 +08:00
|
|
|
# #### Internal Routines ####
|
2008-08-06 02:13:06 +08:00
|
|
|
def _checkindex(self, index):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Check the given index."
|
2017-07-13 01:09:00 +08:00
|
|
|
if not (0 <= index < self.size):
|
2017-01-20 17:20:53 +08:00
|
|
|
raise IndexError('invalid GEOS Geometry index: %s' % index)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def _checkdim(self, dim):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Check the given dimension."
|
2008-08-06 02:13:06 +08:00
|
|
|
if dim < 0 or dim > 2:
|
|
|
|
raise GEOSException('invalid ordinate dimension "%d"' % dim)
|
|
|
|
|
2017-04-05 16:05:03 +08:00
|
|
|
def _get_x(self, index):
|
|
|
|
return capi.cs_getx(self.ptr, index, byref(c_double()))
|
|
|
|
|
|
|
|
def _get_y(self, index):
|
|
|
|
return capi.cs_gety(self.ptr, index, byref(c_double()))
|
|
|
|
|
|
|
|
def _get_z(self, index):
|
|
|
|
return capi.cs_getz(self.ptr, index, byref(c_double()))
|
|
|
|
|
2017-04-05 22:06:37 +08:00
|
|
|
def _set_x(self, index, value):
|
|
|
|
capi.cs_setx(self.ptr, index, value)
|
|
|
|
|
|
|
|
def _set_y(self, index, value):
|
|
|
|
capi.cs_sety(self.ptr, index, value)
|
|
|
|
|
|
|
|
def _set_z(self, index, value):
|
|
|
|
capi.cs_setz(self.ptr, index, value)
|
|
|
|
|
2017-04-05 16:05:03 +08:00
|
|
|
@property
|
|
|
|
def _point_getter(self):
|
|
|
|
return self._get_point_3d if self.dims == 3 and self._z else self._get_point_2d
|
|
|
|
|
|
|
|
def _get_point_2d(self, index):
|
|
|
|
return (self._get_x(index), self._get_y(index))
|
|
|
|
|
|
|
|
def _get_point_3d(self, index):
|
|
|
|
return (self._get_x(index), self._get_y(index), self._get_z(index))
|
|
|
|
|
2017-04-05 22:06:37 +08:00
|
|
|
def _set_point_2d(self, index, value):
|
|
|
|
x, y = value
|
|
|
|
self._set_x(index, x)
|
|
|
|
self._set_y(index, y)
|
|
|
|
|
|
|
|
def _set_point_3d(self, index, value):
|
|
|
|
x, y, z = value
|
|
|
|
self._set_x(index, x)
|
|
|
|
self._set_y(index, y)
|
|
|
|
self._set_z(index, z)
|
|
|
|
|
2015-02-06 02:25:34 +08:00
|
|
|
# #### Ordinate getting and setting routines ####
|
2008-08-06 02:13:06 +08:00
|
|
|
def getOrdinate(self, dimension, index):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the value for the given dimension and index."
|
2008-08-06 02:13:06 +08:00
|
|
|
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):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Set the value for the given dimension and index."
|
2008-08-06 02:13:06 +08:00
|
|
|
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)
|
|
|
|
|
2015-02-06 02:25:34 +08:00
|
|
|
# ### Dimensions ###
|
2008-08-06 02:13:06 +08:00
|
|
|
@property
|
|
|
|
def size(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return 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):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return 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):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return whether this coordinate sequence is 3D. This property value is
|
2008-08-06 02:13:06 +08:00
|
|
|
inherited from the parent Geometry.
|
|
|
|
"""
|
|
|
|
return self._z
|
|
|
|
|
2015-02-06 02:25:34 +08:00
|
|
|
# ### Other Methods ###
|
2008-08-06 02:13:06 +08:00
|
|
|
def clone(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Clone 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):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the KML representation for the coordinates."
|
2008-08-06 02:13:06 +08:00
|
|
|
# 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>' % \
|
2014-12-13 21:04:36 +08:00
|
|
|
''.join(substr % self[i] for i in range(len(self))).strip()
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def tuple(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return a tuple version of this coordinate sequence."
|
2008-08-06 02:13:06 +08:00
|
|
|
n = self.size
|
2017-04-05 16:05:03 +08:00
|
|
|
get_point = self._point_getter
|
2013-10-17 16:17:41 +08:00
|
|
|
if n == 1:
|
2017-04-05 16:05:03 +08:00
|
|
|
return get_point(0)
|
|
|
|
return tuple(get_point(i) for i in range(n))
|
2019-10-17 16:17:42 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_counterclockwise(self):
|
|
|
|
"""Return whether this coordinate sequence is counterclockwise."""
|
|
|
|
if geos_version_tuple() < (3, 7):
|
|
|
|
# A modified shoelace algorithm to determine polygon orientation.
|
|
|
|
# See https://en.wikipedia.org/wiki/Shoelace_formula.
|
|
|
|
area = 0.0
|
|
|
|
n = len(self)
|
|
|
|
for i in range(n):
|
|
|
|
j = (i + 1) % n
|
|
|
|
area += self[i][0] * self[j][1]
|
|
|
|
area -= self[j][0] * self[i][1]
|
|
|
|
return area > 0.0
|
|
|
|
ret = c_byte()
|
|
|
|
if not capi.cs_is_ccw(self.ptr, byref(ret)):
|
|
|
|
raise GEOSException(
|
|
|
|
'Error encountered in GEOS C function "%s".' % capi.cs_is_ccw.func_name
|
|
|
|
)
|
|
|
|
return ret.value == 1
|