shapely 1.0b1__py2.4.egg → 1.0b3__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.
@@ -0,0 +1,155 @@
1
+ """
2
+ Multi-part collection of linestrings.
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.linestring import LineString, geos_linestring_from_py
10
+
11
+
12
+ def geos_multilinestring_from_py(ob):
13
+ """ob must be either a sequence or array of sequences or arrays."""
14
+ try:
15
+ # From array protocol
16
+ array = ob.__array_interface__
17
+ assert len(array['shape']) == 1
18
+ L = array['shape'][0]
19
+ assert L >= 1
20
+
21
+ # Make pointer to the coordinate array
22
+ cp = cast(array['data'][0], POINTER(c_double))
23
+
24
+ # Array of pointers to sub-geometries
25
+ subs = (c_void_p * L)()
26
+
27
+ for l in xrange(L):
28
+ geom, ndims = geos_linestring_from_py(array['data'][l])
29
+ subs[i] = cast(geom, c_void_p)
30
+ N = lgeos.GEOSGeom_getDimensions(subs[0])
31
+
32
+ except AttributeError:
33
+ # Fall back on list
34
+ L = len(ob)
35
+ N = len(ob[0][0])
36
+ assert L >= 1
37
+ assert N == 2 or N == 3
38
+
39
+ # Array of pointers to point geometries
40
+ subs = (c_void_p * L)()
41
+
42
+ # add to coordinate sequence
43
+ for l in xrange(L):
44
+ geom, ndims = geos_linestring_from_py(ob[l])
45
+ subs[l] = cast(geom, c_void_p)
46
+
47
+ return (lgeos.GEOSGeom_createCollection(5, subs, L), N)
48
+
49
+
50
+ class MultiLineString(BaseGeometry):
51
+
52
+ """a multiple linestring geometry.
53
+ """
54
+
55
+ def __init__(self, coordinates=None):
56
+ """Initialize.
57
+
58
+ Parameters
59
+ ----------
60
+
61
+ coordinates : sequence
62
+ Contains coordinate sequences or objects that provide the numpy
63
+ array protocol, providing an M x 2 or M x 3 (with z) array.
64
+
65
+ Example
66
+ -------
67
+
68
+ >>> geom = MultiLineString( [[[0.0, 0.0], [1.0, 2.0]]] )
69
+ >>> geom = MultiLineString( [ array([[0.0, 0.0], [1.0, 2.0]]) ] )
70
+
71
+ Each result in a collection containing one line string.
72
+ """
73
+ BaseGeometry.__init__(self)
74
+
75
+ if coordinates is None:
76
+ # allow creation of null lines, to support unpickling
77
+ pass
78
+ else:
79
+ self._geom, self._ndim = geos_multilinestring_from_py(coordinates)
80
+
81
+ @property
82
+ def __geo_interface__(self):
83
+ return {
84
+ 'type': 'MultiLineString',
85
+ 'coordinates': tuple(tuple(c for c in g.coords) for g in self.geoms)
86
+ }
87
+
88
+ @property
89
+ def ctypes(self):
90
+ raise NotImplementedError, \
91
+ "Multi-part geometries have no ctypes representations"
92
+
93
+ @property
94
+ def __array_interface__(self):
95
+ """Provide the Numpy array protocol."""
96
+ raise NotImplementedError, \
97
+ "Multi-part geometries do not themselves provide the array interface"
98
+
99
+ @property
100
+ def coords(self):
101
+ raise NotImplementedError, \
102
+ "Multi-part geometries do not provide a coordinate sequence"
103
+
104
+ @property
105
+ def geoms(self):
106
+ return GeometrySequence(self, LineString)
107
+
108
+
109
+ class MultiLineStringAdapter(MultiLineString):
110
+
111
+ """Adapts sequences of sequences or numpy arrays to the multilinestring
112
+ interface.
113
+ """
114
+
115
+ context = None
116
+
117
+ def __init__(self, context):
118
+ self.context = context
119
+
120
+ # Override base class __del__
121
+ def __del__(self):
122
+ pass
123
+
124
+ @property
125
+ def _ndim(self):
126
+ try:
127
+ # From array protocol
128
+ array = self.context[0].__array_interface__
129
+ n = array['shape'][1]
130
+ assert n == 2 or n == 3
131
+ return n
132
+ except AttributeError:
133
+ # Fall back on list
134
+ return len(self.context[0][0])
135
+
136
+ @property
137
+ def _geom(self):
138
+ """Keeps the GEOS geometry in synch with the context."""
139
+ return geos_multilinestring_from_py(self.context)[0]
140
+
141
+
142
+ def asMultiLineString(context):
143
+ """Factory for MultiLineStringAdapter instances."""
144
+ return MultiLineStringAdapter(context)
145
+
146
+
147
+ # Test runner
148
+ def _test():
149
+ import doctest
150
+ doctest.testmod()
151
+
152
+
153
+ if __name__ == "__main__":
154
+ _test()
155
+
Binary file
@@ -0,0 +1,182 @@
1
+ """
2
+ Multiple points.
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.point import Point, geos_point_from_py
10
+
11
+
12
+ def geos_multipoint_from_py(ob):
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 >= 1
20
+ assert n == 2 or n == 3
21
+
22
+ # Make pointer to the coordinate array
23
+ cp = cast(array['data'][0], POINTER(c_double))
24
+
25
+ # Array of pointers to sub-geometries
26
+ subs = (c_void_p * m)()
27
+
28
+ for i in xrange(m):
29
+ geom, ndims = geos_point_from_py(cp[n*i:n*i+2])
30
+ subs[i] = cast(geom, c_void_p)
31
+
32
+ except AttributeError:
33
+ # Fall back on list
34
+ m = len(ob)
35
+ n = len(ob[0])
36
+ assert n == 2 or n == 3
37
+
38
+ # Array of pointers to point geometries
39
+ subs = (c_void_p * m)()
40
+
41
+ # add to coordinate sequence
42
+ for i in xrange(m):
43
+ coords = ob[i]
44
+ geom, ndims = geos_point_from_py(coords)
45
+ subs[i] = cast(geom, c_void_p)
46
+
47
+ return (lgeos.GEOSGeom_createCollection(4, subs, m), n)
48
+
49
+
50
+ class MultiPoint(BaseGeometry):
51
+
52
+ """A multiple point geometry.
53
+ """
54
+
55
+ def __init__(self, coordinates=None):
56
+ """Initialize.
57
+
58
+ Parameters
59
+ ----------
60
+
61
+ coordinates : sequence or array
62
+ This may be an object that satisfies the numpy array protocol,
63
+ providing an M x 2 or M x 3 (with z) array, or it may be a sequence
64
+ of x, y (,z) coordinate sequences.
65
+
66
+ Example
67
+ -------
68
+
69
+ >>> geom = MultiPoint([[0.0, 0.0], [1.0, 2.0]])
70
+ >>> geom = MultiPoint(array([[0.0, 0.0], [1.0, 2.0]]))
71
+
72
+ Each result in a line string from (0.0, 0.0) to (1.0, 2.0).
73
+ """
74
+ BaseGeometry.__init__(self)
75
+
76
+ if coordinates is None:
77
+ # allow creation of null lines, to support unpickling
78
+ pass
79
+ else:
80
+ self._geom, self._ndim = geos_multipoint_from_py(coordinates)
81
+
82
+
83
+ @property
84
+ def __geo_interface__(self):
85
+ return {
86
+ 'type': 'MultiPoint',
87
+ 'coordinates': tuple([g.coords[0] for g in self.geoms])
88
+ }
89
+
90
+ @property
91
+ def ctypes(self):
92
+ if not self._ctypes_data:
93
+ temp = c_double()
94
+ n = self._ndim
95
+ m = len(self.geoms)
96
+ array_type = c_double * (m * n)
97
+ data = array_type()
98
+ for i in xrange(m):
99
+ g = self.geoms[i]._geom
100
+ cs = lgeos.GEOSGeom_getCoordSeq(g)
101
+ lgeos.GEOSCoordSeq_getX(cs, 0, byref(temp))
102
+ data[n*i] = temp.value
103
+ lgeos.GEOSCoordSeq_getY(cs, 0, byref(temp))
104
+ data[n*i+1] = temp.value
105
+ if n == 3: # TODO: use hasz
106
+ lgeos.GEOSCoordSeq_getZ(cs, 0, byref(temp))
107
+ data[n*i+2] = temp.value
108
+ self._ctypes_data = data
109
+ return self._ctypes_data
110
+
111
+ def array_interface(self):
112
+ """Provide the Numpy array protocol."""
113
+ ai = self.array_interface_base
114
+ ai.update({'shape': (len(self.geoms), self._ndim)})
115
+ return ai
116
+ __array_interface__ = property(array_interface)
117
+
118
+ @property
119
+ def coords(self):
120
+ raise NotImplementedError, \
121
+ "Multipart geometries do not themselves provide coordinate sequences"
122
+
123
+ @property
124
+ def geoms(self):
125
+ return GeometrySequence(self, Point)
126
+
127
+
128
+ class MultiPointAdapter(MultiPoint):
129
+
130
+ """Adapts a Python coordinate pair or a numpy array to the multipoint
131
+ interface.
132
+ """
133
+
134
+ context = None
135
+
136
+ def __init__(self, context):
137
+ self.context = context
138
+
139
+ # Override base class __del__
140
+ def __del__(self):
141
+ pass
142
+
143
+ @property
144
+ def _ndim(self):
145
+ try:
146
+ # From array protocol
147
+ array = self.context.__array_interface__
148
+ n = array['shape'][1]
149
+ assert n == 2 or n == 3
150
+ return n
151
+ except AttributeError:
152
+ # Fall back on list
153
+ return len(self.context[0])
154
+
155
+ @property
156
+ def _geom(self):
157
+ """Keeps the GEOS geometry in synch with the context."""
158
+ return geos_multipoint_from_py(self.context)[0]
159
+
160
+ @property
161
+ def __array_interface__(self):
162
+ """Provide the Numpy array protocol."""
163
+ try:
164
+ return self.context.__array_interface__
165
+ except AttributeError:
166
+ return self.array_interface()
167
+
168
+
169
+ def asMultiPoint(context):
170
+ """Factory for MultiPointAdapter instances."""
171
+ return MultiPointAdapter(context)
172
+
173
+
174
+ # Test runner
175
+ def _test():
176
+ import doctest
177
+ doctest.testmod()
178
+
179
+
180
+ if __name__ == "__main__":
181
+ _test()
182
+
Binary file
@@ -0,0 +1,141 @@
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