o
    e@                    @   s   d Z ddlmZmZmZ ddlZddlZddlZddl	Z
ddlmZ ddlZddlZddlmZ ddlmZ ddlmZ dd	lmZmZmZmZmZ d
d ZG dd deZ G dd deZ!		dddZ"G dd deZ#G dd deZ$dS )zClasses dealing with polygons.    )print_functiondivisionabsolute_importN   )imgaug)random   )IAugmentable)normalize_shapeinterpolate_points_remove_out_of_image_fraction_project_coords__normalize_shift_argsc           
      C   sv   d}t | tsd}| g} |g}t| D ]\}}t|jD ]\}}||j|| j| |}	|	j|_qq|s9| d S | S )a?  Apply a polygon recoverer to input polygons in-place.

    Parameters
    ----------
    psois : list of imgaug.augmentables.polys.PolygonsOnImage or imgaug.augmentables.polys.PolygonsOnImage
        The possibly broken polygons, e.g. after augmentation.
        The `recoverer` is applied to them.

    psois_orig : list of imgaug.augmentables.polys.PolygonsOnImage or imgaug.augmentables.polys.PolygonsOnImage
        Original polygons that were later changed to `psois`.
        They are an extra input to `recoverer`.

    recoverer : imgaug.augmentables.polys._ConcavePolygonRecoverer
        The polygon recoverer used to repair broken input polygons.

    random_state : None or int or RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState
        An RNG to use during the polygon recovery.

    Returns
    -------
    list of imgaug.augmentables.polys.PolygonsOnImage or imgaug.augmentables.polys.PolygonsOnImage
        List of repaired polygons. Note that this is `psois`, which was
        changed in-place.

    TFr   )
isinstancelist	enumeratepolygonsrecover_fromexterior)
ZpsoisZ
psois_origZ	recovererrandom_stateZinput_was_listiZpsoijpolygonZpoly_rec r   ID:\Projects\ConvertPro\env\Lib\site-packages\imgaug/augmentables/polys.pyrecover_psois_   s    

	r   c                   @   s  e Zd ZdZddddZedd Zedd Zed	d
 Zedd Z	edd Z
edd Zedd Zedd Zedd Zdd Zdd ZdeddZdd Zd d! Zd"d# Zd$d% Zdfd'd(Zejd)d*d+d,d- Zd.d/ Zdgd1d2Zdhd3d4Z						did8d9Zd:d; Z	<	&djd=d>Zd?d@ Z dAdB Z!dCdD Z"dEdF Z#dkdGdHZ$dIdJ Z%dKdL Z&dldMdNZ'e(dddOdPZ)	<	QdmdRdSZ*	<	QdmdTdUZ+dmdVdWZ,dndXdYZ-dndZd[Z.d\d] Z/d^d_ Z0d`da Z1dbdc Z2dS )oPolygona  Class representing polygons.

    Each polygon is parameterized by its corner points, given as absolute
    x- and y-coordinates with sub-pixel accuracy.

    Parameters
    ----------
    exterior : list of imgaug.augmentables.kps.Keypoint or list of tuple of float or (N,2) ndarray
        List of points defining the polygon. May be either a ``list`` of
        :class:`~imgaug.augmentables.kps.Keypoint` objects or a ``list`` of
        ``tuple`` s in xy-form or a numpy array of shape (N,2) for ``N``
        points in xy-form.
        All coordinates are expected to be the absolute subpixel-coordinates
        on the image, given as ``float`` s, e.g. ``x=10.7`` and ``y=3.4`` for a
        point at coordinates ``(10.7, 3.4)``. Their order is expected to be
        clock-wise. They are expected to not be closed (i.e. first and last
        coordinate differ).

    label : None or str, optional
        Label of the polygon, e.g. a string representing the class.

    Nc                 C   s  ddl m} t|tr6|stjdtjd| _nEt|d |r*tdd |D | _n2tdd |D | _n&t	|sBJ d|f |j
d	krN|jd
 d	ksVJ d|jf t|| _t| jd	kovt| jdddf | jdddf }|r| jdd | _|| _dS )zCreate a new Polygon instance.r   Keypointr   r   dtypec                 S   s   g | ]}|j |jgqS r   xy.0pointr   r   r   
<listcomp>r   s    z$Polygon.__init__.<locals>.<listcomp>c                 S   s   g | ]
}|d  |d gqS r   r   r   r%   r   r   r   r(   w   s    zOExpected exterior to be a list of tuples (x, y) or an (N, 2) array, got type %sr   r   z\Expected exterior to be a list of tuples (x, y) or an (N, 2) array, got an array of shape %sN)imgaug.augmentables.kpsr   r   r   npzerosfloat32r   iais_np_arrayndimshapelenZallcloselabel)selfr   r4   r   	is_closedr   r   r   __init__e   s:   


&
zPolygon.__init__c                 C      | j S )a	  Alias for attribute ``exterior``.

        Added in 0.4.0.

        Returns
        -------
        ndarray
            An ``(N, 2)`` ``float32`` ndarray containing the coordinates of
            this polygon. This identical to the attribute ``exterior``.

        r   r5   r   r   r   coords   s   zPolygon.coordsc                 C      | j dddf S )zGet the x-coordinates of all points on the exterior.

        Returns
        -------
        (N,2) ndarray
            ``float32`` x-coordinates array of all points on the exterior.

        Nr   r9   r:   r   r   r   xx      
z
Polygon.xxc                 C   r<   )zGet the y-coordinates of all points on the exterior.

        Returns
        -------
        (N,2) ndarray
            ``float32`` y-coordinates array of all points on the exterior.

        Nr   r9   r:   r   r   r   yy   r>   z
Polygon.yyc                 C      t t | jS )a  Get the discretized x-coordinates of all points on the exterior.

        The conversion from ``float32`` coordinates to ``int32`` is done
        by first rounding the coordinates to the closest integer and then
        removing everything after the decimal point.

        Returns
        -------
        (N,2) ndarray
            ``int32`` x-coordinates of all points on the exterior.

        )r,   int32roundr=   r:   r   r   r   xx_int      zPolygon.xx_intc                 C   r@   )a  Get the discretized y-coordinates of all points on the exterior.

        The conversion from ``float32`` coordinates to ``int32`` is done
        by first rounding the coordinates to the closest integer and then
        removing everything after the decimal point.

        Returns
        -------
        (N,2) ndarray
            ``int32`` y-coordinates of all points on the exterior.

        )r,   rA   rB   r?   r:   r   r   r   yy_int   rD   zPolygon.yy_intc                 C   s   t | jdk r	dS |  jS )a  Estimate whether the polygon has a valid geometry.

        To to be considered valid, the polygon must be made up of at
        least ``3`` points and have a concave shape, i.e. line segments may
        not intersect or overlap. Multiple consecutive points are allowed to
        have the same coordinates.

        Returns
        -------
        bool
            ``True`` if polygon has at least ``3`` points and is concave,
            otherwise ``False``.

           F)r3   r   to_shapely_polygonis_validr:   r   r   r   rH      s   
zPolygon.is_validc                 C   s    t | jdk r	dS |  }|jS )z{Compute the area of the polygon.

        Returns
        -------
        number
            Area of the polygon.

        rF   g        )r3   r   rG   area)r5   polyr   r   r   rI      s   
zPolygon.areac                 C      | j }t|t| S )a  Compute the height of a bounding box encapsulating the polygon.

        The height is computed based on the two exterior coordinates with
        lowest and largest x-coordinates.

        Returns
        -------
        number
            Height of the polygon.

        )r?   maxmin)r5   r?   r   r   r   height      zPolygon.heightc                 C   rK   )a  Compute the width of a bounding box encapsulating the polygon.

        The width is computed based on the two exterior coordinates with
        lowest and largest x-coordinates.

        Returns
        -------
        number
            Width of the polygon.

        )r=   rL   rM   )r5   r=   r   r   r   width
  rO   zPolygon.widthc                 C   s   t | j||| _| S )ay  Project the polygon onto an image with different shape in-place.

        The relative coordinates of all points remain the same.
        E.g. a point at ``(x=20, y=20)`` on an image
        ``(width=100, height=200)`` will be projected on a new
        image ``(width=200, height=100)`` to ``(x=40, y=10)``.

        This is intended for cases where the original image is resized.
        It cannot be used for more complex changes (e.g. padding, cropping).

        Added in 0.4.0.

        Parameters
        ----------
        from_shape : tuple of int
            Shape of the original image. (Before resize.)

        to_shape : tuple of int
            Shape of the new image. (After resize.)

        Returns
        -------
        imgaug.augmentables.polys.Polygon
            Polygon object with new coordinates.
            The object may have been modified in-place.

        )r   r;   r   r5   Z
from_shapeZto_shaper   r   r   project_  s   zPolygon.project_c                 C      |   ||S )a  Project the polygon onto an image with different shape.

        The relative coordinates of all points remain the same.
        E.g. a point at ``(x=20, y=20)`` on an image
        ``(width=100, height=200)`` will be projected on a new
        image ``(width=200, height=100)`` to ``(x=40, y=10)``.

        This is intended for cases where the original image is resized.
        It cannot be used for more complex changes (e.g. padding, cropping).

        Parameters
        ----------
        from_shape : tuple of int
            Shape of the original image. (Before resize.)

        to_shape : tuple of int
            Shape of the new image. (After resize.)

        Returns
        -------
        imgaug.augmentables.polys.Polygon
            Polygon object with new coordinates.

        )deepcopyrR   rQ   r   r   r   project9  s   zPolygon.projectFc           	      C   st   t | jdksJ dg }| jD ]\}}|| d || d  }|| qt|}t|}|r8||| fS |S )a)  Find the index of the exterior point closest to given coordinates.

        "Closeness" is here defined based on euclidean distance.
        This method will raise an ``AssertionError`` if the exterior contains
        no points.

        Parameters
        ----------
        x : number
            X-coordinate around which to search for close points.

        y : number
            Y-coordinate around which to search for close points.

        return_distance : bool, optional
            Whether to also return the distance of the closest point.

        Returns
        -------
        int
            Index of the closest point.

        number
            Euclidean distance to the closest point.
            This value is only returned if `return_distance` was set
            to ``True``.

        r   zOCannot find the closest point on a polygon which's exterior contains no points.r   )r3   r   appendr,   sqrtargmin)	r5   r#   r$   return_distance	distancesx2y2distclosest_idxr   r   r   find_closest_point_indexT  s   

