3
®?d]ð  ã               @   sº   d Z ddlZejd dk reZddlmZmZmZ ddl	m
Z
mZ ddlmZmZmZmZ ddlmZ ddlmZ d	d
gZG dd	„ d	eƒZG dd„ deeƒZdd
„ Zddd„Zdd„ ZdS )z#Line strings and related utilities
é    Né   )Úc_doubleÚcastÚPOINTER)ÚlgeosÚTopologicalError)ÚBaseGeometryÚgeom_factoryÚ
JOIN_STYLEÚgeos_geom_from_py)ÚCachingGeometryProxy)ÚPointÚ
LineStringÚasLineStringc               @   s€   e Zd ZdZddd„Zedd„ ƒZddd	„Zed
d„ ƒZdd„ Z	ee	ƒZ
dd„ ZeejeƒZedd„ ƒZddejdfdd„ZdS )r   zæ
    A one-dimensional figure comprising one or more line segments

    A LineString has non-zero length and zero area. It may approximate a curve
    and need not be straight. Unlike a LinearRing, a LineString is not closed.
    Nc             C   s    t j| ƒ |dk	r| j|ƒ dS )a¶  
        Parameters
        ----------
        coordinates : sequence
            A sequence of (x, y [,z]) numeric coordinate pairs or triples or
            an object that provides the numpy array interface, including
            another instance of LineString.

        Example
        -------
        Create a line with two segments

          >>> a = LineString([[0, 0], [1, 0], [1, 1]])
          >>> a.length
          2.0
        N)r   Ú__init__Ú_set_coords)ÚselfÚcoordinates© r   ú>/tmp/pip-build-op5p2y5_/shapely/shapely/geometry/linestring.pyr      s    
zLineString.__init__c             C   s   dt | jƒdœS )Nr   )Útyper   )ÚtupleÚcoords)r   r   r   r   Ú__geo_interface__2   s    zLineString.__geo_interface__ç      ð?c             C   sH   | j r
dS |dkr | jrdnd}djdd„ | jD ƒƒ}dj|d	| |ƒS )
aw  Returns SVG polyline element for the LineString geometry.

        Parameters
        ==========
        scale_factor : float
            Multiplication factor for the SVG stroke-width.  Default is 1.
        stroke_color : str, optional
            Hex string for stroke color. Default is to use "#66cc99" if
            geometry is valid, and "#ff3333" if invalid.
        z<g />Nz#66cc99z#ff3333ú c             S   s   g | ]}d j |Ž ‘qS )z{0},{1})Úformat)Ú.0Úcr   r   r   ú
<listcomp>H   s    z"LineString.svg.<locals>.<listcomp>zS<polyline fill="none" stroke="{2}" stroke-width="{1}" points="{0}" opacity="0.8" />g       @)Úis_emptyZis_validÚjoinr   r   )r   Zscale_factorZstroke_colorZ
pnt_formatr   r   r   Úsvg9   s    zLineString.svgc             C   s   | j s| jj| _ | j S )N)Z_ctypes_datar   Úctypes)r   r   r   r   r#   N   s    
zLineString.ctypesc             C   s*   | j rdddtd ƒ dœ}n
| jjƒ }|S )z!Provide the Numpy array protocol.r   z<f8r   )ÚversionZtypestrÚshapeÚdata)r   )r    r   r   Úarray_interface)r   Zair   r   r   r'   T   s    
zLineString.array_interfacec             C   s(   | j ƒ  t|ƒ}|d k	r$|\| _| _d S )N)ÚemptyÚgeos_linestring_from_pyÚ_geomÚ_ndim)r   r   Úretr   r   r   r   _   s    zLineString._set_coordsc             C   s   | j jS )zÓSeparate arrays of X and Y coordinate values

        Example:

          >>> x, y = LineString(((0, 0), (1, 1))).xy
          >>> list(x)
          [0.0, 1.0]
          >>> list(y)
          [0.0, 1.0]
        )r   Úxy)r   r   r   r   r-   g   s    zLineString.xyÚrighté   g      @c             C   sL   |dkrt dƒ‚yt| jd | |||||ƒƒS  tk
