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
|
@@ -0,0 +1,504 @@
|
|
|
1
|
+
"""Polygons and their linear ring components
|
|
2
|
+
"""
|
|
3
|
+
|
|
4
|
+
import sys
|
|
5
|
+
|
|
6
|
+
if sys.version_info[0] < 3:
|
|
7
|
+
range = xrange
|
|
8
|
+
|
|
9
|
+
from ctypes import c_double, c_void_p, cast, POINTER
|
|
10
|
+
from ctypes import ArgumentError
|
|
11
|
+
import weakref
|
|
12
|
+
|
|
13
|
+
from shapely.algorithms.cga import signed_area
|
|
14
|
+
from shapely.coords import required
|
|
15
|
+
from shapely.geos import lgeos
|
|
16
|
+
from shapely.geometry.base import BaseGeometry
|
|
17
|
+
from shapely.geometry.linestring import LineString, LineStringAdapter
|
|
18
|
+
from shapely.geometry.proxy import PolygonProxy
|
|
19
|
+
|
|
20
|
+
__all__ = ['Polygon', 'asPolygon', 'LinearRing', 'asLinearRing']
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class LinearRing(LineString):
|
|
24
|
+
"""
|
|
25
|
+
A closed one-dimensional feature comprising one or more line segments
|
|
26
|
+
|
|
27
|
+
A LinearRing that crosses itself or touches itself at a single point is
|
|
28
|
+
invalid and operations on it may fail.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
def __init__(self, coordinates=None):
|
|
32
|
+
"""
|
|
33
|
+
Parameters
|
|
34
|
+
----------
|
|
35
|
+
coordinates : sequence
|
|
36
|
+
A sequence of (x, y [,z]) numeric coordinate pairs or triples
|
|
37
|
+
|
|
38
|
+
Rings are implicitly closed. There is no need to specific a final
|
|
39
|
+
coordinate pair identical to the first.
|
|
40
|
+
|
|
41
|
+
Example
|
|
42
|
+
-------
|
|
43
|
+
Construct a square ring.
|
|
44
|
+
|
|
45
|
+
>>> ring = LinearRing( ((0, 0), (0, 1), (1 ,1 ), (1 , 0)) )
|
|
46
|
+
>>> ring.is_closed
|
|
47
|
+
True
|
|
48
|
+
>>> ring.length
|
|
49
|
+
4.0
|
|
50
|
+
"""
|
|
51
|
+
BaseGeometry.__init__(self)
|
|
52
|
+
if coordinates is not None:
|
|
53
|
+
self._set_coords(coordinates)
|
|
54
|
+
|
|
55
|
+
@property
|
|
56
|
+
def __geo_interface__(self):
|
|
57
|
+
return {
|
|
58
|
+
'type': 'LinearRing',
|
|
59
|
+
'coordinates': tuple(self.coords)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
# Coordinate access
|
|
63
|
+
|
|
64
|
+
_get_coords = BaseGeometry._get_coords
|
|
65
|
+
|
|
66
|
+
def _set_coords(self, coordinates):
|
|
67
|
+
self.empty()
|
|
68
|
+
self._geom, self._ndim = geos_linearring_from_py(coordinates)
|
|
69
|
+
|
|
70
|
+
coords = property(_get_coords, _set_coords)
|
|
71
|
+
|
|
72
|
+
@property
|
|
73
|
+
def is_ccw(self):
|
|
74
|
+
"""True is the ring is oriented counter clock-wise"""
|
|
75
|
+
return bool(self.impl['is_ccw'](self))
|
|
76
|
+
|
|
77
|
+
@property
|
|
78
|
+
def is_simple(self):
|
|
79
|
+
"""True if the geometry is simple, meaning that any self-intersections
|
|
80
|
+
are only at boundary points, else False"""
|
|
81
|
+
return LineString(self).is_simple
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
class LinearRingAdapter(LineStringAdapter):
|
|
85
|
+
|
|
86
|
+
__p__ = None
|
|
87
|
+
|
|
88
|
+
def __init__(self, context):
|
|
89
|
+
self.context = context
|
|
90
|
+
self.factory = geos_linearring_from_py
|
|
91
|
+
|
|
92
|
+
@property
|
|
93
|
+
def __geo_interface__(self):
|
|
94
|
+
return {
|
|
95
|
+
'type': 'LinearRing',
|
|
96
|
+
'coordinates': tuple(self.coords)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
coords = property(BaseGeometry._get_coords)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def asLinearRing(context):
|
|
103
|
+
"""Adapt an object to the LinearRing interface"""
|
|
104
|
+
return LinearRingAdapter(context)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
class InteriorRingSequence(object):
|
|
108
|
+
|
|
109
|
+
_factory = None
|
|
110
|
+
_geom = None
|
|
111
|
+
__p__ = None
|
|
112
|
+
_ndim = None
|
|
113
|
+
_index = 0
|
|
114
|
+
_length = 0
|
|
115
|
+
__rings__ = None
|
|
116
|
+
_gtag = None
|
|
117
|
+
|
|
118
|
+
def __init__(self, parent):
|
|
119
|
+
self.__p__ = parent
|
|
120
|
+
self._geom = parent._geom
|
|
121
|
+
self._ndim = parent._ndim
|
|
122
|
+
|
|
123
|
+
def __iter__(self):
|
|
124
|
+
self._index = 0
|
|
125
|
+
self._length = self.__len__()
|
|
126
|
+
return self
|
|
127
|
+
|
|
128
|
+
def __next__(self):
|
|
129
|
+
if self._index < self._length:
|
|
130
|
+
ring = self._get_ring(self._index)
|
|
131
|
+
self._index += 1
|
|
132
|
+
return ring
|
|
133
|
+
else:
|
|
134
|
+
raise StopIteration
|
|
135
|
+
|
|
136
|
+
if sys.version_info[0] < 3:
|
|
137
|
+
next = __next__
|
|
138
|
+
|
|
139
|
+
def __len__(self):
|
|
140
|
+
return lgeos.GEOSGetNumInteriorRings(self._geom)
|
|
141
|
+
|
|
142
|
+
def __getitem__(self, key):
|
|
143
|
+
m = self.__len__()
|
|
144
|
+
if isinstance(key, int):
|
|
145
|
+
if key + m < 0 or key >= m:
|
|
146
|
+
raise IndexError("index out of range")
|
|
147
|
+
if key < 0:
|
|
148
|
+
i = m + key
|
|
149
|
+
else:
|
|
150
|
+
i = key
|
|
151
|
+
return self._get_ring(i)
|
|
152
|
+
elif isinstance(key, slice):
|
|
153
|
+
res = []
|
|
154
|
+
start, stop, stride = key.indices(m)
|
|
155
|
+
for i in range(start, stop, stride):
|
|
156
|
+
res.append(self._get_ring(i))
|
|
157
|
+
return res
|
|
158
|
+
else:
|
|
159
|
+
raise TypeError("key must be an index or slice")
|
|
160
|
+
|
|
161
|
+
@property
|
|
162
|
+
def _longest(self):
|
|
163
|
+
max = 0
|
|
164
|
+
for g in iter(self):
|
|
165
|
+
l = len(g.coords)
|
|
166
|
+
if l > max:
|
|
167
|
+
max = l
|
|
168
|
+
|
|
169
|
+
def gtag(self):
|
|
170
|
+
return hash(repr(self.__p__))
|
|
171
|
+
|
|
172
|
+
def _get_ring(self, i):
|
|
173
|
+
gtag = self.gtag()
|
|
174
|
+
if gtag != self._gtag:
|
|
175
|
+
self.__rings__ = {}
|
|
176
|
+
if i not in self.__rings__:
|
|
177
|
+
g = lgeos.GEOSGetInteriorRingN(self._geom, i)
|
|
178
|
+
ring = LinearRing()
|
|
179
|
+
ring.__geom__ = g
|
|
180
|
+
ring.__p__ = self
|
|
181
|
+
ring._other_owned = True
|
|
182
|
+
ring._ndim = self._ndim
|
|
183
|
+
self.__rings__[i] = weakref.ref(ring)
|
|
184
|
+
return self.__rings__[i]()
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
class Polygon(BaseGeometry):
|
|
188
|
+
"""
|
|
189
|
+
A two-dimensional figure bounded by a linear ring
|
|
190
|
+
|
|
191
|
+
A polygon has a non-zero area. It may have one or more negative-space
|
|
192
|
+
"holes" which are also bounded by linear rings. If any rings cross each
|
|
193
|
+
other, the feature is invalid and operations on it may fail.
|
|
194
|
+
|
|
195
|
+
Attributes
|
|
196
|
+
----------
|
|
197
|
+
exterior : LinearRing
|
|
198
|
+
The ring which bounds the positive space of the polygon.
|
|
199
|
+
interiors : sequence
|
|
200
|
+
A sequence of rings which bound all existing holes.
|
|
201
|
+
"""
|
|
202
|
+
|
|
203
|
+
_exterior = None
|
|
204
|
+
_interiors = []
|
|
205
|
+
_ndim = 2
|
|
206
|
+
|
|
207
|
+
def __init__(self, shell=None, holes=None):
|
|
208
|
+
"""
|
|
209
|
+
Parameters
|
|
210
|
+
----------
|
|
211
|
+
shell : sequence
|
|
212
|
+
A sequence of (x, y [,z]) numeric coordinate pairs or triples
|
|
213
|
+
holes : sequence
|
|
214
|
+
A sequence of objects which satisfy the same requirements as the
|
|
215
|
+
shell parameters above
|
|
216
|
+
|
|
217
|
+
Example
|
|
218
|
+
-------
|
|
219
|
+
Create a square polygon with no holes
|
|
220
|
+
|
|
221
|
+
>>> coords = ((0., 0.), (0., 1.), (1., 1.), (1., 0.), (0., 0.))
|
|
222
|
+
>>> polygon = Polygon(coords)
|
|
223
|
+
>>> polygon.area
|
|
224
|
+
1.0
|
|
225
|
+
"""
|
|
226
|
+
BaseGeometry.__init__(self)
|
|
227
|
+
|
|
228
|
+
if shell is not None:
|
|
229
|
+
self._geom, self._ndim = geos_polygon_from_py(shell, holes)
|
|
230
|
+
|
|
231
|
+
@property
|
|
232
|
+
def exterior(self):
|
|
233
|
+
if self.is_empty:
|
|
234
|
+
return None
|
|
235
|
+
elif self._exterior is None or self._exterior() is None:
|
|
236
|
+
g = lgeos.GEOSGetExteriorRing(self._geom)
|
|
237
|
+
ring = LinearRing()
|
|
238
|
+
ring.__geom__ = g
|
|
239
|
+
ring.__p__ = self
|
|
240
|
+
ring._other_owned = True
|
|
241
|
+
ring._ndim = self._ndim
|
|
242
|
+
self._exterior = weakref.ref(ring)
|
|
243
|
+
return self._exterior()
|
|
244
|
+
|
|
245
|
+
@property
|
|
246
|
+
def interiors(self):
|
|
247
|
+
if self.is_empty:
|
|
248
|
+
return []
|
|
249
|
+
return InteriorRingSequence(self)
|
|
250
|
+
|
|
251
|
+
@property
|
|
252
|
+
def ctypes(self):
|
|
253
|
+
if not self._ctypes_data:
|
|
254
|
+
self._ctypes_data = self.exterior.ctypes
|
|
255
|
+
return self._ctypes_data
|
|
256
|
+
|
|
257
|
+
@property
|
|
258
|
+
def __array_interface__(self):
|
|
259
|
+
raise NotImplementedError(
|
|
260
|
+
"A polygon does not itself provide the array interface. Its rings do.")
|
|
261
|
+
|
|
262
|
+
def _get_coords(self):
|
|
263
|
+
raise NotImplementedError(
|
|
264
|
+
"Component rings have coordinate sequences, but the polygon does not")
|
|
265
|
+
|
|
266
|
+
def _set_coords(self, ob):
|
|
267
|
+
raise NotImplementedError(
|
|
268
|
+
"Component rings have coordinate sequences, but the polygon does not")
|
|
269
|
+
|
|
270
|
+
@property
|
|
271
|
+
def coords(self):
|
|
272
|
+
raise NotImplementedError(
|
|
273
|
+
"Component rings have coordinate sequences, but the polygon does not")
|
|
274
|
+
|
|
275
|
+
@property
|
|
276
|
+
def __geo_interface__(self):
|
|
277
|
+
coords = [tuple(self.exterior.coords)]
|
|
278
|
+
for hole in self.interiors:
|
|
279
|
+
coords.append(tuple(hole.coords))
|
|
280
|
+
return {
|
|
281
|
+
'type': 'Polygon',
|
|
282
|
+
'coordinates': tuple(coords)
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
def svg(self, scale_factor=1.):
|
|
286
|
+
"""
|
|
287
|
+
SVG representation of the geometry. Scale factor is multiplied by
|
|
288
|
+
the size of the SVG symbol so it can be scaled consistently for a
|
|
289
|
+
consistent appearance based on the canvas size.
|
|
290
|
+
"""
|
|
291
|
+
exterior_coords = [["{0},{1}".format(*c) for c in self.exterior.coords]]
|
|
292
|
+
interior_coords = [
|
|
293
|
+
["{0},{1}".format(*c) for c in interior.coords]
|
|
294
|
+
for interior in self.interiors ]
|
|
295
|
+
path = " ".join([
|
|
296
|
+
"M {0} L {1} z".format(coords[0], " L ".join(coords[1:]))
|
|
297
|
+
for coords in exterior_coords + interior_coords ])
|
|
298
|
+
return """
|
|
299
|
+
<g fill-rule="evenodd" fill="{2}" stroke="#555555"
|
|
300
|
+
stroke-width="{0}" opacity="0.6">
|
|
301
|
+
<path d="{1}" />
|
|
302
|
+
</g>""".format(
|
|
303
|
+
2.*scale_factor, path, "#66cc99" if self.is_valid else "#ff3333")
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
class PolygonAdapter(PolygonProxy, Polygon):
|
|
307
|
+
|
|
308
|
+
def __init__(self, shell, holes=None):
|
|
309
|
+
self.shell = shell
|
|
310
|
+
self.holes = holes
|
|
311
|
+
self.context = (shell, holes)
|
|
312
|
+
self.factory = geos_polygon_from_py
|
|
313
|
+
|
|
314
|
+
@property
|
|
315
|
+
def _ndim(self):
|
|
316
|
+
try:
|
|
317
|
+
# From array protocol
|
|
318
|
+
array = self.shell.__array_interface__
|
|
319
|
+
n = array['shape'][1]
|
|
320
|
+
assert n == 2 or n == 3
|
|
321
|
+
return n
|
|
322
|
+
except AttributeError:
|
|
323
|
+
# Fall back on list
|
|
324
|
+
return len(self.shell[0])
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
def asPolygon(shell, holes=None):
|
|
328
|
+
"""Adapt objects to the Polygon interface"""
|
|
329
|
+
return PolygonAdapter(shell, holes)
|
|
330
|
+
|
|
331
|
+
def orient(polygon, sign=1.0):
|
|
332
|
+
s = float(sign)
|
|
333
|
+
rings = []
|
|
334
|
+
ring = polygon.exterior
|
|
335
|
+
if signed_area(ring)/s >= 0.0:
|
|
336
|
+
rings.append(ring)
|
|
337
|
+
else:
|
|
338
|
+
rings.append(list(ring.coords)[::-1])
|
|
339
|
+
for ring in polygon.interiors:
|
|
340
|
+
if signed_area(ring)/s <= 0.0:
|
|
341
|
+
rings.append(ring)
|
|
342
|
+
else:
|
|
343
|
+
rings.append(list(ring.coords)[::-1])
|
|
344
|
+
return Polygon(rings[0], rings[1:])
|
|
345
|
+
|
|
346
|
+
def geos_linearring_from_py(ob, update_geom=None, update_ndim=0):
|
|
347
|
+
# If numpy is present, we use numpy.require to ensure that we have a
|
|
348
|
+
# C-continguous array that owns its data. View data will be copied.
|
|
349
|
+
ob = required(ob)
|
|
350
|
+
try:
|
|
351
|
+
# From array protocol
|
|
352
|
+
array = ob.__array_interface__
|
|
353
|
+
assert len(array['shape']) == 2
|
|
354
|
+
m = array['shape'][0]
|
|
355
|
+
n = array['shape'][1]
|
|
356
|
+
if m < 3:
|
|
357
|
+
raise ValueError(
|
|
358
|
+
"A LinearRing must have at least 3 coordinate tuples")
|
|
359
|
+
assert n == 2 or n == 3
|
|
360
|
+
|
|
361
|
+
# Make pointer to the coordinate array
|
|
362
|
+
if isinstance(array['data'], tuple):
|
|
363
|
+
# numpy tuple (addr, read-only)
|
|
364
|
+
cp = cast(array['data'][0], POINTER(c_double))
|
|
365
|
+
else:
|
|
366
|
+
cp = array['data']
|
|
367
|
+
|
|
368
|
+
# Add closing coordinates to sequence?
|
|
369
|
+
if cp[0] != cp[m*n-n] or cp[1] != cp[m*n-n+1]:
|
|
370
|
+
M = m + 1
|
|
371
|
+
else:
|
|
372
|
+
M = m
|
|
373
|
+
|
|
374
|
+
# Create a coordinate sequence
|
|
375
|
+
if update_geom is not None:
|
|
376
|
+
cs = lgeos.GEOSGeom_getCoordSeq(update_geom)
|
|
377
|
+
if n != update_ndim:
|
|
378
|
+
raise ValueError(
|
|
379
|
+
"Wrong coordinate dimensions; this geometry has dimensions: %d" \
|
|
380
|
+
% update_ndim)
|
|
381
|
+
else:
|
|
382
|
+
cs = lgeos.GEOSCoordSeq_create(M, n)
|
|
383
|
+
|
|
384
|
+
# add to coordinate sequence
|
|
385
|
+
for i in range(m):
|
|
386
|
+
# Because of a bug in the GEOS C API,
|
|
387
|
+
# always set X before Y
|
|
388
|
+
lgeos.GEOSCoordSeq_setX(cs, i, cp[n*i])
|
|
389
|
+
lgeos.GEOSCoordSeq_setY(cs, i, cp[n*i+1])
|
|
390
|
+
if n == 3:
|
|
391
|
+
lgeos.GEOSCoordSeq_setZ(cs, i, cp[n*i+2])
|
|
392
|
+
|
|
393
|
+
# Add closing coordinates to sequence?
|
|
394
|
+
if M > m:
|
|
395
|
+
# Because of a bug in the GEOS C API,
|
|
396
|
+
# always set X before Y
|
|
397
|
+
lgeos.GEOSCoordSeq_setX(cs, M-1, cp[0])
|
|
398
|
+
lgeos.GEOSCoordSeq_setY(cs, M-1, cp[1])
|
|
399
|
+
if n == 3:
|
|
400
|
+
lgeos.GEOSCoordSeq_setZ(cs, M-1, cp[2])
|
|
401
|
+
|
|
402
|
+
except AttributeError:
|
|
403
|
+
# Fall back on list
|
|
404
|
+
try:
|
|
405
|
+
m = len(ob)
|
|
406
|
+
except TypeError: # Iterators, e.g. Python 3 zip
|
|
407
|
+
ob = list(ob)
|
|
408
|
+
m = len(ob)
|
|
409
|
+
|
|
410
|
+
n = len(ob[0])
|
|
411
|
+
if m < 3:
|
|
412
|
+
raise ValueError(
|
|
413
|
+
"A LinearRing must have at least 3 coordinate tuples")
|
|
414
|
+
assert (n == 2 or n == 3)
|
|
415
|
+
|
|
416
|
+
# Add closing coordinates if not provided
|
|
417
|
+
if m == 3 or ob[0][0] != ob[-1][0] or ob[0][1] != ob[-1][1]:
|
|
418
|
+
M = m + 1
|
|
419
|
+
else:
|
|
420
|
+
M = m
|
|
421
|
+
|
|
422
|
+
# Create a coordinate sequence
|
|
423
|
+
if update_geom is not None:
|
|
424
|
+
cs = lgeos.GEOSGeom_getCoordSeq(update_geom)
|
|
425
|
+
if n != update_ndim:
|
|
426
|
+
raise ValueError(
|
|
427
|
+
"Wrong coordinate dimensions; this geometry has dimensions: %d" \
|
|
428
|
+
% update_ndim)
|
|
429
|
+
else:
|
|
430
|
+
cs = lgeos.GEOSCoordSeq_create(M, n)
|
|
431
|
+
|
|
432
|
+
# add to coordinate sequence
|
|
433
|
+
for i in range(m):
|
|
434
|
+
coords = ob[i]
|
|
435
|
+
# Because of a bug in the GEOS C API,
|
|
436
|
+
# always set X before Y
|
|
437
|
+
lgeos.GEOSCoordSeq_setX(cs, i, coords[0])
|
|
438
|
+
lgeos.GEOSCoordSeq_setY(cs, i, coords[1])
|
|
439
|
+
if n == 3:
|
|
440
|
+
try:
|
|
441
|
+
lgeos.GEOSCoordSeq_setZ(cs, i, coords[2])
|
|
442
|
+
except IndexError:
|
|
443
|
+
raise ValueError("Inconsistent coordinate dimensionality")
|
|
444
|
+
|
|
445
|
+
# Add closing coordinates to sequence?
|
|
446
|
+
if M > m:
|
|
447
|
+
coords = ob[0]
|
|
448
|
+
# Because of a bug in the GEOS C API,
|
|
449
|
+
# always set X before Y
|
|
450
|
+
lgeos.GEOSCoordSeq_setX(cs, M-1, coords[0])
|
|
451
|
+
lgeos.GEOSCoordSeq_setY(cs, M-1, coords[1])
|
|
452
|
+
if n == 3:
|
|
453
|
+
lgeos.GEOSCoordSeq_setZ(cs, M-1, coords[2])
|
|
454
|
+
|
|
455
|
+
if update_geom is not None:
|
|
456
|
+
return None
|
|
457
|
+
else:
|
|
458
|
+
return lgeos.GEOSGeom_createLinearRing(cs), n
|
|
459
|
+
|
|
460
|
+
def update_linearring_from_py(geom, ob):
|
|
461
|
+
geos_linearring_from_py(ob, geom._geom, geom._ndim)
|
|
462
|
+
|
|
463
|
+
def geos_polygon_from_py(shell, holes=None):
|
|
464
|
+
if shell is not None:
|
|
465
|
+
geos_shell, ndim = geos_linearring_from_py(shell)
|
|
466
|
+
if holes is not None and len(holes) > 0:
|
|
467
|
+
ob = holes
|
|
468
|
+
L = len(ob)
|
|
469
|
+
exemplar = ob[0]
|
|
470
|
+
try:
|
|
471
|
+
N = len(exemplar[0])
|
|
472
|
+
except TypeError:
|
|
473
|
+
N = exemplar._ndim
|
|
474
|
+
if not L >= 1:
|
|
475
|
+
raise ValueError("number of holes must be non zero")
|
|
476
|
+
if not N in (2, 3):
|
|
477
|
+
raise ValueError("insufficiant coordinate dimension")
|
|
478
|
+
|
|
479
|
+
# Array of pointers to ring geometries
|
|
480
|
+
geos_holes = (c_void_p * L)()
|
|
481
|
+
|
|
482
|
+
# add to coordinate sequence
|
|
483
|
+
for l in range(L):
|
|
484
|
+
geom, ndim = geos_linearring_from_py(ob[l])
|
|
485
|
+
geos_holes[l] = cast(geom, c_void_p)
|
|
486
|
+
else:
|
|
487
|
+
geos_holes = POINTER(c_void_p)()
|
|
488
|
+
L = 0
|
|
489
|
+
return (
|
|
490
|
+
lgeos.GEOSGeom_createPolygon(
|
|
491
|
+
c_void_p(geos_shell),
|
|
492
|
+
geos_holes,
|
|
493
|
+
L
|
|
494
|
+
),
|
|
495
|
+
ndim
|
|
496
|
+
)
|
|
497
|
+
|
|
498
|
+
# Test runner
|
|
499
|
+
def _test():
|
|
500
|
+
import doctest
|
|
501
|
+
doctest.testmod()
|
|
502
|
+
|
|
503
|
+
if __name__ == "__main__":
|
|
504
|
+
_test()
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""Proxy for coordinates stored outside Shapely geometries
|
|
2
|
+
"""
|
|
3
|
+
|
|
4
|
+
from shapely.geometry.base import deserialize_wkb, EMPTY
|
|
5
|
+
from shapely.geos import lgeos
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class CachingGeometryProxy(object):
|
|
9
|
+
|
|
10
|
+
context = None
|
|
11
|
+
factory = None
|
|
12
|
+
__geom__ = EMPTY
|
|
13
|
+
_gtag = None
|
|
14
|
+
|
|
15
|
+
def __init__(self, context):
|
|
16
|
+
self.context = context
|
|
17
|
+
|
|
18
|
+
@property
|
|
19
|
+
def _is_empty(self):
|
|
20
|
+
return self.__geom__ in [EMPTY, None]
|
|
21
|
+
|
|
22
|
+
def empty(self, val=EMPTY):
|
|
23
|
+
if not self._is_empty and self.__geom__:
|
|
24
|
+
lgeos.GEOSGeom_destroy(self.__geom__)
|
|
25
|
+
self.__geom__ = val
|
|
26
|
+
|
|
27
|
+
@property
|
|
28
|
+
def _geom(self):
|
|
29
|
+
"""Keeps the GEOS geometry in synch with the context."""
|
|
30
|
+
gtag = self.gtag()
|
|
31
|
+
if gtag != self._gtag or self._is_empty:
|
|
32
|
+
self.empty()
|
|
33
|
+
self.__geom__, n = self.factory(self.context)
|
|
34
|
+
self._gtag = gtag
|
|
35
|
+
return self.__geom__
|
|
36
|
+
|
|
37
|
+
def gtag(self):
|
|
38
|
+
return hash(repr(self.context))
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class PolygonProxy(CachingGeometryProxy):
|
|
42
|
+
|
|
43
|
+
@property
|
|
44
|
+
def _geom(self):
|
|
45
|
+
"""Keeps the GEOS geometry in synch with the context."""
|
|
46
|
+
gtag = self.gtag()
|
|
47
|
+
if gtag != self._gtag or self._is_empty:
|
|
48
|
+
self.empty()
|
|
49
|
+
self.__geom__, n = self.factory(self.context[0], self.context[1])
|
|
50
|
+
self._gtag = gtag
|
|
51
|
+
return self.__geom__
|