o
    Jfp,                     @   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 g d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)orientPolygon
LinearRingc                 C   s8   t | }t |}t t |}|rt ||}|S N)shapelyfrom_wkbget_sridlinearringsget_coordinatesset_srid)wkb
linestringsrid
linearring r   d/home/deployuser/azure_apps/autowriter/venv/lib/python3.10/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r!t|tkr|S |jstd|j}n5t	|dr+t
|}t|t
jr:t
|jt
jr:ndd  t
 fdd|D }t
|jt
jsVtdt|d	krat dS t |}t|tsotd
|S )NzLINEARRING EMPTYz"An input LineString must be valid.	__array__c                 S   s"   t | tr
| jd S dd | D S )Nr   c                 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npasarrayndarray
issubdtype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'   r4   )tupler"   r3   r   r   r   __geo_interface__m   s   zLinearRing.__geo_interface__c                 C   s   t tj| ddffS )zWKB doesn't differentiate between LineString and LinearRing so we
        need to move the coordinate sequence into the correct geometry typeT)include_srid)r   r   to_wkbr9   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   r9   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_simpler9   r   r   r   r@   {   s   zLinearRing.is_simpler   )__name__
__module____qualname____doc__	__slots__r6   propertyr:   r=   r?   r@   r   r   r   r   r
      s    "
/

r
      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)r3   parentr   r   r   __init__   s   zInteriorRingSequence.__init__c                 C   s   d| _ |  | _| S )Nr   )_index__len___lengthr9   r   r   r   __iter__   s   
zInteriorRingSequence.__iter__c                 C   s.   | j | jk r| | j }|  j d7  _ |S t)N   )rM   rO   	_get_ringStopIteration)r3   ringr   r   r   __next__   s
   zInteriorRingSequence.__next__c                 C   s   t | jS r   )r   get_num_interior_ringsrI   r9   r   r   r   rN      s   zInteriorRingSequence.__len__c                 C   s   |   }t|tr'|| dk s||krtd|dk r || }n|}| |S t|trIg }||\}}}t|||D ]
}|| | q<|S t	d)Nr   zindex out of rangezkey must be an index or slice)
rN   r!   int
IndexErrorrR   sliceindicesrangeappend	TypeError)r3   keymiresstartstopstrider   r   r   __getitem__   s   



z InteriorRingSequence.__getitem__c                 C   s   t | j|S r   )r   get_interior_ringrI   )r3   r`   r   r   r   rR      s   zInteriorRingSequence._get_ring)rA   rB   rC   rI   rJ   rM   rO   rL   rP   rU   rN   re   rR   r   r   r   r   rH      s    rH   c                       s|   e Zd ZdZg ZdddZedd Zedd Zed	d
 Z	dd Z
 fddZedd ZdddZedd Z  Z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r(t|dkr!d }ndd |D }t j||d}t|ts8td|S )NzPOLYGON EMPTYr   c                 S   r   r   )r
   )r   rT   r   r   r   r      r    z#Polygon.__new__.<locals>.<listcomp>)holesz,Invalid values passed to Polygon constructor)r   r&   r!   r	   r
   r2   polygonsr1   )r3   shellrg   r5   r   r   r   r6      s   


zPolygon.__new__c                 C   s
   t | S r   )r   get_exterior_ringr9   r   r   r   exterior   s   
zPolygon.exteriorc                 C   s   | j rg S t| S r   )is_emptyrH   r9   r   r   r   	interiors   s   zPolygon.interiorsc                 C   s   t d)NzCComponent rings have coordinate sequences, but the polygon does not)NotImplementedErrorr9   r   r   r   r"      s   zPolygon.coordsc                 C   s   t |tstS t |tsdS | j|jf}t|rdS t|r dS | jjgdd | j	D  }|jjgdd |j	D  }t
|t
|ksDdS tdd t||D S )NFTc                 S      g | ]}|j qS r   r"   r   interiorr   r   r   r         z"Polygon.__eq__.<locals>.<listcomp>c                 S   ro   r   rp   rq   r   r   r   r     rs   c                 S   s   g | ]
\}}t ||qS r   )r*   array_equal)r   leftrightr   r   r   r     s    
)r!   r   NotImplementedr	   rl   allanyrk   r"   rm   r2   r*   zip)r3   othercheck_empty	my_coordsother_coordsr   r   r   __eq__  s,   

zPolygon.__eq__c                    s
   t   S r   )super__hash__r9   	__class__r   r   r     s   
zPolygon.__hash__c                 C   sJ   | j t kr	g }nt| j jg}| jD ]
}|t|j qdt|dS )Nr	   r7   )rk   r
   r8   r"   rm   r\   )r3   r"   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      g | ]}d j | qS z{},{}formatr   r   r   r   r   >      zPolygon.svg.<locals>.<listcomp>c                 S   s   g | ]
}d d |j D qS )c                 S   r   r   r   r   r   r   r   r   @  r   z*Polygon.svg.<locals>.<listcomp>.<listcomp>rp   rq   r   r   r   r   ?  s     c              
   S   s*   g | ]}d  |d d|dd qS )zM {} L {} zr   z L rQ   N)r   join)r   r"   r   r   r   r   C  s    za<path fill-rule="evenodd" fill="{2}" stroke="#555555" stroke-width="{0}" opacity="{3}" d="{1}" />g       @)rl   r(   rk   r"   rm   r   r   )r3   scale_factor
fill_coloropacityexterior_coords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   )clsxminyminxmaxymaxr   r   r   from_boundsM  s    zPolygon.from_bounds)NN)r   NN)rA   rB   rC   rD   rE   r6   rF   rk   rm   r"   r   r   r:   r   classmethodr   __classcell__r   r   r   r   r	      s"    #





	"r	      r   c                 C   s   t |}g }| j}t|| dkr|| n|t|jd d d  | jD ]}t|| dkr7|| q'|t|jd d d  q't|d |dd  S )Ng        r   rQ   )r   rk   r   r\   listr"   rm   r	   )polygonsignsringsrT   r   r   r   r   V  s   
r   )r   )rD   numpyr*   r   shapely.algorithms.cgar   r   shapely.errorsr   shapely.geometry.baser   shapely.geometry.linestringr   shapely.geometry.pointr   __all__r   r
   libregistryrH   r	   r   r   r   r   r   <module>   s"    	i2 