| 1 |
""" |
| 2 |
This module houses the GEOSCoordSeq object, which is used internally |
| 3 |
by GEOSGeometry to house the actual coordinates of the Point, |
| 4 |
LineString, and LinearRing geometries. |
| 5 |
""" |
| 6 |
|
| 7 |
from ctypes import byref, c_byte, c_double, c_uint |
| 8 |
|
| 9 |
from django.contrib.gis.geos import prototypes as capi |
| 10 |
from django.contrib.gis.geos.base import GEOSBase |
| 11 |
from django.contrib.gis.geos.error import GEOSException |
| 12 |
from django.contrib.gis.geos.libgeos import CS_PTR |
| 13 |
from django.contrib.gis.shortcuts import numpy |
| 14 |
|
| 15 |
|
| 16 |
class GEOSCoordSeq(GEOSBase): |
| 17 |
"The internal representation of a list of coordinates inside a Geometry." |
| 18 |
|
| 19 |
ptr_type = CS_PTR |
| 20 |
|
| 21 |
def __init__(self, ptr, z=False): |
| 22 |
"Initialize from a GEOS pointer." |
| 23 |
if not isinstance(ptr, CS_PTR): |
| 24 |
raise TypeError("Coordinate sequence should initialize with a CS_PTR.") |
| 25 |
self._ptr = ptr |
| 26 |
self._z = z |
| 27 |
|
| 28 |
def __iter__(self): |
| 29 |
"Iterate over each point in the coordinate sequence." |
| 30 |
for i in range(self.size): |
| 31 |
yield self[i] |
| 32 |
|
| 33 |
def __len__(self): |
| 34 |
"Return the number of points in the coordinate sequence." |
| 35 |
return self.size |
| 36 |
|
| 37 |
def __str__(self): |
| 38 |
"Return the string representation of the coordinate sequence." |
| 39 |
return str(self.tuple) |
| 40 |
|
| 41 |
def __getitem__(self, index): |
| 42 |
"Return the coordinate sequence value at the given index." |
| 43 |
self._checkindex(index) |
| 44 |
return self._point_getter(index) |
| 45 |
|
| 46 |
def __setitem__(self, index, value): |
| 47 |
"Set the coordinate sequence value at the given index." |
| 48 |
# Checking the input value |
| 49 |
if isinstance(value, (list, tuple)): |
| 50 |
pass |
| 51 |
elif numpy and isinstance(value, numpy.ndarray): |
| 52 |
pass |
| 53 |
else: |
| 54 |
raise TypeError( |
| 55 |
"Must set coordinate with a sequence (list, tuple, or numpy array)." |
| 56 |
) |
| 57 |
# Checking the dims of the input |
| 58 |
if self.dims == 3 and self._z: |
| 59 |
n_args = 3 |
| 60 |
point_setter = self._set_point_3d |
| 61 |
else: |
| 62 |
n_args = 2 |
| 63 |
point_setter = self._set_point_2d |
| 64 |
if len(value) != n_args: |
| 65 |
raise TypeError("Dimension of value does not match.") |
| 66 |
self._checkindex(index) |
| 67 |
point_setter(index, value) |
| 68 |
|
| 69 |
# #### Internal Routines #### |
| 70 |
def _checkindex(self, index): |
| 71 |
"Check the given index." |
| 72 |
if not (0 <= index < self.size): |
| 73 |
raise IndexError("invalid GEOS Geometry index: %s" % index) |
| 74 |
|
| 75 |
def _checkdim(self, dim): |
| 76 |
"Check the given dimension." |
| 77 |
if dim < 0 or dim > 2: |
| 78 |
raise GEOSException('invalid ordinate dimension "%d"' % dim) |
| 79 |
|
| 80 |
def _get_x(self, index): |
| 81 |
return capi.cs_getx(self.ptr, index, byref(c_double())) |
| 82 |
|
| 83 |
def _get_y(self, index): |
| 84 |
return capi.cs_gety(self.ptr, index, byref(c_double())) |
| 85 |
|
| 86 |
def _get_z(self, index): |
| 87 |
return capi.cs_getz(self.ptr, index, byref(c_double())) |
| 88 |
|
| 89 |
def _set_x(self, index, value): |
| 90 |
capi.cs_setx(self.ptr, index, value) |
| 91 |
|
| 92 |
def _set_y(self, index, value): |
| 93 |
capi.cs_sety(self.ptr, index, value) |
| 94 |
|
| 95 |
def _set_z(self, index, value): |
| 96 |
capi.cs_setz(self.ptr, index, value) |
| 97 |
|
| 98 |
@property |
| 99 |
def _point_getter(self): |
| 100 |
return self._get_point_3d if self.dims == 3 and self._z else self._get_point_2d |
| 101 |
|
| 102 |
def _get_point_2d(self, index): |
| 103 |
return (self._get_x(index), self._get_y(index)) |
| 104 |
|
| 105 |
def _get_point_3d(self, index): |
| 106 |
return (self._get_x(index), self._get_y(index), self._get_z(index)) |
| 107 |
|
| 108 |
def _set_point_2d(self, index, value): |
| 109 |
x, y = value |
| 110 |
self._set_x(index, x) |
| 111 |
self._set_y(index, y) |
| 112 |
|
| 113 |
def _set_point_3d(self, index, value): |
| 114 |
x, y, z = value |
| 115 |
self._set_x(index, x) |
| 116 |
self._set_y(index, y) |
| 117 |
self._set_z(index, z) |
| 118 |
|
| 119 |
# #### Ordinate getting and setting routines #### |
| 120 |
def getOrdinate(self, dimension, index): |
| 121 |
"Return the value for the given dimension and index." |
| 122 |
self._checkindex(index) |
| 123 |
self._checkdim(dimension) |
| 124 |
return capi.cs_getordinate(self.ptr, index, dimension, byref(c_double())) |
| 125 |
|
| 126 |
def setOrdinate(self, dimension, index, value): |
| 127 |
"Set the value for the given dimension and index." |
| 128 |
self._checkindex(index) |
| 129 |
self._checkdim(dimension) |
| 130 |
capi.cs_setordinate(self.ptr, index, dimension, value) |
| 131 |
|
| 132 |
def getX(self, index): |
| 133 |
"Get the X value at the index." |
| 134 |
return self.getOrdinate(0, index) |
| 135 |
|
| 136 |
def setX(self, index, value): |
| 137 |
"Set X with the value at the given index." |
| 138 |
self.setOrdinate(0, index, value) |
| 139 |
|
| 140 |
def getY(self, index): |
| 141 |
"Get the Y value at the given index." |
| 142 |
return self.getOrdinate(1, index) |
| 143 |
|
| 144 |
def setY(self, index, value): |
| 145 |
"Set Y with the value at the given index." |
| 146 |
self.setOrdinate(1, index, value) |
| 147 |
|
| 148 |
def getZ(self, index): |
| 149 |
"Get Z with the value at the given index." |
| 150 |
return self.getOrdinate(2, index) |
| 151 |
|
| 152 |
def setZ(self, index, value): |
| 153 |
"Set Z with the value at the given index." |
| 154 |
self.setOrdinate(2, index, value) |
| 155 |
|
| 156 |
# ### Dimensions ### |
| 157 |
@property |
| 158 |
def size(self): |
| 159 |
"Return the size of this coordinate sequence." |
| 160 |
return capi.cs_getsize(self.ptr, byref(c_uint())) |
| 161 |
|
| 162 |
@property |
| 163 |
def dims(self): |
| 164 |
"Return the dimensions of this coordinate sequence." |
| 165 |
return capi.cs_getdims(self.ptr, byref(c_uint())) |
| 166 |
|
| 167 |
@property |
| 168 |
def hasz(self): |
| 169 |
""" |
| 170 |
Return whether this coordinate sequence is 3D. This property value is |
| 171 |
inherited from the parent Geometry. |
| 172 |
""" |
| 173 |
return self._z |
| 174 |
|
| 175 |
# ### Other Methods ### |
| 176 |
def clone(self): |
| 177 |
"Clone this coordinate sequence." |
| 178 |
return GEOSCoordSeq(capi.cs_clone(self.ptr), self.hasz) |
| 179 |
|
| 180 |
@property |
| 181 |
def kml(self): |
| 182 |
"Return the KML representation for the coordinates." |
| 183 |
# Getting the substitution string depending on whether the coordinates have |
| 184 |
# a Z dimension. |
| 185 |
if self.hasz: |
| 186 |
substr = "%s,%s,%s " |
| 187 |
else: |
| 188 |
substr = "%s,%s,0 " |
| 189 |
return ( |
| 190 |
"<coordinates>%s</coordinates>" |
| 191 |
% "".join(substr % self[i] for i in range(len(self))).strip() |
| 192 |
) |
| 193 |
|
| 194 |
@property |
| 195 |
def tuple(self): |
| 196 |
"Return a tuple version of this coordinate sequence." |
| 197 |
n = self.size |
| 198 |
get_point = self._point_getter |
| 199 |
if n == 1: |
| 200 |
return get_point(0) |
| 201 |
return tuple(get_point(i) for i in range(n)) |
| 202 |
|
| 203 |
@property |
| 204 |
def is_counterclockwise(self): |
| 205 |
"""Return whether this coordinate sequence is counterclockwise.""" |
| 206 |
ret = c_byte() |
| 207 |
if not capi.cs_is_ccw(self.ptr, byref(ret)): |
| 208 |
raise GEOSException( |
| 209 |
'Error encountered in GEOS C function "%s".' % capi.cs_is_ccw.func_name |
| 210 |
) |
| 211 |
return ret.value == 1 |