3
®?d]Ý  ã               @   sJ   d dl mZmZ d dlmZ ddlmZmZ G dd„ deƒZ	ddd	„Z
d
S )é   )ÚPointÚ
LineString)ÚTopologicalErroré    )ÚheappushÚheappopc               @   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|ƒ| _| j|d  | _d S )NgÍ;fž ö?)ÚxÚyÚhr   ÚcentroidÚ_distÚdistanceÚmax_distance)Úselfr	   r
   r   Úpolygon© r   ú?/tmp/pip-build-op5p2y5_/shapely/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 )N)r   )r   r   r   r   r   Ú__le__   s    zCell.__le__c             C   s   | j |j kS )N)r   )r   r   r   r   r   Ú__eq__    s    zCell.__eq__c             C   s   | j |j kS )N)r   )r   r   r   r   r   Ú__ne__#   s    zCell.__ne__c             C   s   | j |j k S )N)r   )r   r   r   r   r   Ú__gt__&   s    zCell.__gt__c             C   s   | j |j kS )N)r   )r   r   r   r   r   Ú__ge__)   s    zCell.__ge__c             C   s.   |j | jƒ}| jjt|jjƒƒ}|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   r   ZexteriorÚcoords)r   r   Zinsider   r   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   sn  | j stdƒ‚| j\}}}}t|| || ƒ}|d }g }| jjd \}	}
t|	|
d| ƒ}|}	xJ|	|k r¦|}
x0|
|k ršt|t|	| |
| || ƒƒ |
|7 }
qlW |	|7 }	q^W x¾|�rft|ƒ}|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ªW |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.geos.TopologicalError
        If the input polygon is not a valid geometry.

    Example
    -------
    >>> polygon = LineString([(0, 0), (50, 200), (100, 100), (20, 50),
    ... (-100, -20), (-150, -200)]).buffer(100)
    >>> label = polylabel(polygon, tolerance=10)
    >>> label.wkt
    'POINT (59.35615556364569 121.8391962974644)'
    zInvalid polygong       @r   )Zis_validr   ZboundsÚminr   r   r   r   r   r   r   r   r	   r
   )r   Z	toleranceZminxZminyZmaxxZmaxyZ	cell_sizer   Z
cell_queuer	   r
   Z	best_cellÚcellr   r   r   Ú	polylabel8   s6    


   $r%   N)r"   )Zgeometryr   r   Zgeosr   Úheapqr   r   Úobjectr   r%   r   r   r   r   Ú<module>   s   2