shapely 1.3.3__cp34-none-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- Shapely-1.3.3.data/data/shapely/_geos.pxi +52 -0
- Shapely-1.3.3.dist-info/DESCRIPTION.rst +505 -0
- Shapely-1.3.3.dist-info/METADATA +527 -0
- Shapely-1.3.3.dist-info/RECORD +95 -0
- Shapely-1.3.3.dist-info/WHEEL +5 -0
- Shapely-1.3.3.dist-info/metadata.json +1 -0
- Shapely-1.3.3.dist-info/top_level.txt +1 -0
- shapely/DLLs/geos.dll +0 -0
- shapely/DLLs/geos_c.dll +0 -0
- shapely/__init__.py +1 -0
- shapely/_geos.pxi +52 -0
- shapely/affinity.py +255 -0
- shapely/algorithms/__init__.py +0 -0
- shapely/algorithms/cga.py +16 -0
- shapely/coords.py +206 -0
- shapely/ctypes_declarations.py +455 -0
- shapely/examples/__init__.py +1 -0
- shapely/examples/dissolve.py +53 -0
- shapely/examples/geoms.py +51 -0
- shapely/examples/intersect.py +81 -0
- shapely/examples/world.py +21 -0
- shapely/ftools.py +78 -0
- shapely/geometry/__init__.py +20 -0
- shapely/geometry/base.py +797 -0
- shapely/geometry/collection.py +43 -0
- shapely/geometry/geo.py +75 -0
- shapely/geometry/linestring.py +298 -0
- shapely/geometry/multilinestring.py +156 -0
- shapely/geometry/multipoint.py +213 -0
- shapely/geometry/multipolygon.py +186 -0
- shapely/geometry/point.py +269 -0
- shapely/geometry/polygon.py +504 -0
- shapely/geometry/proxy.py +51 -0
- shapely/geos.py +732 -0
- shapely/impl.py +126 -0
- shapely/iterops.py +55 -0
- shapely/linref.py +26 -0
- shapely/ops.py +234 -0
- shapely/predicates.py +23 -0
- shapely/prepared.py +61 -0
- shapely/speedups/__init__.py +43 -0
- shapely/speedups/_speedups.c +5674 -0
- shapely/speedups/_speedups.pyd +0 -0
- shapely/tests/__init__.py +81 -0
- shapely/tests/binascii_hex.txt +47 -0
- shapely/tests/test_affinity.py +255 -0
- shapely/tests/test_box.py +24 -0
- shapely/tests/test_cga.py +37 -0
- shapely/tests/test_collection.py +29 -0
- shapely/tests/test_delegated.py +34 -0
- shapely/tests/test_dlls.py +20 -0
- shapely/tests/test_doctests.py +36 -0
- shapely/tests/test_emptiness.py +53 -0
- shapely/tests/test_equality.py +29 -0
- shapely/tests/test_geointerface.py +74 -0
- shapely/tests/test_geomseq.py +13 -0
- shapely/tests/test_getitem.py +125 -0
- shapely/tests/test_invalid_geometries.py +30 -0
- shapely/tests/test_iterops.py +47 -0
- shapely/tests/test_linear_referencing.py +72 -0
- shapely/tests/test_linemerge.py +52 -0
- shapely/tests/test_linestring.py +111 -0
- shapely/tests/test_locale.py +56 -0
- shapely/tests/test_mapping.py +13 -0
- shapely/tests/test_multilinestring.py +61 -0
- shapely/tests/test_multipoint.py +73 -0
- shapely/tests/test_multipolygon.py +72 -0
- shapely/tests/test_ndarrays.py +54 -0
- shapely/tests/test_operations.py +70 -0
- shapely/tests/test_operators.py +18 -0
- shapely/tests/test_persist.py +34 -0
- shapely/tests/test_pickle.py +22 -0
- shapely/tests/test_point.py +139 -0
- shapely/tests/test_polygon.py +186 -0
- shapely/tests/test_polygonize.py +49 -0
- shapely/tests/test_predicates.py +34 -0
- shapely/tests/test_prepared.py +31 -0
- shapely/tests/test_products_z.py +17 -0
- shapely/tests/test_singularity.py +19 -0
- shapely/tests/test_speedups.py +39 -0
- shapely/tests/test_styles.py +20 -0
- shapely/tests/test_transform.py +84 -0
- shapely/tests/test_union.py +72 -0
- shapely/tests/test_validation.py +12 -0
- shapely/tests/test_vectorized.py +107 -0
- shapely/tests/test_xy.py +17 -0
- shapely/tests/threading_test.py +40 -0
- shapely/topology.py +60 -0
- shapely/validation.py +8 -0
- shapely/vectorized/__init__.py +3 -0
- shapely/vectorized/_vectorized.c +20816 -0
- shapely/vectorized/_vectorized.pyd +0 -0
- shapely/vectorized/_vectorized.pyx +120 -0
- shapely/wkb.py +37 -0
- shapely/wkt.py +26 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
"""Collections of points and related utilities
|
|
2
|
+
"""
|
|
3
|
+
|
|
4
|
+
import sys
|
|
5
|
+
|
|
6
|
+
if sys.version_info[0] < 3:
|
|
7
|
+
range = xrange
|
|
8
|
+
|
|
9
|
+
from ctypes import byref, c_double, c_void_p, cast, POINTER
|
|
10
|
+
from ctypes import ArgumentError
|
|
11
|
+
|
|
12
|
+
from shapely.coords import required
|
|
13
|
+
from shapely.geos import lgeos
|
|
14
|
+
from shapely.geometry.base import BaseMultipartGeometry, exceptNull
|
|
15
|
+
from shapely.geometry.point import Point, geos_point_from_py
|
|
16
|
+
from shapely.geometry.proxy import CachingGeometryProxy
|
|
17
|
+
|
|
18
|
+
__all__ = ['MultiPoint', 'asMultiPoint']
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class MultiPoint(BaseMultipartGeometry):
|
|
22
|
+
|
|
23
|
+
"""A collection of one or more points
|
|
24
|
+
|
|
25
|
+
A MultiPoint has zero area and zero length.
|
|
26
|
+
|
|
27
|
+
Attributes
|
|
28
|
+
----------
|
|
29
|
+
geoms : sequence
|
|
30
|
+
A sequence of Points
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
def __init__(self, points=None):
|
|
34
|
+
"""
|
|
35
|
+
Parameters
|
|
36
|
+
----------
|
|
37
|
+
points : sequence
|
|
38
|
+
A sequence of (x, y [,z]) numeric coordinate pairs or triples or a
|
|
39
|
+
sequence of objects that implement the numpy array interface,
|
|
40
|
+
including instaces of Point.
|
|
41
|
+
|
|
42
|
+
Example
|
|
43
|
+
-------
|
|
44
|
+
Construct a 2 point collection
|
|
45
|
+
|
|
46
|
+
>>> ob = MultiPoint([[0.0, 0.0], [1.0, 2.0]])
|
|
47
|
+
>>> len(ob.geoms)
|
|
48
|
+
2
|
|
49
|
+
>>> type(ob.geoms[0]) == Point
|
|
50
|
+
True
|
|
51
|
+
"""
|
|
52
|
+
super(MultiPoint, self).__init__()
|
|
53
|
+
|
|
54
|
+
if points is None:
|
|
55
|
+
# allow creation of empty multipoints, to support unpickling
|
|
56
|
+
pass
|
|
57
|
+
else:
|
|
58
|
+
self._geom, self._ndim = geos_multipoint_from_py(points)
|
|
59
|
+
|
|
60
|
+
def shape_factory(self, *args):
|
|
61
|
+
return Point(*args)
|
|
62
|
+
|
|
63
|
+
@property
|
|
64
|
+
def __geo_interface__(self):
|
|
65
|
+
return {
|
|
66
|
+
'type': 'MultiPoint',
|
|
67
|
+
'coordinates': tuple([g.coords[0] for g in self.geoms])
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
def svg(self, scale_factor=1.):
|
|
71
|
+
"""
|
|
72
|
+
SVG representation of the geometry. Scale factor is multiplied by
|
|
73
|
+
the size of the SVG symbol so it can be scaled consistently for a
|
|
74
|
+
consistent appearance based on the canvas size.
|
|
75
|
+
"""
|
|
76
|
+
|
|
77
|
+
parts = []
|
|
78
|
+
for part in self.geoms:
|
|
79
|
+
parts.append("""<circle
|
|
80
|
+
cx="{0.x}"
|
|
81
|
+
cy="{0.y}"
|
|
82
|
+
r="{1}"
|
|
83
|
+
stroke="#555555"
|
|
84
|
+
stroke-width="{2}"
|
|
85
|
+
fill="{3}"
|
|
86
|
+
opacity=".6"
|
|
87
|
+
/>""".format(
|
|
88
|
+
part,
|
|
89
|
+
3*scale_factor,
|
|
90
|
+
1*scale_factor, "#66cc99" if self.is_valid else "#ff3333"))
|
|
91
|
+
return "\n".join(parts)
|
|
92
|
+
|
|
93
|
+
@property
|
|
94
|
+
@exceptNull
|
|
95
|
+
def ctypes(self):
|
|
96
|
+
if not self._ctypes_data:
|
|
97
|
+
temp = c_double()
|
|
98
|
+
n = self._ndim
|
|
99
|
+
m = len(self.geoms)
|
|
100
|
+
array_type = c_double * (m * n)
|
|
101
|
+
data = array_type()
|
|
102
|
+
for i in range(m):
|
|
103
|
+
g = self.geoms[i]._geom
|
|
104
|
+
cs = lgeos.GEOSGeom_getCoordSeq(g)
|
|
105
|
+
lgeos.GEOSCoordSeq_getX(cs, 0, byref(temp))
|
|
106
|
+
data[n*i] = temp.value
|
|
107
|
+
lgeos.GEOSCoordSeq_getY(cs, 0, byref(temp))
|
|
108
|
+
data[n*i+1] = temp.value
|
|
109
|
+
if n == 3: # TODO: use hasz
|
|
110
|
+
lgeos.GEOSCoordSeq_getZ(cs, 0, byref(temp))
|
|
111
|
+
data[n*i+2] = temp.value
|
|
112
|
+
self._ctypes_data = data
|
|
113
|
+
return self._ctypes_data
|
|
114
|
+
|
|
115
|
+
@exceptNull
|
|
116
|
+
def array_interface(self):
|
|
117
|
+
"""Provide the Numpy array protocol."""
|
|
118
|
+
ai = self.array_interface_base
|
|
119
|
+
ai.update({'shape': (len(self.geoms), self._ndim)})
|
|
120
|
+
return ai
|
|
121
|
+
__array_interface__ = property(array_interface)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
class MultiPointAdapter(CachingGeometryProxy, MultiPoint):
|
|
125
|
+
|
|
126
|
+
context = None
|
|
127
|
+
_other_owned = False
|
|
128
|
+
|
|
129
|
+
def __init__(self, context):
|
|
130
|
+
self.context = context
|
|
131
|
+
self.factory = geos_multipoint_from_py
|
|
132
|
+
|
|
133
|
+
@property
|
|
134
|
+
def _ndim(self):
|
|
135
|
+
try:
|
|
136
|
+
# From array protocol
|
|
137
|
+
array = self.context.__array_interface__
|
|
138
|
+
n = array['shape'][1]
|
|
139
|
+
assert n == 2 or n == 3
|
|
140
|
+
return n
|
|
141
|
+
except AttributeError:
|
|
142
|
+
# Fall back on list
|
|
143
|
+
return len(self.context[0])
|
|
144
|
+
|
|
145
|
+
@property
|
|
146
|
+
def __array_interface__(self):
|
|
147
|
+
"""Provide the Numpy array protocol."""
|
|
148
|
+
try:
|
|
149
|
+
return self.context.__array_interface__
|
|
150
|
+
except AttributeError:
|
|
151
|
+
return self.array_interface()
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def asMultiPoint(context):
|
|
155
|
+
"""Adapt a sequence of objects to the MultiPoint interface"""
|
|
156
|
+
return MultiPointAdapter(context)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def geos_multipoint_from_py(ob):
|
|
160
|
+
# If numpy is present, we use numpy.require to ensure that we have a
|
|
161
|
+
# C-continguous array that owns its data. View data will be copied.
|
|
162
|
+
ob = required(ob)
|
|
163
|
+
try:
|
|
164
|
+
# From array protocol
|
|
165
|
+
array = ob.__array_interface__
|
|
166
|
+
assert len(array['shape']) == 2
|
|
167
|
+
m = array['shape'][0]
|
|
168
|
+
n = array['shape'][1]
|
|
169
|
+
assert m >= 1
|
|
170
|
+
assert n == 2 or n == 3
|
|
171
|
+
|
|
172
|
+
# Make pointer to the coordinate array
|
|
173
|
+
if isinstance(array['data'], tuple):
|
|
174
|
+
# numpy tuple (addr, read-only)
|
|
175
|
+
cp = cast(array['data'][0], POINTER(c_double))
|
|
176
|
+
else:
|
|
177
|
+
cp = array['data']
|
|
178
|
+
|
|
179
|
+
# Array of pointers to sub-geometries
|
|
180
|
+
subs = (c_void_p * m)()
|
|
181
|
+
|
|
182
|
+
for i in range(m):
|
|
183
|
+
geom, ndims = geos_point_from_py(cp[n*i:n*i+2])
|
|
184
|
+
subs[i] = cast(geom, c_void_p)
|
|
185
|
+
|
|
186
|
+
except AttributeError:
|
|
187
|
+
# Fall back on list
|
|
188
|
+
m = len(ob)
|
|
189
|
+
try:
|
|
190
|
+
n = len(ob[0])
|
|
191
|
+
except TypeError:
|
|
192
|
+
n = ob[0]._ndim
|
|
193
|
+
assert n == 2 or n == 3
|
|
194
|
+
|
|
195
|
+
# Array of pointers to point geometries
|
|
196
|
+
subs = (c_void_p * m)()
|
|
197
|
+
|
|
198
|
+
# add to coordinate sequence
|
|
199
|
+
for i in range(m):
|
|
200
|
+
coords = ob[i]
|
|
201
|
+
geom, ndims = geos_point_from_py(coords)
|
|
202
|
+
subs[i] = cast(geom, c_void_p)
|
|
203
|
+
|
|
204
|
+
return lgeos.GEOSGeom_createCollection(4, subs, m), n
|
|
205
|
+
|
|
206
|
+
# Test runner
|
|
207
|
+
def _test():
|
|
208
|
+
import doctest
|
|
209
|
+
doctest.testmod()
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
if __name__ == "__main__":
|
|
213
|
+
_test()
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"""Collections of polygons and related utilities
|
|
2
|
+
"""
|
|
3
|
+
|
|
4
|
+
import sys
|
|
5
|
+
|
|
6
|
+
if sys.version_info[0] < 3:
|
|
7
|
+
range = xrange
|
|
8
|
+
|
|
9
|
+
from ctypes import c_void_p, cast
|
|
10
|
+
|
|
11
|
+
from shapely.geos import lgeos
|
|
12
|
+
from shapely.geometry.base import BaseMultipartGeometry
|
|
13
|
+
from shapely.geometry.polygon import Polygon, geos_polygon_from_py
|
|
14
|
+
from shapely.geometry.proxy import CachingGeometryProxy
|
|
15
|
+
|
|
16
|
+
__all__ = ['MultiPolygon', 'asMultiPolygon']
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class MultiPolygon(BaseMultipartGeometry):
|
|
20
|
+
|
|
21
|
+
"""A collection of one or more polygons
|
|
22
|
+
|
|
23
|
+
If component polygons overlap the collection is `invalid` and some
|
|
24
|
+
operations on it may fail.
|
|
25
|
+
|
|
26
|
+
Attributes
|
|
27
|
+
----------
|
|
28
|
+
geoms : sequence
|
|
29
|
+
A sequence of `Polygon` instances
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def __init__(self, polygons=None, context_type='polygons'):
|
|
33
|
+
"""
|
|
34
|
+
Parameters
|
|
35
|
+
----------
|
|
36
|
+
polygons : sequence
|
|
37
|
+
A sequence of (shell, holes) tuples where shell is the sequence
|
|
38
|
+
representation of a linear ring (see linearring.py) and holes is
|
|
39
|
+
a sequence of such linear rings
|
|
40
|
+
|
|
41
|
+
Example
|
|
42
|
+
-------
|
|
43
|
+
Construct a collection from a sequence of coordinate tuples
|
|
44
|
+
|
|
45
|
+
>>> ob = MultiPolygon( [
|
|
46
|
+
... (
|
|
47
|
+
... ((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)),
|
|
48
|
+
... [((0.1,0.1), (0.1,0.2), (0.2,0.2), (0.2,0.1))]
|
|
49
|
+
... )
|
|
50
|
+
... ] )
|
|
51
|
+
>>> len(ob.geoms)
|
|
52
|
+
1
|
|
53
|
+
>>> type(ob.geoms[0]) == Polygon
|
|
54
|
+
True
|
|
55
|
+
"""
|
|
56
|
+
super(MultiPolygon, self).__init__()
|
|
57
|
+
|
|
58
|
+
if not polygons:
|
|
59
|
+
# allow creation of empty multipolygons, to support unpickling
|
|
60
|
+
pass
|
|
61
|
+
elif context_type == 'polygons':
|
|
62
|
+
self._geom, self._ndim = geos_multipolygon_from_polygons(polygons)
|
|
63
|
+
elif context_type == 'geojson':
|
|
64
|
+
self._geom, self._ndim = geos_multipolygon_from_py(polygons)
|
|
65
|
+
|
|
66
|
+
def shape_factory(self, *args):
|
|
67
|
+
return Polygon(*args)
|
|
68
|
+
|
|
69
|
+
@property
|
|
70
|
+
def __geo_interface__(self):
|
|
71
|
+
allcoords = []
|
|
72
|
+
for geom in self.geoms:
|
|
73
|
+
coords = []
|
|
74
|
+
coords.append(tuple(geom.exterior.coords))
|
|
75
|
+
for hole in geom.interiors:
|
|
76
|
+
coords.append(tuple(hole.coords))
|
|
77
|
+
allcoords.append(tuple(coords))
|
|
78
|
+
return {
|
|
79
|
+
'type': 'MultiPolygon',
|
|
80
|
+
'coordinates': allcoords
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
def svg(self, scale_factor=1.):
|
|
84
|
+
"""
|
|
85
|
+
SVG representation of the geometry. Scale factor is multiplied by
|
|
86
|
+
the size of the SVG symbol so it can be scaled consistently for a
|
|
87
|
+
consistent appearance based on the canvas size.
|
|
88
|
+
"""
|
|
89
|
+
parts = []
|
|
90
|
+
for part in self.geoms:
|
|
91
|
+
exterior_coords = [["{0},{1}".format(*c) for c in part.exterior.coords]]
|
|
92
|
+
interior_coords = [
|
|
93
|
+
["{0},{1}".format(*c) for c in interior.coords]
|
|
94
|
+
for interior in part.interiors ]
|
|
95
|
+
path = " ".join([
|
|
96
|
+
"M {0} L {1} z".format(coords[0], " L ".join(coords[1:]))
|
|
97
|
+
for coords in exterior_coords + interior_coords ])
|
|
98
|
+
parts.append(
|
|
99
|
+
"""<g fill-rule="evenodd" fill="{2}" stroke="#555555"
|
|
100
|
+
stroke-width="{0}" opacity="0.6">
|
|
101
|
+
<path d="{1}" /></g>""".format(
|
|
102
|
+
2. * scale_factor,
|
|
103
|
+
path,
|
|
104
|
+
"#66cc99" if self.is_valid else "#ff3333"))
|
|
105
|
+
return "\n".join(parts)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
class MultiPolygonAdapter(CachingGeometryProxy, MultiPolygon):
|
|
109
|
+
|
|
110
|
+
context = None
|
|
111
|
+
_other_owned = False
|
|
112
|
+
|
|
113
|
+
def __init__(self, context, context_type='polygons'):
|
|
114
|
+
self.context = context
|
|
115
|
+
if context_type == 'geojson':
|
|
116
|
+
self.factory = geos_multipolygon_from_py
|
|
117
|
+
elif context_type == 'polygons':
|
|
118
|
+
self.factory = geos_multipolygon_from_polygons
|
|
119
|
+
|
|
120
|
+
@property
|
|
121
|
+
def _ndim(self):
|
|
122
|
+
try:
|
|
123
|
+
# From array protocol
|
|
124
|
+
array = self.context[0][0].__array_interface__
|
|
125
|
+
n = array['shape'][1]
|
|
126
|
+
assert n == 2 or n == 3
|
|
127
|
+
return n
|
|
128
|
+
except AttributeError:
|
|
129
|
+
# Fall back on list
|
|
130
|
+
return len(self.context[0][0][0])
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def asMultiPolygon(context):
|
|
134
|
+
"""Adapts a sequence of objects to the MultiPolygon interface"""
|
|
135
|
+
return MultiPolygonAdapter(context)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def geos_multipolygon_from_py(ob):
|
|
139
|
+
"""ob must provide Python geo interface coordinates."""
|
|
140
|
+
L = len(ob)
|
|
141
|
+
assert L >= 1
|
|
142
|
+
|
|
143
|
+
N = len(ob[0][0][0])
|
|
144
|
+
assert N == 2 or N == 3
|
|
145
|
+
|
|
146
|
+
subs = (c_void_p * L)()
|
|
147
|
+
for l in range(L):
|
|
148
|
+
geom, ndims = geos_polygon_from_py(ob[l][0], ob[l][1:])
|
|
149
|
+
subs[l] = cast(geom, c_void_p)
|
|
150
|
+
|
|
151
|
+
return (lgeos.GEOSGeom_createCollection(6, subs, L), N)
|
|
152
|
+
|
|
153
|
+
def geos_multipolygon_from_polygons(ob):
|
|
154
|
+
"""ob must be either a sequence or array of sequences or arrays."""
|
|
155
|
+
obs = getattr(ob, 'geoms', None) or ob
|
|
156
|
+
L = len(obs)
|
|
157
|
+
assert L >= 1
|
|
158
|
+
|
|
159
|
+
exemplar = obs[0]
|
|
160
|
+
try:
|
|
161
|
+
N = len(exemplar[0][0])
|
|
162
|
+
except TypeError:
|
|
163
|
+
N = exemplar._ndim
|
|
164
|
+
|
|
165
|
+
assert N == 2 or N == 3
|
|
166
|
+
|
|
167
|
+
subs = (c_void_p * L)()
|
|
168
|
+
for l in range(L):
|
|
169
|
+
shell = getattr(obs[l], 'exterior', None)
|
|
170
|
+
if shell is None:
|
|
171
|
+
shell = obs[l][0]
|
|
172
|
+
holes = getattr(obs[l], 'interiors', None)
|
|
173
|
+
if holes is None:
|
|
174
|
+
holes = obs[l][1]
|
|
175
|
+
geom, ndims = geos_polygon_from_py(shell, holes)
|
|
176
|
+
subs[l] = cast(geom, c_void_p)
|
|
177
|
+
|
|
178
|
+
return (lgeos.GEOSGeom_createCollection(6, subs, L), N)
|
|
179
|
+
|
|
180
|
+
# Test runner
|
|
181
|
+
def _test():
|
|
182
|
+
import doctest
|
|
183
|
+
doctest.testmod()
|
|
184
|
+
|
|
185
|
+
if __name__ == "__main__":
|
|
186
|
+
_test()
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
"""Points and related utilities
|
|
2
|
+
"""
|
|
3
|
+
|
|
4
|
+
from ctypes import c_double
|
|
5
|
+
from ctypes import cast, POINTER
|
|
6
|
+
|
|
7
|
+
from shapely.coords import required
|
|
8
|
+
from shapely.geos import lgeos, DimensionError
|
|
9
|
+
from shapely.geometry.base import BaseGeometry
|
|
10
|
+
from shapely.geometry.proxy import CachingGeometryProxy
|
|
11
|
+
|
|
12
|
+
__all__ = ['Point', 'asPoint']
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class Point(BaseGeometry):
|
|
16
|
+
"""
|
|
17
|
+
A zero dimensional feature
|
|
18
|
+
|
|
19
|
+
A point has zero length and zero area.
|
|
20
|
+
|
|
21
|
+
Attributes
|
|
22
|
+
----------
|
|
23
|
+
x, y, z : float
|
|
24
|
+
Coordinate values
|
|
25
|
+
|
|
26
|
+
Example
|
|
27
|
+
-------
|
|
28
|
+
>>> p = Point(1.0, -1.0)
|
|
29
|
+
>>> print(p)
|
|
30
|
+
POINT (1.0000000000000000 -1.0000000000000000)
|
|
31
|
+
>>> p.y
|
|
32
|
+
-1.0
|
|
33
|
+
>>> p.x
|
|
34
|
+
1.0
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
def __init__(self, *args):
|
|
38
|
+
"""
|
|
39
|
+
Parameters
|
|
40
|
+
----------
|
|
41
|
+
There are 2 cases:
|
|
42
|
+
|
|
43
|
+
1) 1 parameter: this must satisfy the numpy array protocol.
|
|
44
|
+
2) 2 or more parameters: x, y, z : float
|
|
45
|
+
Easting, northing, and elevation.
|
|
46
|
+
"""
|
|
47
|
+
BaseGeometry.__init__(self)
|
|
48
|
+
if len(args) > 0:
|
|
49
|
+
self._set_coords(*args)
|
|
50
|
+
|
|
51
|
+
# Coordinate getters and setters
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
def x(self):
|
|
55
|
+
"""Return x coordinate."""
|
|
56
|
+
return self.coords[0][0]
|
|
57
|
+
|
|
58
|
+
@property
|
|
59
|
+
def y(self):
|
|
60
|
+
"""Return y coordinate."""
|
|
61
|
+
return self.coords[0][1]
|
|
62
|
+
|
|
63
|
+
@property
|
|
64
|
+
def z(self):
|
|
65
|
+
"""Return z coordinate."""
|
|
66
|
+
if self._ndim != 3:
|
|
67
|
+
raise DimensionError("This point has no z coordinate.")
|
|
68
|
+
return self.coords[0][2]
|
|
69
|
+
|
|
70
|
+
@property
|
|
71
|
+
def __geo_interface__(self):
|
|
72
|
+
return {
|
|
73
|
+
'type': 'Point',
|
|
74
|
+
'coordinates': self.coords[0]
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
def svg(self, scale_factor=1.):
|
|
78
|
+
"""
|
|
79
|
+
SVG representation of the geometry. Scale factor is multiplied by
|
|
80
|
+
the size of the SVG symbol so it can be scaled consistently for a
|
|
81
|
+
consistent appearance based on the canvas size.
|
|
82
|
+
"""
|
|
83
|
+
return """<circle
|
|
84
|
+
cx="{0.x}"
|
|
85
|
+
cy="{0.y}"
|
|
86
|
+
r="{1}"
|
|
87
|
+
stroke="#555555"
|
|
88
|
+
stroke-width="{2}"
|
|
89
|
+
fill="{3}"
|
|
90
|
+
opacity=".6"
|
|
91
|
+
/>""".format(
|
|
92
|
+
self,
|
|
93
|
+
3 * scale_factor,
|
|
94
|
+
1 * scale_factor,
|
|
95
|
+
"#66cc99" if self.is_valid else "#ff3333")
|
|
96
|
+
|
|
97
|
+
@property
|
|
98
|
+
def ctypes(self):
|
|
99
|
+
if not self._ctypes_data:
|
|
100
|
+
array_type = c_double * self._ndim
|
|
101
|
+
array = array_type()
|
|
102
|
+
xy = self.coords[0]
|
|
103
|
+
array[0] = xy[0]
|
|
104
|
+
array[1] = xy[1]
|
|
105
|
+
if self._ndim == 3:
|
|
106
|
+
array[2] = xy[2]
|
|
107
|
+
self._ctypes_data = array
|
|
108
|
+
return self._ctypes_data
|
|
109
|
+
|
|
110
|
+
def array_interface(self):
|
|
111
|
+
"""Provide the Numpy array protocol."""
|
|
112
|
+
ai = self.array_interface_base
|
|
113
|
+
ai.update({'shape': (self._ndim,)})
|
|
114
|
+
return ai
|
|
115
|
+
__array_interface__ = property(array_interface)
|
|
116
|
+
|
|
117
|
+
@property
|
|
118
|
+
def bounds(self):
|
|
119
|
+
xy = self.coords[0]
|
|
120
|
+
return (xy[0], xy[1], xy[0], xy[1])
|
|
121
|
+
|
|
122
|
+
# Coordinate access
|
|
123
|
+
|
|
124
|
+
def _set_coords(self, *args):
|
|
125
|
+
self.empty()
|
|
126
|
+
if len(args) == 1:
|
|
127
|
+
self._geom, self._ndim = geos_point_from_py(args[0])
|
|
128
|
+
else:
|
|
129
|
+
self._geom, self._ndim = geos_point_from_py(tuple(args))
|
|
130
|
+
|
|
131
|
+
coords = property(BaseGeometry._get_coords, _set_coords)
|
|
132
|
+
|
|
133
|
+
@property
|
|
134
|
+
def xy(self):
|
|
135
|
+
"""Separate arrays of X and Y coordinate values
|
|
136
|
+
|
|
137
|
+
Example:
|
|
138
|
+
>>> x, y = Point(0, 0).xy
|
|
139
|
+
>>> list(x)
|
|
140
|
+
[0.0]
|
|
141
|
+
>>> list(y)
|
|
142
|
+
[0.0]
|
|
143
|
+
"""
|
|
144
|
+
return self.coords.xy
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
class PointAdapter(CachingGeometryProxy, Point):
|
|
148
|
+
|
|
149
|
+
_other_owned = False
|
|
150
|
+
|
|
151
|
+
def __init__(self, context):
|
|
152
|
+
self.context = context
|
|
153
|
+
self.factory = geos_point_from_py
|
|
154
|
+
|
|
155
|
+
@property
|
|
156
|
+
def _ndim(self):
|
|
157
|
+
try:
|
|
158
|
+
# From array protocol
|
|
159
|
+
array = self.context.__array_interface__
|
|
160
|
+
n = array['shape'][0]
|
|
161
|
+
assert n == 2 or n == 3
|
|
162
|
+
return n
|
|
163
|
+
except AttributeError:
|
|
164
|
+
# Fall back on list
|
|
165
|
+
return len(self.context)
|
|
166
|
+
|
|
167
|
+
@property
|
|
168
|
+
def __array_interface__(self):
|
|
169
|
+
"""Provide the Numpy array protocol."""
|
|
170
|
+
try:
|
|
171
|
+
return self.context.__array_interface__
|
|
172
|
+
except AttributeError:
|
|
173
|
+
return self.array_interface()
|
|
174
|
+
|
|
175
|
+
_get_coords = BaseGeometry._get_coords
|
|
176
|
+
|
|
177
|
+
def _set_coords(self, ob):
|
|
178
|
+
raise NotImplementedError("Adapters can not modify their sources")
|
|
179
|
+
|
|
180
|
+
coords = property(_get_coords)
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def asPoint(context):
|
|
184
|
+
"""Adapt an object to the Point interface"""
|
|
185
|
+
return PointAdapter(context)
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def geos_point_from_py(ob, update_geom=None, update_ndim=0):
|
|
189
|
+
"""Create a GEOS geom from an object that is a coordinate sequence
|
|
190
|
+
or that provides the array interface.
|
|
191
|
+
|
|
192
|
+
Returns the GEOS geometry and the number of its dimensions.
|
|
193
|
+
"""
|
|
194
|
+
# If numpy is present, we use numpy.require to ensure that we have a
|
|
195
|
+
# C-continguous array that owns its data. View data will be copied.
|
|
196
|
+
ob = required(ob)
|
|
197
|
+
try:
|
|
198
|
+
# From array protocol
|
|
199
|
+
array = ob.__array_interface__
|
|
200
|
+
assert len(array['shape']) == 1
|
|
201
|
+
n = array['shape'][0]
|
|
202
|
+
assert n == 2 or n == 3
|
|
203
|
+
|
|
204
|
+
dz = None
|
|
205
|
+
da = array['data']
|
|
206
|
+
if isinstance(da, tuple):
|
|
207
|
+
cdata = da[0]
|
|
208
|
+
# If we had numpy, we would do
|
|
209
|
+
# from numpy.ctypeslib import as_ctypes
|
|
210
|
+
# cp = as_ctypes(ob) - check that code?
|
|
211
|
+
cp = cast(cdata, POINTER(c_double))
|
|
212
|
+
dx = c_double(cp[0])
|
|
213
|
+
dy = c_double(cp[1])
|
|
214
|
+
if n == 3:
|
|
215
|
+
dz = c_double(cp[2])
|
|
216
|
+
else:
|
|
217
|
+
dx, dy = da[0:2]
|
|
218
|
+
if n == 3:
|
|
219
|
+
dz = da[2]
|
|
220
|
+
|
|
221
|
+
except AttributeError:
|
|
222
|
+
# Fall back on the case of Python sequence data
|
|
223
|
+
# Accept either (x, y) or [(x, y)]
|
|
224
|
+
if not hasattr(ob, '__getitem__'): # Iterators, e.g. Python 3 zip
|
|
225
|
+
ob = list(ob)
|
|
226
|
+
|
|
227
|
+
if isinstance(ob[0], tuple):
|
|
228
|
+
coords = ob[0]
|
|
229
|
+
else:
|
|
230
|
+
coords = ob
|
|
231
|
+
n = len(coords)
|
|
232
|
+
dx = c_double(coords[0])
|
|
233
|
+
dy = c_double(coords[1])
|
|
234
|
+
dz = None
|
|
235
|
+
if n == 3:
|
|
236
|
+
dz = c_double(coords[2])
|
|
237
|
+
|
|
238
|
+
if update_geom:
|
|
239
|
+
cs = lgeos.GEOSGeom_getCoordSeq(update_geom)
|
|
240
|
+
if n != update_ndim:
|
|
241
|
+
raise ValueError(
|
|
242
|
+
"Wrong coordinate dimensions; this geometry has dimensions: "
|
|
243
|
+
"%d" % update_ndim)
|
|
244
|
+
else:
|
|
245
|
+
cs = lgeos.GEOSCoordSeq_create(1, n)
|
|
246
|
+
|
|
247
|
+
# Because of a bug in the GEOS C API, always set X before Y
|
|
248
|
+
lgeos.GEOSCoordSeq_setX(cs, 0, dx)
|
|
249
|
+
lgeos.GEOSCoordSeq_setY(cs, 0, dy)
|
|
250
|
+
if n == 3:
|
|
251
|
+
lgeos.GEOSCoordSeq_setZ(cs, 0, dz)
|
|
252
|
+
|
|
253
|
+
if update_geom:
|
|
254
|
+
return None
|
|
255
|
+
else:
|
|
256
|
+
return lgeos.GEOSGeom_createPoint(cs), n
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def update_point_from_py(geom, ob):
|
|
260
|
+
geos_point_from_py(ob, geom._geom, geom._ndim)
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
# Test runner
|
|
264
|
+
def _test():
|
|
265
|
+
import doctest
|
|
266
|
+
doctest.testmod()
|
|
267
|
+
|
|
268
|
+
if __name__ == "__main__":
|
|
269
|
+
_test()
|