a
    ßÁd‹(  ã                   @   s¸   d Z ddlZddlZddlmZmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	gZd
d„ ZG dd	„ d	eƒZeejjd< G dd„ dƒZG dd„ de
ƒZeejjd< ddd„ZdS )z*Polygons and their linear ring components
é    N)Úis_ccw_implÚsigned_area)ÚTopologicalError)ÚBaseGeometry)Ú
LineString)ÚPointÚPolygonÚ
LinearRingc                 C   s8   t  | ¡}t  |¡}t  t  |¡¡}|r4t  ||¡}|S ©N)ÚshapelyZfrom_wkbZget_sridÚlinearringsZget_coordinatesZset_srid)ZwkbZ
linestringZsridZ
linearring© r   ú[/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/shapely/geometry/polygon.pyÚ_unpickle_linearring   s    

r   c                   @   sJ   e Zd ZdZg Zddd„Zedd„ ƒZdd„ Zed	d
„ ƒZ	edd„ ƒZ
dS )r	   aŽ  
    A geometry type composed of one or more line segments
    that forms a closed loop.

    A LinearRing is a closed, one-dimensional feature.
    A LinearRing that crosses itself or touches itself at a single point is
    invalid and operations on it may fail.

    Parameters
    ----------
    coordinates : sequence
        A sequence of (x, y [,z]) numeric coordinate pairs or triples, or
        an array-like with shape (N, 2) or (N, 3).
        Also can be a sequence of Point objects.

    Notes
    -----
    Rings are automatically closed. There is no need to specify a final
    coordinate pair identical to the first.

    Examples
    --------
    Construct a square ring.

    >>> ring = LinearRing( ((0, 0), (0, 1), (1 ,1 ), (1 , 0)) )
    >>> ring.is_closed
    True
    >>> list(ring.coords)
    [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)]
    >>> ring.length
    4.0

    Nc                    sä   |d u rt  d¡S t|tƒrDt|ƒtkr,|S |js<tdƒ‚q®|j}njt	|dƒrXt
 |¡}t|t
jƒrvt
 |jt
j¡rvn8dd„ ‰ t
 ‡ fdd„|D ƒ¡}t
 |jt
j¡s®tdƒ‚t|ƒd	krÄt  d¡S t  |¡}t|tƒsàtd
ƒ‚|S )NzLINEARRING EMPTYz"An input LineString must be valid.Z	__array__c                 S   s&   t | tƒr| jd S dd„ | D ƒS d S )Nr   c                 S   s   g | ]}t |ƒ‘qS r   )Úfloat©Ú.0Úcr   r   r   Ú
<listcomp>[   ó    z7LinearRing.__new__.<locals>._coords.<locals>.<listcomp>)Ú
isinstancer   Úcoords)Úor   r   r   Ú_coordsW   s    

z#LinearRing.__new__.<locals>._coordsc                    s   g | ]}ˆ |ƒ‘qS r   r   )r   r   ©r   r   r   r   ]   r   z&LinearRing.__new__.<locals>.<listcomp>z&Inconsistent coordinate dimensionalityr   z/Invalid values passed to LinearRing constructor)r   Úfrom_wktr   r   Útyper	   Úis_validr   r   ÚhasattrÚnpZasarrayZndarrayZ
issubdtypeZdtypeÚnumberÚarrayÚ
ValueErrorÚlenr   )ÚselfÚcoordinatesÚgeomr   r   r   Ú__new__>   s0    




ÿ


zLinearRing.__new__c                 C   s   dt | jƒdœS )Nr	   ©r   r%   )Útupler   ©r$   r   r   r   Ú__geo_interface__m   s    zLinearRing.__geo_interface__c                 C   s   t tj| dd�ffS )z�WKB doesn't differentiate between LineString and LinearRing so we
        need to move the coordinate sequence into the correct geometry typeT)Zinclude_srid)r   r   Zto_wkbr*   r   r   r   Ú
__reduce__q   s    zLinearRing.__reduce__c                 C   s   t tƒ | ƒƒS )z/True is the ring is oriented counter clock-wise)Úboolr   r*   r   r   r   Úis_ccwv   s    zLinearRing.is_ccwc                 C   s   t t | ¡ƒS )zsTrue if the geometry is simple, meaning that any self-intersections
        are only at boundary points, else False)r-   r   Ú	is_simpler*   r   r   r   r/   {   s    zLinearRing.is_simple)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r'   Úpropertyr+   r,   r.   r/   r   r   r   r   r	      s   "
/

é   c                   @   sL   e Zd ZdZdZdZdZdd„ Zdd„ Zdd„ Z	d	d
„ Z
dd„ Zdd„ ZdS )ÚInteriorRingSequenceNr   c                 C   s   || _ |j| _d S r
   )Ú_parentÚ_ndim)r$   Úparentr   r   r   Ú__init__Œ   s    zInteriorRingSequence.__init__c                 C   s   d| _ |  ¡ | _| S )Nr   )Ú_indexÚ__len__Ú_lengthr*   r   r   r   Ú__iter__�   s    
zInteriorRingSequence.__iter__c                 C   s2   | j | jk r*|  | j ¡}|  j d7  _ |S t‚d S )Né   )r<   r>   Ú	_get_ringÚStopIteration)r$   Úringr   r   r   Ú__next__•   s
    zInteriorRingSequence.__next__c                 C   s   t  | j¡S r
   )r   Zget_num_interior_ringsr8   r*   r   r   r   r=   �   s    zInteriorRingSequence.__len__c                 C   sž   |   ¡ }t|tƒrN|| dk s&||kr.tdƒ‚|dk r@|| }n|}|  |¡S t|tƒr’g }| |¡\}}}t|||ƒD ]}| |  |¡¡ qx|S t	dƒ‚d S )Nr   zindex out of rangezkey must be an index or slice)
