a
    J5dH                     @   sD   d dl mZmZ ddlmZ ddlmZ G dd dZddd	Zd
S )    )heappopheappush   )TopologicalError)Pointc                   @   sP   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd ZdS )CellaN  A `Cell`'s centroid property is a potential solution to finding the pole
    of inaccessibility for a given polygon. Rich comparison operators are used
    for sorting `Cell` objects in a priority queue based on the potential
    maximum distance of any theoretical point within a cell to a given
    polygon's exterior boundary.
    c                 C   s>   || _ || _|| _t||| _| || _| j|d  | _d S )Ng;f?)xyhr   centroid_distdistancemax_distance)selfr   r	   r
   polygon r   X/var/www/html/django/DPS/env/lib/python3.9/site-packages/shapely/algorithms/polylabel.py__init__   s    zCell.__init__c                 C   s   | j |j kS Nr   r   otherr   r   r   __lt__   s    zCell.__lt__c                 C   s   | j |j kS r   r   r   r   r   r   __le__   s    zCell.__le__c                 C   s   | j |j kS r   r   r   r   r   r   __eq__"   s    zCell.__eq__c                 C   s   | j |j kS r   r   r   r   r   r   __ne__%   s    zCell.__ne__c                 C   s   | j |j k S r   r   r   r   r   r   __gt__(   s    zCell.__gt__c                 C   s   | j |j kS r   r   r   r   r   r   __ge__+   s    zCell.__ge__c                 C   sF   | | j}| j|j}|jD ]}t|| j|}q |r@|S | S )zSigned distance from Cell centroid to polygon outline. The returned
        value is negative if the point is outside of the polygon exterior
        boundary.
        )containsr   r   ZexteriorZ	interiorsmin)r   r   Zinsider   Zinteriorr   r   r   r   .   s    
z
Cell._distN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r   r   r   r      s   r         ?c                 C   s  | j std| j\}}}}|| }|| }t||}|d }	g }
| jjd \}}t||d| }t||d  ||d  d| }|j|jkr|}|}||k r|}||k rt|
t||	 ||	 |	|  ||7 }q||7 }q|
rt	|
}|j|jkr|}|j
|j |krq|jd }	t|
t|j|	 |j|	 |	|  t|
t|j|	 |j|	 |	|  t|
t|j|	 |j|	 |	|  t|
t|j|	 |j|	 |	|  q|jS )a  Finds pole of inaccessibility for a given polygon. Based on
    Vladimir Agafonkin's https://github.com/mapbox/polylabel

    Parameters
    ----------
    polygon : shapely.geometry.Polygon
    tolerance : int or float, optional
                `tolerance` represents the highest resolution in units of the
                input geometry that will be considered for a solution. (default
                value is 1.0).

    Returns
    -------
    shapely.geometry.Point
        A point representing the pole of inaccessibility for the given input
        polygon.

    Raises
    ------
    shapely.errors.TopologicalError
        If the input polygon is not a valid geometry.

    Example
    -------
    >>> from shapely import LineString
    >>> polygon = LineString([(0, 0), (50, 200), (100, 100), (20, 50),
    ... (-100, -20), (-150, -200)]).buffer(100)
    >>> polylabel(polygon, tolerance=10).wkt
    'POINT (59.35615556364569 121.83919629746435)'
    zInvalid polygong       @r   r   )Zis_validr   Zboundsr   r   Zcoordsr   r   r   r   r   r
   r   r	   )r   Z	toleranceZminxZminyZmaxxZmaxywidthheight	cell_sizer
   Z
cell_queuer   r	   Z	best_cellZ	bbox_cellcellr   r   r   	polylabel<   s@    



   "r)   N)r$   )	heapqr   r   errorsr   Zgeometryr   r   r)   r   r   r   r   <module>   s   5