Mapping with Python: Drawing Points and Routes using Folium.

Rohit Kumar Prajapati
Python’s Gurus
Published in
4 min readJun 1, 2024
source: wallpapers.com

In Python, we have a variety of modules to work with maps. Today, I will use Folium, a powerful library that is both easy to grasp and use. Folium leverages the data-wrangling strengths of the Python ecosystem and the mapping strengths of the Leaflet.js library, allowing you to manipulate your data in Python and visualize it in a Leaflet map via Folium.

Drawing Paths on a Map

In this tutorial, we will draw a path on a map using source and destination coordinates.

Points and Paths

  • Point: We need latitude and longitude coordinates to identify and mark a point.
  • Path (Routes): For a path, we need multiple points as we will draw a line connecting these points.

I’m going to use Kaggle geolocation data (Kaggle Dataset) to get latitude and longitude coordinates and place names.

Additionally, we will use OpenRouteService to get path coordinates. You can use any data source you prefer.

Libraries Used

We will use the following libraries:

  • Folium
  • Pandas
  • OpenRouteService

Step-by-Step Guide

Step 1: Get Data with Coordinates details

First, we’ll read the Kaggle cities data and select three cities to create points using Folium.

import pandas as pd

cities_df = pd.read_csv("/content/cities.csv")
cities_df = cities_df[["name", "latitude", "longitude", "country_name"]]
cities_df = cities_df[cities_df["country_name"]=="India"]
cities_df.reset_index(inplace = True, drop = True)
cities_df.index.name = 'id'
cities_df.sample(3)

Step 2: Initialize Map Canvas

Let’s initialize the map canvas. Here, the location refers to a position on the map based on coordinates, showing a specific area when the map is generated.

I'll use `Binaur` coordinates

import folium as F

map = F.Map(location=(26.383333, 80.166667), zoom_start=6)

Folium provides different Map options we can use by adding `tiles` parameters

import folium as F

map = F.Map(location=(26.383333, 80.166667), zoom_start=6, tiles="cartodb positron")

Step 3: Add Points using Markers

To add points, we use the Marker from Folium.

import folium as F

map = F.Map(location=(26.383333, 80.166667), zoom_start=6)

F.Marker((26.383333, 80.166667), popup='Binaur').add_to(map)

Folium provides multiple options for markers, allowing you to customize the appearance and functionality of each marker.

import folium as F

map = F.Map(location=(26.383333, 80.166667), zoom_start=5)

# Add markers for the three points
F.Marker((16.350000, 81.050000), popup='Gudlavalleru', icon=F.Icon(icon="home", prefix='fa', color="lightblue")).add_to(map)
F.Marker((26.383333, 80.166667), popup='Binaur', icon=F.Icon(icon="2", prefix='fa', color="blue")).add_to(map)
F.Marker((28.280720, 78.014110), popup='Shikarpur', icon=F.Icon(icon="", prefix='fa', color="purple")).add_to(map)

Step 4: Create a Path between Points

To create a path or route, we use PolyLine.

# PolyLine
import folium as F

map = F.Map(location=(16.350000, 81.050000), zoom_start=5)

# Add markers for the two points
F.Marker((16.350000, 81.050000), popup='Gudlavalleru').add_to(map)
F.Marker((26.383333, 80.166667), popup='Binaur').add_to(map)

F.PolyLine(locations=[(16.350000, 81.050000), (26.383333, 80.166667)], color='blue').add_to(map)

While this creates a straight line, it is not suitable for navigation as it doesn’t follow the actual roads.

Step 5: Accurate Routing with OpenRouteService

To draw routes accurately, we need detailed coordinates for each turn and road segment. We can get these coordinates using OpenRouteService.

Note: OpenRouteService provides coordinates in the (longitude, latitude) format.

Here’s how to integrate OpenRouteService to get a more accurate path:

# draw accurate path
import openrouteservice as ors
from openrouteservice import convert

API_KEY = 'YOUR_API_KEY'

client = ors.Client(key=API_KEY)

source=(28.280720, 78.014110)
destination = (26.383333, 80.166667)

# Get directions between the two points
# Note: OpenRouteService expects (lng, lat) tuples
directions_coordinates = [tuple(reversed(source)), tuple(reversed(destination))]
route = client.directions(coordinates=directions_coordinates, profile='driving-car', format='geojson')

# extracting only coordinates
routes_coords= [list(reversed(coord)) for coord in route['features'][0]['geometry']['coordinates']]

# o/p
# [[28.280381, 78.013985],
# [28.280266, 78.014388],
# [28.280192, 78.014739],
# [28.280161, 78.014831],
# [28.280103, 78.014876],
# [28.279703, 78.014801],
# [28.279316, 78.014659]] #

We have source to destination all the coordinates by using coordinates we will draw PolyLine.

Customizing Your Line

Folium’s PolyLine also offers various features to customize your line, such as color, weight, opacity, and more. Here is an example:

F.PolyLine(locations=routes_coords,
color='purple',
weight=4, # width of polyline
tooltip="From Shikarpur to Binaur",
smooth_factor=0.1, # for making poliyline straight
).add_to(map)

Conclusion

By using Folium and OpenRouteService, you can effectively plot and customize routes on a map, making it easy to visualize paths and navigation in your Python projects. This combination leverages powerful geospatial tools to create informative and interactive maps.

Python’s Gurus🚀

Thank you for being a part of the Python’s Gurus community!

Before you go:

  • Be sure to clap x50 time and follow the writer ️👏️️
  • Follow us: Newsletter
  • Do you aspire to become a Guru too? Submit your best article or draft to reach our audience.

--

--