3
®?d].!  ã               @   sf   d Z ddlmZmZmZmZ dddddgZdd„ Zd	d
„ Zddd„Z	ddd„Z
ddd„Zddd„ZdS )zBAffine transforms, both in general and specific, named transforms.é    )ÚsinÚcosÚtanÚpiÚaffine_transformÚrotateÚscaleÚskewÚ	translatec                sº  | j r
| S tˆ	ƒdkrjd‰
ˆ	\‰ ‰‰‰‰‰| jrºd‰
d‰d ‰ ‰ ‰ ‰‰ˆ ˆˆˆˆˆˆˆˆˆˆˆf‰	nPtˆ	ƒdkr²d‰
ˆ	\‰ ‰‰‰‰‰‰‰‰‰‰‰| jsºd‰
ˆ ˆˆˆˆˆf‰	ntdƒ‚‡ ‡‡‡‡‡‡‡‡‡
‡‡‡fdd	„}| jdk�r t| ƒt|| jƒƒƒS | jdk�rr| j}t|ƒt||jƒƒƒ}t| jƒ}x0t	|ƒD ]$\}}t|ƒt||jƒƒƒ||< �q<W t| ƒ||ƒS | jj
dƒ�sŒ| jdk�r¨t| ƒ‡	fdd„| jD ƒƒS td| j ƒ‚dS )az  Returns a transformed geometry using an affine transformation matrix.

    The coefficient matrix is provided as a list or tuple with 6 or 12 items
    for 2D or 3D transformations, respectively.

    For 2D affine transformations, the 6 parameter matrix is::

        [a, b, d, e, xoff, yoff]

    which represents the augmented matrix::

                            / a  b xoff \ 
        [x' y' 1] = [x y 1] | d  e yoff |
                            \ 0  0   1  /

    or the equations for the transformed coordinates::

        x' = a * x + b * y + xoff
        y' = d * x + e * y + yoff

    For 3D affine transformations, the 12 parameter matrix is::

        [a, b, c, d, e, f, g, h, i, xoff, yoff, zoff]

    which represents the augmented matrix::

                                 / a  b  c xoff \ 
        [x' y' z' 1] = [x y z 1] | d  e  f yoff |
                                 | g  h  i zoff |
                                 \ 0  0  0   1  /

    or the equations for the transformed coordinates::

        x' = a * x + b * y + c * z + xoff
        y' = d * x + e * y + f * z + yoff
        z' = g * x + h * y + i * z + zoff
    é   é   é   g      ð?g        é   z,'matrix' expects either 6 or 12 coefficientsc             3   sÎ   ˆ	dkrNxÀ| D ]:\}}ˆ | ˆ|  ˆ
 }ˆ| ˆ|  ˆ }||fV  qW n|ˆ	dkrÊxr| D ]j\}}}ˆ | ˆ|  ˆ|  ˆ
 }ˆ| ˆ|  ˆ|  ˆ }ˆ| ˆ|  ˆ|  ˆ }|||fV  q\W dS )z@Internal function to yield affine transform of coordinate tuplesr   r   N© )ZptsÚxÚyZxpZypÚzZzp)ÚaÚbÚcÚdÚeÚfÚgÚhÚiÚndimÚxoffÚyoffÚzoffr   ú3/tmp/pip-build-op5p2y5_/shapely/shapely/affinity.pyÚ
affine_ptsA   s    z$affine_transform.<locals>.affine_ptsÚPointÚ
LineStringÚ
LinearRingZPolygonZMultiZGeometryCollectionc                s   g | ]}t |ˆ ƒ‘qS r   )r   )Ú.0Úpart)Úmatrixr   r    ú
<listcomp>\   s   z$affine_transform.<locals>.<listcomp>zType %r not recognizedN)r"   r#   r$   )Zis_emptyÚlenZhas_zÚ
ValueErrorÚtypeÚlistÚcoordsZexteriorZ	interiorsÚ	enumerateÚ
startswithZgeoms)Úgeomr'   r!   ZringÚshellZholesÚposr   )r   r   r   r   r   r   r   r   r   r'   r   r   r   r   r    r      s>    &$
 c             C   sÀ   |dkr0| j \}}}}|| d || d f}nL|dkrF| jjd }n6t|tƒr^td| ƒ‚nt|dƒr||jdkr||jd }t|ƒdkr�td