z Polygon.find_closest_point_indexc                 C   s4   |  |}t|dkr| jS | jtdd |D  S )a+  Compute the area of the BB that is outside of the image plane.

        Added in 0.4.0.

        Parameters
        ----------
        image : (H,W,...) ndarray or tuple of int
            Image dimensions to use.
            If an ``ndarray``, its shape will be used.
            If a ``tuple``, it is assumed to represent the image shape
            and must contain at least two integers.

        Returns
        -------
        float
            Total area of the bounding box that is outside of the image plane.
            Can be ``0.0``.

        r   c                 S      g | ]}|j qS r   )rI   r&   rJ   r   r   r   r(         z5Polygon.compute_out_of_image_area.<locals>.<listcomp>)clip_out_of_imager3   rI   sum)r5   imageZpolys_clippedr   r   r   compute_out_of_image_area~  s   
z!Polygon.compute_out_of_image_areac                 C   s*   | j }|dkr|  |S | || S )a  Compute fraction of polygon area outside of the image plane.

        This estimates ``f = A_ooi / A``, where ``A_ooi`` is the area of the
        polygon that is outside of the image plane, while ``A`` is the
        total area of the bounding box.

        Added in 0.4.0.

        Parameters
        ----------
        image : (H,W,...) ndarray or tuple of int
            Image dimensions to use.
            If an ``ndarray``, its shape will be used.
            If a ``tuple``, it is assumed to represent the image shape
            and must contain at least two integers.

        Returns
        -------
        float
            Fraction of the polygon area that is outside of the image
            plane. Returns ``0.0`` if the polygon is fully inside of
            the image plane or has zero points. If the polygon has an area
            of zero, the polygon is treated similarly to a :class:`LineString`,
            i.e. the fraction of the line that is outside the image plane is
            returned.

        r   )rI   to_line_stringcompute_out_of_image_fractionrf   )r5   re   rI   r   r   r   rh     s   z%Polygon.compute_out_of_image_fractionc                 C   s   | j |ddd S )a
  Estimate whether the polygon is fully inside an image plane.

        Parameters
        ----------
        image : (H,W,...) ndarray or tuple of int
            Image dimensions to use.
            If an ``ndarray``, its shape will be used.
            If a ``tuple``, it is assumed to represent the image shape and
            must contain at least two ``int`` s.

        Returns
        -------
        bool
            ``True`` if the polygon is fully inside the image area.
            ``False`` otherwise.

        Tfullypartlyis_out_of_imager5   re   r   r   r   is_fully_within_image     zPolygon.is_fully_within_imagec                 C   s   | j |ddd S )a  Estimate whether the polygon is at least partially inside an image.

        Parameters
        ----------
        image : (H,W,...) ndarray or tuple of int
            Image dimensions to use.
            If an ``ndarray``, its shape will be used.
            If a ``tuple``, it is assumed to represent the image shape and
            must contain at least two ``int`` s.

        Returns
        -------
        bool
            ``True`` if the polygon is at least partially inside the image area.
            ``False`` otherwise.

        TFri   rl   rn   r   r   r   is_partly_within_image  rp   zPolygon.is_partly_within_imageTc                 C   s^   t | jdkrtd|  }||rdS |j|dddr |S | |}t |dkr-|S |S )ag  Estimate whether the polygon is partially/fully outside of an image.

        Parameters
        ----------
        image : (H,W,...) ndarray or tuple of int
            Image dimensions to use.
            If an ``ndarray``, its shape will be used.
            If a ``tuple``, it is assumed to represent the image shape and
            must contain at least two ``int`` s.

        fully : bool, optional
            Whether to return ``True`` if the polygon is fully outside of the
            image area.

        partly : bool, optional
            Whether to return ``True`` if the polygon is at least partially
            outside fo the image area.

        Returns
        -------
        bool
            ``True`` if the polygon is partially/fully outside of the image
            area, depending on defined parameters.
            ``False`` otherwise.

        r   zXCannot determine whether the polygon is inside the image, because it contains no points.FTri   )r3   r   	Exceptionrg   ro   rm   rc   )r5   re   rj   rk   lspolysr   r   r   rm     s   

zPolygon.is_out_of_imagezPolygon.clip_out_of_image()z3clip_out_of_image() has the exactly same interface.)Zalt_funccommentc                 C   s
   |  |S )z>Cut off all parts of the polygon that are outside of an image.)rc   rn   r   r   r   cut_out_of_image  s   
zPolygon.cut_out_of_imagec                 C   s  ddl }t| jdkrg S t| jdv r9| jdd}||}t|dks'J t|dkr/g S | j|d jdgS t|rE|j	dd n|dd \}}| 
 }|jd	|df||fd|fg}||}	|jj|jj|jjj|jjf}
t|	|jjr|j|	g}	n6t|	|jjrn.t|	|
r|jg }	n"t|	|jjr|	jsJ g S t|	|| j td
t|	||| jf g }|	jD ]}|tj|| jd qg }|D ]3}d}d}| jD ]\}}|j||dd\}}|du s||k r|}|}q|dur||}|| q|S )aF  Cut off all parts of the polygon that are outside of an image.

        This operation may lead to new points being created.
        As a single polygon may be split into multiple new polygons, the result
        is always a list, which may contain more than one output polygon.

        This operation will return an empty list if the polygon is completely
        outside of the image plane.

        Parameters
        ----------
        image : (H,W,...) ndarray or tuple of int
            Image dimensions to use for the clipping of the polygon.
            If an ``ndarray``, its shape will be used.
            If a ``tuple``, it is assumed to represent the image shape and must
            contain at least two ``int`` s.

        Returns
        -------
        list of imgaug.augmentables.polys.Polygon
            Polygon, clipped to fall within the image dimensions.
            Returned as a ``list``, because the clipping can split the polygon
            into multiple parts. The list may also be empty, if the polygon was
            fully outside of the image plane.

        r   N)r   r   Fclosedr   r9   r   r   r   z}Got an unexpected result of type %s from Shapely for image (%d, %d) and polygon %s. This is an internal error. Please report.r4   Tr#   r$   rY   ) shapely.geometryr3   r   rg   rc   rT   r;   r/   r0   r2   rG   geometryr   intersection
LineStringZMultiLineStringr'   ZPointZ
MultiPointr   MultiPolygonGeometryCollectionZis_emptyprintrr   typegeomsrV   from_shapelyr4   r_   change_first_point_by_index)r5   re   shapelyrs   Z
ls_clippedhwZpoly_shapelyZ
poly_imageZmultipoly_inter_shapelyZignore_typesr   Zpoly_inter_shapelyZpolygons_reorderedr   Zbest_idxZ	best_distr#   r$   	point_idxr]   Zpolygon_reorderedr   r   r   rc   #  s   
( 









zPolygon.clip_out_of_imager   c                 C   s8   | j dddf  |7  < | j dddf  |7  < | S )at  Move this polygon along the x/y-axis in-place.

        The origin ``(0, 0)`` is at the top left of the image.

        Added in 0.4.0.

        Parameters
        ----------
        x : number, optional
            Value to be added to all x-coordinates. Positive values shift
            towards the right images.

        y : number, optional
            Value to be added to all y-coordinates. Positive values shift
            towards the bottom images.

        Returns
        -------
        imgaug.augmentables.polys.Polygon
            Shifted polygon.
            The object may have been modified in-place.

        Nr   r   r9   )r5   r#   r$   r   r   r   shift_  s   zPolygon.shift_c                 C   *   t ||||||d\}}|  j||dS )a  Move this polygon along the x/y-axis.

        The origin ``(0, 0)`` is at the top left of the image.

        Parameters
        ----------
        x : number, optional
            Value to be added to all x-coordinates. Positive values shift
            towards the right images.

        y : number, optional
            Value to be added to all y-coordinates. Positive values shift
            towards the bottom images.

        top : None or int, optional
            Deprecated since 0.4.0.
            Amount of pixels by which to shift this object *from* the
            top (towards the bottom).

        right : None or int, optional
            Deprecated since 0.4.0.
            Amount of pixels by which to shift this object *from* the
            right (towards the left).

        bottom : None or int, optional
            Deprecated since 0.4.0.
            Amount of pixels by which to shift this object *from* the
            bottom (towards the top).

        left : None or int, optional
            Deprecated since 0.4.0.
            Amount of pixels by which to shift this object *from* the
            left (towards the right).

        Returns
        -------
        imgaug.augmentables.polys.Polygon
            Shifted polygon.

        toprightbottomleftr"   r   rT   r   r5   r#   r$   r   r   r   r   r   r   r   shift     )
