o
    Yh@                     @  s  U d Z ddlmZ ddlZddlZddlmZmZmZm	Z	 ddl
mZmZ ddlmZ ddlmZmZmZmZ ddlmZ dd	lmZ dd
lmZ ercddlmZ ddlmZ ddlm Z  ddl!m"Z" e#ej$Z%de&d< h dZ'de&d< h dZ(de&d< dZ)de&d< dZ*de&d< dZ+de&d< g dZ,de&d< G dd dZ-dUd-d.Z.dVd4d5Z/dWd:d;Z0dXd?d@Z1dYdDdEZ2dZdIdJZ3		d[d\dSdTZ4dS )]z+A wrapper for simple PyDeck scatter charts.    )annotationsN)TYPE_CHECKINGAnyFinalcast)configdataframe_util)deck_gl_json_chart)ColorIntColorTupleis_color_liketo_int_color_tuple)StreamlitAPIException)DeckGlJsonChart)gather_metrics)
Collection)	DataFrame)Data)DeltaGeneratorzFinal[dict[str, Any]]_DEFAULT_MAP>   LATlatLATITUDElatituder   _DEFAULT_LAT_COL_NAMES>   LONlon	LONGITUDE	longitude_DEFAULT_LON_COL_NAMES)      r      _DEFAULT_COLORd   _DEFAULT_SIZE   _DEFAULT_ZOOM_LEVEL)ih     Z   -   g     6@g     &@g     @g/$@g"~?g"~?gI+?gI+?gI+?gI+?gI+?gI+?g{Gzt?g~jth?gMbP?gMb@?gMb0?_ZOOM_LEVELSc                   @  sD   e Zd Zed	dddddddddddddZedddZdS )MapMixinmapNT)r   r   colorsizezoomuse_container_widthwidthheightdatar   r   
str | Noner   r.   None | str | Colorr/   None | str | floatr0   
int | Noner1   boolr2   r3   returnr   c                C  s8   t  }
t||||||}t|
||||	d | jd|
S )at  Display a map with a scatterplot overlaid onto it.

        This is a wrapper around ``st.pydeck_chart`` to quickly create
        scatterplot charts on top of a map, with auto-centering and auto-zoom.

        When using this command, a service called Carto_ provides the map tiles to render
        map content. If you're using advanced PyDeck features you may need to obtain
        an API key from Carto first. You can do that as
        ``pydeck.Deck(api_keys={"carto": YOUR_KEY})`` or by setting the CARTO_API_KEY
        environment variable. See `PyDeck's documentation`_ for more information.

        Another common provider for map tiles is Mapbox_. If you prefer to use that,
        you'll need to create an account at https://mapbox.com and specify your Mapbox
        key when creating the ``pydeck.Deck`` object. You can do that as
        ``pydeck.Deck(api_keys={"mapbox": YOUR_KEY})`` or by setting the MAPBOX_API_KEY
        environment variable.

        .. _Carto: https://carto.com
        .. _Mapbox: https://mapbox.com
        .. _PyDeck's documentation: https://deckgl.readthedocs.io/en/latest/deck.html

        Carto and Mapbox are third-party products and Streamlit accepts no responsibility
        or liability of any kind for Carto or Mapbox, or for any content or information
        made available by Carto or Mapbox. The use of Carto or Mapbox is governed by
        their respective Terms of Use.

        Parameters
        ----------
        data : Anything supported by st.dataframe
            The data to be plotted.

        latitude : str or None
            The name of the column containing the latitude coordinates of
            the datapoints in the chart.

            If None, the latitude data will come from any column named 'lat',
            'latitude', 'LAT', or 'LATITUDE'.

        longitude : str or None
            The name of the column containing the longitude coordinates of
            the datapoints in the chart.

            If None, the longitude data will come from any column named 'lon',
            'longitude', 'LON', or 'LONGITUDE'.

        color : str or tuple or None
            The color of the circles representing each datapoint.

            Can be:

            - None, to use the default color.
            - A hex string like "#ffaa00" or "#ffaa0088".
            - An RGB or RGBA tuple with the red, green, blue, and alpha
              components specified as ints from 0 to 255 or floats from 0.0 to
              1.0.
            - The name of the column to use for the color. Cells in this column
              should contain colors represented as a hex string or color tuple,
              as described above.

        size : str or float or None
            The size of the circles representing each point, in meters.

            This can be:

            - None, to use the default size.
            - A number like 100, to specify a single size to use for all
              datapoints.
            - The name of the column to use for the size. This allows each
              datapoint to be represented by a circle of a different size.

        zoom : int
            Zoom level as specified in
            https://wiki.openstreetmap.org/wiki/Zoom_levels.

        use_container_width : bool
            Whether to override the map's native width with the width of
            the parent container. If ``use_container_width`` is ``True``
            (default), Streamlit sets the width of the map to match the width
            of the parent container. If ``use_container_width`` is ``False``,
            Streamlit sets the width of the chart to fit its contents according
            to the plotting library, up to the width of the parent container.

        width : int or None
            Desired width of the chart expressed in pixels. If ``width`` is
            ``None`` (default), Streamlit sets the width of the chart to fit
            its contents according to the plotting library, up to the width of
            the parent container. If ``width`` is greater than the width of the
            parent container, Streamlit sets the chart width to match the width
            of the parent container.

            To use ``width``, you must set ``use_container_width=False``.

        height : int or None
            Desired height of the chart expressed in pixels. If ``height`` is
            ``None`` (default), Streamlit sets the height of the chart to fit
            its contents according to the plotting library.

        Examples
        --------
        >>> import pandas as pd
        >>> import streamlit as st
        >>> from numpy.random import default_rng as rng
        >>>
        >>> df = pd.DataFrame(
        >>>     rng(0).standard_normal((1000, 2)) / [50, 50] + [37.76, -122.4],
        >>>     columns=["lat", "lon"],
        >>> )
        >>>
        >>> st.map(df)

        .. output::
           https://doc-map.streamlit.app/
           height: 600px

        You can also customize the size and color of the datapoints:

        >>> st.map(df, size=20, color="#0044ff")

        And finally, you can choose different columns to use for the latitude
        and longitude components, as well as set size and color of each
        datapoint dynamically based on other columns:

        >>> import pandas as pd
        >>> import streamlit as st
        >>> from numpy.random import default_rng as rng
        >>>
        >>> df = pd.DataFrame(
        ...     {
        ...         "col1": rng(0).standard_normal(1000) / 50 + 37.76,
        ...         "col2": rng(1).standard_normal(1000) / 50 + -122.4,
        ...         "col3": rng(2).standard_normal(1000) * 100,
        ...         "col4": rng(3).standard_normal((1000, 4)).tolist(),
        ...     }
        ... )
        >>>
        >>> st.map(df, latitude="col1", longitude="col2", size="col3", color="col4")

        .. output::
           https://doc-map-color.streamlit.app/
           height: 600px

        )r2   r3   r	   )DeckGlJsonChartPrototo_deckgl_jsonmarshalldg_enqueue)selfr4   r   r   r.   r/   r0   r1   r2   r3   	map_protodeck_gl_json rC   W/var/www/html/Persson_Maskin/env/lib/python3.10/site-packages/streamlit/elements/map.pyr-   N   s    
