/usr/share/pyshared/shapely/geometry/multipoint.py is in python-shapely 1.2.14-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | """Collections of points and related utilities
"""
from ctypes import byref, c_double, c_void_p, cast, POINTER
from ctypes import ArgumentError
from shapely.geos import lgeos
from shapely.geometry.base import BaseMultipartGeometry, exceptNull
from shapely.geometry.point import Point, geos_point_from_py
from shapely.geometry.proxy import CachingGeometryProxy
__all__ = ['MultiPoint', 'asMultiPoint']
class MultiPoint(BaseMultipartGeometry):
"""A collection of one or more points
A MultiPoint has zero area and zero length.
Attributes
----------
geoms : sequence
A sequence of Points
"""
def __init__(self, points=None):
"""
Parameters
----------
points : sequence
A sequence of (x, y [,z]) numeric coordinate pairs or triples or a
sequence of objects that implement the numpy array interface,
including instaces of Point.
Example
-------
Construct a 2 point collection
>>> ob = MultiPoint([[0.0, 0.0], [1.0, 2.0]])
>>> len(ob.geoms)
2
>>> type(ob.geoms[0]) == Point
True
"""
super(MultiPoint, self).__init__()
if points is None:
# allow creation of empty multipoints, to support unpickling
pass
else:
self._geom, self._ndim = geos_multipoint_from_py(points)
def shape_factory(self, *args):
return Point(*args)
@property
def __geo_interface__(self):
return {
'type': 'MultiPoint',
'coordinates': tuple([g.coords[0] for g in self.geoms])
}
@property
@exceptNull
def ctypes(self):
if not self._ctypes_data:
temp = c_double()
n = self._ndim
m = len(self.geoms)
array_type = c_double * (m * n)
data = array_type()
for i in xrange(m):
g = self.geoms[i]._geom
cs = lgeos.GEOSGeom_getCoordSeq(g)
lgeos.GEOSCoordSeq_getX(cs, 0, byref(temp))
data[n*i] = temp.value
lgeos.GEOSCoordSeq_getY(cs, 0, byref(temp))
data[n*i+1] = temp.value
if n == 3: # TODO: use hasz
lgeos.GEOSCoordSeq_getZ(cs, 0, byref(temp))
data[n*i+2] = temp.value
self._ctypes_data = data
return self._ctypes_data
@exceptNull
def array_interface(self):
"""Provide the Numpy array protocol."""
ai = self.array_interface_base
ai.update({'shape': (len(self.geoms), self._ndim)})
return ai
__array_interface__ = property(array_interface)
class MultiPointAdapter(CachingGeometryProxy, MultiPoint):
context = None
_owned = False
def __init__(self, context):
self.context = context
self.factory = geos_multipoint_from_py
@property
def _ndim(self):
try:
# From array protocol
array = self.context.__array_interface__
n = array['shape'][1]
assert n == 2 or n == 3
return n
except AttributeError:
# Fall back on list
return len(self.context[0])
@property
def __array_interface__(self):
"""Provide the Numpy array protocol."""
try:
return self.context.__array_interface__
except AttributeError:
return self.array_interface()
def asMultiPoint(context):
"""Adapt a sequence of objects to the MultiPoint interface"""
return MultiPointAdapter(context)
def geos_multipoint_from_py(ob):
try:
# From array protocol
array = ob.__array_interface__
assert len(array['shape']) == 2
m = array['shape'][0]
n = array['shape'][1]
assert m >= 1
assert n == 2 or n == 3
# Make pointer to the coordinate array
if isinstance(array['data'], tuple):
# numpy tuple (addr, read-only)
cp = cast(array['data'][0], POINTER(c_double))
else:
cp = array['data']
# Array of pointers to sub-geometries
subs = (c_void_p * m)()
for i in xrange(m):
geom, ndims = geos_point_from_py(cp[n*i:n*i+2])
subs[i] = cast(geom, c_void_p)
except AttributeError:
# Fall back on list
m = len(ob)
try:
n = len(ob[0])
except TypeError:
n = ob[0]._ndim
assert n == 2 or n == 3
# Array of pointers to point geometries
subs = (c_void_p * m)()
# add to coordinate sequence
for i in xrange(m):
coords = ob[i]
geom, ndims = geos_point_from_py(coords)
subs[i] = cast(geom, c_void_p)
return lgeos.GEOSGeom_createCollection(4, subs, m), n
# Test runner
def _test():
import doctest
doctest.testmod()
if __name__ == "__main__":
_test()
|