o
    uvXj  ã                   @   sT   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d„ deƒZdS )	é    )ÚbyrefÚc_uint)Ú
prototypes)ÚGEOSGeometry)ÚGEOM_PTR)Ú
LinearRingc                       sÂ   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zedd	„ ƒZd
d„ Z	dd„ Z
	d!dd„Zdd„ Zdd„ Zdd„ ZejZejZedd„ ƒZdd„ Zdd„ ZeeeƒZeZedd„ ƒZeZedd „ ƒZ‡  ZS )"ÚPolygoné   c                    s°   |st ƒ j|  dd¡fi |¤Ž dS |^}}t|ƒ}|dkrAt|d ttfƒrA|d s0d}d}nt|d d tƒrA|d }t|ƒ}|  |d |g|¢¡}t ƒ j|fi |¤Ž dS )aÓ  
        Initialize on an exterior ring and a sequence of holes (both
        instances may be either LinearRing instances, or a tuple/list
        that may be constructed into a LinearRing).

        Examples of initialization, where shell, hole1, and hole2 are
        valid LinearRing geometries:
        >>> from django.contrib.gis.geos import LinearRing, Polygon
        >>> shell = hole1 = hole2 = LinearRing()
        >>> poly = Polygon(shell, hole1, hole2)
        >>> poly = Polygon(shell, (hole1, hole2))

        >>> # Example where a tuple parameters are used:
        >>> poly = Polygon(((0, 0), (0, 10), (10, 10), (0, 10), (0, 0)),
        ...                ((4, 4), (4, 6), (6, 6), (6, 4), (4, 4)))
        r   Nr	   © )ÚsuperÚ__init__Ú_create_polygonÚlenÚ
isinstanceÚtupleÚlistr   )ÚselfÚargsÚkwargsÚext_ringÚ
init_holesÚn_holesÚpolygon©Ú	__class__r
   ú\/var/www/html/myproject/venv/lib/python3.10/site-packages/django/contrib/gis/geos/polygon.pyr      s   zPolygon.__init__c                 c   s"   � t t| ƒƒD ]}| | V  qdS )z&Iterate over each ring in the polygon.N)Úranger   )r   Úir
   r
   r   Ú__iter__1   s   €ÿzPolygon.__iter__c                 C   s
   | j d S )z+Return the number of rings in this Polygon.r	   )Únum_interior_rings©r   r
   r
   r   Ú__len__6   s   
zPolygon.__len__c                 C   sn   |\}}}}|D ]}t |ttfƒs#td||||||||||f
 ƒ  S qt||f||f||f||f||ffƒS )z2Construct a Polygon from a bounding box (4-tuple).z,POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s)))r   ÚfloatÚintr   r   )ÚclsÚbboxÚx0Úy0Úx1Úy1Úzr
   r
   r   Ú	from_bbox:   s   
ÿÿ&zPolygon.from_bboxc           	         s–   |st  ¡ S g }|D ]}t|tƒr| |¡ q
| ˆ  |¡¡ q
ˆ  | d¡¡}|d }|r@t| ‡ fdd„|D ƒŽ }t|ƒ}nd }t  	||t
|ƒ¡S )Nr   r	   c                    s   g | ]}ˆ   |¡‘qS r
   )Ú_clone)Ú.0Úrr    r
   r   Ú
<listcomp>X   s    z+Polygon._create_polygon.<locals>.<listcomp>)ÚcapiÚcreate_empty_polygonr   r   ÚappendÚ_construct_ringr,   Úpopr   Úcreate_polygonr   )	r   ÚlengthÚitemsÚringsr.   Úshellr   ÚholesÚholes_paramr
   r    r   r   E   s   

zPolygon._create_polygonc                 C   s    t |tƒr
t |¡S t |j¡S ©N)r   r   r0   Ú
geom_cloneÚptr)r   Úgr
   r
   r   r,   _   s   