zMapMixin.mapc                 C  s
   t d| S )zGet our DeltaGenerator.r   )r   )r@   rC   rC   rD   r>      s   
zMapMixin.dgN)r4   r   r   r5   r   r5   r.   r6   r/   r7   r0   r8   r1   r9   r2   r8   r3   r8   r:   r   )r:   r   )__name__
__module____qualname__r   r-   propertyr>   rC   rC   rC   rD   r,   M   s      #r,   r4   r   r   r5   r   r/   r7   r.   None | str | Collection[float]r0   r8   r:   strc              	   C  s  | d u r	t tS t| dr| jrt tS t| }t|d|t}t|d|t	}t
||t\}	}
t
||t\}}tdd |||
|hD }|| }t|||}t||||\}}}tt}||d d< ||d d< ||d d< dd	| d
| d|	dd||ddg|d< t |S )Nemptyr   r   c                 S  s   g | ]}|d ur|qS rE   rC   ).0crC   rC   rD   
<listcomp>  s
    z"to_deckgl_json.<locals>.<listcomp>initialViewStater0   ScatterplotLayerz@@=[, ]   metersrecords)z@@typegetPosition	getRadiusradiusMinPixelsradiusUnitsgetFillColorr4   layers)jsondumpsr   hasattrrL   r   convert_anything_to_pandas_df_get_lat_or_lon_col_namer   r   _get_value_and_col_namer%   r#   sorted_convert_color_arg_or_column_get_viewport_detailscopydeepcopyto_dict)r4   r   r   r/   r.   r0   dflat_col_namelon_col_namesize_argsize_col_name	color_argcolor_col_nameused_columnsconverted_color_arg
