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,141 +0,0 @@
1
- """
2
- Multi-part collection of polygons.
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, GeometrySequence
9
- from shapely.geometry.polygon import Polygon, geos_polygon_from_py
10
-
11
-
12
- def geos_multipolygon_from_py(ob):
13
- """ob must be either a sequence or array of sequences or arrays."""
14
- L = len(ob)
15
- N = len(ob[0][0][0])
16
- assert L >= 1
17
- assert N == 2 or N == 3
18
-
19
- subs = (c_void_p * L)()
20
- for l in xrange(L):
21
- geom, ndims = geos_polygon_from_py(ob[l][0], ob[l][1])
22
- subs[l] = cast(geom, c_void_p)
23
-
24
- return (lgeos.GEOSGeom_createCollection(6, subs, L), N)
25
-
26
-
27
- class MultiPolygon(BaseGeometry):
28
-
29
- """a multiple polygon geometry.
30
- """
31
-
32
- def __init__(self, polygons=None):
33
- """Initialize.
34
-
35
- Parameters
36
- ----------
37
-
38
- polygons : sequence
39
- A sequence of (shell, holes) tuples where shell is the sequence
40
- representation of a linear ring (see linearring.py) and holes is
41
- a sequence of such linear rings.
42
-
43
- Example
44
- -------
45
- >>> geom = 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
- """
52
- BaseGeometry.__init__(self)
53
-
54
- if polygons is None:
55
- # allow creation of null collections, to support unpickling
56
- pass
57
- else:
58
- self._geom, self._ndim = geos_multipolygon_from_py(polygons)
59
-
60
- @property
61
- def __geo_interface__(self):
62
- allcoords = []
63
- for geom in self.geoms:
64
- coords = []
65
- coords.append(tuple(geom.exterior.coords))
66
- for hole in geom.interiors:
67
- coords.append(tuple(hole.coords))
68
- allcoords.append(coords)
69
- return {
70
- 'type': 'MultiPolygon',
71
- 'coordinates': allcoords
72
- }
73
-
74
- @property
75
- def ctypes(self):
76
- raise NotImplementedError, \
77
- "Multi-part geometries have no ctypes representations"
78
-
79
- @property
80
- def __array_interface__(self):
81
- """Provide the Numpy array protocol."""
82
- raise NotImplementedError, \
83
- "Multi-part geometries do not themselves provide the array interface"
84
-
85
- @property
86
- def coords(self):
87
- raise NotImplementedError, \
88
- "Multi-part geometries do not provide a coordinate sequence"
89
-
90
- @property
91
- def geoms(self):
92
- return GeometrySequence(self, Polygon)
93
-
94
-
95
- class MultiPolygonAdapter(MultiPolygon):
96
-
97
- """Adapts sequences of sequences or numpy arrays to the multipolygon
98
- interface.
99
- """
100
-
101
- context = None
102
-
103
- def __init__(self, context):
104
- self.context = context
105
-
106
- # Override base class __del__
107
- def __del__(self):
108
- pass
109
-
110
- @property
111
- def _ndim(self):
112
- try:
113
- # From array protocol
114
- array = self.context[0][0].__array_interface__
115
- n = array['shape'][1]
116
- assert n == 2 or n == 3
117
- return n
118
- except AttributeError:
119
- # Fall back on list
120
- return len(self.context[0][0][0])
121
-
122
- @property
123
- def _geom(self):
124
- """Keeps the GEOS geometry in synch with the context."""
125
- return geos_multipolygon_from_py(self.context)[0]
126
-
127
-
128
- def asMultiPolygon(context):
129
- """Factory for MultiLineStringAdapter instances."""
130
- return MultiPolygonAdapter(context)
131
-
132
-
133
- # Test runner
134
- def _test():
135
- import doctest
136
- doctest.testmod()
137
-
138
-
139
- if __name__ == "__main__":
140
- _test()
141
-
Binary file
shapely/geometry/point.py DELETED
@@ -1,249 +0,0 @@
1
- """
2
- Points.
3
- """
4
-
5
- from ctypes import string_at, create_string_buffer, \
6
- c_char_p, c_double, c_float, c_int, c_uint, c_size_t, c_ubyte, \
7
- c_void_p, byref
8
- from ctypes import cast, POINTER
9
-
10
- from shapely.geos import lgeos, DimensionError
11
- from shapely.geometry.base import BaseGeometry, CoordinateSequence
12
-
13
-
14
- def geos_point_from_py(ob, update_geom=None, update_ndim=0):
15
- """Create a GEOS geom from an object that is a coordinate sequence
16
- or that provides the array interface.
17
-
18
- Returns the GEOS geometry and the number of its dimensions.
19
- """
20
- try:
21
- # From array protocol
22
- array = ob.__array_interface__
23
- assert len(array['shape']) == 1
24
- n = array['shape'][0]
25
- assert n == 2 or n == 3
26
-
27
- cdata = array['data'][0]
28
- cp = cast(cdata, POINTER(c_double))
29
- dx = c_double(cp[0])
30
- dy = c_double(cp[1])
31
- dz = None
32
- if n == 3:
33
- dz = c_double(cp[2])
34
- ndim = 3
35
- except AttributeError:
36
- # Fall back on list
37
- coords = ob
38
- n = len(coords)
39
- dx = c_double(coords[0])
40
- dy = c_double(coords[1])
41
- dz = None
42
- if n == 3:
43
- dz = c_double(coords[2])
44
-
45
- if update_geom:
46
- cs = lgeos.GEOSGeom_getCoordSeq(update_geom)
47
- if n != update_ndim:
48
- raise ValueError, \
49
- "Wrong coordinate dimensions; this geometry has dimensions: %d" \
50
- % update_ndim
51
- else:
52
- cs = lgeos.GEOSCoordSeq_create(1, n)
53
-
54
- # Because of a bug in the GEOS C API, always set X before Y
55
- lgeos.GEOSCoordSeq_setX(cs, 0, dx)
56
- lgeos.GEOSCoordSeq_setY(cs, 0, dy)
57
- if n == 3:
58
- lgeos.GEOSCoordSeq_setZ(cs, 0, dz)
59
-
60
- if update_geom:
61
- return None
62
- else:
63
- return (lgeos.GEOSGeom_createPoint(cs), n)
64
-
65
- def update_point_from_py(geom, ob):
66
- geos_point_from_py(ob, geom._geom, geom._ndim)
67
-
68
-
69
- class Point(BaseGeometry):
70
-
71
- """A point geometry.
72
-
73
- Attributes
74
- ----------
75
- x, y, z : float
76
- Coordinate values
77
-
78
- Example
79
- -------
80
- >>> p = Point(1.0, -1.0)
81
- >>> str(p)
82
- 'POINT (1.0000000000000000 -1.0000000000000000)'
83
- >>> p.y = 0.0
84
- >>> p.y
85
- 0.0
86
- >>> p.x
87
- 1.0
88
- >>> p.array
89
- [[1.0, 0.0]]
90
- """
91
-
92
- def __init__(self, *args):
93
- """This *copies* the given data to a new GEOS geometry.
94
-
95
- Parameters
96
- ----------
97
-
98
- There are 2 cases:
99
-
100
- 1) 1 parameter: this must satisfy the numpy array protocol.
101
- 2) 2 or more parameters: x, y, z : float
102
- Easting, northing, and elevation.
103
- """
104
- BaseGeometry.__init__(self)
105
-
106
- if len(args) == 0:
107
- # allow creation of null points, to support unpickling
108
- pass
109
- else:
110
- if len(args) == 1:
111
- self._geom, self._ndim = geos_point_from_py(args[0])
112
- else:
113
- self._geom, self._ndim = geos_point_from_py(tuple(args))
114
-
115
- # Coordinate getters and setters
116
-
117
- @property
118
- def x(self):
119
- """Return x coordinate."""
120
- cs = lgeos.GEOSGeom_getCoordSeq(self._geom)
121
- d = c_double()
122
- lgeos.GEOSCoordSeq_getX(cs, 0, byref(d))
123
- return d.value
124
-
125
- @property
126
- def y(self):
127
- """Return y coordinate."""
128
- cs = lgeos.GEOSGeom_getCoordSeq(self._geom)
129
- d = c_double()
130
- lgeos.GEOSCoordSeq_getY(cs, 0, byref(d))
131
- return d.value
132
-
133
- @property
134
- def z(self):
135
- """Return z coordinate."""
136
- if self._ndim != 3:
137
- raise DimensionError, "This point has no z coordinate."
138
- cs = lgeos.GEOSGeom_getCoordSeq(self._geom)
139
- d = c_double()
140
- lgeos.GEOSCoordSeq_getZ(cs, 0, byref(d))
141
- return d.value
142
-
143
- @property
144
- def __geo_interface__(self):
145
- return {
146
- 'type': 'Point',
147
- 'coordinates': self.coords[0]
148
- }
149
-
150
- @property
151
- def ctypes(self):
152
- if not self._ctypes_data:
153
- array_type = c_double * self._ndim
154
- array = array_type()
155
- array[0] = self.x
156
- array[1] = self.y
157
- if self._ndim == 3:
158
- array[2] = self.z
159
- self._ctypes_data = array
160
- return self._ctypes_data
161
-
162
- @property
163
- def __array_interface__(self):
164
- """Provide the Numpy array protocol."""
165
- return {
166
- 'version': 3,
167
- 'shape': (self._ndim,),
168
- 'typestr': '<f8',
169
- 'data': self.ctypes,
170
- }
171
-
172
- @property
173
- def bounds(self):
174
- cs = lgeos.GEOSGeom_getCoordSeq(self._geom)
175
- x = c_double()
176
- y = c_double()
177
- lgeos.GEOSCoordSeq_getX(cs, 0, byref(x))
178
- lgeos.GEOSCoordSeq_getY(cs, 0, byref(y))
179
- return (x.value, y.value, x.value, y.value)
180
-
181
- # Coordinate access
182
-
183
- def set_coords(self, coordinates):
184
- update_point_from_py(self, coordinates)
185
-
186
- coords = property(BaseGeometry.get_coords, set_coords)
187
-
188
-
189
- class PointAdapter(Point):
190
-
191
- """Adapts a Python coordinate pair or a numpy array to the point interface.
192
- """
193
-
194
- context = None
195
-
196
- def __init__(self, context):
197
- self.context = context
198
-
199
- # Override base class __del__
200
- def __del__(self):
201
- pass
202
-
203
- @property
204
- def _ndim(self):
205
- try:
206
- # From array protocol
207
- array = self.context.__array_interface__
208
- n = array['shape'][0]
209
- assert n == 2 or n == 3
210
- return n
211
- except AttributeError:
212
- # Fall back on list
213
- return len(self.context)
214
-
215
- @property
216
- def _geom(self):
217
- """Keeps the GEOS geometry in synch with the context."""
218
- return geos_point_from_py(self.context)[0]
219
-
220
- # TODO: reimplement x, y, z properties without calling invoking _geom
221
-
222
- @property
223
- def __array_interface__(self):
224
- """Provide the Numpy array protocol."""
225
- try:
226
- return self.context.__array_interface__
227
- except AttributeError:
228
- return {
229
- 'version': 3,
230
- 'shape': (self._ndim,),
231
- 'typestr': '<f8',
232
- 'data': self.ctypes,
233
- }
234
-
235
- coords = property(BaseGeometry.get_coords)
236
-
237
- def asPoint(context):
238
- """Factory for PointAdapter instances."""
239
- return PointAdapter(context)
240
-
241
-
242
- # Test runner
243
- def _test():
244
- import doctest
245
- doctest.testmod()
246
-
247
- if __name__ == "__main__":
248
- _test()
249
-
Binary file