o
    Pf8                  
   @   sF  d Z ddlZddlZddlZddlZddlZddlZddlZddlZddlm	Z	m
Z
mZ ddlmZmZ zddlZW n eyE   dZY nw ejrOddlmZ ejejeZdd Zd(d	d
Zdd Zd)ddZd*ddZ		d+de	dedede
df de f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 ),zA
Utilities
-------

Utility module for Folium helper functions.

    N)AnyCallableUnionEnvironmentPackageLoaderColorMapc                   C   s   t tdddS )zGet Jinja templates.branca	templates)loaderr    r   r   \/home/deployuser/azure_apps/autowriter/venv/lib/python3.10/site-packages/branca/utilities.pyget_templates!   s   r         $@c                 C   sb   t | |k r
| }|S ttt | | }g }| dd| D ]}||g7 }|dg|d  7 }q|S )z
    Downsamples the number of legend values so that there isn't a collision
    of text on the legend colorbar (within reason). The colorbar seems to
    support ~10 entries as a maximum.

    N    )lenintmathceil)legend_values
max_labelslegend_ticksspacerir   r   r   legend_scaler&   s   
r   c              
   C   s   dd }g }t | dd | dd D ]F\}}d}t|D ];}||dd |dd ||}||dd |dd ||}	||dd	 |dd	 ||}