ƒ‚|dkr¤|dd… S t|ƒdkr¸|d S |S dS )a6  Returns interpreted coordinate tuple for origin parameter.

    This is a helper function for other transform functions.

    The point of origin can be a keyword 'center' for the 2D bounding box
    center, 'centroid' for the geometry's 2D centroid, a Point object or a
    coordinate tuple (x0, y0, z0).
    Úcenterg       @Úcentroidr   z%'origin' keyword %r is not recognizedr+   r"   r   r   z8Expected number of items in 'origin' to be either 2 or 3ç        N)r   r   )r5   )	Zboundsr4   r-   Ú
isinstanceÚstrr*   Úhasattrr+   r)   )r0   Úoriginr   ZminxZminyZmaxxZmaxyr   r   r    Úinterpret_originb   s     


r:   r3   Fc       	      C   s˜   |s|t d 9 }t|ƒ}t|ƒ}t|ƒdk r0d}t|ƒdk r@d}t| |dƒ\}}|| d||dddd|||  ||  |||  ||  df}t| |ƒS )aç  Returns a rotated geometry on a 2D plane.

    The angle of rotation can be specified in either degrees (default) or
    radians by setting ``use_radians=True``. Positive angles are
    counter-clockwise and negative are clockwise rotations.

    The point of origin can be a keyword 'center' for the bounding box
    center (default), 'centroid' for the geometry's centroid, a Point object
    or a coordinate tuple (x0, y0).

    The affine transformation matrix for 2D rotation is:

      / cos(r) -sin(r) xoff \ 
      | sin(r)  cos(r) yoff |
      \   0       0      1  /

    where the offsets are calculated from the origin Point(x0, y0):

        xoff = x0 - x0 * cos(r) + y0 * sin(r)
        yoff = y0 - x0 * sin(r) - y0 * cos(r)
    g     €f@gVçž¯²<g        r   g      ð?)r   r   r   Úabsr:   r   )	r0   Zangler9   Úuse_radiansZcospZsinpÚx0Úy0r'   r   r   r    r   „   s    *ç      ð?c       	      C   sP   t | |dƒ\}}}|ddd|ddd||||  |||  |||  f}t| |ƒS )a¸  Returns a scaled geometry, scaled by factors along each dimension.

    The point of origin can be a keyword 'center' for the 2D bounding box
    center (default), 'centroid' for the geometry's 2D centroid, a Point
    object or a coordinate tuple (x0, y0, z0).

    Negative scale factors will mirror or reflect coordinates.

    The general 3D affine transformation matrix for scaling is:

        / xfact  0    0   xoff \ 
        |   0  yfact  0   yoff |
        |   0    0  zfact zoff |
        \   0    0    0     1  /

    where the offsets are calculated from the origin Point(x0, y0, z0):

        xoff = x0 - x0 * xfact
        yoff = y0 - y0 * yfact
        zoff = z0 - z0 * zfact
    r   g        )r:   r   )	r0   ZxfactZyfactZzfactr9   r=   r>   Zz0r'   r   r   r    r   «   s    "ç        c       
      C   sŽ   |s|t d 9 }|t d 9 }t|ƒ}t|ƒ}t|ƒdk r<d}t|ƒdk rLd}t| |dƒ\}}d|d|ddddd| | | | df}	t| |	ƒS )a™  Returns a skewed geometry, sheared by angles along x and y dimensions.

    The shear angle can be specified in either degrees (default) or radians
    by setting ``use_radians=True``.

    The point of origin can be a keyword 'center' for the bounding box
    center (default), 'centroid' for the geometry's centroid, a Point object
    or a coordinate tuple (x0, y0).

    The general 2D affine transformation matrix for skewing is:

        /   1    tan(xs) xoff \ 
        | tan(ys)  1     yoff |
        \   0      0       1  /

    where the offsets are calculated from the origin Point(x0, y0):

        xoff = -y0 * tan(xs)
        yoff = -x0 * tan(ys)
    g     €f@gVçž¯²<g        r   g      ð?)r   r   r;   r:   r   )
r0   ÚxsZysr9   r<   ZtanxZtanyr=   r>   r'   r   r   r    r	   Ê   s    c             C   s&   ddddddddd|||f}t | |ƒS )zöReturns a translated geometry shifted by offsets along each dimension.

    The general 3D affine transformation matrix for translation is:

        / 1  0  0 xoff \ 
        | 0  1  0 yoff |
        | 0  0  1 zoff |
        \ 0  0  0   1  /
    g      ð?g        )r   )r0   r   r   r   r'   r   r   r    r
   ñ   s
    

N)r3   F)r?   r?   r?   r3   )r@   r@   r3   F)r@   r@   r@   )Ú__doc__Úmathr   r   r   r   Ú__all__r   r:   r   r   r	   r
   r   r   r   r    Ú<module>   s   Z"
'

'