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m	Z	 ddlm
Z
 ddlZddlmZ ddlmZ ddlmZmZ dd	lmZmZ dd
lmZ ddddgZG dd„ deƒZG dd„ deƒZdd„ ZG dd„ deƒZG dd„ deƒZG dd„ deeƒZd"dd„Z d#dd„Z!d$dd„Z"dd„ Z#d%d d!„Z$dS )&z*Polygons and their linear ring components
é    Né   )Úc_doubleÚc_void_pÚcastÚPOINTER)ÚArgumentError)Úsigned_area)Úlgeos)ÚBaseGeometryÚgeos_geom_from_py)Ú
LineStringÚLineStringAdapter)ÚPolygonProxyÚPolygonÚ	asPolygonÚ
LinearRingÚasLinearRingc                   sf   e Zd ZdZddd„Zedd„ ƒZejZdd„ Z	eee	ƒZ
‡ fd	d
„Zedd„ ƒZedd„ ƒZ‡  ZS )r   zÇ
    A closed one-dimensional feature comprising one or more line segments

    A LinearRing that crosses itself or touches itself at a single point is
    invalid and operations on it may fail.
    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

        Rings are implicitly closed. There is no need to specific a final
        coordinate pair identical to the first.

        Example
        -------
        Construct a square ring.

          >>> ring = LinearRing( ((0, 0), (0, 1), (1 ,1 ), (1 , 0)) )
          >>> ring.is_closed
          True
          >>> ring.length
          4.0
        N)r
   Ú__init__Ú_set_coords)ÚselfÚcoordinates© r   ú;/tmp/pip-build-op5p2y5_/shapely/shapely/geometry/polygon.pyr      s    
zLinearRing.__init__c             C   s   dt | jƒdœS )Nr   )Útyper   )ÚtupleÚcoords)r   r   r   r   Ú__geo_interface__7   s    zLinearRing.__geo_interface__c             C   s(   | j ƒ  t|ƒ}|d k	r$|\| _| _d S )N)ÚemptyÚgeos_linearring_from_pyÚ_geomÚ_ndim)r   r   Úretr   r   r   r   B   s    zLinearRing._set_coordsc                sB   t t| ƒj|ƒ tj| jƒ}tj|ƒ}tj| jƒ tj|ƒ| _dS )z�WKB doesn't differentiate between LineString and LinearRing so we
        need to move the coordinate sequence into the correct geometry typeN)	Úsuperr   Ú__setstate__r	   ÚGEOSGeom_getCoordSeqZ__geom__ZGEOSCoordSeq_cloneZGEOSGeom_destroyÚGEOSGeom_createLinearRing)r   ÚstateÚcsZcs_clone)Ú	__class__r   r   r#   J   s
    
zLinearRing.__setstate__c             C   s   t | jd | ƒƒS )z/True is the ring is oriented counter clock-wiseÚis_ccw)ÚboolÚimpl)r   r   r   r   r)   S   s    zLinearRing.is_ccwc             C   s
   t | ƒjS )zsTrue if the geometry is simple, meaning that any self-intersections
        are only at boundary points, else False)r   Ú	is_simple)r   r   r   r   r,   X   s    zLinearRing.is_simple)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r
   Ú_get_coordsr   r   r#   r)   r,   Ú__classcell__r   r   )r(   r   r      s   
	
	c               @   s.   e Zd ZdZdd„ Zedd„ ƒZeejƒZ	dS )ÚLinearRingAdapterNc             C   s   || _ t| _d S )N)Úcontextr   Úfactory)r   r5   r   r   r   r   c   s    zLinearRingAdapter.__init__c             C   s   dt | jƒdœS )Nr   )r   r   )r   r   )r   r   r   r   r   g   s    z#LinearRingAdapter.__geo_interface__)
r-   r.   r/   Ú__p__r   r1   r   r
   r2   r   r   r   r   r   r4   _   s   r4   c             C   s   t | ƒS )z+Adapt an object to the LinearRing interface)r4   )r5   r   r   r   r   q   s    c               @   s‚   e Zd ZdZdZdZdZdZdZdZ	dZ
