Changeset 1046
- Timestamp:
- 02/08/08 07:51:56
- Files:
-
- Shapely/trunk/CHANGES.txt (modified) (1 diff)
- Shapely/trunk/shapely/geometry/base.py (modified) (1 diff)
- Shapely/trunk/shapely/geometry/linestring.py (modified) (1 diff)
- Shapely/trunk/shapely/geos.py (modified) (1 diff)
- Shapely/trunk/tests/LineString.txt (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
Shapely/trunk/CHANGES.txt
r956 r1046 1 All ticket numbers are rooted at http://trac.gispython.org/projects/PCL/ticket/ 2 3 1.0.1: 8 February 2008 4 ---------------------- 5 - Allow chaining expressions involving coordinate sequences and geometry parts 6 (#151). 7 - Protect against abnormal use of coordinate accessors (#152). 8 - Coordinate sequences now implement the numpy array protocol (#153). Shapely/trunk/shapely/geometry/base.py
r1044 r1046 113 113 else: 114 114 return (dx.value, dy.value) 115 116 @property 117 def ctypes(self): 118 self.update_cseq() 119 n = self._ndim 120 m = self.__len__() 121 array_type = c_double * (m * n) 122 data = array_type() 123 temp = c_double() 124 125 for i in xrange(m): 126 lgeos.GEOSCoordSeq_getX(self._cseq, i, byref(temp)) 127 data[n*i] = temp.value 128 lgeos.GEOSCoordSeq_getY(self._cseq, i, byref(temp)) 129 data[n*i+1] = temp.value 130 if n == 3: # TODO: use hasz 131 lgeos.GEOSCoordSeq_getZ(self._cseq, i, byref(temp)) 132 data[n*i+2] = temp.value 133 return data 134 135 def array_interface(self): 136 """Provide the Numpy array protocol.""" 137 if sys.byteorder == 'little': 138 typestr = '<f8' 139 elif sys.byteorder == 'big': 140 typestr = '>f8' 141 else: 142 raise ValueError, \ 143 "Unsupported byteorder: neither little nor big-endian" 144 ai = { 145 'version': 3, 146 'typestr': typestr, 147 'data': self.ctypes, 148 } 149 ai.update({'shape': (len(self), self._ndim)}) 150 return ai 151 __array_interface__ = property(array_interface) 115 152 116 153 Shapely/trunk/shapely/geometry/linestring.py
r1044 r1046 139 139 def ctypes(self): 140 140 if not self._ctypes_data: 141 cs = lgeos.GEOSGeom_getCoordSeq(self._geom) 142 cs_len = c_int(0) 143 lgeos.GEOSCoordSeq_getSize(cs, byref(cs_len)) 144 temp = c_double() 145 n = self._ndim 146 m = cs_len.value 147 array_type = c_double * (m * n) 148 data = array_type() 149 for i in xrange(m): 150 lgeos.GEOSCoordSeq_getX(cs, i, byref(temp)) 151 data[n*i] = temp.value 152 lgeos.GEOSCoordSeq_getY(cs, i, byref(temp)) 153 data[n*i+1] = temp.value 154 if n == 3: # TODO: use hasz 155 lgeos.GEOSCoordSeq_getZ(cs, i, byref(temp)) 156 data[n*i+2] = temp.value 157 self._ctypes_data = data 141 self._ctypes_data = self.coords.ctypes 158 142 return self._ctypes_data 159 143 160 144 def array_interface(self): 161 145 """Provide the Numpy array protocol.""" 162 ai = self.array_interface_base 163 ai.update({'shape': (len(self.coords), self._ndim)}) 164 return ai 146 return self.coords.array_interface() 147 165 148 __array_interface__ = property(array_interface) 166 149 Shapely/trunk/shapely/geos.py
r1017 r1046 32 32 # Try the major versioned name first, falling back on the unversioned name. 33 33 try: 34 lgeos = CDLL('libgeos_c.so. 1')35 except ImportError:34 lgeos = CDLL('libgeos_c.so.2') 35 except (OSError, ImportError): 36 36 lgeos = CDLL('libgeos_c.so') 37 37 except: Shapely/trunk/tests/LineString.txt
r1006 r1046 5 5 -------------- 6 6 7 >>> from shapely.geometry import LineString7 >>> from shapely.geometry import LineString 8 8 9 9 Construct from a numpy array 10 10 11 >>> from numpy import array12 >>> line = LineString(array([[0.0, 0.0], [1.0, 2.0]]))13 >>> len(line.coords)14 215 >>> line.wkt16 'LINESTRING (0.0000000000000000 0.0000000000000000, 1.0000000000000000 2.0000000000000000)'11 >>> from numpy import array 12 >>> line = LineString(array([[0.0, 0.0], [1.0, 2.0]])) 13 >>> len(line.coords) 14 2 15 >>> line.wkt 16 'LINESTRING (0.0000000000000000 0.0000000000000000, 1.0000000000000000 2.0000000000000000)' 17 17 18 18 From coordinate tuples 19 19 20 >>> line = LineString(((1.0, 2.0), (3.0, 4.0)))21 >>> len(line.coords)22 223 >>> line.wkt24 'LINESTRING (1.0000000000000000 2.0000000000000000, 3.0000000000000000 4.0000000000000000)'20 >>> line = LineString(((1.0, 2.0), (3.0, 4.0))) 21 >>> len(line.coords) 22 2 23 >>> line.wkt 24 'LINESTRING (1.0000000000000000 2.0000000000000000, 3.0000000000000000 4.0000000000000000)' 25 25 26 26 … … 28 28 ------------- 29 29 30 >>> from numpy import asarray 31 >>> la = asarray(line) 32 >>> la 33 array([[ 1., 2.], 34 [ 3., 4.]]) 35 >>> la[0] 36 array([ 1., 2.]) 30 >>> from numpy import asarray 31 >>> la = asarray(line) 32 >>> la 33 array([[ 1., 2.], 34 [ 3., 4.]]) 35 >>> la[0] 36 array([ 1., 2.]) 37 38 Coordinate sequences can be adapted as well 39 40 >>> asarray(line.coords) 41 array([[ 1., 2.], 42 [ 3., 4.]]) 37 43 38 44 Bounds 39 45 ------ 40 46 41 >>> line.bounds42 (1.0, 2.0, 3.0, 4.0)47 >>> line.bounds 48 (1.0, 2.0, 3.0, 4.0) 43 49 44 50 Coordinate Access 45 51 ----------------- 46 52 47 >>> tuple(line.coords)48 ((1.0, 2.0), (3.0, 4.0))53 >>> tuple(line.coords) 54 ((1.0, 2.0), (3.0, 4.0)) 49 55 50 >>> line.coords[0]51 (1.0, 2.0)52 >>> line.coords[1]53 (3.0, 4.0)54 >>> line.coords[2]55 Traceback (most recent call last):56 ...57 IndexError: index out of range56 >>> line.coords[0] 57 (1.0, 2.0) 58 >>> line.coords[1] 59 (3.0, 4.0) 60 >>> line.coords[2] 61 Traceback (most recent call last): 62 ... 63 IndexError: index out of range 58 64 59 65 Geo interface 60 66 ------------- 61 67 62 >>> line.__geo_interface__63 {'type': 'LineString', 'coordinates': ((1.0, 2.0), (3.0, 4.0))}68 >>> line.__geo_interface__ 69 {'type': 'LineString', 'coordinates': ((1.0, 2.0), (3.0, 4.0))} 64 70 65 71 … … 67 73 ----------------------- 68 74 69 >>> line.coords = ((-1.0, -1.0), (1.0, 1.0))70 >>> line.__geo_interface__71 {'type': 'LineString', 'coordinates': ((-1.0, -1.0), (1.0, 1.0))}75 >>> line.coords = ((-1.0, -1.0), (1.0, 1.0)) 76 >>> line.__geo_interface__ 77 {'type': 'LineString', 'coordinates': ((-1.0, -1.0), (1.0, 1.0))} 72 78 73 79 … … 75 81 ------- 76 82 77 >>> from shapely.geometry import asLineString83 >>> from shapely.geometry import asLineString 78 84 79 Adapt a Numpy array to a line string85 Adapt a Numpy array to a line string 80 86 81 >>> a = array([[1.0, 2.0], [3.0, 4.0]])82 >>> la = asLineString(a)83 >>> la.context84 array([[ 1., 2.],85 [ 3., 4.]])87 >>> a = array([[1.0, 2.0], [3.0, 4.0]]) 88 >>> la = asLineString(a) 89 >>> la.context 90 array([[ 1., 2.], 91 [ 3., 4.]]) 86 92 87 >>> la.wkt88 'LINESTRING (1.0000000000000000 2.0000000000000000, 3.0000000000000000 4.0000000000000000)'93 >>> la.wkt 94 'LINESTRING (1.0000000000000000 2.0000000000000000, 3.0000000000000000 4.0000000000000000)' 89 95 90 Now, the inverse96 Now, the inverse 91 97 92 >>> la.__array_interface__ == la.context.__array_interface__93 True94 >>> as = asarray(la)95 >>> as96 array([[ 1., 2.],97 [ 3., 4.]])98 >>> la.__array_interface__ == la.context.__array_interface__ 99 True 100 >>> as = asarray(la) 101 >>> as 102 array([[ 1., 2.], 103 [ 3., 4.]]) 98 104 99 Adapt a coordinate list to a line string105 Adapt a coordinate list to a line string 100 106 101 >>> coords = [[5.0, 6.0], [7.0, 8.0]]102 >>> la = asLineString(coords)103 >>> la.wkt104 'LINESTRING (5.0000000000000000 6.0000000000000000, 7.0000000000000000 8.0000000000000000)'107 >>> coords = [[5.0, 6.0], [7.0, 8.0]] 108 >>> la = asLineString(coords) 109 >>> la.wkt 110 'LINESTRING (5.0000000000000000 6.0000000000000000, 7.0000000000000000 8.0000000000000000)' 105 111 106 112 Test Non-operability of Null geometry 107 113 108 >>> l_null = LineString()109 >>> l_null.wkt # doctest: +ELLIPSIS110 Traceback (most recent call last):111 ...112 ValueError: Null geometry supports no operations114 >>> l_null = LineString() 115 >>> l_null.wkt # doctest: +ELLIPSIS 116 Traceback (most recent call last): 117 ... 118 ValueError: Null geometry supports no operations 113 119 114 >>> l_null.length # doctest: +ELLIPSIS115 Traceback (most recent call last):116 ...117 ValueError: Null geometry supports no operations120 >>> l_null.length # doctest: +ELLIPSIS 121 Traceback (most recent call last): 122 ... 123 ValueError: Null geometry supports no operations 118 124 119 125 Check that we can set coordinates of a null geometry 120 126 121 >>> l_null.coords = [(0, 0), (1, 1)]122 >>> print l_null.length # doctest: +ELLIPSIS123 1.414...127 >>> l_null.coords = [(0, 0), (1, 1)] 128 >>> print l_null.length # doctest: +ELLIPSIS 129 1.414... 124 130
