shapely 1.0a7__py2.4.egg → 1.0b1__py2.4.egg

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.
@@ -1,414 +0,0 @@
1
- """
2
- Polygons and their linear ring components.
3
- """
4
-
5
- from ctypes import byref, c_double, c_int, c_void_p, cast, POINTER, pointer
6
-
7
- from shapely.geos import lgeos
8
- from shapely.geometry.base import BaseGeometry
9
- from shapely.geometry.linestring import LineString, LineStringAdapter
10
-
11
-
12
- def geos_linearring_from_py(ob, update_geom=None, update_ndim=0):
13
- try:
14
- # From array protocol
15
- array = ob.__array_interface__
16
- assert len(array['shape']) == 2
17
- m = array['shape'][0]
18
- n = array['shape'][1]
19
- assert m >= 2
20
- assert n == 2 or n == 3
21
-
22
- # Make pointer to the coordinate array
23
- try:
24
- cp = cast(array['data'][0], POINTER(c_double))
25
- except ArgumentError:
26
- cp = array['data']
27
-
28
- # Add closing coordinates to sequence?
29
- if cp[0] != cp[m*n-n] or cp[1] != cp[m*n-n+1]:
30
- M = m + 1
31
- else:
32
- M = m
33
-
34
- # Create a coordinate sequence
35
- if update_geom is not None:
36
- cs = lgeos.GEOSGeom_getCoordSeq(update_geom)
37
- if n != update_ndim:
38
- raise ValueError, \
39
- "Wrong coordinate dimensions; this geometry has dimensions: %d" \
40
- % update_ndim
41
- else:
42
- cs = lgeos.GEOSCoordSeq_create(M, n)
43
-
44
- # add to coordinate sequence
45
- for i in xrange(m):
46
- dx = c_double(cp[n*i])
47
- dy = c_double(cp[n*i+1])
48
- dz = None
49
- if n == 3:
50
- dz = c_double(cp[n*i+2])
51
-
52
- # Because of a bug in the GEOS C API,
53
- # always set X before Y
54
- lgeos.GEOSCoordSeq_setX(cs, i, dx)
55
- lgeos.GEOSCoordSeq_setY(cs, i, dy)
56
- if n == 3:
57
- lgeos.GEOSCoordSeq_setZ(cs, i, dz)
58
-
59
- # Add closing coordinates to sequence?
60
- if M > m:
61
- dx = c_double(cp[0])
62
- dy = c_double(cp[1])
63
- dz = None
64
- if n == 3:
65
- dz = c_double(cp[2])
66
-
67
- # Because of a bug in the GEOS C API,
68
- # always set X before Y
69
- lgeos.GEOSCoordSeq_setX(cs, M-1, dx)
70
- lgeos.GEOSCoordSeq_setY(cs, M-1, dy)
71
- if n == 3:
72
- lgeos.GEOSCoordSeq_setZ(cs, M-1, dz)
73
-
74
- except AttributeError:
75
- # Fall back on list
76
- m = len(ob)
77
- n = len(ob[0])
78
- assert m >= 2
79
- assert n == 2 or n == 3
80
-
81
- # Add closing coordinates if not provided
82
- if ob[0][0] != ob[-1][0] or ob[0][1] != ob[-1][1]:
83
- M = m + 1
84
- else:
85
- M = m
86
-
87
- # Create a coordinate sequence
88
- if update_geom is not None:
89
- cs = lgeos.GEOSGeom_getCoordSeq(update_geom)
90
- if n != update_ndim:
91
- raise ValueError, \
92
- "Wrong coordinate dimensions; this geometry has dimensions: %d" \
93
- % update_ndim
94
- else:
95
- cs = lgeos.GEOSCoordSeq_create(M, n)
96
-
97
- # add to coordinate sequence
98
- for i in xrange(m):
99
- coords = ob[i]
100
- dx = c_double(coords[0])
101
- dy = c_double(coords[1])
102
- dz = None
103
- if n == 3:
104
- dz = c_double(coords[2])
105
-
106
- # Because of a bug in the GEOS C API,
107
- # always set X before Y
108
- lgeos.GEOSCoordSeq_setX(cs, i, dx)
109
- lgeos.GEOSCoordSeq_setY(cs, i, dy)
110
- if n == 3:
111
- lgeos.GEOSCoordSeq_setZ(cs, i, dz)
112
-
113
- # Add closing coordinates to sequence?
114
- if M > m:
115
- coords = ob[0]
116
- dx = c_double(coords[0])
117
- dy = c_double(coords[1])
118
- dz = None
119
- if n == 3:
120
- dz = c_double(coords[2])
121
-
122
- # Because of a bug in the GEOS C API,
123
- # always set X before Y
124
- lgeos.GEOSCoordSeq_setX(cs, M-1, dx)
125
- lgeos.GEOSCoordSeq_setY(cs, M-1, dy)
126
- if n == 3:
127
- lgeos.GEOSCoordSeq_setZ(cs, M-1, dz)
128
-
129
- if update_geom is not None:
130
- return None
131
- else:
132
- return (lgeos.GEOSGeom_createLinearRing(cs), n)
133
-
134
- def update_linearring_from_py(geom, ob):
135
- geos_linearring_from_py(ob, geom._geom, geom._ndim)
136
-
137
-
138
- class LinearRing(LineString):
139
-
140
- """A linear ring.
141
- """
142
-
143
- _ndim = 2
144
-
145
- def __init__(self, coordinates=None):
146
- """Initialize.
147
-
148
- Parameters
149
- ----------
150
- coordinates : sequence or array
151
- This may be an object that satisfies the numpy array protocol,
152
- providing an M x 2 or M x 3 (with z) array, or it may be a sequence
153
- of x, y (,z) coordinate sequences.
154
-
155
- Rings are implicitly closed. There is no need to specific a final
156
- coordinate pair identical to the first.
157
-
158
- Example
159
- -------
160
- >>> ring = LinearRing( ((0.,0.), (0.,1.), (1.,1.), (1.,0.)) )
161
-
162
- Produces a 1x1 square.
163
- """
164
- BaseGeometry.__init__(self)
165
-
166
- if coordinates is None:
167
- # allow creation of null lines, to support unpickling
168
- pass
169
- else:
170
- self._geom, self._ndims = geos_linearring_from_py(coordinates)
171
-
172
- @property
173
- def __geo_interface__(self):
174
- return {
175
- 'type': 'LinearRing',
176
- 'coordinates': tuple(self.coords)
177
- }
178
-
179
- # Coordinate access
180
-
181
- def set_coords(self, coordinates):
182
- update_linearring_from_py(self, coordinates)
183
-
184
- coords = property(BaseGeometry.get_coords, set_coords)
185
-
186
-
187
- class LinearRingAdapter(LineStringAdapter):
188
-
189
- @property
190
- def _geom(self):
191
- """Keeps the GEOS geometry in synch with the context."""
192
- return geos_linearring_from_py(self.context)[0]
193
-
194
- @property
195
- def __geo_interface__(self):
196
- return {
197
- 'type': 'LinearRing',
198
- 'coordinates': tuple(self.coords)
199
- }
200
-
201
- coords = property(BaseGeometry.get_coords)
202
-
203
-
204
- def asLinearRing(context):
205
- return LinearRingAdapter(context)
206
-
207
-
208
- class InteriorRingSequence(object):
209
-
210
- _factory = None
211
- _geom = None
212
- _ndim = None
213
- _index = 0
214
- _length = 0
215
-
216
- def __init__(self, geom):
217
- self._geom = geom._geom
218
- self._ndim = geom._ndim
219
-
220
- def __iter__(self):
221
- self._index = 0
222
- self._length = self.__len__()
223
- return self
224
-
225
- def next(self):
226
- if self._index < self._length:
227
- g = LinearRing()
228
- g._owned = True
229
- g._geom = lgeos.GEOSGetInteriorRingN(self._geom, self._index)
230
- self._index += 1
231
- return g
232
- else:
233
- raise StopIteration
234
-
235
- def __len__(self):
236
- return lgeos.GEOSGetNumInteriorRings(self._geom)
237
-
238
- def __getitem__(self, i):
239
- M = self.__len__()
240
- if i + M < 0 or i >= M:
241
- raise IndexError, "index out of range"
242
- if i < 0:
243
- ii = M + i
244
- else:
245
- ii = i
246
- g = LinearRing()
247
- g._owned = True
248
- g._geom = lgeos.GEOSGetInteriorRingN(self._geom, ii)
249
- return g
250
-
251
- @property
252
- def _longest(self):
253
- max = 0
254
- for g in iter(self):
255
- l = len(g.coords)
256
- if l > max:
257
- max = l
258
-
259
-
260
- def geos_polygon_from_py(shell, holes=None):
261
- if shell is not None:
262
- geos_shell, ndims = geos_linearring_from_py(shell)
263
- ## Polygon geometry takes ownership of the ring
264
- #self._exterior._owned = True
265
-
266
- if holes:
267
- ob = holes
268
- L = len(ob)
269
- try:
270
- N = len(ob[0][0])
271
- except:
272
- import pdb; pdb.set_trace()
273
- assert L >= 1
274
- assert N == 2 or N == 3
275
-
276
- # Array of pointers to ring geometries
277
- geos_holes = (c_void_p * L)()
278
-
279
- # add to coordinate sequence
280
- for l in xrange(L):
281
- geom, ndims = geos_linearring_from_py(ob[l])
282
- geos_holes[l] = cast(geom, c_void_p)
283
-
284
- else:
285
- geos_holes = POINTER(c_void_p)()
286
- L = 0
287
-
288
- return (
289
- lgeos.GEOSGeom_createPolygon(
290
- c_void_p(geos_shell),
291
- geos_holes,
292
- L
293
- ),
294
- ndims
295
- )
296
-
297
- class Polygon(BaseGeometry):
298
-
299
- """A line string, also known as a polyline.
300
- """
301
-
302
- _exterior = None
303
- _interiors = []
304
- _ndim = 2
305
-
306
- def __init__(self, shell=None, holes=None):
307
- """Initialize.
308
-
309
- Parameters
310
- ----------
311
- exterior : sequence or array
312
- This may be an object that satisfies the numpy array protocol,
313
- providing an M x 2 or M x 3 (with z) array, or it may be a sequence
314
- of x, y (,z) coordinate sequences.
315
-
316
- Example
317
- -------
318
- >>> coords = ((0., 0.), (0., 1.), (1., 1.), (1., 0.), (0., 0.))
319
- >>> polygon = Polygon(coords)
320
- """
321
- BaseGeometry.__init__(self)
322
-
323
- if shell is not None:
324
- self._geom, self._ndims = geos_polygon_from_py(shell, holes)
325
-
326
- @property
327
- def exterior(self):
328
- if self._exterior is None:
329
- # A polygon created from the abstract factory will have a null
330
- # _exterior attribute.
331
- ring = lgeos.GEOSGetExteriorRing(self._geom)
332
- self._exterior = LinearRing()
333
- self._exterior._geom = ring
334
- self._exterior._owned = True
335
- return self._exterior
336
-
337
- @property
338
- def interiors(self):
339
- return InteriorRingSequence(self)
340
-
341
- @property
342
- def ctypes(self):
343
- if not self._ctypes_data:
344
- self._ctypes_data = self.exterior.ctypes
345
- return self._ctypes_data
346
-
347
- @property
348
- def __array_interface__(self):
349
- raise NotImplementedError, \
350
- "A polygon does not itself provide the array interface. Its rings do."
351
-
352
- @property
353
- def coords(self):
354
- raise NotImplementedError, \
355
- "Component rings have coordinate sequences, but the polygon does not"
356
-
357
- @property
358
- def __geo_interface__(self):
359
- coords = [tuple(self.exterior.coords)]
360
- for hole in self.interiors:
361
- coords.append(tuple(hole.coords))
362
- return {
363
- 'type': 'Polygon',
364
- 'coordinates': tuple(coords)
365
- }
366
-
367
-
368
- class PolygonAdapter(Polygon):
369
-
370
- """Adapts sequences of sequences or numpy arrays to the polygon
371
- interface.
372
- """
373
-
374
- context = None
375
-
376
- def __init__(self, shell, holes=None):
377
- self.shell = shell
378
- self.holes = holes
379
-
380
- # Override base class __del__
381
- def __del__(self):
382
- pass
383
-
384
- @property
385
- def _ndim(self):
386
- try:
387
- # From array protocol
388
- array = self.shell.__array_interface__
389
- n = array['shape'][1]
390
- assert n == 2 or n == 3
391
- return n
392
- except AttributeError:
393
- # Fall back on list
394
- return len(self.shell[0])
395
-
396
- @property
397
- def _geom(self):
398
- """Keeps the GEOS geometry in synch with the context."""
399
- return geos_polygon_from_py(self.shell, self.holes)[0]
400
-
401
-
402
- def asPolygon(shell, holes):
403
- """Factory for PolygonAdapter instances."""
404
- return PolygonAdapter(shell, holes)
405
-
406
-
407
- # Test runner
408
- def _test():
409
- import doctest
410
- doctest.testmod()
411
-
412
- if __name__ == "__main__":
413
- _test()
414
-
Binary file
shapely/json.py DELETED
@@ -1,47 +0,0 @@
1
- """
2
- Load/dump geometries using JSON.
3
-
4
- Includes a JSON-based abstract geometry factory.
5
- """
6
-
7
- import geojson
8
-
9
- def json_factory(ob):
10
- """Attempts to create a geometry from JSON.
11
-
12
- Else, returns the original data.
13
- """
14
- try:
15
- coords = ob.get('coordinates', [])
16
- geom_type = str(ob.get('type'))
17
- mod = __import__(
18
- 'shapely.geometry',
19
- globals(),
20
- locals(),
21
- [geom_type],
22
- )
23
- geom_class = getattr(mod, geom_type)
24
- return geom_class(*coords)
25
- except (KeyError, TypeError):
26
- raise
27
- return ob
28
-
29
- # Pickle-like convenience functions
30
-
31
- def loads(data):
32
- """Load a geometry from a GeoJSON representation."""
33
- return geojson.loads(data, json_factory)
34
-
35
- def load(fp):
36
- """Load a geometry from an open file."""
37
- data = fp.read()
38
- return loads(data)
39
-
40
- def dumps(ob):
41
- """Dump a GeoJSON representation of a geometry to a string."""
42
- return geojson.dumps(ob)
43
-
44
- def dump(ob, fp):
45
- """Dump a geometry to an open file."""
46
- fp.write(dumps(ob))
47
-
shapely/json.pyc DELETED
Binary file