|d
d||	|
g qqg }t|D ]}t||d  }t|t|d  }|||  q`|S )a  
    Given a list of hexcode values, will return a list of length
    nColors where the colors are linearly interpolated between the
    (r, g, b) tuples that are given.

    Examples
    --------
    >>> linear_gradient([(0, 0, 0), (255, 0, 0), (255, 255, 0)], 100)

    c                 S   sd   d}t ||d  }t||t| | }ttt| |||  dd }t|dkr0d| }|S )z
        Return the value correct value of a number that is in between start
        and finish, for use in a loop of length *length*.

           r   x   0)floatr   hexsplitr   )startfinishlengthr   basefractionrayngethexr   r   r   _scaleD   s   $zlinear_gradient.<locals>._scaleNr   r   i           r   #)ziprangeappendjoinr"   r   r   )hexListnColorsr,   	allColorsr%   endnInterpolateindexrgbresultcounterr)   r   r   r   linear_gradient8   s    "r@      c                 C   s  d}d}t |tstdt| ||krtd||k r!tdt | ts0tdt|  d| dd	 d
krL| d	d }|d t|d }d}n| }|d t|d }d}ttj	
td}t| }W d	   n1 svw   Y  ttj	
td}t| }	W d	   n1 sw   Y  ttj	
td}t| d }
W d	   n1 sw   Y  ||
vrt|d d}||d	u rd}|s]|	| dkrg }|D ]}|d |v r|t|dd  qtd| d tt| d tt| |}d}| D ]-}d|vrq|dd |kr(qt|dd |kr?|}t|dd }q|sNt|||}|S t||d	d	d |}|S |sh||d	}|S ||d	d	d	d }|S )z
    Generate a colorbrewer color scheme of length 'len', type 'scheme.
    Live examples can be seen at http://colorbrewer2.org/

       r-   zn has to be an int, not a %szLThe maximum number of colors in a ColorBrewer sequential color series is 253zJThe minimum number of colors in a ColorBrewer sequential color series is 3z color should be a string, not a .N_r_r    TFz_schemes.jsonzscheme_info.jsonzscheme_base_codes.jsoncodesz  is not a valid ColorBrewer codeQualitativer   zgExpanded color support is not available for Qualitative schemes; restrict the number of colors for the z code to between z and r   r   )
isinstancer   	TypeErrortype
ValueErrorstrzfillopenospathr4   rootpathjsonloadsreadgetr3   r$   minmaxkeysr@   )
color_coden	maximum_n	minimum_n	base_codecore_color_codecolor_reversefschemesscheme_infocore_schemesexplicit_schemematching_qualskeylongest_scheme_namelongest_scheme_nsn_namecolor_schemer   r   r   color_brewerh   s   




	rl   upperc                 C   s   t | dr&t | dr| j dd }nd}d|t|  d}n)t	| t
sGt	| tsGt | drGt| ||d	}d
t|d }ntt| }|ddS )as  Infers the type of an image argument and transforms it into a URL.

    Parameters
    ----------
    image: string, file or array-like object
        * If string, it will be written directly in the output file.
        * If file, it's content will be converted as embedded in the
          output file.
        * If array-like, it will be converted to PNG base64 string and
          embedded in the output.
    origin : ['upper' | 'lower'], optional, default 'upper'
        Place the [0, 0] index of the array in the upper left or
        lower left corner of the axes.
    colormap : callable, used only for `mono` image.
        Function of the form [x -> (r,g,b)] or [x -> (r,g,b,a)]
        for transforming a mono image into RGB.
        It must output iterables of length 3 or 4, with values between
        0. and 1.  Hint : you can use colormaps from `matplotlib.cm`.
    rU   namerC   r   pngzdata:image/{};base64,{}zutf-8__iter__)origincolormapzdata:image/png;base64,
 )hasattrrn   lowerr$   formatbase64	b64encoderU   decoderI   rM   bytes	write_pngrS   rT   dumpsreplace)imagerr   rq   
fileformaturlro   r   r   r   image_to_url   s    

r   datarq   rr   r	   returnc           
         s  ddl m} tdu rtdt||r|j}nt|r|}ndd }t|   j\}}}|dvr5t	d j|||fks?J |d	krdt
tt|    jd	 }|d
vr\t	d |||f  j|||fksnJ |dkrtj t||d	ffdd d} j|||fksJ |dksJ  jdkrtjddd  d  jddd  d t  < W d   n1 sw   Y   d |dkrۈ dddddddf  d fddt|D }dd }	dd|	dtd||d d!ddd|	d"t|d#|	d$dgS )%a^  
    Transform an array of data into a PNG string.
    This can be written to disk using binary I/O, or encoded using base64
    for an inline PNG like this:

    >>> png_str = write_png(array)
    >>> "data:image/png;base64," + png_str.encode("base64")

    Inspired from
    http://stackoverflow.com/questions/902761/saving-a-numpy-array-as-an-image

    Parameters
    ----------
    data: numpy array or equivalent list-like object.
         Must be NxM (mono), NxMx3 (RGB) or NxMx4 (RGBA)
    origin : ['upper' | 'lower'], optional, default 'upper'
        Place the [0,0] index of the array in the upper left or lower left
        corner of the axes.
    colormap : ColorMap subclass or callable, optional
        Only needed to transform mono images into RGB. You have three options:
        - use a subclass of `ColorMap` like `LinearColorMap`
        - use a colormap from `matplotlib.cm`
        - use a custom function of the form [x -> (r,g,b)] or [x -> (r,g,b,a)].
          It must output iterables of length 3 or 4 with values between 0 and 1.

    Returns
    -------
    PNG formatted byte string
    r   r   Nz4The NumPy package is required for this functionalityc                 S   s   | | | dfS )Nr   r   r   r   r   r   <lambda>"  s    zwrite_png.<locals>.<lambda>)r   r-      z5Data must be NxM (mono), NxMx3 (RGB), or NxMx4 (RGBA)r   )r-   r   z9colormap must provide colors oflength 3 (RGB) or 4 (RGBA)r-   r    )axisr   uint8ignore)divideinvalidg     o@)r   r   )r   r   r   rv   r       c                    s*   g | ]}d  |ddddf    qS )    N)tobytes).0r   arrayr   r   
<listcomp>G  s   * zwrite_png.<locals>.<listcomp>c                 S   s2   | | }t dt|| t ddt|@  S )Nz!Il    )structpackr   zlibcrc32)png_tagr   
chunk_headr   r   r   png_packI  s   zwrite_png.<locals>.png_packs   PNG

s   IHDRz!2I5B   rA   s   IDAT	   s   IEND)branca.colormapr	   npImportErrorrI   rgba_floats_tuplecallable
atleast_3dshaperL   r   listmapravelreshapeconcatenateonesdtypeerrstaterX   isfiniteastyper4   r2   r   r   r   compress)
r   rq   rr   r	   colormap_callableheightwidthnblayersraw_datar   r   r   r   r|      sX   "




r|   c                    s.   d  fddtt D dddS )Nr   c                    s   g | ]<\}}|t  d  k r | r  |d    r d|  n|t  d  k r:| r: |d    r:| d n| qS )r   rF   )r   isupperislowerrv   )r   r   r   outr   r   r   _  s     z_camelify.<locals>.<listcomp>rF   __)r4   	enumerater   lstripr~   r   r   r   r   	_camelify[  s   

r   c                 C   s   t | ttfrt| dfS t | tr0td|  }|r(t|d|dfS td| dt | t	rOt | d ttfrOt | d trOt| d | d fS t
d| d)	Npxz([\d.]+)\s?(\w{1,5}|%)r   r    zCannot parse z+, it should be a number followed by a unit.r   zC, it should be a number or a string containing a number and a unit.)rI   r   r"   rM   re	fullmatchstripgrouprL   tuplerJ   )valuematchr   r   r   _parse_sizev  s&   


r   c                 C   s<   t | drt | d drttt| S t| ddd S | S )zMirrors the points in a list-of-list-of-...-of-list-of-points.
    For example:
    >>> _locations_mirror([[[1, 2], [3, 4]], [5, 6], [7, 8]])
    [[[2, 1], [4, 3]], [6, 5], [8, 7]]

    rp   r   Nr   )ru   r   r   _locations_mirrorr   r   r   r   r     s
   
r   c                 C   s   t | drttt| S | S )z?Transforms recursively a list of iterables into a list of list.rp   )ru   r   r   _locations_tolistr   r   r   r   r     s   
r   c                 C   "   | d u r|S |d u r| S t | |S N)rW   r   yr   r   r   none_min  
   
r   c                 C   r   r   )rX   r   r   r   r   none_max  r   r   c                 C   s`   t | ttfr(t| r&t | d ttfr#g }| D ]}|t|7 }q|S | gS g S td| d)zIterates over a list representing a feature, and returns a list of points,
    whatever the shape of the array (Point, MultiPolyline, etc).
    r   zList/tuple type expected. Got rC   )rI   r   r   r   iter_pointsrL   )r   r   r   r   r   r   r     s   r   )r   )rA   )Nrm   )rm   N))__doc__rx   rS   r   rP   r   r   typingr   r   r   r   jinja2r   r   numpyr   r   TYPE_CHECKINGr   r	   rQ   abspathdirname__file__rR   r   r   r@   rl   r   rM   r{   r|   r   r   r   r   r   r   r   r   r   r   r   <module>   sT    

0
a/
e		