zPolygon.shiftr      r         ?r   c                 C   s:  dd }dd }|d| |d| |d|
 ||t |}||t |d }||t |d }|||d }|||}||	|}	|||
}|||
d	 }|jd
krgt|scJ dt|f |g}n|jd	kryt|ry|g|jd  }|dk rd}n|dkrd}|r| |rtdt| |jf |j	}|
t j}tjj| j| j|jd\}}t|dkr|dkrt ||||f< n|dkrnd| |||ddf  |t |  |||f< | jdd}| jdd}|j|||||d}|j|||	||d}|jt jkrt t |dd
|}|S |
|}|S )a  Draw the polygon on an image.

        Parameters
        ----------
        image : (H,W,C) ndarray
            The image onto which to draw the polygon. Usually expected to be
            of dtype ``uint8``, though other dtypes are also handled.

        color : iterable of int, optional
            The color to use for the whole polygon.
            Must correspond to the channel layout of the image. Usually RGB.
            The values for `color_face`, `color_lines` and `color_points`
            will be derived from this color if they are set to ``None``.
            This argument has no effect if `color_face`, `color_lines`
            and `color_points` are all set anything other than ``None``.

        color_face : None or iterable of int, optional
            The color to use for the inner polygon area (excluding perimeter).
            Must correspond to the channel layout of the image. Usually RGB.
            If this is ``None``, it will be derived from ``color * 1.0``.

        color_lines : None or iterable of int, optional
            The color to use for the line (aka perimeter/border) of the
            polygon.
            Must correspond to the channel layout of the image. Usually RGB.
            If this is ``None``, it will be derived from ``color * 0.5``.

        color_points : None or iterable of int, optional
            The color to use for the corner points of the polygon.
            Must correspond to the channel layout of the image. Usually RGB.
            If this is ``None``, it will be derived from ``color * 0.5``.

        alpha : float, optional
            The opacity of the whole polygon, where ``1.0`` denotes a
            completely visible polygon and ``0.0`` an invisible one.
            The values for `alpha_face`, `alpha_lines` and `alpha_points`
            will be derived from this alpha value if they are set to ``None``.
            This argument has no effect if `alpha_face`, `alpha_lines`
            and `alpha_points` are all set anything other than ``None``.

        alpha_face : None or number, optional
            The opacity of the polygon's inner area (excluding the perimeter),
            where ``1.0`` denotes a completely visible inner area and ``0.0``
            an invisible one.
            If this is ``None``, it will be derived from ``alpha * 0.5``.

        alpha_lines : None or number, optional
            The opacity of the polygon's line (aka perimeter/border),
            where ``1.0`` denotes a completely visible line and ``0.0`` an
            invisible one.
            If this is ``None``, it will be derived from ``alpha * 1.0``.

        alpha_points : None or number, optional
            The opacity of the polygon's corner points, where ``1.0`` denotes
            completely visible corners and ``0.0`` invisible ones.
            If this is ``None``, it will be derived from ``alpha * 1.0``.

        size : int, optional
            Size of the polygon.
            The sizes of the line and points are derived from this value,
            unless they are set.

        size_lines : None or int, optional
            Thickness of the polygon's line (aka perimeter/border).
            If ``None``, this value is derived from `size`.

        size_points : int, optional
            Size of the points in pixels.
            If ``None``, this value is derived from ``3 * size``.

        raise_if_out_of_image : bool, optional
            Whether to raise an error if the polygon is fully
            outside of the image. If set to ``False``, no error will be
            raised and only the parts inside the image will be drawn.

        Returns
        -------
        (H,W,C) ndarray
            Image with the polygon drawn on it. Result dtype is the same as the
            input dtype.

        c                 S   s    |d usJ d| t |f d S )Nz*Expected '%s' to not be None, got type %s.)r   )Zarg_name	arg_valuer   r   r   _assert_not_none2  s
   
z/Polygon.draw_on_image.<locals>._assert_not_nonec                 S   s   | d u r|S | S Nr   )vardefaultr   r   r   _default_to7  s   z*Polygon.draw_on_image.<locals>._default_tocoloralphasizeg      ?rF   r   zMGot a 2D image. Expected then 'color_face' to be a single number, but got %s.r*   {Gz?r   gGz?r   z.Cannot draw polygon %s on image with shape %s.r2   NFrw   T)r   r   r   raise_if_out_of_imager   )r,   arrayr1   r/   Zis_single_numberstrr2   rm   rr   r!   astyper.   skimagedrawr   rE   rC   r3   rg   Zdraw_lines_on_imageZdraw_points_on_imager   Zuint8ZcliprB   )r5   re   r   
color_facecolor_linescolor_pointsr   
alpha_facealpha_linesalpha_pointsr   
size_linessize_pointsr   r   r   Zinput_dtyperesultrrccZls_openZ	ls_closedr   r   r   draw_on_image  sv   [









zPolygon.draw_on_imagec                 C   s
  |j dv sJ d|jf t| jdkrtd|  }||}| j|dddr+|S | j}| j	}|t
| }|t
| }t
|}t
|}	tjj||||	fd\}
}t
j||	ft
jd	}d||
|f< |j d
krt
|ddddt
jf dd|jd f}|| S )a  Extract all image pixels within the polygon area.

        This method returns a rectangular image array. All pixels within
        that rectangle that do not belong to the polygon area will be filled
        with zeros (i.e. they will be black).
        The method will also zero-pad the image if the polygon is
        partially/fully outside of the image.

        Parameters
        ----------
        image : (H,W) ndarray or (H,W,C) ndarray
            The image from which to extract the pixels within the polygon.

        Returns
        -------
        (H',W') ndarray or (H',W',C) ndarray
            Pixels within the polygon. Zero-padded if the polygon is
            partially/fully outside of the image.

        )r   rF   z0Expected image of shape (H,W,[C]), got shape %s.r   zOPolygon must be made up of at least 3 points to extract its area from an image.TFri   r   r    rF   Nr   )r1   r2   r3   r   rr   to_bounding_boxextract_from_imagerm   rC   rE   r,   rM   rL   r   r   r   r-   booltileZnewaxis)r5   re   ZbbZbb_arear=   r?   Zxx_maskZyy_maskZheight_maskZ
width_maskZrr_faceZcc_facemaskr   r   r   r     s2   





,zPolygon.extract_from_image-C6?c                 C   s|   t | jdkrtd| j||dd\}}|dur9||kr9|s#|  S | j|ddf }td|d |d |f | |S )a4  
        Reorder exterior points so that the point closest to given x/y is first.

        This method takes a given ``(x,y)`` coordinate, finds the closest
        corner point on the exterior and reorders all exterior corner points
        so that the found point becomes the first one in the array.

        If no matching points are found, an exception is raised.

        Parameters
        ----------
        x : number
            X-coordinate of the point.

        y : number
            Y-coordinate of the point.

        max_distance : None or number, optional
            Maximum distance past which possible matches are ignored.
            If ``None`` the distance limit is deactivated.

        raise_if_too_far_away : bool, optional
            Whether to raise an exception if the closest found point is too
            far away (``True``) or simply return an unchanged copy if this
            object (``False``).

        Returns
        -------
        imgaug.augmentables.polys.Polygon
            Copy of this polygon with the new point order.

        r   z=Cannot reorder polygon points, because it contains no points.Tr{   NzFClosest found point (%.9f, %.9f) exceeds max_distance of %.9f exceededr   )r3   r   rr   r_   rT   r   )r5   r#   r$   max_distanceZraise_if_too_far_awayr^   Zclosest_distZclosest_pointr   r   r   change_first_point_by_coords  s    "

z$Polygon.change_first_point_by_coordsc                 C   s   d|  krt | jk sn J dt | j|f |dkr!|  S tj| j|dddf | jd|ddf fdd}| j|dS )a  
        Reorder exterior points so that the point with given index is first.

        This method takes a given index and reorders all exterior corner points
        so that the point with that index becomes the first one in the array.

        An ``AssertionError`` will be raised if the index does not match
        any exterior point's index or the exterior does not contain any points.

        Parameters
        ----------
        point_idx : int
            Index of the desired starting point.

        Returns
        -------
        imgaug.augmentables.polys.Polygon
            Copy of this polygon with the new point order.

        r   zWExpected index of new first point to be in the discrete interval [0..%d). Got index %d.NZaxisr9   )r3   r   rT   r,   concatenate)r5   r   r   r   r   r   r     s   *z#Polygon.change_first_point_by_indexc                 C   s@   t | jdkr	| S | jdd}||}|jdd }|| _| S )a  Derive a new poly with ``N`` interpolated points per edge in-place.

        See :func:`~imgaug.augmentables.lines.LineString.subdivide` for details.

        Added in 0.4.0.

        Parameters
        ----------
        points_per_edge : int
            Number of points to interpolate on each edge.

        Returns
        -------
        imgaug.augmentables.polys.Polygon
            Polygon with subdivided edges.
            The object may have been modified in-place.

        r   Trw   Nr*   )r3   r   rg   	subdivider;   )r5   points_per_edgers   Zls_subZexterior_subdividedr   r   r   
