o
    JÉfT  ã                   @   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   úh/home/deployuser/azure_apps/autowriter/venv/lib/python3.10/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__   ó   zCell.__lt__c                 C   s   | j |j kS r   r   r   r   r   r   Ú__le__   r   zCell.__le__c                 C   s   | j |j kS r   r   r   r   r   r   Ú__eq__"   r   zCell.__eq__c                 C   s   | j |j kS r   r   r   r   r   r   Ú__ne__%   r   zCell.__ne__c                 C   s   | j |j k S r   r   r   r   r   r   Ú__gt__(   r   zCell.__gt__c                 C   s   | j |j kS r   r   r   r   r   r   Ú__ge__+   r   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   ÚexteriorÚ	interiorsÚmin)r   r   Úinsider   Ú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rG|}|}||k rq|}||k rit|
t||	 ||	 |	| ƒƒ ||7 }||k sS||7 }||k sM|
rÏt	|
ƒ}|j|jkr|}|j
|j |krˆqq|jd }	t|
t|j|	 |j|	 |	| ƒƒ t|
t|j|	 |j|	 |	| ƒƒ t|
t|j|	 |j|	 |	| ƒƒ t|
t|j|	 |j|	 |	| ƒƒ |
ss|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   é   )Úis_validr   Úboundsr!   r
   Úcoordsr   r   r   r   r   r	   r   r   )r   Ú	toleranceÚminxÚminyÚmaxxÚmaxyÚwidthÚheightÚ	cell_sizer	   Ú
cell_queuer   r   Ú	best_cellÚ	bbox_cellÚcellr   r   r   Ú	polylabel<   sF   
þû
    ïr9   N)r(   )	Úheapqr   r   Úshapely.errorsr   Úshapely.geometryr   r   r9   r   r   r   r   Ú<module>   s
    5