dd„ Zdd„ Zdd„ Zejd d	k rReZd
d„ Zdd„ Zedd„ ƒZdd„ Zdd„ ZdS )ÚInteriorRingSequenceNr   c             C   s   || _ |j| _|j| _d S )N)r7   r   r    )r   Úparentr   r   r   r   �   s    zInteriorRingSequence.__init__c             C   s   d| _ | jƒ | _| S )Nr   )Ú_indexÚ__len__Ú_length)r   r   r   r   Ú__iter__†   s    
zInteriorRingSequence.__iter__c             C   s2   | j | jk r*| j| j ƒ}|  j d7  _ |S t‚d S )Né   )r:   r<   Ú	_get_ringÚStopIteration)r   Úringr   r   r   Ú__next__‹   s
    zInteriorRingSequence.__next__r   c             C   s   t j| jƒS )N)r	   ZGEOSGetNumInteriorRingsr   )r   r   r   r   r;   –   s    zInteriorRingSequence.__len__c             C   s¢   | j ƒ }t|tƒrN|| dk s&||kr.tdƒ‚|dk r@|| }n|}| j|ƒS t|tƒr–g }|j|ƒ\}}}x$t|||ƒD ]}|j| j|ƒƒ qzW |S t	dƒ‚d S )Nr   zindex out of rangezkey must be an index or slice)
r;   Ú
isinstanceÚintÚ
IndexErrorr?   ÚsliceÚindicesÚrangeÚappendÚ	TypeError)r   ÚkeyÚmÚiÚresÚstartÚstopZstrider   r   r   Ú__getitem__™   s    



z InteriorRingSequence.__getitem__c             C   s0   d}x&t | ƒD ]}t|jƒ}||kr|}qW d S )Nr   )ÚiterÚlenr   )r   ÚmaxÚgÚlr   r   r   Ú_longest¬   s
    
zInteriorRingSequence._longestc             C   s   t t| jƒƒS )N)ÚhashÚreprr7   )r   r   r   r   Úgtag´   s    zInteriorRingSequence.gtagc             C   sl   | j ƒ }|| jkri | _|| jkr`tj| j|ƒ}tƒ }||_| |_d|_| j	|_	t
j|ƒ| j|< | j| ƒ S )NT)rZ   Ú_gtagÚ	__rings__r	   ZGEOSGetInteriorRingNr   r   r7   Ú_other_ownedr    ÚweakrefÚref)r   rM   rZ   rU   rA   r   r   r   r?   ·   s    

zInteriorRingSequence._get_ring)r-   r.   r/   Ú_factoryr   r7   r    r:   r<   r\   r[   r   r=   rB   ÚsysÚversion_infoÚnextr;   rQ   r1   rW   rZ   r?   r   r   r   r   r8   v   s$   r8   c               @   s¨   e Zd ZdZdZg ZdZddd„Zedd„ ƒZ	edd	„ ƒZ
d
d„ Zdd„ ZdZedd„ ƒZedd„ ƒZdd„ Zdd„ Zedd„ ƒZedd„ ƒZd dd„Zedd„ ƒZdS )!r   aâ  
    A two-dimensional figure bounded by a linear ring

    A polygon has a non-zero area. It may have one or more negative-space
    "holes" which are also bounded by linear rings. If any rings cross each
    other, the feature is invalid and operations on it may fail.

    Attributes
    ----------
    exterior : LinearRing
        The ring which bounds the positive space of the polygon.
    interiors : sequence
        A sequence of rings which bound all existing holes.
    Né   c             C   s>   t j| ƒ |dk	r:t||ƒ}|dk	r2|\| _| _n| jƒ  dS )a  
        Parameters
        ----------
        shell : sequence
            A sequence of (x, y [,z]) numeric coordinate pairs or triples
        holes : sequence
            A sequence of objects which satisfy the same requirements as the
            shell parameters above

        Example
        -------
        Create a square polygon with no holes

          >>> coords = ((0., 0.), (0., 1.), (1., 1.), (1., 0.), (0., 0.))
          >>> polygon = Polygon(coords)
          >>> polygon.area
          1.0
        N)r
   r   Úgeos_polygon_from_pyr   r    r   )r   ÚshellÚholesr!   r   r   r   r   Ú   s    

zPolygon.__init__c             C   s`   | j r
d S | jd ks | jƒ d krXtj| jƒ}tƒ }||_| |_d|_| j|_t	j
|ƒ| _| jƒ S )NT)Úis_emptyÚ	_exteriorr	   ZGEOSGetExteriorRingr   r   r7   r]   r    r^   r_   )r   rU   rA   r   r   r   Úexteriorö   s    zPolygon.exteriorc             C   s   | j r
g S t| ƒS )N)rh   r8   )r   r   r   r   Ú	interiors  s    zPolygon.interiorsc             C   sr   t |tƒsdS | j|jf}t|ƒr&dS t|ƒr2dS t| jjƒdd„ | jD ƒg}t|jjƒdd„ |jD ƒg}||kS )NFTc             S   s   g | ]}t |jƒ‘qS r   )r   r   )Ú.0Úinteriorr   r   r   ú
<listcomp>  s    z"Polygon.__eq__.<locals>.<listcomp>c             S   s   g | ]}t |jƒ‘qS r   )r   r   )rl   rm   r   r   r   rn     s    )	rC   r   rh   ÚallÚanyr   rj   r   rk   )r   ÚotherZcheck_emptyZ	my_coordsZother_coordsr   r   r   Ú__eq__
  s    