subdivide_  s   
zPolygon.subdivide_c                 C      |   |S )a  Derive a new polygon with ``N`` interpolated points per edge.

        See :func:`~imgaug.augmentables.lines.LineString.subdivide` for details.

        Added in 0.4.0.

        Parameters
        ----------
        points_per_edge : int
            Number of points to interpolate on each edge.

        Returns
        -------
        imgaug.augmentables.polys.Polygon
            Polygon with subdivided edges.

        rT   r   r5   r   r   r   r   r   /  s   zPolygon.subdividec                 C   s    ddl }|jdd | jD S )zConvert this polygon to a ``Shapely`` ``Polygon``.

        Returns
        -------
        shapely.geometry.Polygon
            The ``Shapely`` ``Polygon`` matching this polygon's exterior.

        r   Nc                 S      g | ]
}|d  |d fqS r)   r   r%   r   r   r   r(   P      z.Polygon.to_shapely_polygon.<locals>.<listcomp>)r|   r}   r   r   )r5   r   r   r   r   rG   C  s   
zPolygon.to_shapely_polygonc                 C   s   t | j||dS )aJ  Convert this polygon to a ``Shapely`` ``LineString`` object.

        Parameters
        ----------
        closed : bool, optional
            Whether to return the line string with the last point being
            identical to the first point.

        interpolate : int, optional
            Number of points to interpolate between any pair of two
            consecutive points. These points are added to the final line string.

        Returns
        -------
        shapely.geometry.LineString
            The ``Shapely`` ``LineString`` matching the polygon's exterior.

        )rx   interpolate)&_convert_points_to_shapely_line_stringr   )r5   rx   r   r   r   r   to_shapely_line_stringR  s   zPolygon.to_shapely_line_stringc                 C   s<   ddl m} | j}| j}|t|t|t|t|| jdS )zConvert this polygon to a bounding box containing the polygon.

        Returns
        -------
        imgaug.augmentables.bbs.BoundingBox
            Bounding box that tightly encapsulates the polygon.

        r   )BoundingBox)x1r[   y1r\   r4   )Zimgaug.augmentables.bbsr   r=   r?   rM   rL   r4   )r5   r   r=   r?   r   r   r   r   h  s   
zPolygon.to_bounding_boxc                    s    ddl m   fdd| jD S )zConvert this polygon's exterior to ``Keypoint`` instances.

        Returns
        -------
        list of imgaug.augmentables.kps.Keypoint
            Exterior vertices as :class:`~imgaug.augmentables.kps.Keypoint`
            instances.

        r   r   c                    s    g | ]} |d  |d dqS )r   r   r"   r   r%   r   r   r   r(     s     z(Polygon.to_keypoints.<locals>.<listcomp>)r+   r   r   r:   r   r   r   to_keypointsz  s   zPolygon.to_keypointsc                 C   s^   ddl m} |rt| jdkr|| j| jdS |tj| j| jddddf gdd| jdS )a  Convert this polygon's exterior to a ``LineString`` instance.

        Parameters
        ----------
        closed : bool, optional
            Whether to close the line string, i.e. to add the first point of
            the `exterior` also as the last point at the end of the line string.
            This has no effect if the polygon has a single point or zero
            points.

        Returns
        -------
        imgaug.augmentables.lines.LineString
            Exterior of the polygon as a line string.

        r   )r   r   rz   Nr   )Zimgaug.augmentables.linesr   r3   r   r4   r,   r   )r5   rx   r   r   r   r   rg     s   $zPolygon.to_line_stringc                 C   sv   ddl }t| |jjsJ dt| f | jdu p t| jjdk}|r)tg |dS t	dd | jjD }t||dS )a  Create a polygon from a ``Shapely`` ``Polygon``.

        .. note::

            This will remove any holes in the shapely polygon.

        Parameters
        ----------
        polygon_shapely : shapely.geometry.Polygon
             The shapely polygon.

        label : None or str, optional
            The label of the new polygon.

        Returns
        -------
        imgaug.augmentables.polys.Polygon
            A polygon with the same exterior as the ``Shapely`` ``Polygon``.

        r   NzDExpected the input to be a shapely.geometry.Polgon instance. Got %s.rz   c                 S   s   g | ]\}}||gqS r   r   )r&   r#   r$   r   r   r   r(     s    z(Polygon.from_shapely.<locals>.<listcomp>)
r|   r   r}   r   r   r   r3   r;   r,   r.   )Zpolygon_shapelyr4   r   Zhas_no_exteriorr   r   r   r   r     s   

zPolygon.from_shapely   c                 C   s   | j |||dS )a  Alias for :func:`Polygon.exterior_almost_equals`.

        Parameters
        ----------
        other : imgaug.augmentables.polys.Polygon or (N,2) ndarray or list of tuple
            See
            :func:`~imgaug.augmentables.polys.Polygon.exterior_almost_equals`.

        max_distance : number, optional
            See
            :func:`~imgaug.augmentables.polys.Polygon.exterior_almost_equals`.

        points_per_edge : int, optional
            See
            :func:`~imgaug.augmentables.polys.Polygon.exterior_almost_equals`.

        Returns
        -------
        bool
            Whether the two polygon's exteriors can be viewed as equal
            (approximate test).

        r   r   )exterior_almost_equalsr5   otherr   r   r   r   r   coords_almost_equals  s   zPolygon.coords_almost_equalsc                 C   sj   t |trtt|}nt|rt|}nt |ts%J dt|f | jddj	|jdd||dS )a  Estimate if this and another polygon's exterior are almost identical.

        The two exteriors can have different numbers of points, but any point
        randomly sampled on the exterior of one polygon should be close to the
        closest point on the exterior of the other polygon.

        .. note::

            This method works in an approximative way. One can come up with
            polygons with fairly different shapes that will still be estimated
            as equal by this method. In practice however this should be
            unlikely to be the case. The probability for something like that
            goes down as the interpolation parameter is increased.

        Parameters
        ----------
        other : imgaug.augmentables.polys.Polygon or (N,2) ndarray or list of tuple
            The other polygon with which to compare the exterior.
            If this is an ``ndarray``, it is assumed to represent an exterior.
            It must then have dtype ``float32`` and shape ``(N,2)`` with the
            second dimension denoting xy-coordinates.
            If this is a ``list`` of ``tuple`` s, it is assumed to represent
            an exterior. Each tuple then must contain exactly two ``number`` s,
            denoting xy-coordinates.

        max_distance : number, optional
            The maximum euclidean distance between a point on one polygon and
            the closest point on the other polygon. If the distance is exceeded
            for any such pair, the two exteriors are not viewed as equal. The
            points are either the points contained in the polygon's exterior
            ndarray or interpolated points between these.

        points_per_edge : int, optional
            How many points to interpolate on each edge.

        Returns
        -------
        bool
            Whether the two polygon's exteriors can be viewed as equal
            (approximate test).

        zbExpected 'other' to be a list of coordinates, a coordinate array or a single Polygon. Got type %s.Trw   r   )
r   r   r   r,   r.   r/   r0   r   rg   r   r   r   r   r   r     s   
,


zPolygon.exterior_almost_equalsc                 C   s    | j |j krdS | j|||dS )aS  
        Estimate if this polygon's and another's geometry/labels are similar.

        This is the same as
        :func:`~imgaug.augmentables.polys.Polygon.exterior_almost_equals` but
        additionally compares the labels.

        Parameters
        ----------
        other : imgaug.augmentables.polys.Polygon
            The other object to compare against. Expected to be a ``Polygon``.

        max_distance : float, optional
            See
            :func:`~imgaug.augmentables.polys.Polygon.exterior_almost_equals`.

        points_per_edge : int, optional
            See
            :func:`~imgaug.augmentables.polys.Polygon.exterior_almost_equals`.

        Returns
        -------
        bool
            ``True`` if the coordinates are almost equal and additionally
            the labels are equal. Otherwise ``False``.

        Fr   )r4   r   r   r   r   r   almost_equals   s
   zPolygon.almost_equalsc                 C   s   | j ||dS )a3  Create a shallow copy of this object.

        Parameters
        ----------
        exterior : list of imgaug.augmentables.kps.Keypoint or list of tuple or (N,2) ndarray, optional
            List of points defining the polygon. See
            :func:`~imgaug.augmentables.polys.Polygon.__init__` for details.

        label : None or str, optional
            If not ``None``, the ``label`` of the copied object will be set
            to this value.

        Returns
        -------
        imgaug.augmentables.polys.Polygon
            Shallow copy.

        r   r4   rT   r5   r   r4   r   r   r   copyA  s   zPolygon.copyc                 C   s2   t |du rt| jn||du r| jdS |dS )a  Create a deep copy of this object.

        Parameters
        ----------
        exterior : list of Keypoint or list of tuple or (N,2) ndarray, optional
            List of points defining the polygon. See
            `imgaug.augmentables.polys.Polygon.__init__` for details.

        label : None or str
            If not ``None``, the ``label`` of the copied object will be set
            to this value.

        Returns
        -------
        imgaug.augmentables.polys.Polygon
            Deep copy.

        Nr   )r   r,   r   r   r4   r   r   r   r   rT   V  s   zPolygon.deepcopyc                 C   
   | j | S )zGet the coordinate(s) with given indices.

        Added in 0.4.0.

        Returns
        -------
        ndarray
            xy-coordinate(s) as ``ndarray``.

        r9   r5   indicesr   r   r   __getitem__m     
zPolygon.__getitem__c                 C   
   t | jS )zIterate over the coordinates of this instance.

        Added in 0.4.0.

        Yields
        ------
        ndarray
            An ``(2,)`` ``ndarray`` denoting an xy-coordinate pair.

        )iterr   r:   r   r   r   __iter__z  r   zPolygon.__iter__c                 C      |   S r   __str__r:   r   r   r   __repr__     zPolygon.__repr__c                 C   s,   d dd | jD }d|t| j| jf S )N, c                 S   s    g | ]}d |d |d f qS )z(x=%.3f, y=%.3f)r   r   r   r%   r   r   r   r(     s    z#Polygon.__str__.<locals>.<listcomp>z#Polygon([%s] (%d points), label=%s))joinr   r3   r4   )r5   Z
points_strr   r   r   r     s   
zPolygon.__str__r   )FTFry   r   r   NNNNr   NNNr   NNNr   NNF)r   TFr   )T)r   r   NN)3__name__
__module____qualname____doc__r7   propertyr;   r=   r?   rC   rE   rH   rI   rN   rP   rR   rU   r_   rf   rh   ro   rq   rm   r/   
deprecatedrv   rc   r   r   r   r   r   r   r   r   rG   r   r   r   rg   staticmethodr   r   r   r   r   rT   r   r   r   r   r   r   r   r   r   M   s    
+









