| 1 |
from ctypes import c_uint |
| 2 |
|
| 3 |
from django.contrib.gis import gdal |
| 4 |
from django.contrib.gis.geos import prototypes as capi |
| 5 |
from django.contrib.gis.geos.error import GEOSException |
| 6 |
from django.contrib.gis.geos.geometry import GEOSGeometry |
| 7 |
|
| 8 |
|
| 9 |
class Point(GEOSGeometry): |
| 10 |
_minlength = 2 |
| 11 |
_maxlength = 3 |
| 12 |
has_cs = True |
| 13 |
|
| 14 |
def __init__(self, x=None, y=None, z=None, srid=None): |
| 15 |
""" |
| 16 |
The Point object may be initialized with either a tuple, or individual |
| 17 |
parameters. |
| 18 |
|
| 19 |
For example: |
| 20 |
>>> p = Point((5, 23)) # 2D point, passed in as a tuple |
| 21 |
>>> p = Point(5, 23, 8) # 3D point, passed in with individual parameters |
| 22 |
""" |
| 23 |
if x is None: |
| 24 |
coords = [] |
| 25 |
elif isinstance(x, (tuple, list)): |
| 26 |
# Here a tuple or list was passed in under the `x` parameter. |
| 27 |
coords = x |
| 28 |
elif isinstance(x, (float, int)) and isinstance(y, (float, int)): |
| 29 |
# Here X, Y, and (optionally) Z were passed in individually, as parameters. |
| 30 |
if isinstance(z, (float, int)): |
| 31 |
coords = [x, y, z] |
| 32 |
else: |
| 33 |
coords = [x, y] |
| 34 |
else: |
| 35 |
raise TypeError("Invalid parameters given for Point initialization.") |
| 36 |
|
| 37 |
point = self._create_point(len(coords), coords) |
| 38 |
|
| 39 |
# Initializing using the address returned from the GEOS |
| 40 |
# createPoint factory. |
| 41 |
super().__init__(point, srid=srid) |
| 42 |
|
| 43 |
def _to_pickle_wkb(self): |
| 44 |
return None if self.empty else super()._to_pickle_wkb() |
| 45 |
|
| 46 |
def _from_pickle_wkb(self, wkb): |
| 47 |
return self._create_empty() if wkb is None else super()._from_pickle_wkb(wkb) |
| 48 |
|
| 49 |
def _ogr_ptr(self): |
| 50 |
return ( |
| 51 |
gdal.geometries.Point._create_empty() if self.empty else super()._ogr_ptr() |
| 52 |
) |
| 53 |
|
| 54 |
@classmethod |
| 55 |
def _create_empty(cls): |
| 56 |
return cls._create_point(None, None) |
| 57 |
|
| 58 |
@classmethod |
| 59 |
def _create_point(cls, ndim, coords): |
| 60 |
""" |
| 61 |
Create a coordinate sequence, set X, Y, [Z], and create point |
| 62 |
""" |
| 63 |
if not ndim: |
| 64 |
return capi.create_point(None) |
| 65 |
|
| 66 |
if ndim < 2 or ndim > 3: |
| 67 |
raise TypeError("Invalid point dimension: %s" % ndim) |
| 68 |
|
| 69 |
cs = capi.create_cs(c_uint(1), c_uint(ndim)) |
| 70 |
i = iter(coords) |
| 71 |
capi.cs_setx(cs, 0, next(i)) |
| 72 |
capi.cs_sety(cs, 0, next(i)) |
| 73 |
if ndim == 3: |
| 74 |
capi.cs_setz(cs, 0, next(i)) |
| 75 |
|
| 76 |
return capi.create_point(cs) |
| 77 |
|
| 78 |
def _set_list(self, length, items): |
| 79 |
ptr = self._create_point(length, items) |
| 80 |
if ptr: |
| 81 |
srid = self.srid |
| 82 |
capi.destroy_geom(self.ptr) |
| 83 |
self._ptr = ptr |
| 84 |
if srid is not None: |
| 85 |
self.srid = srid |
| 86 |
self._post_init() |
| 87 |
else: |
| 88 |
# can this happen? |
| 89 |
raise GEOSException("Geometry resulting from slice deletion was invalid.") |
| 90 |
|
| 91 |
def _set_single(self, index, value): |
| 92 |
self._cs.setOrdinate(index, 0, value) |
| 93 |
|
| 94 |
def __iter__(self): |
| 95 |
"Iterate over coordinates of this Point." |
| 96 |
for i in range(len(self)): |
| 97 |
yield self[i] |
| 98 |
|
| 99 |
def __len__(self): |
| 100 |
"Return the number of dimensions for this Point (either 0, 2 or 3)." |
| 101 |
if self.empty: |
| 102 |
return 0 |
| 103 |
if self.hasz: |
| 104 |
return 3 |
| 105 |
else: |
| 106 |
return 2 |
| 107 |
|
| 108 |
def _get_single_external(self, index): |
| 109 |
if index == 0: |
| 110 |
return self.x |
| 111 |
elif index == 1: |
| 112 |
return self.y |
| 113 |
elif index == 2: |
| 114 |
return self.z |
| 115 |
|
| 116 |
_get_single_internal = _get_single_external |
| 117 |
|
| 118 |
@property |
| 119 |
def x(self): |
| 120 |
"Return the X component of the Point." |
| 121 |
return self._cs.getOrdinate(0, 0) |
| 122 |
|
| 123 |
@x.setter |
| 124 |
def x(self, value): |
| 125 |
"Set the X component of the Point." |
| 126 |
self._cs.setOrdinate(0, 0, value) |
| 127 |
|
| 128 |
@property |
| 129 |
def y(self): |
| 130 |
"Return the Y component of the Point." |
| 131 |
return self._cs.getOrdinate(1, 0) |
| 132 |
|
| 133 |
@y.setter |
| 134 |
def y(self, value): |
| 135 |
"Set the Y component of the Point." |
| 136 |
self._cs.setOrdinate(1, 0, value) |
| 137 |
|
| 138 |
@property |
| 139 |
def z(self): |
| 140 |
"Return the Z component of the Point." |
| 141 |
return self._cs.getOrdinate(2, 0) if self.hasz else None |
| 142 |
|
| 143 |
@z.setter |
| 144 |
def z(self, value): |
| 145 |
"Set the Z component of the Point." |
| 146 |
if not self.hasz: |
| 147 |
raise GEOSException("Cannot set Z on 2D Point.") |
| 148 |
self._cs.setOrdinate(2, 0, value) |
| 149 |
|
| 150 |
# ### Tuple setting and retrieval routines. ### |
| 151 |
@property |
| 152 |
def tuple(self): |
| 153 |
"Return a tuple of the point." |
| 154 |
return self._cs.tuple |
| 155 |
|
| 156 |
@tuple.setter |
| 157 |
def tuple(self, tup): |
| 158 |
"Set the coordinates of the point with the given tuple." |
| 159 |
self._cs[0] = tup |
| 160 |
|
| 161 |
# The tuple and coords properties |
| 162 |
coords = tuple |