zPolygon.__eq__c             C   s   | j |ƒ S )N)rr   )r   rq   r   r   r   Ú__ne__  s    zPolygon.__ne__c             C   s   | j s| jj| _ | j S )N)Z_ctypes_datarj   Úctypes)r   r   r   r   rt   !  s    
zPolygon.ctypesc             C   s   t dƒ‚d S )NzDA polygon does not itself provide the array interface. Its rings do.)ÚNotImplementedError)r   r   r   r   Ú__array_interface__'  s    zPolygon.__array_interface__c             C   s   t dƒ‚d S )NzCComponent rings have coordinate sequences, but the polygon does not)ru   )r   r   r   r   r2   ,  s    zPolygon._get_coordsc             C   s   t dƒ‚d S )NzCComponent rings have coordinate sequences, but the polygon does not)ru   )r   Úobr   r   r   r   0  s    zPolygon._set_coordsc             C   s   t dƒ‚d S )NzCComponent rings have coordinate sequences, but the polygon does not)ru   )r   r   r   r   r   4  s    zPolygon.coordsc             C   sH   | j sg }n.t| j jƒg}x| jD ]}|jt|jƒƒ q"W dt|ƒdœS )Nr   )r   r   )rj   r   r   rk   rI   )r   r   Zholer   r   r   r   9  s    zPolygon.__geo_interface__ç      ð?c             C   sn   | j r
dS |dkr | jrdnd}dd„ | jjD ƒg}dd„ | jD ƒ}djd	d„ || D ƒƒ}d
jd| ||ƒS )al  Returns SVG path element for the Polygon geometry.

        Parameters
        ==========
        scale_factor : float
            Multiplication factor for the SVG stroke-width.  Default is 1.
        fill_color : str, optional
            Hex string for fill color. Default is to use "#66cc99" if
            geometry is valid, and "#ff3333" if invalid.
        z<g />Nz#66cc99z#ff3333c             S   s   g | ]}d j |Ž ‘qS )z{0},{1})Úformat)rl   Úcr   r   r   rn   U  s    zPolygon.svg.<locals>.<listcomp>c             S   s   g | ]}d d„ |j D ƒ‘qS )c             S   s   g | ]}d j |Ž ‘qS )z{0},{1})ry   )rl   rz   r   r   r   rn   W  s    z*Polygon.svg.<locals>.<listcomp>.<listcomp>)r   )rl   rm   r   r   r   rn   W  s   ú c             S   s*   g | ]"}d j |d dj|dd… ƒƒ‘qS )zM {0} L {1} zr   z L r>   N)ry   Újoin)rl   r   r   r   r   rn   Z  s   za<path fill-rule="evenodd" fill="{2}" stroke="#555555" stroke-width="{0}" opacity="0.6" d="{1}" />g       @)rh   Zis_validrj   r   rk   r|   ry   )r   Zscale_factorZ
fill_colorZexterior_coordsZinterior_coordsÚpathr   r   r   ÚsvgE  s    
zPolygon.svgc             C   s    | ||f||f||f||fgƒS )z,Construct a `Polygon()` from spatial bounds.r   )ÚclsZxminZyminZxmaxZymaxr   r   r   Úfrom_boundsa  s
    zPolygon.from_bounds)NN)rx   N)r-   r.   r/   r0   ri   Z
_interiorsr    r   r1   rj   rk   rr   rs   Ú__hash__rt   rv   r2   r   r   r   r~   Úclassmethodr€   r   r   r   r   r   Æ   s$   