*"
8

i
1
 54
3!

'


;
!
r   c                   @   s  e Zd ZdZdd Zedd Zejdd Zedd Zd	d
 Z	dd Z
						d@ddZdAddZdAddZdd Zdd Zdd Zdd  ZdBd"d#ZdCd$d%Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 ZdDd2d3ZdDd4d5Zd6d7 Zd8d9 Zd:d; Zd<d= Zd>d? Z dS )EPolygonsOnImagea  Container for all polygons on a single image.

    Parameters
    ----------
    polygons : list of imgaug.augmentables.polys.Polygon
        List of polygons on the image.

    shape : tuple of int or ndarray
        The shape of the image on which the objects are placed.
        Either an image with shape ``(H,W,[C])`` or a ``tuple`` denoting
        such an image shape.

    Examples
    --------
    >>> import numpy as np
    >>> from imgaug.augmentables.polys import Polygon, PolygonsOnImage
    >>> image = np.zeros((100, 100))
    >>> polys = [
    >>>     Polygon([(0.5, 0.5), (100.5, 0.5), (100.5, 100.5), (0.5, 100.5)]),
    >>>     Polygon([(50.5, 0.5), (100.5, 50.5), (50.5, 100.5), (0.5, 50.5)])
    >>> ]
    >>> polys_oi = PolygonsOnImage(polys, shape=image.shape)

    c                 C   s   || _ t|| _d S r   )r   r
   r2   r5   r   r2   r   r   r   r7     s   zPolygonsOnImage.__init__c                 C   r8   )zGet the polygons in this container.

        Added in 0.4.0.

        Returns
        -------
        list of Polygon
            Polygons within this container.

        r   r:   r   r   r   items  s   zPolygonsOnImage.itemsc                 C   s
   || _ dS )zSet the polygons in this container.

        Added in 0.4.0.

        Parameters
        ----------
        value : list of Polygon
            Polygons within this container.

        Nr   )r5   valuer   r   r   r     s   
c                 C   s   t | jdkS )zEstimate whether this object contains zero polygons.

        Returns
        -------
        bool
            ``True`` if this object contains zero polygons.

        r   )r3   r   r:   r   r   r   empty  s   
zPolygonsOnImage.emptyc                 C   s^   t |}|dd | jdd kr|| _| S t| jD ]\}}|| j|| j|< q|| _| S )a  Project all polygons from one image shape to a new one in-place.

        Added in 0.4.0.

        Parameters
        ----------
        image : ndarray or tuple of int
            New image onto which the polygons are to be projected.
            May also simply be that new image's shape ``tuple``.

        Returns
        -------
        imgaug.augmentables.polys.PolygonsOnImage
            Object containing all projected polygons.
            The object and its items may have been modified in-place.

        r   r   )r
   r2   r   r   rR   r   )r5   re   Zon_shaper   itemr   r   r   on_  s   zPolygonsOnImage.on_c                 C   r   )a  Project all polygons from one image shape to a new one.

        Parameters
        ----------
        image : ndarray or tuple of int
            New image onto which the polygons are to be projected.
            May also simply be that new image's shape ``tuple``.

        Returns
        -------
        imgaug.augmentables.polys.PolygonsOnImage
            Object containing all projected polygons.

        )rT   r   rn   r   r   r   on     zPolygonsOnImage.onr   Nr   r   Fc                 C   s4   | j D ]}|j|||||||||	|
|||d}q|S )aQ  Draw all polygons onto a given image.

        Parameters
        ----------
        image : (H,W,C) ndarray
            The image onto which to draw the bounding boxes.
            This image should usually have the same shape as set in
            ``PolygonsOnImage.shape``.

        color : iterable of int, optional
            The color to use for the whole polygons.
            Must correspond to the channel layout of the image. Usually RGB.
            The values for `color_face`, `color_lines` and `color_points`
            will be derived from this color if they are set to ``None``.
            This argument has no effect if `color_face`, `color_lines`
            and `color_points` are all set anything other than ``None``.

        color_face : None or iterable of int, optional
            The color to use for the inner polygon areas (excluding perimeters).
            Must correspond to the channel layout of the image. Usually RGB.
            If this is ``None``, it will be derived from ``color * 1.0``.

        color_lines : None or iterable of int, optional
            The color to use for the lines (aka perimeters/borders) of the
            polygons. Must correspond to the channel layout of the image.
            Usually RGB. If this is ``None``, it will be derived
            from ``color * 0.5``.

        color_points : None or iterable of int, optional
            The color to use for the corner points of the polygons.
            Must correspond to the channel layout of the image. Usually RGB.
            If this is ``None``, it will be derived from ``color * 0.5``.

        alpha : float, optional
            The opacity of the whole polygons, where ``1.0`` denotes
            completely visible polygons and ``0.0`` invisible ones.
            The values for `alpha_face`, `alpha_lines` and `alpha_points`
            will be derived from this alpha value if they are set to ``None``.
            This argument has no effect if `alpha_face`, `alpha_lines`
            and `alpha_points` are all set anything other than ``None``.

        alpha_face : None or number, optional
            The opacity of the polygon's inner areas (excluding the perimeters),
            where ``1.0`` denotes completely visible inner areas and ``0.0``
            invisible ones.
            If this is ``None``, it will be derived from ``alpha * 0.5``.

        alpha_lines : None or number, optional
            The opacity of the polygon's lines (aka perimeters/borders),
            where ``1.0`` denotes completely visible perimeters and ``0.0``
            invisible ones.
            If this is ``None``, it will be derived from ``alpha * 1.0``.

        alpha_points : None or number, optional
            The opacity of the polygon's corner points, where ``1.0`` denotes
            completely visible corners and ``0.0`` invisible ones.
            Currently this is an on/off choice, i.e. only ``0.0`` or ``1.0``
            are allowed.
            If this is ``None``, it will be derived from ``alpha * 1.0``.

        size : int, optional
            Size of the polygons.
            The sizes of the line and points are derived from this value,
            unless they are set.

        size_lines : None or int, optional
            Thickness of the polygon lines (aka perimeter/border).
            If ``None``, this value is derived from `size`.

        size_points : int, optional
            The size of all corner points. If set to ``C``, each corner point
            will be drawn as a square of size ``C x C``.

        raise_if_out_of_image : bool, optional
            Whether to raise an error if any polygon is fully
            outside of the image. If set to False, no error will be raised and
            only the parts inside the image will be drawn.

        Returns
        -------
        (H,W,C) ndarray
            Image with drawn polygons.

        )r   r   r   r   r   r   r   r   r   r   r   r   )r   r   )r5   re   r   r   r   r   r   r   r   r   r   r   r   r   rJ   r   r   r   r   	  s"   
\zPolygonsOnImage.draw_on_imageTc                    s    fddj D _ S )a  Remove all polygons that are fully/partially OOI in-place.

        'OOI' is the abbreviation for 'out of image'.

        Added in 0.4.0.

        Parameters
        ----------
        fully : bool, optional
            Whether to remove polygons that are fully outside of the image.

        partly : bool, optional
            Whether to remove polygons that are partially outside of the image.

        Returns
        -------
        imgaug.augmentables.polys.PolygonsOnImage
            Reduced set of polygons. Those that are fully/partially
            outside of the given image plane are removed.
            The object and its items may have been modified in-place.

        c                    s"   g | ]}|j j d s|qS )ri   )rm   r2   ra   rj   rk   r5   r   r   r(     s    z8PolygonsOnImage.remove_out_of_image_.<locals>.<listcomp>r   r5   rj   rk   r   r   r   remove_out_of_image_w  s   z$PolygonsOnImage.remove_out_of_image_c                 C   rS   )a  Remove all polygons that are fully/partially outside of an image.

        Parameters
        ----------
        fully : bool, optional
            Whether to remove polygons that are fully outside of the image.

        partly : bool, optional
            Whether to remove polygons that are partially outside of the image.

        Returns
        -------
        imgaug.augmentables.polys.PolygonsOnImage
            Reduced set of polygons. Those that are fully/partially
            outside of the given image plane are removed.

        )rT   r  r   r   r   r   remove_out_of_image  s   z#PolygonsOnImage.remove_out_of_imagec                 C   s
   t | |S )a  Remove all Polys with an OOI fraction of ``>=fraction`` in-place.

        Added in 0.4.0.

        Parameters
        ----------
        fraction : number
            Minimum out of image fraction that a polygon has to have in
            order to be removed. A fraction of ``1.0`` removes only polygons
            that are ``100%`` outside of the image. A fraction of ``0.0``
            removes all polygons.

        Returns
        -------
        imgaug.augmentables.polys.PolygonsOnImage
            Reduced set of polygons, with those that had an out of image
            fraction greater or equal the given one removed.
            The object and its items may have been modified in-place.

        )r   r5   fractionr   r   r   remove_out_of_image_fraction_  s   
