o
    h                     @   sp   d Z ddlZddlmZ ddlmZ dd Zd	d
 Zdd Zdd Z	dd Z
dddZdd ZdefddZdS )zO
Functions that make it easier to provide a default centering
for a view state
    N   )	ViewState   )is_pandas_dfc                 C   s   ||  ||   S )N )xx0r   r   V/var/www/vscode/kcb/lib/python3.10/site-packages/pydeck/data_utils/viewport_helpers.py_squared_diff
   s   r
   c                 C   s6   t | t |kstdttdd t| |D S )a-  Euclidean distance in n-dimensions

    Parameters
    ----------
    y : tuple of float
        A point in n-dimensions
    y1 : tuple of float
        A point in n-dimensions

    Examples
    --------
    >>> EPSILON = 0.001
    >>> euclidean((3, 6, 5), (7, -5, 1)) - 12.369 < EPSILON
    True
    z,Input coordinates must be of the same lengthc                 S   s   g | ]	\}}t ||qS r   )r
   ).0r   r   r   r   r	   
<listcomp>        zeuclidean.<locals>.<listcomp>)len	Exceptionmathsqrtsumzip)yy1r   r   r	   	euclidean   s   r   c                 C   s<   t dd | D t|  }t dd | D t|  }||fS )zGets centroid in a series of points

    Parameters
    ----------
    points : list of list of float
        List of (x, y) coordinates

    Returns
    -------
    tuple
        The centroid of a list of points
    c                 S      g | ]}t |d  qS r   floatr   pr   r   r	   r   0       z"geometric_mean.<locals>.<listcomp>c                 S   r   r   r   r   r   r   r	   r   1   r   )r   r   )pointsavg_xavg_yr   r   r	   geometric_mean#   s   r"   c                 C   sL   dd | D }dd | D }t |}t |}t|}t|}||f||ffS )a  Get the bounding box around the data,

    Parameters
    ----------
    points : list of list of float
        List of (x, y) coordinates

    Returns
    -------
    dict
        Dictionary containing the top left and bottom right points of a bounding box
    c                 S      g | ]}|d  qS r   r   r   r   r   r	   r   B       zget_bbox.<locals>.<listcomp>c                 S   r#   r   r   r   r   r   r	   r   C   r$   )maxmin)r   xsysmax_xmax_ymin_xmin_yr   r   r	   get_bbox5   s   r-   c                    s<    fdd| D }t |dd d}dd |D dt| S )a  Gets the k furthest points from the center

    Parameters
    ----------
    points : list of list of float
        List of (x, y) coordinates
    center : list of list of float
        Center point
    k : int
        Number of points

    Returns
    -------
    list
        Index of the k furthest points

    Todo
    ---
    Currently implemently naively, needs to be more efficient
    c                    s   g | ]	}|t | fqS r   )r   )r   ptcenterr   r	   r   `   r   z'k_nearest_neighbors.<locals>.<listcomp>c                 S   s   | d S )Nr   r   )r   r   r   r	   <lambda>a   s    z%k_nearest_neighbors.<locals>.<lambda>)keyc                 S   r#   r   r   )r   r   r   r   r	   r   b   r$   N)sortedint)r   r0   kpts_with_distance
sorted_ptsr   r/   r	   k_nearest_neighborsK   s   r8   c                 C   s2   |dkr| S t | }t|t|  }t| ||S )as  Computes the bounding box of the maximum zoom for the specified list of points

    Parameters
    ----------
    points : list of list of float
        List of (x, y) coordinates
    proportion : float, default 1
        Value between 0 and 1 representing the minimum proportion of data to be captured

    Returns
    -------
    list
        k nearest data points
    r   )r"   r   floorr   r8   )r   
proportioncentroid	n_to_keepr   r   r	   	get_n_pcte   s
   r=   c                 C   s   t | d d | d d t| d d | d d  }t | d d | d d t| d d | d d  }t ||}d}|dtdd k rId}|S tdt|td	 tdtd   }|dk rgd}|S )
aJ  Computes the zoom level of a lat/lng bounding box

    Parameters
    ----------
    bbox : list of list of float
        Northwest and southeast corners of a bounding box, given as two points in a list

    Returns
    -------
    int
        Zoom level of map in a WGS84 Mercator projection (e.g., like that of Google Maps)
    r   r   Ng     v@r         g       @)r%   r&   r   powr4   log)bboxlat_difflng_diffmax_diff
zoom_levelr   r   r	   bbox_to_zoom_level}   s   44
0rH   c                 C   sL   t | r
| jdd} tt| |}t|}t| }||d |d |d}|S )a\  Automatically computes a zoom level for the points passed in.

    Parameters
    ----------
    points : list of list of float or pandas.DataFrame
        A list of points
    view_propotion : float, default 1
        Proportion of the data that is meaningful to plot
    view_type : class constructor for pydeck.ViewState, default :class:`pydeck.bindings.view_state.ViewState`
        Class constructor for a viewport. In the current version of pydeck,
        users most likely do not have to modify this attribute.

    Returns
    -------
    pydeck.Viewport
        Viewport fitted to the data
    F)indexr   r   )latitude	longitudezoom)r   
to_recordsr-   r=   rH   r"   )r   view_proportion	view_typerC   rL   r0   instancer   r   r	   compute_view   s   rQ   r   )__doc__r   bindings.view_stater   type_checkingr   r
   r   r"   r-   r8   r=   rH   rQ   r   r   r   r	   <module>   s    
