o
    <g6                     @   sD   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 )    )
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), (10, 0), (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   R/var/www/html/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__0   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__5   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_bbox9   s   &zPolygon.from_bboxc                    s   |st  S g }|D ]}t|tr|| q
| | q
 |d}|d }|r<t|  fdd|D  }nd }t |||S )Nr   r   c                    s   g | ]}  |qS r   )_clone).0rr   r   r   
<listcomp>Y   s    z+Polygon._create_polygon.<locals>.<listcomp>)	capicreate_empty_polygonr   r   append_construct_ringr*   popcreate_polygon)r   lengthitemsringsr,   shellr   holes_paramr   r   r   r   F   s   
zPolygon._create_polygonc                 C   s    t |tr
t|S t|jS N)r   r   r.   
geom_cloneptr)r   gr   r   r   r*   _   s   

zPolygon._cloneYParameter must be a sequence of LinearRings or objects that can initialize to LinearRingsc                 C   s0   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msgr   r   r   r1   e   s   
	
zPolygon._construct_ringc                 C   s2   | j }| j}| ||| _ |r|| _t| d S r9   )r;   sridr   r.   destroy_geom)r   r4   r5   prev_ptrrA   r   r   r   	_set_listu   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   )r.   get_extringr;   get_intringr   indexr   r   r   _get_single_internal   s   zPolygon._get_single_internalc                 C   s   t t| || jdS )N)rA   )r   r.   r:   rI   rA   rG   r   r   r   _get_single_external   s   zPolygon._get_single_externalc                 C   s   t | jS )z$Return the number of interior rings.)r.   
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   ringr   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 r9   )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)kmlrO   r   r   r   rP      s
    
zPolygon.kml.<locals>.<genexpr>z:<Polygon><outerBoundaryIs>%s</outerBoundaryIs>%s</Polygon>r   )joinr   r   rR   )r   	inner_kmlr   r   r   rR      s   zPolygon.kml)r=   )__name__
__module____qualname__
_minlengthr
   r   r   classmethodr)   r   r*   r1   rD   rI   rJ   r   _set_single_rebuild_set_single_assign_extended_slice_rebuild_assign_extended_slicepropertyr   rL   rN   exterior_ringr7   r   coordsrR   __classcell__r   r   r   r   r      s6    &






r   N)
django.contrib.gis.geosr   r.    django.contrib.gis.geos.geometryr   django.contrib.gis.geos.libgeosr   "django.contrib.gis.geos.linestringr   r   r   r   r   r   <module>   s
    