Changeset 1046

Show
Ignore:
Timestamp:
02/08/08 07:51:56
Author:
seang
Message:

Coordinate sequences implement the numpy array protocol and can be adapted into numpy arrays (#153)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • Shapely/trunk/CHANGES.txt

    r956 r1046  
     1All ticket numbers are rooted at http://trac.gispython.org/projects/PCL/ticket/ 
     2 
     31.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  
    113113        else: 
    114114            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) 
    115152 
    116153 
  • Shapely/trunk/shapely/geometry/linestring.py

    r1044 r1046  
    139139    def ctypes(self): 
    140140        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 
    158142        return self._ctypes_data 
    159143 
    160144    def array_interface(self): 
    161145        """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     
    165148    __array_interface__ = property(array_interface) 
    166149 
  • Shapely/trunk/shapely/geos.py

    r1017 r1046  
    3232    # Try the major versioned name first, falling back on the unversioned name. 
    3333    try: 
    34         lgeos = CDLL('libgeos_c.so.1') 
    35     except ImportError
     34        lgeos = CDLL('libgeos_c.so.2') 
     35    except (OSError, ImportError)
    3636        lgeos = CDLL('libgeos_c.so') 
    3737    except: 
  • Shapely/trunk/tests/LineString.txt

    r1006 r1046  
    55-------------- 
    66 
    7   >>> from shapely.geometry import LineString 
     7    >>> from shapely.geometry import LineString 
    88 
    99Construct from a numpy array 
    1010 
    11   >>> from numpy import array 
    12   >>> line = LineString(array([[0.0, 0.0], [1.0, 2.0]])) 
    13   >>> len(line.coords) 
    14  
    15   >>> line.wkt 
    16   '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   
     15    >>> line.wkt 
     16    'LINESTRING (0.0000000000000000 0.0000000000000000, 1.0000000000000000 2.0000000000000000)' 
    1717 
    1818From coordinate tuples 
    1919 
    20   >>> line = LineString(((1.0, 2.0), (3.0, 4.0))) 
    21   >>> len(line.coords) 
    22  
    23   >>> line.wkt 
    24   '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   
     23    >>> line.wkt 
     24    'LINESTRING (1.0000000000000000 2.0000000000000000, 3.0000000000000000 4.0000000000000000)' 
    2525 
    2626 
     
    2828------------- 
    2929 
    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 
     38Coordinate sequences can be adapted as well 
     39 
     40    >>> asarray(line.coords) 
     41    array([[ 1.,  2.], 
     42           [ 3.,  4.]]) 
    3743 
    3844Bounds 
    3945------ 
    4046 
    41   >>> line.bounds 
    42   (1.0, 2.0, 3.0, 4.0) 
     47    >>> line.bounds 
     48    (1.0, 2.0, 3.0, 4.0) 
    4349 
    4450Coordinate Access 
    4551----------------- 
    4652 
    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)) 
    4955 
    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 range 
     56    >>> 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 
    5864 
    5965Geo interface 
    6066------------- 
    6167 
    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))} 
    6470 
    6571 
     
    6773----------------------- 
    6874 
    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))} 
    7278 
    7379 
     
    7581------- 
    7682 
    77   >>> from shapely.geometry import asLineString 
     83    >>> from shapely.geometry import asLineString 
    7884 
    79   Adapt a Numpy array to a line string 
     85    Adapt a Numpy array to a line string 
    8086 
    81   >>> a = array([[1.0, 2.0], [3.0, 4.0]]) 
    82   >>> la = asLineString(a) 
    83   >>> la.context 
    84   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.]]) 
    8692 
    87   >>> la.wkt 
    88   'LINESTRING (1.0000000000000000 2.0000000000000000, 3.0000000000000000 4.0000000000000000)' 
     93    >>> la.wkt 
     94    'LINESTRING (1.0000000000000000 2.0000000000000000, 3.0000000000000000 4.0000000000000000)' 
    8995 
    90   Now, the inverse 
     96Now, the inverse 
    9197 
    92   >>> la.__array_interface__ == la.context.__array_interface__ 
    93   True 
    94   >>> as = asarray(la) 
    95   >>> as 
    96   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.]]) 
    98104 
    99   Adapt a coordinate list to a line string 
     105Adapt a coordinate list to a line string 
    100106 
    101   >>> coords = [[5.0, 6.0], [7.0, 8.0]] 
    102   >>> la = asLineString(coords) 
    103   >>> la.wkt 
    104   '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)' 
    105111 
    106112Test Non-operability of Null geometry 
    107113 
    108   >>> l_null = LineString() 
    109   >>> l_null.wkt # doctest: +ELLIPSIS 
    110   Traceback (most recent call last): 
    111   ... 
    112   ValueError: Null geometry supports no operations 
     114    >>> l_null = LineString() 
     115    >>> l_null.wkt # doctest: +ELLIPSIS 
     116    Traceback (most recent call last): 
     117    ... 
     118    ValueError: Null geometry supports no operations 
    113119 
    114   >>> l_null.length # doctest: +ELLIPSIS 
    115   Traceback (most recent call last): 
    116   ... 
    117   ValueError: Null geometry supports no operations 
     120    >>> l_null.length # doctest: +ELLIPSIS 
     121    Traceback (most recent call last): 
     122    ... 
     123    ValueError: Null geometry supports no operations 
    118124 
    119125Check that we can set coordinates of a null geometry 
    120126 
    121   >>> l_null.coords = [(0, 0), (1, 1)] 
    122   >>> print l_null.length # doctest: +ELLIPSIS 
    123   1.414... 
     127    >>> l_null.coords = [(0, 0), (1, 1)] 
     128    >>> print l_null.length # doctest: +ELLIPSIS 
     129    1.414... 
    124130