center_lat
center_londefaultrC   rC   rD   r<      sF   






r<   r   human_readable_namecol_name_from_userdefault_col_namesset[str]c           	      C  s   t |tr|| jv r|}n8d}|D ]}|| jv r|} nq|du rCdttt|}dttt| j}td| d| d| |}t	| | 
 jrVtd| d|S )z=Returns the column name to be used for latitude or longitude.NrR   zMap data must contain a z column named: z. Existing columns: zColumn zB is not allowed to contain null values, such as NaN, NaT, or None.)
isinstancerK   columnsjoinr-   reprrc   listr   anyisnaarray)	r4   ru   rv   rw   col_namecandidate_col_namerN   formatted_allowed_col_nameformmated_col_namesrC   rC   rD   ra   5  s0   

ra   value_or_namer   default_valuetuple[str, str | None]c                 C  sF   t |tr|| jv r|}d| }||fS d}|du r|n|}||fS )a  Take a value_or_name passed in by the Streamlit developer and return a PyDeck
    argument and column name for that property.

    This is used for the size and color properties of the chart.

    Example:
    - If the user passes size=None, this returns the default size value and no column.
    - If the user passes size=42, this returns 42 and no column.
    - If the user passes size="my_col_123", this returns "@@=my_col_123" and "my_col_123".
    z@@=N)ry   rK   rz   )r4   r   r   r   
pydeck_argrC   rC   rD   rb   b  s   
rb   rn   ro   None | str | IntColorTuplec                 C  s   d}|dur6t | | dkr.t| | jd r.| jdd|f t| jdd|f< |}|S td| d|dur>t|}|S )aR  Converts color to a format accepted by PyDeck.

    For example:
    - If color_arg is "#fff", then returns (255, 255, 255, 255).
    - If color_col_name is "my_col_123", then it converts everything in column my_col_123 to
      an accepted color format such as (0, 100, 200, 255).

    NOTE: This function mutates the data argument.
    Nr   zColumn "z*" does not appear to contain valid colors.)lenr   iloclocr-   r   r   )r4   rn   ro   color_arg_outrC   rC   rD   rd     s    
rd   rj   rk   tuple[int, float, float]c                 C  s   | |   }| |  }| |   }| |  }|| d }|| d }	t|| }
t|| }|du r=t||
}t|}|||	fS )z3Auto-set viewport when not fully specified by user.g       @N)minmaxabs_get_zoom_level)r4   rj   rk   r0   min_latmax_latmin_lonmax_lonrr   rs   	range_lon	range_latlongitude_distancerC   rC   rD   re     s   

re   distancefloatintc                 C  sD   t ttd D ]}t|d  |   k rt| kr|  S  qqtS )a9  Get the zoom level for a given distance in degrees.

    See https://wiki.openstreetmap.org/wiki/Zoom_levels for reference.

    Parameters
    ----------
    distance : float
        How many degrees of longitude should fit in the map.

    Returns
    -------
    int
        The zoom level, from 0 to 20.

       )ranger   r+   r'   )r   irC   rC   rD   r     s
   r   pydeck_protor;   pydeck_jsonr1   r9   r3   r2   Nonec                 C  sB   || _ || _|r|| _|r|| _d| _td}|r|| _d S d S )N zmapbox.token)r]   r1   r2   r3   idr   
get_optionmapbox_token)r   r   r1   r3   r2   r   rC   rC   rD   r=     s   

r=   )r4   r   r   r5   r   r5   r/   r7   r.   rJ   r0   r8   r:   rK   )
r4   r   ru   rK   rv   r5   rw   rx   r:   rK   )r4   r   r   r   r   r   r:   r   )r4   r   rn   rK   ro   r5   r:   r   )
r4   r   rj   rK   rk   rK   r0   r8   r:   r   )r   r   r:   r   )NN)r   r;   r   rK   r1   r9   r3   r8   r2   r8   r:   r   )5__doc__
__future__r   rf   r]   typingr   r   r   r   	streamlitr   r   streamlit.elementsr	   !streamlit.elements.lib.color_utilr
   r   r   r   streamlit.errorsr   #streamlit.proto.DeckGlJsonChart_pb2r   r;   streamlit.runtime.metrics_utilr   collections.abcr   pandasr   streamlit.dataframe_utilr   streamlit.delta_generatorr   dict	EMPTY_MAPr   __annotations__r   r   r#   r%   r'   r+   r,   r<   ra   rb   rd   re   r   r=   rC   rC   rC   rD   <module>   sD    
+
>
-

'
