Search code examples
pythongeospatialgeopandasfolium

Plotting longitude and latitude on a map and create line for each subset (every row in DataFarame)


I worked a lot on my data set to make it like following. I know how to plot each latitude and longitude in folium as below. I have no idea how to plot line (Polygone) between each pairs of latitude and longitude of my data set in each row. As we see each row is combination of latitude and longitude.

The code is for showing each point( lat and long) on the map.

Notice: I didn't write this code for the provided DataFrame. it is for a data frame with a column named "start_station_latitude" and other column start_station_longitude.

 
import matplotlib.pyplot as plt

#installation
# Create a world map to show distributions of users 
import folium
from folium.plugins import MarkerCluster
#empty map
world_map= folium.Map(tiles="CartoDB dark_matter")
marker_cluster = MarkerCluster().add_to(world_map)
#for each coordinate, create circlemarker of user percent
for i in range(len(trip_counts_10)):
        lat = trip_counts_10.iloc[i]['start_station_latitude']
        long = trip_counts_10.iloc[i]['start_station_longitude']
           
        
        radius=5
        popup_text = """Users : {}<br>"""
        popup_text = popup_text.format(trip_counts_10.iloc[i]['start_station_name'])


        folium.CircleMarker(location = [lat, long], radius=radius, popup= popup_text, fill =True).add_to(marker_cluster)

plt.savefig('x', dpi=300)
#show the map
world_map
#tiles="CartoDB dark_matter"
#cartodbpositron

        lan0     lan1       lan2           long0      long1      long2
    59.915667   59.913796   59.922539   10.777567   10.735802   10.704541
    59.929853   59.919463   59.910356   10.711515   10.743829   10.705106
    ....

I also wrote the code below to get a list of each row, But I don't know what I should do the next.

import geopandas as gpd
from shapely.geometry import Polygon

Row_list = []
for index, rows in indices_df.iterrows():
    my_list =[rows.lan0, rows.lan1, rows.lan2,rows.long0, rows.long1, rows.long2]
    Row_list.append(my_list)
print(Row_list)
    #polygon_geom = Polygon(zip(lon_point_list, lat_point_list))
    

Solution

  • I'm not sure I understood your question perfectly. How I understand it, you have a df with three latitudes and three longitudes on every row and you want to draw a line throug these three points. If that is what you mean, you could try this:

    folium.PolyLine(points).add_to(world_map)
    

    Here points is a list of tuples with latitude and longitude. You could make this list like this:

    mid_i = int(len(my_list)/2)
    points = [(my_list[i], my_list(mid_i + i]) for i in range(mid_i)]
    

    If you would want the line to be closed, you just add the first point to the end of this list.

    points.append(points[0])
    

    See this page for more info.

    Also, I think you made some questionable design choises. Discussing these is not really the point of this answer but I would advise you to never use the name lan for latitude, that makes no sense.