a
    û1$e¥  ã                   @   s:   d dl Zddd„Zdd„ Zdd„ ZdZd	d
„ Zdd„ ZdS )é    Né   c                 C   sD   | j  ¡ |  ¡  ¡ B rtdƒ‚| j}t|||ƒ\}}t|||ƒ}|S )a'  
    Calculate the distance along a Hilbert curve.

    The distances are calculated for the midpoints of the geometries in the
    GeoDataFrame.

    Parameters
    ----------
    geoms : GeometryArray
    total_bounds : 4-element array
        Total bounds of geometries - array
    level : int (1 - 16), default 16
        Determines the precision of the curve (points on the curve will
        have coordinates in the range [0, 2^level - 1]).

    Returns
    -------
    np.ndarray
        Array containing distances along the Hilbert curve

    zTHilbert distance cannot be computed on a GeoSeries with empty or missing geometries.)Zis_emptyÚanyZisnaÚ
ValueErrorÚboundsÚ_continuous_to_discrete_coordsÚ_encode)ZgeomsÚtotal_boundsÚlevelr   ÚxÚyZ	distances© r   úY/var/www/html/django/DPS/env/lib/python3.9/site-packages/geopandas/tools/hilbert_curve.pyÚ_hilbert_distance   s    ÿr   c                 C   s´   d| d }| dd…df | dd…df  d }| dd…df | dd…df  d }|du r€t  |¡t  |¡t  |¡t  |¡f}|\}}}}	t|||f|ƒ}
t|||	f|ƒ}|
|fS )a§  
    Calculates mid points & ranges of geoms and returns
    as discrete coords

    Parameters
    ----------

    bounds : Bounds of each geometry - array

    p : The number of iterations used in constructing the Hilbert curve

    total_bounds : Total bounds of geometries - array

    Returns
    -------
    Discrete two-dimensional numpy array
    Two-dimensional array Array of hilbert distances for each geom

    é   é   Nr   g       @é   )ÚnpZnanminZnanmaxÚ_continuous_to_discrete)r   r	   r   Zside_lengthZx_midsZy_midsZxminZyminZxmaxZymaxZx_intZy_intr   r   r   r   *   s    $$ür   c                 C   sZ   |d |d  }|dkr(t j| t jdS | |d  ||  }t j|d||d | t j¡S )aP  
    Convert a continuous one-dimensional array to discrete integer values
    based their ranges

    Parameters
    ----------
    vals : Array of continuous values

    val_range : Tuple containing range of continuous values

    n : Number of discrete values

    Returns
    -------
    One-dimensional array of discrete ints

    r   r   ©Zdtype)Úout)r   Z
zeros_likeÚuint32ZclipZastype)ÚvalsZ	val_rangeÚnÚwidthÚresr   r   r   r   W   s    r   c                 C   sD   | | d> B d@ } | | d> B d@ } | | d> B d@ } | | d> B d@ } | S )	Né   iÿ ÿ é   ir   i3333r   iUUUUr   )r
   r   r   r   Ú_interleavez   s
    r   c                 C   s€  t j|dd}t j|dd}| tkr,tdƒ‚|d|  > }|d|  > }||A }d|A }d||B A }||dA @ }||d? B }|d? |A }|d? ||d? @ A |A }	||d? @ |d? A |A }
| ¡ }| ¡ }|	 ¡ }|
 ¡ }||d? @ ||d? @ A }||d? @ |||A d? @ A }|	||d? @ ||d? @ A N }	|
||d? @ ||A |d? @ A N }
| ¡ }| ¡ }|	 ¡ }|
 ¡ }||d? @ ||d? @ A }||d? @ |||A d? @ A }|	||d? @ ||d? @ A N }	|
||d? @ ||A |d? @ A N }
| ¡ }| ¡ }|	 ¡ }|
 ¡ }|	||d	? @ ||d	? @ A N }	|
||d	? @ ||A |d	? @ A N }
|	|	d? A }|
|
d? A }||A }|d||B A B }t|ƒd> t|ƒB d
d|   ? S )Nr   r   zLevel out of ranger   iÿÿ  r   r   r   r   é    )r   ZasarrayÚ	MAX_LEVELr   Úcopyr   )r	   r
   r   ÚaÚbÚcÚdÚAÚBÚCÚDZi0Úi1r   r   r   r   ‚   sR       r   )Nr   )Únumpyr   r   r   r   r   r   r   r   r   r   r   Ú<module>   s   
&- 