z-PolygonsOnImage.remove_out_of_image_fraction_c                 C   r   )a  Remove all Polys with an out of image fraction of ``>=fraction``.

        Added in 0.4.0.

        Parameters
        ----------
        fraction : number
            Minimum out of image fraction that a polygon has to have in
            order to be removed. A fraction of ``1.0`` removes only polygons
            that are ``100%`` outside of the image. A fraction of ``0.0``
            removes all polygons.

        Returns
        -------
        imgaug.augmentables.polys.PolygonsOnImage
            Reduced set of polygons, with those that had an out of image
            fraction greater or equal the given one removed.

        )r   r  r  r   r   r   remove_out_of_image_fraction  s   z,PolygonsOnImage.remove_out_of_image_fractionc                    s    fdd j D  _  S )a  Clip off all parts from all polygons that are OOI in-place.

        'OOI' is the abbreviation for 'out of image'.

        .. note::

            The result can contain fewer polygons than the input did. That
            happens when a polygon is fully outside of the image plane.

        .. note::

            The result can also contain *more* polygons than the input
            did. That happens when distinct parts of a polygon are only
            connected by areas that are outside of the image plane and hence
            will be clipped off, resulting in two or more unconnected polygon
            parts that are left in the image plane.

        Added in 0.4.0.

        Returns
        -------
        imgaug.augmentables.polys.PolygonsOnImage
            Polygons, clipped to fall within the image dimensions.
            The count of output polygons may differ from the input count.
            The object and its items may have been modified in-place.

        c                    s"   g | ]}|  jD ]}|q
