Geographical visualization of a route or road path such as bus transits in Python

Wins with Data
3 min readOct 21, 2022

--

This tutorial shows how to visualize an interactive map in python. We visualize a map of polyline or a route or road map in python. We use an example of transit or bus route map to visualize in this blog. Given a set of pre-determined coordinates, latitudes and longitudes, we set up customization for choice of icons of transit bus stops, colors, patterns of connected lines or polylines and so on and so forth.

In the following we provide and step-by-step python tutorial on map or GIS visualization using folium package examples.

  • Goal of this video tutorial: visualize a road route or path such as bus transit route in a geographical map given its coordinates

The video tutorial for this blog is available:

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Step1: initial settings

In this tutorial, we connect the code to a google sheet spreadsheet where the coordinates information exist.

# required packages and initial settings for reading a google sheet data from python in google colab
from google.colab import auth
auth.authenticate_user()
import gspread
from google.auth import default
creds, _ = default()
gc = gspread.authorize(creds)# other required packages depending on the need
import pandas as pd
import folium

Read the coordinates data file

Step2: reading the coordinates of the route as a data frame object

spreadsheet1 = gc.open('sample bus route coordinates')
worksheet1 = spreadsheet1.worksheet('Sheet1')
# get_all_values gives a list of rows.
rows = worksheet1.get_all_values()
# Convert to a DataFrame and render.
data = pd.DataFrame.from_records(rows[1:],columns=rows[0])

Step3: initialize the road map from its starting point

m = folium.Map(location=[43.07763272871393, -79.75923542123184],
zoom_start=13)
# in this example, 43.07763272871393, -79.75923542123184 are the coordiantes of starting point

Step4: separate coordinates to two lists of latitudes and longitudes

Visualize the path and specific points in the path

# use this step if your coordinates are comma separated in format of x,y in one column
# place_lat = data['coordinates'].str.split(',', expand=True)[0].astype(float).tolist()
# place_lng = data['coordinates'].str.split(',', expand=True)[1].astype(float).tolist()# use this step if your coordinates are already seaprated in two columns
place_lat = data['latitude'].astype(float).tolist()
place_lng = data['longitude'].astype(float).tolist()

Step5: read the coordinates of the whole path from the file and add to the visual

points = []# read a series of points from coordinates and assign them to points object
for i in range(len(place_lat)):
points.append([place_lat[i], place_lng[i]])
# specify an icon of your desired shape or chosing in place for the coordinates points
for index,lat in enumerate(place_lat):
folium.Marker([lat,
place_lng[index]],
popup=('Bus Station{} \n '.format(index))
,icon = folium.Icon(color='blue',icon_color='white',prefix='fa', icon='bus')
).add_to(m)

Step6: visualize the map

Related links:

--

--

Wins with Data
Wins with Data

Written by Wins with Data

Economist by training (PhD) & Data Scientist by trade. Data Science Lead in industry, former academic. Find me in socials: https://linktr.ee/winswithdata

No responses yet