Creating Maps from Geolocations in Python with Folium

Alexandra Yanina
4 min readMar 18, 2023

--

Photo by Z on Unsplash

Introduction:

In this blog article, we will learn how to create maps from geolocations in Python using the Folium library. Folium is a powerful and easy-to-use library for generating interactive maps with various features, such as markers and popups. We will be using randomly generated geopoints within Europe to demonstrate this process.

The following code example shows how to generate a map with random geopoints in Europe. The code will be explained later.

import random
import folium

def generate_random_geopoints_europe(num_points):
lat1, lng1 = 36.022, -9.393
lat2, lng2 = 71.919, 31.852
geopoints = [(random.uniform(lat1, lat2), random.uniform(lng1, lng2)) for x in range(num_points)]
return geopoints

points = generate_random_geopoints_europe(10)
print(points)

m = folium.Map(location=[50.0, 10.0], zoom_start=4)

for geopoint in points:
folium.Marker(location=[geopoint[0], geopoint[1]]).add_to(m)

m.save('map.html')

Now, let's create a more comprehensive example by adding popups with additional information for each marker:

import folium
import pandas as pd

geopoints = generate_random_geopoints_europe(10)

df = pd.DataFrame({
'Name': [f'Point {chr(65 + i)}' for i in range(10)],
'Description': [f'Description {chr(65 + i)}' for i in range(10)]
})

m = folium.Map(location=[50.0, 10.0], zoom_start=4)

for i, geopoint in enumerate(geopoints):
popup = folium.Popup(f'<b>{df["Name"][i]}</b><br>{df["Description"][i]}')
folium.Marker(location=[geopoint[0], geopoint[1]], popup=popup).add_to(m)

m.save('map_with_popups.html')

Step 1: Import required libraries

To get started, we need to import the required libraries, which are random, folium, and pandas:

import random
import folium
import pandas as pd

Step 2: Generate random geopoints

def generate_random_geopoints_europe(num_points):
lat1, lng1 = 36.022, -9.393
lat2, lng2 = 71.919, 31.852
geopoints = [(random.uniform(lat1, lat2), random.uniform(lng1, lng2)) for x in range(num_points)]
return geopoints

The function above generates random geopoints (lat and long pairs). With the num_points argument you can specify the amount of points to be created. All generated points are in the boundaries of Europe. Let's break down the code step by step:

  • def generate_random_geopoints_europe(num_points):: This line defines the function and accepts one parameter, num_points, which indicates the number of random geopoints to generate.
  • lat1, lng1 = 36.022, -9.393: These are the coordinates of the southwest corner of the bounding box that contains Europe.
  • lat2, lng2 = 71.919, 31.852: These are the coordinates of the northeast corner of the bounding box that contains Europe.
  • geopoints = [(random.uniform(lat1, lat2), random.uniform(lng1, lng2)) for x in range(num_points)]: The random.uniform(a, b) function returns a random float number between a and b. The code uses a list comprehension to generate a list of num_points geopoints, where each geopoint is a tuple with two float numbers: the random latitude (between lat1 and lat2) and the random longitude (between lng1 and lng2).
  • return geopoints: This line returns the list of generated geopoints.

In summary, this function generates a list of random geopoints within the specified boundaries of Europe. You can call this function with the desired number of points, and it will return a list of tuples containing the latitude and longitude for each point.

Step 3: Create a basic map with markers

Now, we can create a map with markers for the random geopoints using Folium:

points = generate_random_geopoints_europe(10)

m = folium.Map(location=[50.0, 10.0], zoom_start=4)

for geopoint in points:
folium.Marker(location=[geopoint[0], geopoint[1]]).add_to(m)

m.save('map.html')

This code will generate a basic map with markers for each of the random geopoints and save it as an HTML file named map.html. To display the map in Jupyter Notebook, execute a cell with the content m.

Step 4: Add popups with additional information

To make our map more informative, we can add popups containing additional information for each marker. We will use a Pandas DataFrame to store the information:

geopoints = generate_random_geopoints_europe(10)

df = pd.DataFrame({
'Name': [f'Point {chr(65 + i)}' for i in range(10)],
'Description': [f'Description {chr(65 + i)}' for i in range(10)]
})

Now, we will create a map with markers and popups for each random geopoint:

m = folium.Map(location=[50.0, 10.0], zoom_start=4)

for i, geopoint in enumerate(geopoints):
popup = folium.Popup(f'<b>{df["Name"][i]}</b><br>{df["Description"][i]}')
folium.Marker(location=[geopoint[0], geopoint[1]], popup=popup).add_to(m)

m.save('map_with_popups.html')

This code will generate a map with markers and popups for each of the random geopoints, displaying the name and description from the DataFrame, and save it as an HTML file named map_with_popups.html. To display the map in Jupyter Notebook, execute a cell with the content m.

Conclusion

In this blog article, we learned how to create maps from geolocations in Python using the Folium library. We showed how to generate random geopoints within Europe, create a basic map with markers, and enhance the map with popups containing additional information. By following these steps, you can create interactive maps with customized markers and popups using Folium in your own projects.

If you liked this article and would like to learn more about using Python for data related tasks and other topics, make sure to follow my profile!

To ensure you've grasped the principles explained in this article, here's a short quiz:

Which Python library is used to create interactive maps in this article?
a) Geopy
b) Folium
c) Matplotlib

What is the purpose of the generate_random_geopoints_europe function?
a) To generate a map of Europe
b) To create random geopoints within the boundaries of Europe
c) To add markers to the map

How do you add a marker to a Folium map?
a) folium.Map(location=[latitude, longitude])
b) folium.Marker(location=[latitude, longitude])
c) folium.Marker(location=[latitude, longitude]).add_to(map)

How can you add a popup with additional information to a marker?
a) Use folium.Popup with the marker's location
b) Use folium.Popup with the desired content and add it to the marker
c) Use folium.Popup with the desired content and location

Answers:

  1. b
  2. b
  3. c
  4. b

Some parts of this tutorial were created with the help of chatGPT.

--

--

Alexandra Yanina
Alexandra Yanina

Written by Alexandra Yanina

Data Scientist, Working on predicting deforestation in the brazilian amazon with deep learning methods

No responses yet