zPolygon._cloneúYParameter must be a sequence of LinearRings or objects that can initialize to LinearRingsc                 C   s4   t |tƒr|S zt|ƒ}|W S  ty   t|ƒ‚w )z1Try to construct a ring from the given parameter.)r   r   Ú	TypeError)r   ÚparamÚmsgÚringr
   r
   r   r3   e   s   
ÿzPolygon._construct_ringc                 C   s2   | j }| j}|  ||¡| _ |r|| _t |¡ d S r<   )r>   Úsridr   r0   Údestroy_geom)r   r6   r7   Úprev_ptrrE   r
   r
   r   Ú	_set_listp   s   zPolygon._set_listc                 C   s&   |dkr
t  | j¡S t  | j|d ¡S )a7  
        Return the ring at the specified index. The first index, 0, will
        always return the exterior ring.  Indices > 0 will return the
        interior ring at the given index (e.g., poly[1] and poly[2] would
        return the first and second interior ring, respectively).

        CAREFUL: Internal/External are not the same as Interior/Exterior!
        Return a pointer from the existing geometries for use internally by the
        object's methods. _get_single_external() returns a clone of the same
        geometry for use by external code.
        r   r	   )r0   Úget_extringr>   Úget_intring©r   Úindexr
   r
   r   Ú_get_single_internalz   s   zPolygon._get_single_internalc                 C   s   t t |  |¡¡| jd�S )N)rE   )r   r0   r=   rM   rE   rK   r
   r
   r   Ú_get_single_externalŒ   s   zPolygon._get_single_externalc                 C   s   t  | j¡S )z$Return the number of interior rings.)r0   Ú
get_nringsr>   r    r
   r
   r   r   “   s   zPolygon.num_interior_ringsc                 C   s   | d S )z%Get the exterior ring of the Polygon.r   r
   r    r
   r
   r   Ú_get_ext_ring™   s   zPolygon._get_ext_ringc                 C   s   || d< dS )z%Set the exterior ring of the Polygon.r   Nr
   )r   rD   r
   r
   r   Ú_set_ext_ring�   s   zPolygon._set_ext_ringc                    s   t ‡ fdd„ttˆ ƒƒD ƒƒS )z,Get the tuple for each ring in this Polygon.c                 3   s   � | ]}ˆ | j V  qd S r<   )r   ©r-   r   r    r
   r   Ú	<genexpr>¨   s   € z Polygon.tuple.<locals>.<genexpr>)r   r   r   r    r
   r    r   r   ¥   s   zPolygon.tuplec                    s0   d  ‡ fdd„tˆ jƒD ƒ¡}dˆ d j|f S )z.Return the KML representation of this Polygon.Ú c                 3   s"   � | ]}d ˆ |d  j  V  qdS )z%<innerBoundaryIs>%s</innerBoundaryIs>r	   N)ÚkmlrR   r    r
   r   rS   ®   s
   € ÿ
ÿzPolygon.kml.<locals>.<genexpr>z:<Polygon><outerBoundaryIs>%s</outerBoundaryIs>%s</Polygon>r   )Újoinr   r   rU   )r   Ú	inner_kmlr
   r    r   rU   «   s   þzPolygon.kml)r@   )Ú__name__Ú
__module__Ú__qualname__Ú
_minlengthr   r   r!   Úclassmethodr+   r   r,   r3   rH   rM   rN   r   Ú_set_single_rebuildÚ_set_singleÚ_assign_extended_slice_rebuildÚ_assign_extended_sliceÚpropertyr   rP   rQ   Úexterior_ringr9   r   ÚcoordsrU   Ú__classcell__r
   r
   r   r   r   	   s6    %


ÿ



r   N)Úctypesr   r   Údjango.contrib.gis.geosr   r0   Ú django.contrib.gis.geos.geometryr   Údjango.contrib.gis.geos.libgeosr   Ú"django.contrib.gis.geos.linestringr   r   r
   r
   r
   r   Ú<module>   s    