a
    ßÁdH  ã                   @   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   ú_/home/yuhw/wc/wire-cell-python/wcpy/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 )z¨Signed 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 rÌt|
t||	 ||	 |	| ƒƒ ||7 }qž||7 }q’|
�r”t	|
ƒ}|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ÚheightZ	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