r=   r   ÚintÚ
IndexErrorrA   ÚsliceÚindicesÚrangeÚappendÚ	TypeError)r$   ÚkeyÚmÚiÚresÚstartÚstopZstrider   r   r   Ú__getitem__    s    



z InteriorRingSequence.__getitem__c                 C   s   t  | j|¡S r
   )r   Zget_interior_ringr8   )r$   rN   r   r   r   rA   ³   s    zInteriorRingSequence._get_ring)r0   r1   r2   r8   r9   r<   r>   r;   r?   rD   r=   rR   rA   r   r   r   r   r7   …   s   r7   c                   @   sd   e Zd ZdZg Zddd„Zedd„ ƒZedd„ ƒZed	d
„ ƒZ	edd„ ƒZ
ddd„Zedd„ ƒZdS )r   aE  
    A geometry type representing an area that is enclosed by a linear ring.

    A polygon is a two-dimensional feature and 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.

    Parameters
    ----------
    shell : sequence
        A sequence of (x, y [,z]) numeric coordinate pairs or triples, or
        an array-like with shape (N, 2) or (N, 3).
        Also can be a sequence of Point objects.
    holes : sequence
        A sequence of objects which satisfy the same requirements as the
        shell parameters above

    Attributes
    ----------
    exterior : LinearRing
        The ring which bounds the positive space of the polygon.
    interiors : sequence
        A sequence of rings which bound all existing holes.

    Examples
    --------
    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
    Nc                 C   st   |d u rt  d¡S t|tƒr |S t|ƒ}|d urPt|ƒdkrBd }ndd„ |D ƒ}t j||d�}t|tƒsptdƒ‚|S )NzPOLYGON EMPTYr   c                 S   s   g | ]}t |ƒ‘qS r   )r	   )r   rC   r   r   r   r   í   r   z#Polygon.__new__.<locals>.<listcomp>)Úholesz,Invalid values passed to Polygon constructor)r   r   r   r   r	   r#   Zpolygonsr"   )r$   ÚshellrS   r&   r   r   r   r'   Ý   s    


zPolygon.__new__c                 C   s
   t  | ¡S r
   )r   Zget_exterior_ringr*   r   r   r   Úexteriorô   s    zPolygon.exteriorc                 C   s   | j r
g S t| ƒS r
   )Úis_emptyr7   r*   r   r   r   Ú	interiorsø   s    zPolygon.interiorsc                 C   s   t dƒ‚d S )NzCComponent rings have coordinate sequences, but the polygon does not)ÚNotImplementedErrorr*   r   r   r   r   þ   s    ÿzPolygon.coordsc                 C   sJ   | j tƒ krg }n*t| j jƒg}| jD ]}| t|jƒ¡ q&dt|ƒdœS )Nr   r(   )rU   r	   r)   r   rW   rJ   )r$   r   Zholer   r   r   r+     s    
zPolygon.__geo_interface__ç      ð?c                 C   s|   | j r
dS |du r | jrdnd}|du r,d}dd„ | jjD ƒg}dd„ | jD ƒ}d	 d
d„ || D ƒ¡}d d| |||¡S )aÕ  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.
        opacity : float
            Float number between 0 and 1 for color opacity. Default value is 0.6
        z<g />Nz#66cc99z#ff3333g333333ã?c                 S   s   g | ]}d j |Ž ‘qS ©z{},{}©Úformatr   r   r   r   r   !  r   zPolygon.svg.<locals>.<listcomp>c                 S   s   g | ]}d d„ |j D ƒ‘qS )c                 S   s   g | ]}d j |Ž ‘qS rZ   r[   r   r   r   r   r   #  r   z*Polygon.svg.<locals>.<listcomp>.<listcomp>)r   )r   Zinteriorr   r   r   r   "  s   ú c              
   S   s*   g | ]"}d   |d d |dd… ¡¡‘qS )zM {} L {} zr   z L r@   N)r\   Újoin)r   r   r   r   r   r   &  s   ÿza<path fill-rule="evenodd" fill="{2}" stroke="#555555" stroke-width="{0}" opacity="{3}" d="{1}" />g       @)rV   r   rU   r   rW   r^   r\   )r$   Zscale_factorZ
fill_colorZopacityZexterior_coordsZinterior_coordsÚpathr   r   r   Úsvg  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_bounds0  s    zPolygon.from_bounds)NN)rY   NN)r0   r1   r2   r3   r4   r'   r5   rU   rW   r   r+   r`   Úclassmethodrb   r   r   r   r   r   ·   s   #




	
"é   rY   c                 C   s    t |ƒ}g }| j}t|ƒ| dkr.| |¡ n| t|jƒd d d… ¡ | jD ]:}t|ƒ| dkrn| |¡ qN| t|jƒd d d… ¡ qNt|d |dd … ƒS )Ng        éÿÿÿÿr   r@   )r   rU   r   rJ   Úlistr   rW   r   )ZpolygonÚsignÚsZringsrC   r   r   r   Úorient9  s    
ri   )rY   )r3   Únumpyr   r   Zshapely.algorithms.cgar   r   Zshapely.errorsr   Zshapely.geometry.baser   Zshapely.geometry.linestringr   Zshapely.geometry.pointr   Ú__all__r   r	   ÚlibÚregistryr7   r   ri   r   r   r   r   Ú<module>   s   	i2