qS r   )rc   r2   )r&   rJ   Zpoly_clippedr:   r   r   r(     s    
z6PolygonsOnImage.clip_out_of_image_.<locals>.<listcomp>r   r:   r   r:   r   clip_out_of_image_  s   
z"PolygonsOnImage.clip_out_of_image_c                 C   s   |    S )aE  Clip off all parts from all polygons that are outside of an image.

        .. note::

            The result can contain fewer polygons than the input did. That
            happens when a polygon is fully outside of the image plane.

        .. note::

            The result can also contain *more* polygons than the input
            did. That happens when distinct parts of a polygon are only
            connected by areas that are outside of the image plane and hence
            will be clipped off, resulting in two or more unconnected polygon
            parts that are left in the image plane.

        Returns
        -------
        imgaug.augmentables.polys.PolygonsOnImage
            Polygons, clipped to fall within the image dimensions.
            The count of output polygons may differ from the input count.

        )r   r  r:   r   r   r   rc     s   z!PolygonsOnImage.clip_out_of_imager   c                 C   s,   t | jD ]\}}|j||d| j|< q| S )aE  Move the polygons along the x/y-axis in-place.

        The origin ``(0, 0)`` is at the top left of the image.

        Added in 0.4.0.

        Parameters
        ----------
        x : number, optional
            Value to be added to all x-coordinates. Positive values shift
            towards the right images.

        y : number, optional
            Value to be added to all y-coordinates. Positive values shift
            towards the bottom images.

        Returns
        -------
        imgaug.augmentables.polys.PolygonsOnImage
            Shifted polygons.

        r"   )r   r   r   )r5   r#   r$   r   rJ   r   r   r   r     s   zPolygonsOnImage.shift_c                 C   r   )a  Move the polygons along the x/y-axis.

        The origin ``(0, 0)`` is at the top left of the image.

        Parameters
        ----------
        x : number, optional
            Value to be added to all x-coordinates. Positive values shift
            towards the right images.

        y : number, optional
            Value to be added to all y-coordinates. Positive values shift
            towards the bottom images.

        top : None or int, optional
            Deprecated since 0.4.0.
            Amount of pixels by which to shift all objects *from* the
            top (towards the bottom).

        right : None or int, optional
            Deprecated since 0.4.0.
            Amount of pixels by which to shift all objects *from* the
            right (towads the left).

        bottom : None or int, optional
            Deprecated since 0.4.0.
            Amount of pixels by which to shift all objects *from* the
            bottom (towards the top).

        left : None or int, optional
            Deprecated since 0.4.0.
            Amount of pixels by which to shift all objects *from* the
            left (towards the right).

        Returns
        -------
        imgaug.augmentables.polys.PolygonsOnImage
            Shifted polygons.

        r   r"   r   r   r   r   r   r   +  r   zPolygonsOnImage.shiftc                 C   s(   t | jD ]\}}||| j|< q| S a?  Interpolate ``N`` points on each polygon.

        Added in 0.4.0.

        Parameters
        ----------
        points_per_edge : int
            Number of points to interpolate on each edge.

        Returns
        -------
        imgaug.augmentables.polys.PolygonsOnImage
            Subdivided polygons.

        )r   r   r   )r5   r   r   rJ   r   r   r   r   X  s   zPolygonsOnImage.subdivide_c                 C   r   r  r   r   r   r   r   r   l  r   zPolygonsOnImage.subdividec                 C   s,   | j rtjdtjdS tdd | jD S )zConvert all polygon coordinates to one array of shape ``(N,2)``.

        Added in 0.4.0.

        Returns
        -------
        (N, 2) ndarray
            Array containing all xy-coordinates of all polygons within this
            instance.

        r   r    c                 S   r`   r   r9   ra   r   r   r   r(     rb   z/PolygonsOnImage.to_xy_array.<locals>.<listcomp>)r   r,   r-   r.   r   r   r:   r   r   r   to_xy_array~  s   zPolygonsOnImage.to_xy_arrayc                 C   s   t j|t jd}|jd dks#|jdkr|jd dks#J d|jf d}| jD ]4}t|j}|| t|ksIJ dt|tdd | jD f ||||  |jd	d	d
f< ||7 }q(|t|ksmJ dt||f | S )a  Modify the corner coordinates of all polygons in-place.

        .. note::

            This currently expects that `xy` contains exactly as many
            coordinates as the polygons within this instance have corner
            points. Otherwise, an ``AssertionError`` will be raised.

        .. warning::

            This does not validate the new coordinates or repair the resulting
            polygons. If bad coordinates are provided, the result will be
            invalid polygons (e.g. self-intersections).

        Added in 0.4.0.

        Parameters
        ----------
        xy : (N, 2) ndarray or iterable of iterable of number
            XY-Coordinates of ``N`` corner points. ``N`` must match the
            number of corner points in all polygons within this instance.

        Returns
        -------
        PolygonsOnImage
            This instance itself, with updated coordinates.
            Note that the instance was modified in-place.

        r    r   r   r*   z7Expected input array to have shape (N,2), got shape %s.zpReceived fewer points than there are corner points in the exteriors of all polygons. Got %d points, expected %d.c                 S   s   g | ]}t |jqS r   )r3   r   )r&   pr   r   r   r(         z7PolygonsOnImage.fill_from_xy_array_.<locals>.<listcomp>N.zExpected to get exactly as many xy-coordinates as there are points in the exteriors of all polygons within this instance. Got %d points, could only assign %d points.)	r,   r   r.   r2   r1   r   r3   r   rd   )r5   ZxycounterrJ   Z	nb_pointsr   r   r   fill_from_xy_array_  s0   (


z#PolygonsOnImage.fill_from_xy_array_c                 C   sJ   ddl m} | jr|g | jdS tjdd | jD dd}|j|| jdS )a  Convert the polygons to one ``KeypointsOnImage`` instance.

        Added in 0.4.0.

        Returns
        -------
        imgaug.augmentables.kps.KeypointsOnImage
            A keypoints instance containing ``N`` coordinates for a total
            of ``N`` points in all exteriors of the polygons within this
            container. Order matches the order in ``polygons``.

        r   )KeypointsOnImager   c                 S   r`   r   r9   ra   r   r   r   r(     rb   z9PolygonsOnImage.to_keypoints_on_image.<locals>.<listcomp>r   r   ) r  r   r2   r,   r   r   Zfrom_xy_array)r5   r  	exteriorsr   r   r   to_keypoints_on_image  s   z%PolygonsOnImage.to_keypoints_on_imagec           	      C   s   | j }dd |D }tdd |D }t|j|ks%J d|t|jf | }d}|D ]!}|j}|||t| ddf |ddddf< |t|7 }q-|j| _| S )a4  Invert the output of ``to_keypoints_on_image()`` in-place.

        This function writes in-place into this ``PolygonsOnImage``
        instance.

        Added in 0.4.0.

        Parameters
        ----------
        kpsoi : imgaug.augmentables.kps.KeypointsOnImages
            Keypoints to convert back to polygons, i.e. the outputs
            of ``to_keypoints_on_image()``.

        Returns
        -------
        PolygonsOnImage
            Polygons container with updated coordinates.
            Note that the instance is also updated in-place.

        c                 S   r`   r   r9   ra   r   r   r   r(     rb   zAPolygonsOnImage.invert_to_keypoints_on_image_.<locals>.<listcomp>c                 S      g | ]}t |qS r   r3   )r&   r   r   r   r   r(         z Expected %d coordinates, got %d.r   N)r   rd   r3   Z	keypointsr	  r   r2   )	r5   Zkpsoirt   r  Znb_points_expZxy_arrr  rJ   r   r   r   r   invert_to_keypoints_on_image_  s    
,z-PolygonsOnImage.invert_to_keypoints_on_image_c                 C   s2   |du r| j dd }|du rt| j}t||S )a  Create a shallow copy of this object.

        Parameters
        ----------
        polygons : None or list of imgaug.augmentables.polys.Polygons, optional
            List of polygons on the image.
            If not ``None``, then the ``polygons`` attribute of the copied
            object will be set to this value.

        shape : None or tuple of int or ndarray, optional
            The shape of the image on which the objects are placed.
            Either an image with shape ``(H,W,[C])`` or a tuple denoting
            such an image shape.
            If not ``None``, then the ``shape`` attribute of the copied object
            will be set to this value.

        Returns
        -------
        imgaug.augmentables.polys.PolygonsOnImage
            Shallow copy.

        Nr   tupler2   r   r   r   r   r   r     s
   

zPolygonsOnImage.copyc                 C   s4   |du rdd | j D }|du rt| j}t||S )a  Create a deep copy of this object.

        Parameters
        ----------
        polygons : None or list of imgaug.augmentables.polys.Polygons, optional
            List of polygons on the image.
            If not ``None``, then the ``polygons`` attribute of the copied
            object will be set to this value.

        shape : None or tuple of int or ndarray, optional
            The shape of the image on which the objects are placed.
            Either an image with shape ``(H,W,[C])`` or a tuple denoting
            such an image shape.
            If not ``None``, then the ``shape`` attribute of the copied object
            will be set to this value.

        Returns
        -------
        imgaug.augmentables.polys.PolygonsOnImage
            Deep copy.

        Nc                 S   s   g | ]}|  qS r   r   ra   r   r   r   r(   9  r  z,PolygonsOnImage.deepcopy.<locals>.<listcomp>r  r   r   r   r   rT      s
   

zPolygonsOnImage.deepcopyc                 C   r   )zGet the polygon(s) with given indices.

        Added in 0.4.0.

        Returns
        -------
        list of imgaug.augmentables.polys.Polygon
            Polygon(s) with given indices.

        r   r   r   r   r   r   @  r   zPolygonsOnImage.__getitem__c                 C   r   )a  Iterate over the polygons in this container.

        Added in 0.4.0.

        Yields
        ------
        Polygon
            A polygon in this container.
            The order is identical to the order in the polygon list
            provided upon class initialization.

        )r   r   r:   r   r   r   r   M  s   
zPolygonsOnImage.__iter__c                 C   r   )zGet the number of items in this instance.

        Added in 0.4.0.

        Returns
        -------
        int
            Number of items in this instance.

        )r3   r   r:   r   r   r   __len__\  r   zPolygonsOnImage.__len__c                 C   r   r   r   r:   r   r   r   r   i  r   zPolygonsOnImage.__repr__c                 C   s   dt | j| jf S )NzPolygonsOnImage(%s, shape=%s))r   r   r2   r:   r   r   r   r   l  s   zPolygonsOnImage.__str__r   r   ry   r   r   )!r   r   r   r   r7   r   r   setterr   r   r   r   r  r  r  r  r  rc   r   r   r   r   r	  r  r  r  r   rT   r   r   r  r   r   r   r   r   r   r     sN    




n
"

-8
&
 r   Fc                 C   sp   dd l }t| dkrtdt| f dd | D }|dkr#t||}|r2t| dkr2||d  |j|S )Nr   r   ziConversion to shapely line string requires at least two points, but points input contains only %d points.c                 S   r   r)   r   r%   r   r   r   r(   {  r   z:_convert_points_to_shapely_line_string.<locals>.<listcomp>)r|   r3   rr   r   rV   r}   r   )pointsrx   r   r   Zpoints_tuplesr   r   r   r   q  s   
r   c                   @   s~   e Zd Z		dddZdddZdd	 Zd
d Zedd Zdd Z	edd Z
		dddZdd Zedd Zdd ZdS ) _ConcavePolygonRecovererr   r   c                 C   sL   || _ || _|| _|| _d| _d| _d| _d| _d| _d| _	d| _
d| _d S )NK   d   rF   r   r   iP     )threshold_duplicate_pointsnoise_strengthoversamplingmax_segment_differenceoversample_up_to_n_points_maxfit_n_changes_maxfit_n_iters_maxfit_max_dist_first_iterfit_max_dist_other_iters fit_n_candidates_before_sort_max$limit_coords_values_for_inter_searchdecimals)r5   r  r   r!  r"  r   r   r   r7     s   
z!_ConcavePolygonRecoverer.__init__r   c           	         s  t |tst|r|jdkr|jd dksJ dt|f t|dks)J d|j|d}|j	r4|S t
|}|d}| |}| ||d }| ||d }| j|| jd}| jd urk| jdkrk| ||}| || |  |d } fd	d
|D }|j|dS )Nr   r   z8Expected exterior as list or (N,2) ndarray, got type %s.rF   z=Cannot recover a concave polygon from less than three points.r9   r   )r*  c                       g | ]} | qS r   r   r&   idxZnew_exterior_interr   r   r(         z9_ConcavePolygonRecoverer.recover_from.<locals>.<listcomp>)r   r   r/   r0   r1   r2   r   r3   rT   rH   iarandomZRNGZ	duplicate$_remove_consecutive_duplicate_points_fix_polygon_is_line_jitter_duplicate_points_generate_intersection_pointsr*  r!  _oversample_intersection_points_insert_intersection_points_fit_best_valid_polygon)	r5   Znew_exteriorZold_polygonr   r   Zrsssegment_add_pointsZnew_exterior_concave_idsZnew_exterior_concaver   r.  r   r     sJ   





z%_ConcavePolygonRecoverer.recover_fromc                 C   s   g }|D ]&}|r%t jt |t |d  }|| jk }|s$|| q|| qt|dkrRt jt |d t |d  }|| jk }|rP|dd n|}|S )Nr*   r   r   )r,   linalgnormr.   r  rV   r3   )r5   r  r   r'   r]   Zis_samer   r   r   r1    s$   


z=_ConcavePolygonRecoverer._remove_consecutive_duplicate_pointsc                 C   s   t |dksJ dt |f | j}| |rF|j| |t |dfdtj}dd t||D }|d }|dksAJ d	|f | |s|S )
NrF   zfCan only fix line-like polygons with an exterior containing at least 3 points. Got one with %d points.r   r   c                 S   s0   g | ]\}}|d  |d   |d |d  fqS r)   r   )r&   r'   Znoise_ir   r   r   r(   	  s    $zA_ConcavePolygonRecoverer._fix_polygon_is_line.<locals>.<listcomp>
   r   +Expected noise strength to be >0, got %.4f.)r3   r   _is_polygon_lineuniformr   r,   r.   zip)r5   r   r   r   noiser   r   r   r2    s.   



z-_ConcavePolygonRecoverer._fix_polygon_is_linec                 C   sl   t ddg}|d }t }|dd  D ]}t |t | }t||}|t|d  qt|dkS )Nr   r   i  )r,   r.   setr/   Zangle_between_vectorsaddintr3   )clsr   Zvec_downpoint1ZanglesZpoint2ZvecZangler   r   r   r>  	  s   z)_ConcavePolygonRecoverer._is_polygon_linec           
         s    fdd} j }|dksJ d|f |d d  }d}|sg||}t|rc|j j   j t|dfdtj}t|D ] \}}	|	r]|| d || d  || d || d  f||< q=|d	9 }nd
}|r|S )Nc              	      sD  t t}t| D ]7\}}tt|d d j  }tt|d d j  }dD ]}dD ]}||| || f | q/q+q	dgt	|  }|D ]U}	||	 }
t|
D ]J\}}|
| }| | }|| reqTt
|d t	|
D ]/}|
| }| | }|| r}qnt|d |d  d |d |d  d  }| jk rd||< qnqTqJ|S )Nr   g?r   )r*   r   r   Fr   T)collectionsdefaultdictr   r   rD  r,   rB   r  rV   r3   rangerW   )Zexterior_with_duplicatesZ
points_mapr   r'   r#   r$   Z
direction0Z
direction1
duplicateskey
candidatesZp0_idxpoint0r   Zp1_idxrF  r]   r:   r   r   _find_duplicates	  sJ   

zK_ConcavePolygonRecoverer._jitter_duplicate_points.<locals>._find_duplicatesr   r=  Fr   r;  r   r<  T)r   anyr?  r3   r   r,   r.   r   )
r5   r   r   rN  r   	convergedrJ  rA  r   Zis_duplicater   r:   r   r3  	  s6   %


z1_ConcavePolygonRecoverer._jitter_duplicate_pointsc                 C   s   t |dksJ dt |f tj|tjd}tjt |dftjd}||d d ddf< |dd ddf |ddddf< |dddf |dddf< tjj|d d ddf |d d ddf  dd	}t|S )
NrF   zLNeed at least 3 points on the exterior to compute the circumference. Got %d.r    r  r   r   r   r*   r   )r3   r,   r   r.   r-   r9  r:  rd   )rE  r  Zpoints_matrixrZ   r   r   r   _calculate_circumference[	  s   $(
z1_ConcavePolygonRecoverer._calculate_circumferenceTr  c                    s  t t t jt jd}|| jk}|r)td| jf  dd tt	D S t
r2ttts@J dtf tdd D sWJ ddd	d D  t	d
kr_g S  fddtt	D }d
dlm} z||}W n+ ty }	 ztdt|	f  t  dd tt	D W  Y d }	~	S d }	~	ww dd tt	|D }
|D ]\}}|r|d
 g}|D ]\}}g }g }|D ]U\}}t j|t | }t j|t | }t j|t | }t j|t | }t|| || }|| |t j|d
 |d
  |d |d  f qt |}|| jk r=tt |}|
| ||| f qtd qqg }tt	|
D ]8}dd |
| D }dd |
| D }t	|dk rn|| qLtt||dd d}|dd |D  qL|S )Nr    aw  Encountered during polygon repair a polygon with extremely large coordinate values beyond %d. Will skip intersection point computation for that polygon. This avoids exceptions and is -- due to the extreme distortion -- likely pointless anyways (i.e. the polygon is already broken beyond repair). Try using weaker augmentation parameters to avoid such large coordinate values.c                 S      g | ]}g qS r   r   r&   _r   r   r   r(   {	      zJ_ConcavePolygonRecoverer._generate_intersection_points.<locals>.<listcomp>z;Expected 'exterior' to be a list or a ndarray. Got type %s.c                 S   s   g | ]}t |d kqS )r   r  r%   r   r   r   r(   	      zFExpected 'exterior' to contain (x,y) coordinate pairs. Got lengths %s.r   c                 S      g | ]}t t|qS r   )r   r3   r%   r   r   r   r(   	  rV  r   c                    s   g | ]>}t t| d   t t| d  ft t|d t  d   t t|d t  d  ffqS r)   )r,   rB   floatr3   r&   r   r*  r   r   r   r(   	  s    )isect_segments_include_segmentszhEncountered exception %s during polygon repair in segment intersection computation. Will skip that step.c                 S   rR  r   r   rS  r   r   r   r(   	  rU  c                 S   rR  r   r   rS  r   r   r   r(   	  rU  r   z^Couldn't find fitting segment in _generate_intersection_points(). Ignoring intersection point.c                 S      g | ]}|d  qS r   r   r&   tr   r   r   r(   	  r  c                 S   r\  )r   r   r^  r   r   r   r(   	  r  r   c                 S   s   | d S )Nr   r   )r_  r   r   r   <lambda>	  s    zH_ConcavePolygonRecoverer._generate_intersection_points.<locals>.<lambda>rK  c                 S   s   g | ]\}}|qS r   r   )r&   a_br   r   r   r(   	  r  )r,   rL   absr   r.   r)  r/   warnrI  r3   r0   r   r   r   allr   Z'imgaug.external.poly_point_isect_py2py3r[  rr   r   	traceback	print_excr9  r:  rM   rV   r"  rD  rX   sortedr@  )r5   r   Zone_point_per_intersectionr*  Zlargest_valueZtoo_large_valuessegmentsr[  ZintersectionsexcZsegments_add_pointsr'   Zassociated_segmentsZseg_inter_p0Zseg_inter_p1ZdiffsdistsZseg_p0Zseg_p1Z	dist_p0p0Z	dist_p1p1Z	dist_p0p1Z	dist_p1p0diffZmin_diffr-  Zsegment_add_points_sortedr  Zbothr   rZ  r   r4  i	  s   


"



z6_ConcavePolygonRecoverer._generate_intersection_pointsc              	   C   s  | j d u s
| j dkr|S dd tt|D }t|}t|D ]\}}t|| D ]\}}|d |d  |d |d  f}	|dkrHd| j  g}
n| j d| j  g}
|
D ]}|d ||	d   |d ||	d   f}|| | qR|| | |}|t|| d k}|r||d t|  }|d |d  |d |d  f}	|| |d | j |	d   |d | j |	d   f || d }|t|| 7 }|| jkr|    S q)q|S )Nr   c                 S   rR  r   r   rS  r   r   r   r(   	  s    zL_ConcavePolygonRecoverer._oversample_intersection_points.<locals>.<listcomp>r   r   r*   )r!  rI  r3   r   rV   r#  )r5   r   r8  Zsegment_add_points_sorted_oversZn_pointsr   lastr   p_inter	directionZ
oversampler]   Z
point_overZis_last_in_groupZexterior_pointr   r   r   r5  	  sJ   
 
"z8_ConcavePolygonRecoverer._oversample_intersection_pointsc                 C   sl   t |t |ksJ dt |t |f g }t|D ]\}}|| }|| || D ]}|| q+q|S )NzExpected one entry in 'segment_add_points' for every point in the exterior. Got %d (segment_add_points) and %d (exterior) entries instead.)r3   r   rV   )rE  r   r8  Zexterior_interpr   rM  ro  r   r   r   r6  
  s   
z4_ConcavePolygonRecoverer._insert_intersection_pointsc              
      s  t dk rd S dd }t}|jrtt S tj}t|j	fddt
t D }d}d}d}	|	sg  dd	 tD }
ttt|t tttt t|g}tj|tj|jd d
ftjdfdd}|d d df |d d df k }|d d ddf |d d ddf< ||df ||df< ||df ||d
f< t|d d d
f |d d df  t |d d d
f  |d d df  |d d df< | j}|dkr| j}||d d df |k }| jd uot || jk}|r|| |d| j }|D ]>}|d |d |
 }}|d t   }| }| }|dkr8d}n| ||} |d |f qtt  }t| fddd}| jd urk|d | j }d}t }|D ]i} | d  | d f|vrڇfddtD d }d d  }||d  tfdd|D }|jrd}|fdd|D }|d7 }|| jkr̈  S |f |f qr|s|dkrd}	|d7 }| j d uo|| j k}|r	 S |	r;S )Nr   c                 S   s   |d |d  }|d |d  }t || d  || d   |d |d   |d |d   }t|d |d  }|dkrTt| d |d  d | d |d  d  S || S )Nr   r   r   )rd  r,   rW   )r'   
line_startline_endZx_diffZy_diffnumZdenr   r   r   _compute_distance_point_to_line#
  s"   zY_ConcavePolygonRecoverer._fit_best_valid_polygon.<locals>._compute_distance_point_to_linec                    s   g | ]}| vr|qS r   r   rY  )points_keptr   r   r(   7
  rV  zD_ConcavePolygonRecoverer._fit_best_valid_polygon.<locals>.<listcomp>r   Fc                 S   s   i | ]\}}||qS r   r   r&   r   r   r   r   r   
<dictcomp>M
  s    
zD_ConcavePolygonRecoverer._fit_best_valid_polygon.<locals>.<dictcomp>rF   r    r   r   r  c                    s    |  d  |  d fS )Nr   rF   r   )r-  )rL  r   r   r`  
  rV  zB_ConcavePolygonRecoverer._fit_best_valid_polygon.<locals>.<lambda>ra  c                    s   g | ]
