
 X                 @   s   d  d l  m Z m Z d  d l m Z d  d l m Z d  d l m	 Z	 m
 Z
 d  d l m Z d  d l m Z d  d l m Z Gd d	   d	 e  Z d
 S)    )byrefc_uint)
prototypes)GEOSGeometry)GEOM_PTRget_pointer_arr)
LinearRing)six)rangec                   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   S)!Polygon   c                s  | s/ t  t |   j |  j d d  |  d S| d } | d d  } t |  } | d k r t | d t t f  r t | d  d k r f  } d } q t | d d t  r | d } t |  } q n  |  j | d | f |  } t  t |   j | |  d S)a  
        Initializes 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   )	superr   __init___create_polygonlen
isinstancetuplelistr   )selfargskwargsZext_ringZ
init_holesn_holesZpolygon)	__class__ M/home/ubuntu/projects/ifolica/build/django/django/contrib/gis/geos/polygon.pyr      s    %
%	
zPolygon.__init__c             c   s*   x# t  t |    D] } |  | Vq Wd S)z'Iterates over each ring in the polygon.N)r
   r   )r   ir   r   r   __iter__4   s    zPolygon.__iter__c             C   s   |  j  d S)z,Returns the number of rings in this Polygon.r   )num_interior_rings)r   r   r   r   __len__9   s    zPolygon.__len__c             C   s   | \ } } } } xS | D]K } t  | t j t f  s t d | | | | | | | | | | f
  Sq Wt | | f | | f | | f | | f | | f f  S)z3Constructs a Polygon from a bounding box (4-tuple).z,POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s)))r   r	   integer_typesfloatr   r   )clsZbboxZx0Zy0Zx1y1zr   r   r   	from_bbox=   s    *zPolygon.from_bboxc       
      C   s   | s t  j   Sg  } xC | D]; } t | t  rB | j |  q | j |  j |   q W|  j | j d   } | d } | r t |  } xB t	 |  D]+ \ } } |  j |  | | <t
 |  }	 q Wn d  }	 t  j | |	 t |   S)Nr   r   )capiZcreate_empty_polygonr   r   append_construct_ring_clonepopr   	enumerater   Zcreate_polygonr   )
r   lengthitemsZringsrshellr   Zholesr   Zholes_paramr   r   r   r   H   s     

zPolygon._create_polygonc             C   s0   t  | t  r t j |  St j | j  Sd  S)N)r   r   r%   
geom_cloneptr)r   gr   r   r   r(   d   s    zPolygon._clonezYParameter must be a sequence of LinearRings or objects that can initialize to LinearRingsc             C   sL   t  | t  r | Sy t |  } | SWn t k
 rG t |   Yn Xd S)zGHelper routine for trying to construct a ring from the given parameter.N)r   r   	TypeError)r   parammsgringr   r   r   r'   j   s    zPolygon._construct_ringc             C   sJ   |  j  } |  j } |  j | |  |  _  | r9 | |  _ n  t j |  d  S)N)r0   sridr   r%   Zdestroy_geom)r   r+   r,   Zprev_ptrr6   r   r   r   	_set_listu   s    		zPolygon._set_listc             C   s7   | d k r t  j |  j  St  j |  j | d  Sd S)aN  
        Returns 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!
        _get_single_internal returns 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   N)r%   Zget_extringr0   Zget_intring)r   indexr   r   r   _get_single_internal   s    zPolygon._get_single_internalc             C   s%   t  t j |  j |   d |  j S)Nr6   )r   r%   r/   r9   r6   )r   r8   r   r   r   _get_single_external   s    zPolygon._get_single_externalc             C   s   t  j |  j  S)z%Returns the number of interior rings.)r%   Z
get_nringsr0   )r   r   r   r   r      s    zPolygon.num_interior_ringsc             C   s   |  d S)z&Gets 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&Sets the exterior ring of the Polygon.r   Nr   )r   r5   r   r   r   _set_ext_ring   s    zPolygon._set_ext_ringc                s)   t    f d d   t t     D  S)z-Gets the tuple for each ring in this Polygon.c             3   s   |  ] }   | j  Vq d  S)N)r   ).0r   )r   r   r   	<genexpr>   s    z Polygon.tuple.<locals>.<genexpr>)r   r
   r   )r   r   )r   r   r      s    zPolygon.tuplec                s@   d j    f d d   t   j  D  } d   d j | f S)z/Returns the KML representation of this Polygon. c             3   s$   |  ] } d    | d j  Vq d S)z%<innerBoundaryIs>%s</innerBoundaryIs>r   N)kml)r=   r   )r   r   r   r>      s   zPolygon.kml.<locals>.<genexpr>z:<Polygon><outerBoundaryIs>%s</outerBoundaryIs>%s</Polygon>r   )joinr
   r   r@   )r   Z	inner_kmlr   )r   r   r@      s    zPolygon.kml)__name__
__module____qualname__Z
_minlengthr   r   r   classmethodr$   r   r(   r'   r7   r9   r:   r   Z_set_single_rebuildZ_set_singleZ_assign_extended_slice_rebuildZ_assign_extended_slicepropertyr   r;   r<   Zexterior_ringr.   r   Zcoordsr@   r   r   )r   r   r      s*   &

		r   N)ctypesr   r   Zdjango.contrib.gis.geosr   r%   Z django.contrib.gis.geos.geometryr   Zdjango.contrib.gis.geos.libgeosr   r   Z"django.contrib.gis.geos.linestringr   Zdjango.utilsr	   Zdjango.utils.six.movesr
   r   r   r   r   r   <module>   s   