Search code examples
pythonfolium

Plotting Origin and Destination clusters with Folium


I have a dataset of origin and destination coordinates that basically look like this:

import pandas as pd
import numpy as np

index = [1, 2, 3, 4, 5]
o_lt = pd.Series([19.285423, 19.285423, 19.639463, 19.631464, 19.631464], index = index)
o_lg = pd.Series([-99.126699, -99.126699, -99.094227, -99.123784, -99.123784], index = index)
d_lt = pd.Series([19.359331, 19.368288, 19.443874, 19.443874, 19.443874], index = index)
d_lg = pd.Series([-99.270102, -99.259745, -99.153412, -99.153412, -99.153412], index = index)
cluster = pd.Series([33, 33, 33, 33, 33], index = index)

cdmx_df = pd.DataFrame(np.c_[o_lt, o_lg, d_lt, d_lg, cluster], columns = ["origin_latitude", 'origin_longitude', 'destination_latitude', 'destination_longitude', 'cluster'])

print(cdmx_df)

My dataset contains 1,600,000 observations. So what I did is a k-means clustering and that is how I got the column cluster.

Now what I want to do next is plot my observations in a map in folium. I want to observe both origin and destination, to see which routes are being demanded the most.

I am doing this with folium:

cdmx_map = folium.map.FeatureGroup()

for lat1, lng1, lat2, lng2, cluster in zip(cdmx_df['origin_latitude'],
                                                 cdmx_df['origin_longitude'],
                                                 cdmx_df['destination_latitude'],
                                                 cdmx_df['destination_longitude'],
                                                 cdmx_df['cluster'],):
    cdmx_map.add_child(
        folium.vector_layers.CircleMarker(
            [lat1, lng1, lat2, lng2],
            
            color = 'green',
            fill = True,
            fill_color = 'blue',
            fill_opacity = 0.6,
            tooltip = str(lng1) + ',' + str(lat1) + ',' + str(lat2) + ',' + str(lng2)
        )
    )

The problem with this code is that it only lets me plot using one coordinate :´( So it is not running.

Is there anyway to plot my destination coordinates and origin coordinates in a single folium map?


Solution

  • Your purpose is a bit unclear. My understanding is how to draw a starting point and a destination on one map. So, create a group and add a circle marker for the starting point and a circle marker for the destination point to the group. Finally, associate the group with the map. This code corresponds to one cluster, but if multiple clusters are needed, add a cluster creation loop process outside of the current loop process.

    In your code, the circle markers are used to set the latitude and longitude of the starting point for the location and the latitude and longitude of the destination point for the location, but the circle markers are set as a single latitude and longitude, and if marker polylines are needed, they are set as a two-dimensional list.

    import folium
    from folium import FeatureGroup, LayerControl
    
    cdmx_map = folium.map.FeatureGroup(name='Origin to destination')
    
    m = folium.Map(location=[cdmx_df['origin_latitude'].mean(), cdmx_df['origin_longitude'].mean()], zoom_start=10)
    
    for lat1, lng1, lat2, lng2, cluster in zip(cdmx_df['origin_latitude'],
                                                     cdmx_df['origin_longitude'],
                                                     cdmx_df['destination_latitude'],
                                                     cdmx_df['destination_longitude'],
                                                     cdmx_df['cluster'],):
    
        origin = folium.vector_layers.CircleMarker(
            [lat1, lng1],
            radius=10,
            color = 'green',
            fill = True,
            fill_color = 'green',
            fill_opacity = 0.6,
            tooltip = str(lng1) + ',' + str(lat1)
            ).add_to(cdmx_map)
    
        destination = folium.vector_layers.CircleMarker(
            [lat2, lng2],
            radius=10,
            color = 'red',
            fill = True,
            fill_color = 'red',
            fill_opacity = 0.6,
            tooltip = str(lat2) + ',' + str(lng2)
            ).add_to(cdmx_map)
        
    cdmx_map.add_to(m)
    LayerControl().add_to(m)
    m
    

    enter image description here