Unveiling Earthquake Patterns: A Geospatial Analysis with Python, Streamlit, and Folium

Shravani Hariprasad
4 min readMar 4, 2024

--

Introduction:

Earthquakes are among the most unpredictable natural disasters, impacting communities worldwide. Analyzing earthquake data not only aids in disaster management but also contributes to scientific research on seismic activity. In this tutorial, we’ll harness the power of Python along with Streamlit and Folium to conduct geospatial analysis on earthquake data, unraveling patterns and trends.

Setting up the Environment:

Begin by ensuring you have the necessary libraries installed and import them. We’ll be utilizing Streamlit for creating interactive web applications, Pandas for data manipulation, Folium for geospatial visualization, and Geopy for geocoding.

import streamlit as st
import pandas as pd
import folium
from streamlit_folium import st_folium
from folium import plugins
from geopy.geocoders import Nominatim
import time

Loading and Exploring the Data:

Our analysis will be based on the earthquake dataset sourced from Kaggle, encompassing 782 seismic events worldwide between 1/1/2001 and 1/1/2023. Let’s commence by giving a title to our stream lit app and then load the dataset using Pandas and obtaining an overview of its structure.

#set the title
APP_TITLE = 'Earthquake Analysis'
APP_SUB_TITLE = 'Source: Kaggle Earthquake Dataset'

#load the dataset
df = pd.read_csv('data/earthquake.csv')
df.head()

Geocoding Locations:

In our earthquake analysis project, one crucial aspect is geocoding locations to latitude and longitude coordinates. This process is essential for accurately plotting earthquake occurrences on maps and conducting geospatial analysis.

The get_lat_lon Function:

To facilitate geocoding, we’ve implemented a Python function named get_lat_lon. Internally, the function utilizes the Nominatim geocoding service, which translates location strings into corresponding latitude and longitude coordinates. This service leverages OpenStreetMap data to provide accurate geospatial information.

def get_lat_lon(location):
geolocator = Nominatim(user_agent="geoapi")
loc = geolocator.geocode(location)
if loc:
return loc.latitude, loc.longitude
else:
return None, None

Geo-Spatial Analysis:

Conducting geo-spatial analysis enhances our understanding of seismic activity. We’ll utilize Folium’s capabilities to plot earthquake occurrences on an interactive map, exploring geographical correlations and clustering.

Visualizing Earthquakes by Country:

Streamlit’s sidebar enables users to select a specific country for viewing earthquake distribution. Leveraging Folium, we’ll generate a heatmap centered around the chosen country, facilitating intuitive visualization of seismic activity.

# Sidebar filter for country selection
selected_country = st.sidebar.selectbox("Select Country", df["country"].unique())
st.write(f"Earthquakes in {selected_country}")

# Filter data based on selected country
filtered_df = df[df["country"] == selected_country]

# Get latitude and longitude of the selected country
country_lat, country_lon = get_lat_lon(selected_country)

if country_lat is not None and country_lon is not None:
# Create map centered around selected country
country_map = folium.Map(location=[country_lat, country_lon], zoom_start=5)

# Generate heatmap data
heat_map_data = filtered_df[["latitude", "longitude"]].values.tolist()

# Add heatmap layer to map
c_map=country_map.add_child(plugins.HeatMap(heat_map_data, min_opacity=0.3, radius=13))

# Display the map
st_c_map = st_folium(c_map, width=700, height=450)
else:
st.write("Location data not found for the selected country.")
Earthquakes in Japan
Earthquakes in Indonesia
Earthquakes in Papua New Guinea

Analyzing Worldwide Earthquake Distribution:

Moving forward, we’ll delve into the global distribution of earthquakes. By creating a heat map encompassing all seismic events worldwide, centered around the mean latitude and longitude of the dataset, we can discern overarching patterns and hotspots.

# Display the title
st.write("Earthquakes Across the World - Heat Map")

# Create the base map centered around the mean latitude and longitude of the dataset
world_map = folium.Map(location=[df["latitude"].mean(), df["longitude"].mean()], zoom_start=1)

# Add different tile layers
folium.TileLayer('cartodbdark_matter').add_to(world_map)

# # Add Layer Control to enable switching between different tile layers
# folium.LayerControl().add_to(world_map)

# Generate heatmap data
heat_map_data = df[["latitude", "longitude"]].values.tolist()

# Add HeatMap layer to the map
plugins.HeatMap(heat_map_data, min_opacity=0.3, radius=13).add_to(world_map)

# Display the map
st_folium(world_map, width=700, height=450)
Earthquakes across the world

Exploring Earthquakes by Year:

Streamlit’s sidebar offers the flexibility to filter earthquake data by year. Visualizing earthquake distribution for the selected year allows us to identify noteworthy events and trends, facilitating deeper insights.

selected_year = st.sidebar.selectbox("Select Year", df["Year"].unique())
st.write(f"Earthquakes in {selected_year}")

# Filter data based on selected year
filtered_df = df[df["Year"] == selected_year]

# Get latitude and longitude of the selected year
year_lat, year_lon = get_lat_lon(selected_year)

if year_lat is not None and year_lon is not None:

# Create map centered around selected year
year_map = folium.Map(location=[year_lat, year_lon], zoom_start=1)
# Generate heatmap data
heat_map_data = filtered_df[["latitude", "longitude"]].values.tolist()
# Add heatmap layer to map
y_map=year_map.add_child(plugins.HeatMap(heat_map_data, min_opacity=0.3, radius=13))
# Display the map
st_y_map = st_folium(y_map, width=700, height=450)
else:
st.write("Location data not found for the selected country.")
Earthquakes by Year

Conclusion:

In this comprehensive tutorial, we’ve harnessed the capabilities of Python, Streamlit, and Folium to conduct extensive geospatial analysis on earthquake data. By leveraging interactive visualizations and geo-spatial techniques, we’ve gained valuable insights into seismic patterns and trends. I encourage you to explore the code further and delve into the nuances of earthquake analysis.

References:

Code Repository: The code utilized in this tutorial is available on GitHub: https://github.com/shravani-01/Earthquake-Analysis

--

--