\}}| kr|qS r   r   rv  )point_kept_idxr   r   r(   
  s
    c                    r+  r   r   r,  )r  r   r   r(   
  r/  Tc                    s   g | ]}| kr|qS r   r   )r&   r   )point_left_idxr   r   r(   
  s
    )!r3   r   rH   smxrangescipyZspatialZ
ConvexHullr   ZverticesrI  r   r,   Z	transposer   rA   repeatr   r-   r2   minimumr'  r&  r(  shufflerV   Zarangeri  r$  rB  insertrC  r%  )r5   r  r   rt  rJ   ZhullZpoints_left	iterationZ	n_changesrP  Zpoint_kept_idx_to_posZcombosr   Zmax_distZcandidate_rowsZdo_limitrowZin_points_kept_posZsegment_start_idxZsegment_end_idxZsegment_startZsegment_endZ	dist_euclZcandidate_idsrH   doneZcandidate_idxZin_points_kept_idxZpoints_kept_hypothesisZpoly_hypothesisZhas_reached_iters_maxr   )rL  rx  ry  r  ru  r   r7  
  s   
 $&






  z0_ConcavePolygonRecoverer._fit_best_valid_polygonN)r   r   r   r   r]  )Tr  )r   r   r   r7   r   r1  r2  classmethodr>  r3  rQ  r4  r5  r6  r7  r   r   r   r   r    s&    

.3

A

w/
r  c                   @   s&   e Zd ZdZdd ZedddZdS )r   z
    Class that represents several polygons.

    Parameters
    ----------
    geoms : list of imgaug.augmentables.polys.Polygon
        List of the polygons.

    c                 C   sD   t |dkstdd |D sJ dddd |D  || _dS )z#Create a new MultiPolygon instance.r   c                 S   s   g | ]}t |tqS r   )r   r   r&   elr   r   r   r(   
  r  z)MultiPolygon.__init__.<locals>.<listcomp>z>Expected 'geoms' to a list of Polygon instances. Got types %s.r   c                 S   r  r   )r   r  r   r   r   r(   
  r  N)r3   rf  r   r   )r5   r   r   r   r   r7   
  s   
zMultiPolygon.__init__Nc                    s   ddl t| jjrt fdd| jD S t| jjr(ttj|  dgS t| jjjrWt	fdd| jD sKJ dd
d	d | jD  t fd
d| jD S tdt| f )aW  Create a MultiPolygon from a shapely object.

        This also creates all necessary ``Polygon`` s contained in this
        ``MultiPolygon``.

        Parameters
        ----------
        geometry : shapely.geometry.MultiPolygon or shapely.geometry.Polygon or shapely.geometry.collection.GeometryCollection
            The object to convert to a MultiPolygon.

        label : None or str, optional
            A label assigned to all Polygons within the MultiPolygon.

        Returns
        -------
        imgaug.augmentables.polys.MultiPolygon
            The derived MultiPolygon.

        r   Nc                       g | ]	}t j| d qS rz   r   r   ra   rz   r   r   r(   
      z-MultiPolygon.from_shapely.<locals>.<listcomp>rz   c                    s   g | ]	}t | jjqS r   )r   r}   r   ra   )r   r   r   r(     r  zPExpected the geometry collection to only contain shapely polygons. Got types %s.r   c                 S   rW  r   )r   r   )r&   vr   r   r   r(     rV  c                    r  r  r  ra   rz   r   r   r(     r  zUnknown datatype '%s'. Expected shapely.geometry.Polygon or shapely.geometry.MultiPolygon or shapely.geometry.collections.GeometryCollection.)r|   r   r}   r   r   r   r   Z
collectionr   rf  r   rr   r   )r}   r4   r   )r4   r   r   r   
  s4   
zMultiPolygon.from_shapelyr   )r   r   r   r   r7   r   r   r   r   r   r   r   
  s
    		r   r   )%r   
__future__r   r   r   rg  rG  numpyr,   Zscipy.spatial.distancer|  Z	six.movesmovesrz  Zskimage.drawr   Zskimage.measurer  r   r/   r   r0  baser	   utilsr
   r   r   r   r   r   objectr   r   r   r  r   r   r   r   r   <module>   sP    6          Q     b
    H