rF   tƒ ‚Y nX dS )a@  Returns a LineString or MultiLineString geometry at a distance from
        the object on its right or its left side.

        The side parameter may be 'left' or 'right' (default is 'right'). The
        resolution of the buffer around each vertex of the object increases by
        increasing the resolution keyword parameter or third positional
        parameter. Vertices of right hand offset lines will be ordered in
        reverse.

        The join style is for outside corners between line segments. Accepted
        values are JOIN_STYLE.round (1), JOIN_STYLE.mitre (2), and
        JOIN_STYLE.bevel (3).

        The mitre ratio limit is used for very sharp corners. It is the ratio
        of the distance from the corner to the end of the mitred offset corner.
        When two line segments meet at a sharp angle, a miter join will extend
        far beyond the original geometry. To prevent unreasonable geometry, the
        mitre limit allows controlling the maximum length of the join corner.
        Corners with a ratio which exceed the limit will be beveled.
        g        z3Cannot compute offset from zero-length line segmentÚparallel_offsetN)Ú
ValueErrorr	   ÚimplÚOSErrorr   )r   ZdistanceZsideÚ
resolutionZ
join_styleZmitre_limitr   r   r   r0   u   s    
zLineString.parallel_offset)N)r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r"   r#   r'   Ú__array_interface__r   r   Ú_get_coordsr   r-   r
   Úroundr0   r   r   r   r   r      s   

c               @   sB   e Zd Zdd„ Zedd„ ƒZedd„ ƒZejZdd„ Z	eeƒZ
d	S )
ÚLineStringAdapterc             C   s   || _ t| _d S )N)Úcontextr)   Úfactory)r   r>   r   r   r   r   ™   s    zLineStringAdapter.__init__c             C   sP   y,| j j}|d d }|dks*|dks*t‚|S  tk
rJ   t| j d ƒS X d S )Nr%   é   é   r   r   )r>   r:   ÚAssertionErrorÚAttributeErrorÚlen)r   ÚarrayÚnr   r   r   r+   �   s    zLineStringAdapter._ndimc             C   s&   y| j jS  tk
r    | jƒ S X dS )z!Provide the Numpy array protocol.N)r>   r:   rC   r'   )r   r   r   r   r:   ©   s    z%LineStringAdapter.__array_interface__c             C   s   t dƒ‚d S )Nz0Adapters can not modify their coordinate sources)ÚNotImplementedError)r   Úobr   r   r   r   ³   s    zLineStringAdapter._set_coordsN)r5   r6   r7   r   r9   r+   r:   r   r;   r   r   r   r   r   r   r=   —   s   r=   c             C   s   t | ƒS )z(Adapt an object the LineString interface)r=   )r>   r   r   r   r   º   s    c       	      C   s¦  t | tƒr*t| ƒtkrt| ƒS t| tjƒS yt| ƒ}W n$ tk
rZ   t| ƒ} t| ƒ}Y nX |dkrhd S |dkrxt	dƒ‚dd„ }yt|| d ƒƒ}W n$ tk
r¸   t	dt
| ƒ ƒ‚Y nX |dksÎ|dksÎt‚|d k	rötj|ƒ}||krôt	d	| ƒ‚ntj||ƒ}x‚t|ƒD ]v}|| | ƒ}tj|||d ƒ tj|||d ƒ |dk�rytj|||d ƒ W n tk
�r~   t	d
ƒ‚Y nX �qW |d k	�r”d S tj|ƒ|fS d S )Nr   r@   z2LineStrings must have at least 2 coordinate tuplesc             S   s   t | tƒr| jd S | S d S )Nr   )Ú
isinstancer   r   )Úor   r   r   Ú_coordsØ   s    

z(geos_linestring_from_py.<locals>._coordsz,Input %s is the wrong shape for a LineStringrA   r   z=Wrong coordinate dimensions; this geometry has dimensions: %dz&Inconsistent coordinate dimensionality)rI   r   r   r   r   ZGEOSGeom_createLineStringrD   Ú	TypeErrorÚlistr1   ÚstrrB   ZGEOSGeom_getCoordSeqZGEOSCoordSeq_createÚrangeZGEOSCoordSeq_setXZGEOSCoordSeq_setYZGEOSCoordSeq_setZÚ
IndexError)	rH   Zupdate_geomZupdate_ndimÚmrK   rF   ÚcsÚir   r   r   r   r)   ¿   sN    




r)   c             C   s   t || j| jƒ d S )N)r)   r*   r+   )ZgeomrH   r   r   r   Úupdate_linestring_from_py  s    rT   )Nr   )r8   ÚsysÚversion_infoÚxrangerO   r#   r   r   r   Zshapely.geosr   r   Zshapely.geometry.baser   r	   r
   r   Zshapely.geometry.proxyr   Zshapely.geometry.pointr   Ú__all__r   r=   r   r)   rT   r   r   r   r   Ú<module>   s    #
C