a
    `mN                     @   s   d dl mZmZmZ d dlZd dlZd dlmZ d dlZ	d dl
Zd dlZd dlmZ d dlmZ d dlmZ d dlZzd dlmZ W n ey   edZY n0 zd dlZW n ey   dZY n0 zd dlmZ W n ey   edZY n0 G dd	 d	eZe	j d
d
dZ!e	j d
d
d
dZ"e#dd e$de	j%& D Z'G dd de(Z)G dd de(Z*dd Z+dd Z,dd Z-dd Z.dd Z/dd Z0dGd d!Z1d"d# Z2dHd%d&Z3dId)d*Z4d+d, Z5dJd0d1Z6d2d3 Z7d4d5 Z8d6d7 Z9ed8d9 d:d;d< Z:d=d> Z;d?d@ Z<dAdB Z=dCdD Z>dEdF Z?dS )K    )absolute_importdivisionprint_functionN)getmro)memoize)	DataArray)RaggedDtype)GeometryDtypec                   @   s   e Zd ZdZdS )VisibleDeprecationWarningzVisible deprecation warning.

    By default, python will not show deprecation warnings, so this class
    can be used when a very visible warning is helpful, for example because
    the usage is most likely a user bug.
    N)__name__
__module____qualname____doc__ r   r   /lib/python3.9/site-packages/datashader/utils.pyr
   "   s   r
   T)nopythonnogil)r   r   Zparallelc                 C   s   g | ]}t |qS r   )int).0xr   r   r   
<listcomp>/       r   z([0-9]+)\.([0-9]+)\.([0-9]+)c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )ExprzBase class for expression-like objects.

    Implements hashing and equality checks. Subclasses should implement an
    ``inputs`` attribute/property, containing a tuple of everything that fully
    defines that expression.
    c                 C   s   t t| |  fS N)hashtype_hashable_inputsselfr   r   r   __hash__;   s    zExpr.__hash__c                 C   s    t | t |u o|  | kS r   )r   r   r   otherr   r   r   __eq__>   s    zExpr.__eq__c                 C   s
   | |k S r   r   r    r   r   r   __ne__B   s    zExpr.__ne__c                 C   s\   g }| j D ]H}t|ttfr,|t| q
t|tjrH||  q
|| q
t|S )zt
        Return a version of the inputs tuple that is suitable for hashing and
        equality comparisons
        )	Zinputs
isinstancelistsetappendtuplenpZndarraytobytes)r   resultZipr   r   r   r   E   s    
zExpr._hashable_inputsN)r   r   r   r   r   r"   r#   r   r   r   r   r   r   4   s
   r   c                   @   s*   e Zd ZdZdd Zd	ddZdd ZdS )

DispatcherzSimple single dispatch.c                 C   s
   i | _ d S r   )_lookupr   r   r   r   __init__X   s    zDispatcher.__init__Nc                    sF   |du r fddS t tr8D ]} || q$n
| j< |S )z6Register dispatch of `func` on arguments of type `typ`Nc                    s     | S r   )register)fr   typr   r   <lambda>^   r   z%Dispatcher.register.<locals>.<lambda>)r$   r(   r/   r-   )r   r2   functr   r1   r   r/   [   s    

zDispatcher.registerc                 O   s~   | j }t|}||v r0|| |g|R i |S t|dd  D ]*}||v r@|| |g|R i |  S q@td|d S )N   zNo dispatch for {0} type)r-   r   r   	TypeErrorformat)r   headrestkwargsZlkr2   clsr   r   r   __call__f   s     zDispatcher.__call__)N)r   r   r   r   r.   r/   r=   r   r   r   r   r,   V   s   
r,   c                 C   s$   t j| } t| t jo"| t jjv S )zCheck if a datashape is numeric and real.

    Example
    -------
    >>> isrealfloat('int32')
    False
    >>> isrealfloat('float64')
    True
    >>> isrealfloat('string')
    False
    >>> isrealfloat('complex64')
    False
    )	datashape
predicateslaunderr$   UnittypesetsZfloatingZdtr   r   r   isrealfloatt   s    rD   c                 C   s$   t j| } t| t jo"| t jjv S )zCheck if a datashape is numeric and real.

    Example
    -------
    >>> isreal('int32')
    True
    >>> isreal('float64')
    True
    >>> isreal('string')
    False
    >>> isreal('complex64')
    False
    )r>   r?   r@   r$   rA   rB   realrC   r   r   r   isreal   s    rF   c                 C   sf   t t| j}|| |d| | |} t| }tj|dd}|| @ }t	|d| j
ddS )aW  nansum where all-NaN values remain NaNs.

    Note: In NumPy <=1.9 NaN is returned for slices that are
    all NaN, while later versions return 0. This function emulates
    the older behavior, which allows using NaN as a missing value
    indicator.

    Parameters
    ----------
    array: Array to sum over
    axis:  Axis to sum over
    r   axis)r%   rangendimremoveinsertZ	transposer)   Zisnanallwheresum)arrayrH   TZmissing_valsZ	all_emptyZset_to_zeror   r   r   nansum_missing   s    



rR   c           	      C   sp   | j dd \}}| jdd \}}| | j}| | j}|d |d  |d  }|d |d  |d  }||fS )ziCalculate the resolution of xarray.DataArray raster and return it as the
    two-tuple (xres, yres).
    Nr   r6   )shapeZdimsvalues)	rasterhwZydimZxdimZxcoordsZycoordsZxresZyresr   r   r   calc_res   s    

rZ   c              	   C   s.  |d dk r|   n|  }|d dk r0| n|  }tj }}tj  }}t|d d|gd|d  |gg dg}	ddt|ft| dft| t|ffD ]V\}
}t|	t|
|dg\}}}||k r|}||kr|}||k r|}||kr|}q|d d |d d  }}|| || || || fS )a  Calculate the bounding box of a raster, and return it in a four-element
    tuple: (xmin, ymin, xmax, ymax). This calculation assumes the raster is
    uniformly sampled (equivalent to a flat-earth assumption, for geographic
    data) so that an affine transform (using the "Augmented Matrix" approach)
    suffices:
    https://en.wikipedia.org/wiki/Affine_transformation#Augmented_matrix

    Parameters
    ----------
    xs : numpy.array
        1D NumPy array of floats representing the x-values of a raster. This
        likely originated from an xarray.DataArray or xarray.Dataset object
        (xr.open_rasterio).
    ys : numpy.array
        1D NumPy array of floats representing the y-values of a raster. This
        likely originated from an xarray.DataArray or xarray.Dataset object
        (xr.open_rasterio).
    res : tuple
        Two-tuple (int, int) which includes x and y resolutions (aka "grid/cell
        sizes"), respectively.
    r   r6           )r[   r[         ?)r   r   r\          @)maxminr)   infrP   lendot)xsysresZxboundZyboundZxminZyminZxmaxZymaxZAbZx_Zy_r   y_xpadypadr   r   r   	calc_bbox   s     
0rj   c                 C   s   t |}t|d }| |  }}|| }| | | || |  } }t| | | t|| |  }	}
|
|	k rz|	|	fS |	|
fS )ai  
    Transform continuous start and end coordinates into array indices.

    Parameters
    ----------
    start : float
        coordinate of the lower bound.
    end : float
        coordinate of the upper bound.
    coords : numpy.ndarray
        coordinate values along the axis.
    res : tuple
        Resolution along an axis (aka "grid/cell sizes")
    r]   )ra   absr_   r^   r   )startendcoordsre   sizeZhalfZvminZvmaxspanZsidxZeidxr   r   r   get_indices   s    "rq   c                 C   s   |du rt | }| j}|dur*||d  }t|d tjrFtddnd}t|d tjrftddnd}|jdkr|d |k r|dddddf }|d |kr|ddd }nJ|d |k r|dddddddf }|d |kr|dddddf }|S )a  
    Reorients the array to a canonical orientation depending on
    whether the x and y-resolution values are positive or negative.

    Parameters
    ----------
    raster : DataArray
        xarray DataArray to be reoriented
    res : tuple
        Two-tuple (int, int) which includes x and y resolutions (aka "grid/cell
        sizes"), respectively.
    layer : int
        Index of the raster layer to be reoriented (optional)

    Returns
    -------
    array : numpy.ndarray
        Reoriented 2d NumPy ndarray
    Nr6   r   ns   rT   )rZ   datar$   r)   Ztimedelta64rJ   )rW   re   ZlayerrP   Zr0zeroZr1zeror   r   r   orient_array   s      
"("ru   c                 C   s   || \}}\}}|| t |  }	|| t | }
t|	d t|
d  }}|| ||  }}|| ||  }}t||| }t|||}|d dk r|ddd }|d dkr|ddd }||fS )a  
    Computes DataArray coordinates at bin centers

    Parameters
    ----------
    width : int
        Number of coordinates along the x-axis
    height : int
        Number of coordinates along the y-axis
    x_range : tuple
        Left and right edge of the coordinates
    y_range : tuple
        Bottom and top edges of the coordinates
    res : tuple
        Two-tuple (int, int) which includes x and y resolutions (aka "grid/cell
        sizes"), respectively. Used to determine coordinate orientation.

    Returns
    -------
    xs : numpy.ndarray
        1D array of x-coordinates
    ys : numpy.ndarray
        1D array of y-coordinates
    r]   r   NrT   r6   )floatrk   r)   linspace)widthZheightZx_rangeZy_rangere   Zx0Zx1Zy0Zy1ZxdZydrh   ri   rc   rd   r   r   r   compute_coords!  s    ry   meanc                    s  | j dd \}}| d||t  d||t  f  t fddtD }|dkrrtj|ddS |dkrtj|ddS |d	krtj|ddS |d
krtj|ddS |dkrtj	|ddS |dkrtj
|ddS |dk rtj|ddS tddS )z3Create downsampled aggregate factor in pixels unitsNrs   c                    s&   g | ]  fd dt D qS )c                    s$   g | ]} |d d f qS r   r   )r   i)crarrfactorjr   r   r   K  s   z3downsample_aggregate.<locals>.<listcomp>.<listcomp>)rI   )r   r|   r}   )r~   r   r   K  s   z(downsample_aggregate.<locals>.<listcomp>rz   r   rG   rO   r^   r_   ZmedianZstdvarzNInvalid 'how' downsample method. Options mean, sum, max, min, median, std, var)rU   r   r)   ZconcatenaterI   ZnanmeanZnansumnanmaxnanminZ	nanmedianZnanstdZnanvar
ValueError)	aggregater}   howrd   rc   concatr   r   r   downsample_aggregateG  s(    ,
r   linear   c                 C   s   t | j}t | j}|dkr4| j| jdk  }|dkrXt |||dddf }n2t jdt || t j	||j
d| dddf }t|||fS )zHelper function similar to np.linspace which return values from aggregate min value to aggregate max value in either linear or log space.
    r   r   N)basenumdtype)r)   r   rV   r   rt   r_   rw   ZlogspaceZlog1per   r   )r   r   r   Zmax_valZmin_valvalsr   r   r   summarize_aggregate_valuesa  s     
r   c                    s   g  fdd}|S )z&
    simple arg caching decorator
    c                     s,   rd | kr$|  |  fd d < d S )Nr   r6   r   )argsr0   Zlastr   r   rg   |  s    zhold.<locals>._r   )r0   rg   r   r   r   holdv  s    r   .png. c                 C   sX   ddl m} tj|s"t| |r0|| |} |  tj|||  |rT| S dS )zPGiven a datashader Image object, saves it to a disk file in the requested formatr   )set_backgroundN)	Zdatashader.transfer_functionsr   ospathexistsmkdirZto_pilZsavejoin)ZimgfilenameZfmtZ_returnZexport_pathZ
backgroundr   r   r   r   export_image  s    

r   c                 C   sv   t | ttfrt| } t |ttfr0t|}tjd }| | d }ttd| tj d | tj }||fS )a  
    Projects the given (longitude, latitude) values into Web Mercator
    coordinates (meters East of Greenwich and meters North of the Equator).

    Longitude and latitude can be provided as scalars, Pandas columns,
    or Numpy arrays, and will be returned in the same form.  Lists
    or tuples will be converted to Numpy arrays.

    Examples:
       easting, northing = lnglat_to_meters(-74,40.71)

       easting, northing = lnglat_to_meters(np.array([-74]),np.array([40.71]))

       df=pandas.DataFrame(dict(longitude=np.array([-74]),latitude=np.array([40.71])))
       df.loc[:, 'longitude'], df.loc[:, 'latitude'] = lnglat_to_meters(df.longitude,df.latitude)
    iRa g     f@Z   g     v@)r$   r%   r(   r)   rP   ZpilogZtan)Z	longitudeZlatitudeZorigin_shiftZeastingZnorthingr   r   r   lnglat_to_meters  s    


(r   c                 C   sZ  t | jttjjs<t | jtjjjs<trt | jtj	j
jr| jj}t |tjrX| }trpt |tjrp| }t|}|jjdkr|d}tdt| jj|j}tj||| jjdS | jjdk rt| jdd}|durt|}ttj|dS t | jttfr| jS tj !| j}|tj"kr6tj#n|}|tj#tj$fv rVt|S |S )	zZReturn an object from datashape.coretypes given a column from a pandas
    dataframe.
    Uobjectz{} * {})r   orderedMtzN)r   )%r$   r   r   pdCategoricalapitypesCategoricalDtypecudfcoredtypescat
categoriesddZIndexcomputeZ	to_pandasr)   rP   kindastyper>   dshaper8   ra   r   getattrstrZOptionZDateTimer   r	   ZCTypeZfrom_numpy_dtypeZobject_stringZ	datetime_)colZpd_categoriesr   Z
cat_dshaper   r   r   r   r   dshape_from_pandas_helper  sF    



r   c                    s"   t  t fdd jD  S )z=Return a datashape.DataShape object given a pandas dataframe.c                    s   g | ]}|t  | fqS r   r   r   kdfr   r   r     s   z&dshape_from_pandas.<locals>.<listcomp>)ra   r>   Recordcolumnsr   r   r   r   dshape_from_pandas  s    r   c                 C   s   t | d  S )Nr   )r(   Z__dask_keys__)r   r;   r   r   r   r3     r   r3   )keyc                    sB    fdd j D } j|dd tjt fdd j D  S )z;Return a datashape.DataShape object given a dask dataframe.c                    sP   g | ]H}t  | jttjjs6t  | jtjjjrt | j	d ds|qS )ZknownT)
r$   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r     s
   z$dshape_from_dask.<locals>.<listcomp>F)indexc                    s"   g | ]}|t  | d fqS )r   )r   Zget_partitionr   r   r   r   r     s   )r   Z
categorizer>   r   r   )r   Zcat_columnsr   r   r   dshape_from_dask  s    
r   c                    s.   t jt  fddt jt j D  S )z;Return a datashape.DataShape object given a xarray Dataset.c                    s   g | ]}|t  | fqS r   r   r   Zxr_dsr   r   r     s   z.dshape_from_xarray_dataset.<locals>.<listcomp>)r>   r   r   r%   Z	data_varsrn   r   r   r   r   dshape_from_xarray_dataset  s    r   c                 C   s   t | jd d }t j|d< | |dd< t ||jd }t |jd |jd d f}t j|dddf< ||ddddf< t|| dS )aH  
   Converts a set of multiple sequences (eg: time series), stored as a 2 dimensional
   numpy array into a pandas dataframe that can be plotted by datashader.
   The pandas dataframe eventually contains two columns ('x' and 'y') with the data.
   Each time series is separated by a row of NaNs.
   Discussion at: https://github.com/bokeh/datashader/issues/286#issuecomment-334619499

   x_values: 1D numpy array with the values to be plotted on the x axis (eg: time)
   y_values: 2D numpy array with the sequences to be plotted of shape (num sequences X length of each sequence)

   r   r6   rT   N)r   rf   )r)   ZzerosrU   nanZtiler   	DataFrameZflatten)Zx_valuesZy_valuesr   rf   r   r   r   !dataframe_from_multiple_sequences  s    
r   c                 C   s  g d}| j |j d|f tjddf }|\}}}t|| ||  dkrZg d}|j dd|f }|jdks|tj}tj| j |dd}|t	|j
dd |j
d }tj|| jd}	t| jdk}
|
s|jd	 }|j ddd	f d	|	|< |	S )
zkHelper for ``datashader.utils.mesh()``. Both arguments are assumed to be
    Pandas DataFrame objects.
    )r   r6   rs   r   Nrs   )r   rs   r6   int64rG   )r      )rV   r   r)   r   Zcrossitemr   ZtakeZreshapeZprodrU   r   r   r   ra   repeat)vertices	simplicesZwindingZ	first_triabcZvertex_idxsr   re   Zverts_have_weightsZ
weight_colr   r   r   _pd_mesh  s     $

"
r   c                 C   sP   t |  | }t| j|j}ttt|d|  d }tj	||d}|S )ziHelper for ``datashader.utils.mesh()``. Both arguments are assumed to be
    Dask DataFrame objects.
    r   )	chunksize)
r   r   r^   Znpartitionsr   r)   Zceilra   r   Zfrom_pandas)r   r   re   Zapprox_npartitionsr   r   r   r   _dd_mesh0  s
    r   c                 C   s   |j jd dksJ d|jjdd dd  }|sBJ dt| jdksh|j jd dkshJ d	t| t	j
rt|t	j
rt| |S t| |S )
zMerge vertices and simplices into a triangular mesh, suitable to be
    passed into the ``Canvas.trimesh()`` method via the ``mesh``
    keyword-argument. Both arguments are assumed to be Dask DataFrame
    objects.
    r6   r   zFAt least three vertex columns are required for the triangle definitionNc                 S   s   t | t jS r   )r)   Z
issubdtypeZintegerrC   r   r   r   r3   M  r   zmesh.<locals>.<lambda>z^Simplices must be integral. You may consider casting simplices to integers with ".astype(int)"rs   zMIf no vertex weight column is provided, a triangle weight column is required.)rV   rU   r   ZilocmaprM   ra   r   r$   r   r   r   r   )r   r   Zsimplices_all_intsr   r   r   meshB  s    &
r   )NN)rz   )r   r   )r   Tr   r   )@Z
__future__r   r   r   r   reinspectr   ZnumbanbZnumpyr)   Zpandasr   Ztoolzr   Zxarrayr   Zdask.dataframeZ	dataframer   r>   Zdatashader.datatypesr   ImportErrorr   r   	ExceptionZspatialpandas.geometryr	   UserWarningr
   ZjitZngjitZngjit_parallelr(   match__version__groupsZnumba_versionr   r   r,   rD   rF   rR   rZ   rj   rq   ru   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sj   
	"(
#&


)
