Monday, May 11, 2020

Folium for map displays

Sources:

https://www.kaggle.com/daveianhickey/how-to-folium-for-maps-heatmaps-time-data
https://nbviewer.jupyter.org/github/python-visualization/folium/blob/master/examples/Plugins.ipynb
https://alysivji.github.io/getting-started-with-folium.html



Create a function

def map_points(df, lat_col='latitude', lon_col='longitude', zoom_start=11, \
                plot_points=False, pt_radius=15, \
                draw_heatmap=False, heat_map_weights_col=None, \
                heat_map_weights_normalize=True, heat_map_radius=15, label_col_name = None,
              use_existing_map = False, existing_map = None):
    """Creates a map given a dataframe of points. Can also produce a heatmap overlay

    Arg:
        df: dataframe containing points to maps
        lat_col: Column containing latitude (string)
        lon_col: Column containing longitude (string)
        zoom_start: Integer representing the initial zoom of the map
        plot_points: Add points to map (boolean)
        pt_radius: Size of each point
        draw_heatmap: Add heatmap to map (boolean)
        heat_map_weights_col: Column containing heatmap weights
        heat_map_weights_normalize: Normalize heatmap weights (boolean)
        heat_map_radius: Size of heatmap point

    Returns:
        folium map object
    """

    ## center map in the middle of points center in
    middle_lat = df[lat_col].median()
    middle_lon = df[lon_col].median()

    if use_existing_map:
        curr_map = existing_map
    else:
        curr_map = folium.Map(location=[middle_lat, middle_lon],
                          zoom_start=zoom_start)

       

       
    # add points to map
    if plot_points:
        for _, row in df.iterrows():
            folium.CircleMarker([row[lat_col], row[lon_col]],
                                radius=pt_radius,
                                popup=row[label_col_name]
                                #fill_color="#3db7e4", # divvy color
                               ).add_to(curr_map)

    # add heatmap
    if draw_heatmap:
        # convert to (n, 2) or (n, 3) matrix format
        if heat_map_weights_col is None:
            cols_to_pull = [lat_col, lon_col]
        else:
            # if we have to normalize
            if heat_map_weights_normalize:
                df[heat_map_weights_col] = \
                    df[heat_map_weights_col] / df[heat_map_weights_col].sum()

            cols_to_pull = [lat_col, lon_col, heat_map_weights_col]

        stations = df[cols_to_pull].to_numpy()
        curr_map.add_child(plugins.HeatMap(stations, radius=heat_map_radius))

    return curr_map



2. Create a dataframe with lon, lat, popup display, weight for heatmap




mp = map_points(disp[['mstr_latitude','mstr_longitude','ticket_rate_in_peak','site_id','diff']], lat_col='mstr_latitude', lon_col='mstr_longitude', zoom_start=6, \
                plot_points=True, pt_radius=1, \
                draw_heatmap=False, heat_map_weights_col='diff', \
                heat_map_weights_normalize=False, heat_map_radius=15, label_col_name = 'ticket_rate_in_peak')

mp_1 = map_points(disp_heat.loc[:,['mstr_latitude','mstr_longitude','ticket_rate_in_peak','site_id','diff']], lat_col='mstr_latitude', lon_col='mstr_longitude', zoom_start=6, \
                plot_points=False, pt_radius=2, \
                draw_heatmap=True, heat_map_weights_col='ticket_rate_in_peak', \
                heat_map_weights_normalize=True, heat_map_radius=20, label_col_name = 'ticket_rate_in_peak', use_existing_map = True, existing_map = mp)

plugins.Fullscreen(
    position='topright',
    title='Expand me',
    title_cancel='Exit me',
    force_separate_button=True
).add_to(mp_1)

mp_1.save('C:/Users/mohamed.ibrahim/Google Drive/box/TAWAL/05_data/predmain/01_code/arc/SCECO_outage_raw_may_june.html')

mp_1


No comments:

Post a Comment

Loud fan of desktop

 Upon restart the fan of the desktop got loud again. I cleaned the desktop from the dust but it was still loud (Lower than the first sound) ...