c               @   s"   e Zd Zddd„Zedd„ ƒZdS )ÚPolygonAdapterNc             C   s    || _ || _||f| _t| _d S )N)rf   rg   r5   re   r6   )r   rf   rg   r   r   r   r   m  s    
zPolygonAdapter.__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 )NÚshaper>   rd   r   r   )rf   rv   ÚAssertionErrorÚAttributeErrorrS   )r   ÚarrayÚnr   r   r   r    s  s    zPolygonAdapter._ndim)N)r-   r.   r/   r   r1   r    r   r   r   r   rƒ   k  s   
rƒ   c             C   s
   t | |ƒS )z&Adapt objects to the Polygon interface)rƒ   )rf   rg   r   r   r   r   €  s    ç      ð?c             C   s¤   t |ƒ}g }| j}t|ƒ| dkr.|j|ƒ n|jt|jƒd d d… ƒ xD| jD ]:}t|ƒ| dkrp|j|ƒ qP|jt|jƒd d d… ƒ qPW t|d |dd … ƒS )Ng        r>   r   éÿÿÿÿrŠ   )Úfloatrj   r   rI   Úlistr   rk   r   )ZpolygonÚsignÚsZringsrA   r   r   r   Úorient…  s    r�   c       	      C   s8  t | tƒrHt| ƒtkrt| ƒS | jr>t| jƒdkr>t| tj	ƒS t
| jƒ} yt| ƒ}W n$ tk
rx   t
| ƒ} t| ƒ}Y nX |dkr†d S t| d ƒ}|dk r¢tdƒ‚|dks¶|dks¶t‚|dksð| d d | d	 d ksð| d d | d
 d k� rú|d }n|}|d k	�r.||k�r"tdj||ƒƒ‚tj|ƒ}ntj||ƒ}x~t|ƒD ]r}| | }tj|||d ƒ tj|||d ƒ |dk�rDytj|||d ƒ W n tk
�r²   tdƒ‚Y nX �qDW ||k�r| d }tj||d |d ƒ tj||d |d ƒ |dk�rtj||d |d ƒ |d k	�r&d S tj	|ƒ|fS d S )Né   r   r   z3A LinearRing must have at least 3 coordinate tuplesrd   r>   zPCoordinate dimensions mismatch: target geom has {} dims, update geom has {} dimsz&Inconsistent coordinate dimensionalityrŠ   rŠ   )rC   r   r   r   r   Z	is_closedrS   r   r	   r%   rŒ   rJ   Ú
ValueErrorr…   ry   r$   ZGEOSCoordSeq_createrH   ZGEOSCoordSeq_setXZGEOSCoordSeq_setYZGEOSCoordSeq_setZrE   )	rw   Zupdate_geomZupdate_ndimrL   rˆ   ÚMr'   rM   r   r   r   r   r   •  s\    

:







r   c             C   s   t || j| jƒ d S )N)r   r   r    )Úgeomrw   r   r   r   Úupdate_linearring_from_pyá  s    r”   c             C   s  | d krd S t | tƒrt| ƒS | d k	�rt| ƒ}|d kr<d S |\}}|d k	rôt|ƒdkrô|}t|ƒ}|d }yt|d ƒ}W n tk
r–   |j}Y nX |dks¨tdƒ‚|dkr¸tdƒ‚t| ƒ }	x>t	|ƒD ]"}
t||
 ƒ\}}t
|tƒ|	|
< qÌW nttƒƒ }	d}tjt|ƒ|	|ƒ|fS d S )Nr   r>   z number of holes must be non zerord   r   z!insufficiant coordinate dimension)rd   r   )rC   r   r   r   rS   rJ   r    r‘   r   rH   r   r   r	   ZGEOSGeom_createPolygon)rf   rg   r!   Z
geos_shellÚndimrw   ÚLZexemplarÚNZ
geos_holesrV   r“   r   r   r   re   å  s:    



re   )N)r‰   )Nr   )N)%r0   ra   rb   ÚxrangerH   rt   r   r   r   r   r   r^   Zshapely.algorithms.cgar   Zshapely.geosr	   Zshapely.geometry.baser
   r   Zshapely.geometry.linestringr   r   Zshapely.geometry.proxyr   Ú__all__r   r4   r   Úobjectr8   r   rƒ   r   r�   r   r”   re   r   r   r   r   Ú<module>   s0   HP &


L