Plotting Lines and Multilines on Maps Using Folium
Last Updated :
22 Jul, 2024
Folium is a powerful Python library that simplifies the process of creating interactive maps using Leaflet.js. One of its many features includes the ability to add lines and multilines to a map, which can be useful for visualizing routes, boundaries, and other linear data. This article will guide you through the steps to print a line or multiline in a new layer over a map using Folium.
Plotting Lines and Multilines
Folium is a Python library that bridges the gap between Python data manipulation and the Leaflet.js library. It allows for the creation of interactive maps that can be easily embedded in Jupyter notebooks and web applications. Folium supports various types of data visualizations, including markers, lines, polygons, and more.
To plot a line or multiline in Folium, you can use the PolyLine
class. This class takes a list of points as input, where each point is a tuple of latitude and longitude coordinates. Here is a basic example of how to use it:
Setting Up Your Environment
Before starting, ensure you have Folium installed in your Python environment. You can install it using pip:
pip install folium
Creating a Basic Map
First, let's create a basic map centered at a specific location. We'll use the folium.Map
class for this purpose.
Python
import folium
# Create a map centered at a specific location
m = folium.Map(location=[45.5236, -122.6750], zoom_start=13)
# Display the map
m
Output:
Basic Map1. Adding a Single Line to the Map
To add a single line to the map, we use the folium.PolyLine
class. This class takes a list of coordinates and other optional parameters such as color, weight, and opacity.
Python
# Define the coordinates for the line
line_coordinates = [
[45.5236, -122.6750],
[45.5289, -122.6800],
[45.5300, -122.6900]
]
# Create a PolyLine object
line = folium.PolyLine(locations=line_coordinates, color='blue', weight=5, opacity=0.8)
# Add the line to the map
line.add_to(m)
# Display the map
m
Output:
Single Line to the Map2. Adding Multiple Lines (Multiline) to the Map
To add multiple lines, you can create several PolyLine
objects and add them to the map. Alternatively, you can use a FeatureGroup
to manage multiple lines more efficiently.
Python
# Define the coordinates for multiple lines
line1_coordinates = [
[45.5236, -122.6750],
[45.5289, -122.6800]
]
line2_coordinates = [
[45.5300, -122.6900],
[45.5350, -122.6950]
]
# Create PolyLine objects
line1 = folium.PolyLine(locations=line1_coordinates, color='red', weight=5, opacity=0.8)
line2 = folium.PolyLine(locations=line2_coordinates, color='green', weight=5, opacity=0.8)
# Add the lines to the map
line1.add_to(m)
line2.add_to(m)
# Display the map
m
Output:
Multiple Lines (Multiline) to the MapCustomizing Line Properties Over a Map
Folium allows you to customize various properties of the lines, such as color, weight, and opacity. You can also add tooltips and popups to the lines for additional interactivity.
Python
# Create a PolyLine with custom properties
custom_line = folium.PolyLine(
locations=line_coordinates,
color='purple',
weight=10,
opacity=0.5,
tooltip='Custom Line',
popup='This is a custom line'
)
# Add the custom line to the map
custom_line.add_to(m)
# Display the map
m
Output:
Customizing Line PropertiesAdding Lines to a New Layer
To add lines to a new layer that can be toggled on and off, use the FeatureGroup
class. This allows you to group multiple lines and control their visibility.
Python
# Create a FeatureGroup for the lines
lines_layer = folium.FeatureGroup(name='Lines Layer')
# Add lines to the FeatureGroup
line1.add_to(lines_layer)
line2.add_to(lines_layer)
# Add the FeatureGroup to the map
lines_layer.add_to(m)
# Add LayerControl to the map to toggle layers
folium.LayerControl().add_to(m)
# Display the map
m
Output:
Adding Lines to a New LayerOpen the map_with_lines.html
file in your web browser to see the interactive map with lines and layers.
Conclusion
In this article, we covered how to print a line or multiline in a new layer over a map using Folium. We started with creating a basic map, adding single and multiple lines, customizing line properties, and finally adding lines to a new layer. Folium's flexibility and ease of use make it an excellent choice for creating interactive maps in Python.