shapely 1.3.3__cp34-none-win32.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
shapely/_geos.pxi
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# The beginnings of a Cython definition of GEOS. In the future much of this
|
|
2
|
+
# could be auto-generated.
|
|
3
|
+
|
|
4
|
+
ctypedef long ptr
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
cdef extern from "geos_c.h":
|
|
8
|
+
ctypedef void *GEOSContextHandle_t
|
|
9
|
+
ctypedef struct GEOSGeometry
|
|
10
|
+
ctypedef struct GEOSCoordSequence
|
|
11
|
+
ctypedef struct GEOSPreparedGeometry
|
|
12
|
+
|
|
13
|
+
GEOSCoordSequence *GEOSCoordSeq_create_r(GEOSContextHandle_t, unsigned int, unsigned int) nogil
|
|
14
|
+
GEOSCoordSequence *GEOSGeom_getCoordSeq_r(GEOSContextHandle_t, GEOSGeometry *) nogil
|
|
15
|
+
|
|
16
|
+
int GEOSCoordSeq_getSize_r(GEOSContextHandle_t, GEOSCoordSequence *, int *) nogil
|
|
17
|
+
int GEOSCoordSeq_setX_r(GEOSContextHandle_t, GEOSCoordSequence *, int, double) nogil
|
|
18
|
+
int GEOSCoordSeq_setY_r(GEOSContextHandle_t, GEOSCoordSequence *, int, double) nogil
|
|
19
|
+
int GEOSCoordSeq_setZ_r(GEOSContextHandle_t, GEOSCoordSequence *, int, double) nogil
|
|
20
|
+
int GEOSCoordSeq_getX_r(GEOSContextHandle_t, GEOSCoordSequence *, int, double *) nogil
|
|
21
|
+
int GEOSCoordSeq_getY_r(GEOSContextHandle_t, GEOSCoordSequence *, int, double *) nogil
|
|
22
|
+
int GEOSCoordSeq_getZ_r(GEOSContextHandle_t, GEOSCoordSequence *, int, double *) nogil
|
|
23
|
+
|
|
24
|
+
GEOSGeometry *GEOSGeom_createPoint_r(GEOSContextHandle_t, GEOSCoordSequence *) nogil
|
|
25
|
+
GEOSGeometry *GEOSGeom_createLineString_r(GEOSContextHandle_t, GEOSCoordSequence *) nogil
|
|
26
|
+
GEOSGeometry *GEOSGeom_createLinearRing_r(GEOSContextHandle_t, GEOSCoordSequence *) nogil
|
|
27
|
+
|
|
28
|
+
void GEOSGeom_destroy_r(GEOSContextHandle_t, GEOSGeometry *) nogil
|
|
29
|
+
|
|
30
|
+
char GEOSPreparedContains_r(GEOSContextHandle_t, const GEOSPreparedGeometry*, const GEOSGeometry*) nogil
|
|
31
|
+
char GEOSPreparedContainsProperly_r(GEOSContextHandle_t, const GEOSPreparedGeometry*, const GEOSGeometry*) nogil
|
|
32
|
+
char GEOSPreparedCoveredBy_r(GEOSContextHandle_t, const GEOSPreparedGeometry*, const GEOSGeometry*) nogil
|
|
33
|
+
char GEOSPreparedCovers_r(GEOSContextHandle_t, const GEOSPreparedGeometry*, const GEOSGeometry*) nogil
|
|
34
|
+
char GEOSPreparedCrosses_r(GEOSContextHandle_t, const GEOSPreparedGeometry*, const GEOSGeometry*) nogil
|
|
35
|
+
char GEOSPreparedDisjoint_r(GEOSContextHandle_t, const GEOSPreparedGeometry*, const GEOSGeometry*) nogil
|
|
36
|
+
char GEOSPreparedIntersects_r(GEOSContextHandle_t, const GEOSPreparedGeometry*, const GEOSGeometry*) nogil
|
|
37
|
+
char GEOSPreparedOverlaps_r(GEOSContextHandle_t, const GEOSPreparedGeometry*, const GEOSGeometry*) nogil
|
|
38
|
+
char GEOSPreparedTouches_r(GEOSContextHandle_t, const GEOSPreparedGeometry*, const GEOSGeometry*) nogil
|
|
39
|
+
char GEOSPreparedWithin_r(GEOSContextHandle_t, const GEOSPreparedGeometry*, const GEOSGeometry*) nogil
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
cdef GEOSContextHandle_t get_geos_context_handle():
|
|
43
|
+
# Note: This requires that lgeos is defined, so needs to be imported as:
|
|
44
|
+
from shapely.geos import lgeos
|
|
45
|
+
cdef ptr handle = lgeos.geos_handle
|
|
46
|
+
return <GEOSContextHandle_t>handle
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
cdef GEOSPreparedGeometry *geos_from_prepared(shapely_geom) except *:
|
|
50
|
+
"""Get the Prepared GEOS geometry pointer from the given shapely geometry."""
|
|
51
|
+
cdef ptr geos_geom = shapely_geom._geom
|
|
52
|
+
return <GEOSPreparedGeometry *>geos_geom
|
shapely/affinity.py
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
"""Affine transforms, both in general and specific, named transforms."""
|
|
2
|
+
|
|
3
|
+
from math import sin, cos, tan, pi
|
|
4
|
+
|
|
5
|
+
__all__ = ['affine_transform', 'rotate', 'scale', 'skew', 'translate']
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def affine_transform(geom, matrix):
|
|
9
|
+
"""Returns a transformed geometry using an affine transformation matrix.
|
|
10
|
+
|
|
11
|
+
The coefficient matrix is provided as a list or tuple with 6 or 12 items
|
|
12
|
+
for 2D or 3D transformations, respectively.
|
|
13
|
+
|
|
14
|
+
For 2D affine transformations, the 6 parameter matrix is:
|
|
15
|
+
|
|
16
|
+
[a, b, d, e, xoff, yoff]
|
|
17
|
+
|
|
18
|
+
which represents the augmented matrix:
|
|
19
|
+
|
|
20
|
+
/ a b xoff \
|
|
21
|
+
[x' y' 1] = [x y 1] | d e yoff |
|
|
22
|
+
\ 0 0 1 /
|
|
23
|
+
|
|
24
|
+
or the equations for the transformed coordinates:
|
|
25
|
+
|
|
26
|
+
x' = a * x + b * y + xoff
|
|
27
|
+
y' = d * x + e * y + yoff
|
|
28
|
+
|
|
29
|
+
For 3D affine transformations, the 12 parameter matrix is:
|
|
30
|
+
|
|
31
|
+
[a, b, c, d, e, f, g, h, i, xoff, yoff, zoff]
|
|
32
|
+
|
|
33
|
+
which represents the augmented matrix:
|
|
34
|
+
|
|
35
|
+
/ a b c xoff \
|
|
36
|
+
[x' y' z' 1] = [x y z 1] | d e f yoff |
|
|
37
|
+
| g h i zoff |
|
|
38
|
+
\ 0 0 0 1 /
|
|
39
|
+
|
|
40
|
+
or the equations for the transformed coordinates:
|
|
41
|
+
|
|
42
|
+
x' = a * x + b * y + c * z + xoff
|
|
43
|
+
y' = d * x + e * y + f * z + yoff
|
|
44
|
+
z' = g * x + h * y + i * z + zoff
|
|
45
|
+
"""
|
|
46
|
+
if geom.is_empty:
|
|
47
|
+
return geom
|
|
48
|
+
if len(matrix) == 6:
|
|
49
|
+
ndim = 2
|
|
50
|
+
a, b, d, e, xoff, yoff = matrix
|
|
51
|
+
if geom.has_z:
|
|
52
|
+
ndim = 3
|
|
53
|
+
i = 1.0
|
|
54
|
+
c = f = g = h = zoff = 0.0
|
|
55
|
+
matrix = a, b, c, d, e, f, g, h, i, xoff, yoff, zoff
|
|
56
|
+
elif len(matrix) == 12:
|
|
57
|
+
ndim = 3
|
|
58
|
+
a, b, c, d, e, f, g, h, i, xoff, yoff, zoff = matrix
|
|
59
|
+
if not geom.has_z:
|
|
60
|
+
ndim = 2
|
|
61
|
+
matrix = a, b, d, e, xoff, yoff
|
|
62
|
+
else:
|
|
63
|
+
raise ValueError("'matrix' expects either 6 or 12 coefficients")
|
|
64
|
+
|
|
65
|
+
def affine_pts(pts):
|
|
66
|
+
"""Internal function to yield affine transform of coordinate tuples"""
|
|
67
|
+
if ndim == 2:
|
|
68
|
+
for x, y in pts:
|
|
69
|
+
xp = a * x + b * y + xoff
|
|
70
|
+
yp = d * x + e * y + yoff
|
|
71
|
+
yield (xp, yp)
|
|
72
|
+
elif ndim == 3:
|
|
73
|
+
for x, y, z in pts:
|
|
74
|
+
xp = a * x + b * y + c * z + xoff
|
|
75
|
+
yp = d * x + e * y + f * z + yoff
|
|
76
|
+
zp = g * x + h * y + i * z + zoff
|
|
77
|
+
yield (xp, yp, zp)
|
|
78
|
+
|
|
79
|
+
# Process coordinates from each supported geometry type
|
|
80
|
+
if geom.type in ('Point', 'LineString', 'LinearRing'):
|
|
81
|
+
return type(geom)(list(affine_pts(geom.coords)))
|
|
82
|
+
elif geom.type == 'Polygon':
|
|
83
|
+
ring = geom.exterior
|
|
84
|
+
shell = type(ring)(list(affine_pts(ring.coords)))
|
|
85
|
+
holes = list(geom.interiors)
|
|
86
|
+
for pos, ring in enumerate(holes):
|
|
87
|
+
holes[pos] = type(ring)(list(affine_pts(ring.coords)))
|
|
88
|
+
return type(geom)(shell, holes)
|
|
89
|
+
elif geom.type.startswith('Multi') or geom.type == 'GeometryCollection':
|
|
90
|
+
# Recursive call
|
|
91
|
+
# TODO: fix GeometryCollection constructor
|
|
92
|
+
return type(geom)([affine_transform(part, matrix)
|
|
93
|
+
for part in geom.geoms])
|
|
94
|
+
else:
|
|
95
|
+
raise ValueError('Type %r not recognized' % geom.type)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def interpret_origin(geom, origin, ndim):
|
|
99
|
+
"""Returns interpreted coordinate tuple for origin parameter.
|
|
100
|
+
|
|
101
|
+
This is a helper function for other transform functions.
|
|
102
|
+
|
|
103
|
+
The point of origin can be a keyword 'center' for the 2D bounding box
|
|
104
|
+
center, 'centroid' for the geometry's 2D centroid, a Point object or a
|
|
105
|
+
coordinate tuple (x0, y0, z0).
|
|
106
|
+
"""
|
|
107
|
+
# get coordinate tuple from 'origin' from keyword or Point type
|
|
108
|
+
if origin == 'center':
|
|
109
|
+
# bounding box center
|
|
110
|
+
minx, miny, maxx, maxy = geom.bounds
|
|
111
|
+
origin = ((maxx + minx)/2.0, (maxy + miny)/2.0)
|
|
112
|
+
elif origin == 'centroid':
|
|
113
|
+
origin = geom.centroid.coords[0]
|
|
114
|
+
elif isinstance(origin, str):
|
|
115
|
+
raise ValueError("'origin' keyword %r is not recognized" % origin)
|
|
116
|
+
elif hasattr(origin, 'type') and origin.type == 'Point':
|
|
117
|
+
origin = origin.coords[0]
|
|
118
|
+
|
|
119
|
+
# origin should now be tuple-like
|
|
120
|
+
if len(origin) not in (2, 3):
|
|
121
|
+
raise ValueError("Expected number of items in 'origin' to be "
|
|
122
|
+
"either 2 or 3")
|
|
123
|
+
if ndim == 2:
|
|
124
|
+
return origin[0:2]
|
|
125
|
+
else: # 3D coordinate
|
|
126
|
+
if len(origin) == 2:
|
|
127
|
+
return origin + (0.0,)
|
|
128
|
+
else:
|
|
129
|
+
return origin
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def rotate(geom, angle, origin='center', use_radians=False):
|
|
133
|
+
"""Returns a rotated geometry on a 2D plane.
|
|
134
|
+
|
|
135
|
+
The angle of rotation can be specified in either degrees (default) or
|
|
136
|
+
radians by setting ``use_radians=True``. Positive angles are
|
|
137
|
+
counter-clockwise and negative are clockwise rotations.
|
|
138
|
+
|
|
139
|
+
The point of origin can be a keyword 'center' for the bounding box
|
|
140
|
+
center (default), 'centroid' for the geometry's centroid, a Point object
|
|
141
|
+
or a coordinate tuple (x0, y0).
|
|
142
|
+
|
|
143
|
+
The affine transformation matrix for 2D rotation is:
|
|
144
|
+
|
|
145
|
+
/ cos(r) -sin(r) xoff \
|
|
146
|
+
| sin(r) cos(r) yoff |
|
|
147
|
+
\ 0 0 1 /
|
|
148
|
+
|
|
149
|
+
where the offsets are calculated from the origin Point(x0, y0):
|
|
150
|
+
|
|
151
|
+
xoff = x0 - x0 * cos(r) + y0 * sin(r)
|
|
152
|
+
yoff = y0 - x0 * sin(r) - y0 * cos(r)
|
|
153
|
+
"""
|
|
154
|
+
if not use_radians: # convert from degrees
|
|
155
|
+
angle *= pi/180.0
|
|
156
|
+
cosp = cos(angle)
|
|
157
|
+
sinp = sin(angle)
|
|
158
|
+
if abs(cosp) < 2.5e-16:
|
|
159
|
+
cosp = 0.0
|
|
160
|
+
if abs(sinp) < 2.5e-16:
|
|
161
|
+
sinp = 0.0
|
|
162
|
+
x0, y0 = interpret_origin(geom, origin, 2)
|
|
163
|
+
|
|
164
|
+
matrix = (cosp, -sinp, 0.0,
|
|
165
|
+
sinp, cosp, 0.0,
|
|
166
|
+
0.0, 0.0, 1.0,
|
|
167
|
+
x0 - x0 * cosp + y0 * sinp, y0 - x0 * sinp - y0 * cosp, 0.0)
|
|
168
|
+
return affine_transform(geom, matrix)
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def scale(geom, xfact=1.0, yfact=1.0, zfact=1.0, origin='center'):
|
|
172
|
+
"""Returns a scaled geometry, scaled by factors along each dimension.
|
|
173
|
+
|
|
174
|
+
The point of origin can be a keyword 'center' for the 2D bounding box
|
|
175
|
+
center (default), 'centroid' for the geometry's 2D centroid, a Point
|
|
176
|
+
object or a coordinate tuple (x0, y0, z0).
|
|
177
|
+
|
|
178
|
+
Negative scale factors will mirror or reflect coordinates.
|
|
179
|
+
|
|
180
|
+
The general 3D affine transformation matrix for scaling is:
|
|
181
|
+
|
|
182
|
+
/ xfact 0 0 xoff \
|
|
183
|
+
| 0 yfact 0 yoff |
|
|
184
|
+
| 0 0 zfact zoff |
|
|
185
|
+
\ 0 0 0 1 /
|
|
186
|
+
|
|
187
|
+
where the offsets are calculated from the origin Point(x0, y0, z0):
|
|
188
|
+
|
|
189
|
+
xoff = x0 - x0 * xfact
|
|
190
|
+
yoff = y0 - y0 * yfact
|
|
191
|
+
zoff = z0 - z0 * zfact
|
|
192
|
+
"""
|
|
193
|
+
x0, y0, z0 = interpret_origin(geom, origin, 3)
|
|
194
|
+
|
|
195
|
+
matrix = (xfact, 0.0, 0.0,
|
|
196
|
+
0.0, yfact, 0.0,
|
|
197
|
+
0.0, 0.0, zfact,
|
|
198
|
+
x0 - x0 * xfact, y0 - y0 * yfact, z0 - z0 * zfact)
|
|
199
|
+
return affine_transform(geom, matrix)
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def skew(geom, xs=0.0, ys=0.0, origin='center', use_radians=False):
|
|
203
|
+
"""Returns a skewed geometry, sheared by angles along x and y dimensions.
|
|
204
|
+
|
|
205
|
+
The shear angle can be specified in either degrees (default) or radians
|
|
206
|
+
by setting ``use_radians=True``.
|
|
207
|
+
|
|
208
|
+
The point of origin can be a keyword 'center' for the bounding box
|
|
209
|
+
center (default), 'centroid' for the geometry's centroid, a Point object
|
|
210
|
+
or a coordinate tuple (x0, y0).
|
|
211
|
+
|
|
212
|
+
The general 2D affine transformation matrix for skewing is:
|
|
213
|
+
|
|
214
|
+
/ 1 tan(xs) xoff \
|
|
215
|
+
| tan(ys) 1 yoff |
|
|
216
|
+
\ 0 0 1 /
|
|
217
|
+
|
|
218
|
+
where the offsets are calculated from the origin Point(x0, y0):
|
|
219
|
+
|
|
220
|
+
xoff = -y0 * tan(xs)
|
|
221
|
+
yoff = -x0 * tan(ys)
|
|
222
|
+
"""
|
|
223
|
+
if not use_radians: # convert from degrees
|
|
224
|
+
xs *= pi/180.0
|
|
225
|
+
ys *= pi/180.0
|
|
226
|
+
tanx = tan(xs)
|
|
227
|
+
tany = tan(ys)
|
|
228
|
+
if abs(tanx) < 2.5e-16:
|
|
229
|
+
tanx = 0.0
|
|
230
|
+
if abs(tany) < 2.5e-16:
|
|
231
|
+
tany = 0.0
|
|
232
|
+
x0, y0 = interpret_origin(geom, origin, 2)
|
|
233
|
+
|
|
234
|
+
matrix = (1.0, tanx, 0.0,
|
|
235
|
+
tany, 1.0, 0.0,
|
|
236
|
+
0.0, 0.0, 1.0,
|
|
237
|
+
-y0 * tanx, -x0 * tany, 0.0)
|
|
238
|
+
return affine_transform(geom, matrix)
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def translate(geom, xoff=0.0, yoff=0.0, zoff=0.0):
|
|
242
|
+
"""Returns a translated geometry shifted by offsets along each dimension.
|
|
243
|
+
|
|
244
|
+
The general 3D affine transformation matrix for translation is:
|
|
245
|
+
|
|
246
|
+
/ 1 0 0 xoff \
|
|
247
|
+
| 0 1 0 yoff |
|
|
248
|
+
| 0 0 1 zoff |
|
|
249
|
+
\ 0 0 0 1 /
|
|
250
|
+
"""
|
|
251
|
+
matrix = (1.0, 0.0, 0.0,
|
|
252
|
+
0.0, 1.0, 0.0,
|
|
253
|
+
0.0, 0.0, 1.0,
|
|
254
|
+
xoff, yoff, zoff)
|
|
255
|
+
return affine_transform(geom, matrix)
|
|
File without changes
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
|
|
2
|
+
def signed_area(ring):
|
|
3
|
+
"""Return the signed area enclosed by a ring in linear time using the
|
|
4
|
+
algorithm at: http://www.cgafaq.info/wiki/Polygon_Area.
|
|
5
|
+
"""
|
|
6
|
+
xs, ys = ring.coords.xy
|
|
7
|
+
xs.append(xs[1])
|
|
8
|
+
ys.append(ys[1])
|
|
9
|
+
return sum(xs[i]*(ys[i+1]-ys[i-1]) for i in range(1, len(ring.coords)))/2.0
|
|
10
|
+
|
|
11
|
+
def is_ccw_impl(name):
|
|
12
|
+
"""Predicate implementation"""
|
|
13
|
+
def is_ccw_op(ring):
|
|
14
|
+
return signed_area(ring) >= 0.0
|
|
15
|
+
return is_ccw_op
|
|
16
|
+
|
shapely/coords.py
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
"""Coordinate sequence utilities
|
|
2
|
+
"""
|
|
3
|
+
|
|
4
|
+
import sys
|
|
5
|
+
from array import array
|
|
6
|
+
from ctypes import byref, c_double, c_uint
|
|
7
|
+
|
|
8
|
+
from shapely.geos import lgeos
|
|
9
|
+
from shapely.topology import Validating
|
|
10
|
+
|
|
11
|
+
if sys.version_info[0] < 3:
|
|
12
|
+
range = xrange
|
|
13
|
+
|
|
14
|
+
try:
|
|
15
|
+
import numpy
|
|
16
|
+
has_numpy = True
|
|
17
|
+
except ImportError:
|
|
18
|
+
has_numpy = False
|
|
19
|
+
|
|
20
|
+
def required(ob):
|
|
21
|
+
"""Return an object that meets Shapely requirements for self-owned
|
|
22
|
+
C-continguous data, copying if necessary, or just return the original
|
|
23
|
+
object."""
|
|
24
|
+
if (hasattr(ob, '__array_interface__')
|
|
25
|
+
and ob.__array_interface__.get('strides')):
|
|
26
|
+
if has_numpy:
|
|
27
|
+
return numpy.require(ob, numpy.float64, ["C", "OWNDATA"])
|
|
28
|
+
else:
|
|
29
|
+
# raise an error if strided. See issue #52.
|
|
30
|
+
raise ValueError("C-contiguous data is required")
|
|
31
|
+
else:
|
|
32
|
+
return ob
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class CoordinateSequence(object):
|
|
36
|
+
"""
|
|
37
|
+
Iterative access to coordinate tuples from the parent geometry's coordinate
|
|
38
|
+
sequence.
|
|
39
|
+
|
|
40
|
+
Example:
|
|
41
|
+
|
|
42
|
+
>>> from shapely.wkt import loads
|
|
43
|
+
>>> g = loads('POINT (0.0 0.0)')
|
|
44
|
+
>>> list(g.coords)
|
|
45
|
+
[(0.0, 0.0)]
|
|
46
|
+
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
# Attributes
|
|
50
|
+
# ----------
|
|
51
|
+
# _cseq : c_void_p
|
|
52
|
+
# Ctypes pointer to GEOS coordinate sequence
|
|
53
|
+
# _ndim : int
|
|
54
|
+
# Number of dimensions (2 or 3, generally)
|
|
55
|
+
# __p__ : object
|
|
56
|
+
# Parent (Shapely) geometry
|
|
57
|
+
_cseq = None
|
|
58
|
+
_ndim = None
|
|
59
|
+
__p__ = None
|
|
60
|
+
|
|
61
|
+
def __init__(self, parent):
|
|
62
|
+
self.__p__ = parent
|
|
63
|
+
|
|
64
|
+
def _update(self):
|
|
65
|
+
self._ndim = self.__p__._ndim
|
|
66
|
+
self._cseq = lgeos.GEOSGeom_getCoordSeq(self.__p__._geom)
|
|
67
|
+
|
|
68
|
+
def __len__(self):
|
|
69
|
+
self._update()
|
|
70
|
+
cs_len = c_uint(0)
|
|
71
|
+
lgeos.GEOSCoordSeq_getSize(self._cseq, byref(cs_len))
|
|
72
|
+
return cs_len.value
|
|
73
|
+
|
|
74
|
+
def __iter__(self):
|
|
75
|
+
self._update()
|
|
76
|
+
dx = c_double()
|
|
77
|
+
dy = c_double()
|
|
78
|
+
dz = c_double()
|
|
79
|
+
has_z = self._ndim == 3
|
|
80
|
+
for i in range(self.__len__()):
|
|
81
|
+
lgeos.GEOSCoordSeq_getX(self._cseq, i, byref(dx))
|
|
82
|
+
lgeos.GEOSCoordSeq_getY(self._cseq, i, byref(dy))
|
|
83
|
+
if has_z:
|
|
84
|
+
lgeos.GEOSCoordSeq_getZ(self._cseq, i, byref(dz))
|
|
85
|
+
yield (dx.value, dy.value, dz.value)
|
|
86
|
+
else:
|
|
87
|
+
yield (dx.value, dy.value)
|
|
88
|
+
|
|
89
|
+
def __getitem__(self, key):
|
|
90
|
+
self._update()
|
|
91
|
+
dx = c_double()
|
|
92
|
+
dy = c_double()
|
|
93
|
+
dz = c_double()
|
|
94
|
+
m = self.__len__()
|
|
95
|
+
has_z = self._ndim == 3
|
|
96
|
+
if isinstance(key, int):
|
|
97
|
+
if key + m < 0 or key >= m:
|
|
98
|
+
raise IndexError("index out of range")
|
|
99
|
+
if key < 0:
|
|
100
|
+
i = m + key
|
|
101
|
+
else:
|
|
102
|
+
i = key
|
|
103
|
+
lgeos.GEOSCoordSeq_getX(self._cseq, i, byref(dx))
|
|
104
|
+
lgeos.GEOSCoordSeq_getY(self._cseq, i, byref(dy))
|
|
105
|
+
if has_z:
|
|
106
|
+
lgeos.GEOSCoordSeq_getZ(self._cseq, i, byref(dz))
|
|
107
|
+
return (dx.value, dy.value, dz.value)
|
|
108
|
+
else:
|
|
109
|
+
return (dx.value, dy.value)
|
|
110
|
+
elif isinstance(key, slice):
|
|
111
|
+
res = []
|
|
112
|
+
start, stop, stride = key.indices(m)
|
|
113
|
+
for i in range(start, stop, stride):
|
|
114
|
+
lgeos.GEOSCoordSeq_getX(self._cseq, i, byref(dx))
|
|
115
|
+
lgeos.GEOSCoordSeq_getY(self._cseq, i, byref(dy))
|
|
116
|
+
if has_z:
|
|
117
|
+
lgeos.GEOSCoordSeq_getZ(self._cseq, i, byref(dz))
|
|
118
|
+
res.append((dx.value, dy.value, dz.value))
|
|
119
|
+
else:
|
|
120
|
+
res.append((dx.value, dy.value))
|
|
121
|
+
return res
|
|
122
|
+
else:
|
|
123
|
+
raise TypeError("key must be an index or slice")
|
|
124
|
+
|
|
125
|
+
@property
|
|
126
|
+
def ctypes(self):
|
|
127
|
+
self._update()
|
|
128
|
+
has_z = self._ndim == 3
|
|
129
|
+
n = self._ndim
|
|
130
|
+
m = self.__len__()
|
|
131
|
+
array_type = c_double * (m * n)
|
|
132
|
+
data = array_type()
|
|
133
|
+
temp = c_double()
|
|
134
|
+
for i in range(m):
|
|
135
|
+
lgeos.GEOSCoordSeq_getX(self._cseq, i, byref(temp))
|
|
136
|
+
data[n*i] = temp.value
|
|
137
|
+
lgeos.GEOSCoordSeq_getY(self._cseq, i, byref(temp))
|
|
138
|
+
data[n*i+1] = temp.value
|
|
139
|
+
if has_z:
|
|
140
|
+
lgeos.GEOSCoordSeq_getZ(self._cseq, i, byref(temp))
|
|
141
|
+
data[n*i+2] = temp.value
|
|
142
|
+
return data
|
|
143
|
+
|
|
144
|
+
def array_interface(self):
|
|
145
|
+
"""Provide the Numpy array protocol."""
|
|
146
|
+
if sys.byteorder == 'little':
|
|
147
|
+
typestr = '<f8'
|
|
148
|
+
elif sys.byteorder == 'big':
|
|
149
|
+
typestr = '>f8'
|
|
150
|
+
else:
|
|
151
|
+
raise ValueError(
|
|
152
|
+
"Unsupported byteorder: neither little nor big-endian")
|
|
153
|
+
ai = {
|
|
154
|
+
'version': 3,
|
|
155
|
+
'typestr': typestr,
|
|
156
|
+
'data': self.ctypes,
|
|
157
|
+
}
|
|
158
|
+
ai.update({'shape': (len(self), self._ndim)})
|
|
159
|
+
return ai
|
|
160
|
+
|
|
161
|
+
__array_interface__ = property(array_interface)
|
|
162
|
+
|
|
163
|
+
@property
|
|
164
|
+
def xy(self):
|
|
165
|
+
"""X and Y arrays"""
|
|
166
|
+
self._update()
|
|
167
|
+
m = self.__len__()
|
|
168
|
+
x = array('d')
|
|
169
|
+
y = array('d')
|
|
170
|
+
temp = c_double()
|
|
171
|
+
for i in range(m):
|
|
172
|
+
lgeos.GEOSCoordSeq_getX(self._cseq, i, byref(temp))
|
|
173
|
+
x.append(temp.value)
|
|
174
|
+
lgeos.GEOSCoordSeq_getY(self._cseq, i, byref(temp))
|
|
175
|
+
y.append(temp.value)
|
|
176
|
+
return x, y
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
class BoundsOp(Validating):
|
|
180
|
+
|
|
181
|
+
def __init__(self, *args):
|
|
182
|
+
pass
|
|
183
|
+
|
|
184
|
+
def __call__(self, this):
|
|
185
|
+
self._validate(this)
|
|
186
|
+
env = this.envelope
|
|
187
|
+
if env.geom_type == 'Point':
|
|
188
|
+
return env.bounds
|
|
189
|
+
cs = lgeos.GEOSGeom_getCoordSeq(env.exterior._geom)
|
|
190
|
+
cs_len = c_uint(0)
|
|
191
|
+
lgeos.GEOSCoordSeq_getSize(cs, byref(cs_len))
|
|
192
|
+
minx = 1.e+20
|
|
193
|
+
maxx = -1e+20
|
|
194
|
+
miny = 1.e+20
|
|
195
|
+
maxy = -1e+20
|
|
196
|
+
temp = c_double()
|
|
197
|
+
for i in range(cs_len.value):
|
|
198
|
+
lgeos.GEOSCoordSeq_getX(cs, i, byref(temp))
|
|
199
|
+
x = temp.value
|
|
200
|
+
if x < minx: minx = x
|
|
201
|
+
if x > maxx: maxx = x
|
|
202
|
+
lgeos.GEOSCoordSeq_getY(cs, i, byref(temp))
|
|
203
|
+
y = temp.value
|
|
204
|
+
if y < miny: miny = y
|
|
205
|
+
if y > maxy: maxy = y
|
|
206
|
+
return (minx, miny, maxx, maxy)
|