Open In Colab

1.12. (Exercise) Hurricane Track Analysis#

376px-Florence_2018-09-11_1750Z.jpg

Can you quickly find out which US states hurricane Florence passed through using GeoPandas?

Image source: NASA Worldview

Tip

Installing GeoPandas

Please run the following code blocks in this section to:

  • Install the GeoPandas’s dependencies and GeoPandas

  • Download and unzip the data used in this notebook

  • Import GeoPandas and other required modules for the notebook

#Install GeoPandas' dependencies
!pip install --upgrade pyshp

!pip install --upgrade shapely

!pip install --upgrade descartes

!pip install --upgrade rtree
Requirement already satisfied: pyshp in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (2.3.1)
Requirement already satisfied: shapely in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (2.0.6)
Requirement already satisfied: numpy<3,>=1.14 in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (from shapely) (1.25.1)
Requirement already satisfied: descartes in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (1.1.0)
Requirement already satisfied: matplotlib in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (from descartes) (3.7.2)
Requirement already satisfied: contourpy>=1.0.1 in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (from matplotlib->descartes) (1.1.0)
Requirement already satisfied: cycler>=0.10 in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (from matplotlib->descartes) (0.11.0)
Requirement already satisfied: fonttools>=4.22.0 in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (from matplotlib->descartes) (4.40.0)
Requirement already satisfied: kiwisolver>=1.0.1 in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (from matplotlib->descartes) (1.4.4)
Requirement already satisfied: numpy>=1.20 in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (from matplotlib->descartes) (1.25.1)
Requirement already satisfied: packaging>=20.0 in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (from matplotlib->descartes) (23.1)
Requirement already satisfied: pillow>=6.2.0 in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (from matplotlib->descartes) (10.0.0)
Requirement already satisfied: pyparsing<3.1,>=2.3.1 in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (from matplotlib->descartes) (3.0.9)
Requirement already satisfied: python-dateutil>=2.7 in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (from matplotlib->descartes) (2.8.2)
Requirement already satisfied: importlib-resources>=3.2.0 in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (from matplotlib->descartes) (6.0.0)
Requirement already satisfied: zipp>=3.1.0 in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (from importlib-resources>=3.2.0->matplotlib->descartes) (3.16.0)
Requirement already satisfied: six>=1.5 in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (from python-dateutil>=2.7->matplotlib->descartes) (1.16.0)
Requirement already satisfied: rtree in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (1.3.0)
#Install GeoPandas
!pip install --upgrade geopandas
Requirement already satisfied: geopandas in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (1.0.1)
Requirement already satisfied: numpy>=1.22 in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (from geopandas) (1.25.1)
Requirement already satisfied: pyogrio>=0.7.2 in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (from geopandas) (0.9.0)
Requirement already satisfied: packaging in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (from geopandas) (23.1)
Requirement already satisfied: pandas>=1.4.0 in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (from geopandas) (2.0.3)
Requirement already satisfied: pyproj>=3.3.0 in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (from geopandas) (3.6.0)
Requirement already satisfied: shapely>=2.0.0 in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (from geopandas) (2.0.6)
Requirement already satisfied: python-dateutil>=2.8.2 in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (from pandas>=1.4.0->geopandas) (2.8.2)
Requirement already satisfied: pytz>=2020.1 in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (from pandas>=1.4.0->geopandas) (2023.3)
Requirement already satisfied: tzdata>=2022.1 in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (from pandas>=1.4.0->geopandas) (2023.3)
Requirement already satisfied: certifi in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (from pyogrio>=0.7.2->geopandas) (2024.8.30)
Requirement already satisfied: six>=1.5 in c:\users\tbeucler\.conda\envs\jb\lib\site-packages (from python-dateutil>=2.8.2->pandas>=1.4.0->geopandas) (1.16.0)
#Download the data used in this notebook
!gdown 1b1lngOIvuNnZxepbT8RyV3KX1itRky5z
'gdown' is not recognized as an internal or external command,
operable program or batch file.
#Unzip the data used in this notebook
!unzip '/content/data.zip'
unzip:  cannot find either '/content/data.zip' or '/content/data.zip'.zip.

References:

  1. Geopandas official website: Introduction to GeoPandas https://geopandas.org/en/stable/getting_started/introduction.html

  2. Automating GIS process https://autogis-site.readthedocs.io/en/latest/notebooks/L2/01-geopandas-basics.html

  3. Use Data for Earth and Environmental Science in Open Source Python https://www.earthdatascience.org/courses/use-data-open-source-python/

  4. The Shapely User Manual https://shapely.readthedocs.io/en/stable/manual.html

  5. Geospatial Analysis with Python and R https://kodu.ut.ee/~kmoch/geopython2020/index.html

  6. Introduction to Geospatial Data in Python https://www.datacamp.com/tutorial/geospatial-data-python

Exercise’s Instructions

  • Apply GeoPandas to read in the geospatial data, plot, and analyse the track of Hurricane Florence from August 30th to September 18th 2018.

  • Either write your own code, or fill in the blanks in the hidden cells 🙂

1.12.1. Q1) import geopandas, pandas#

#Import GeoPandas (as gpd), pandas, and matplotlib
# And ignore warnings
import warnings
warnings.filterwarnings('ignore')
#@markdown Fill in the blank code. Double-click here to reveal
#Import GeoPandas (as gpd), pandas, and matplotlib
import ____ as gpd
import ____ as pd
import matplotlib.pyplot as plt
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[7], line 3
      1 #@markdown Fill in the blank code. Double-click here to reveal
      2 #Import GeoPandas (as gpd), pandas, and matplotlib
----> 3 import ____ as gpd
      4 import ____ as pd
      5 import matplotlib.pyplot as plt

ModuleNotFoundError: No module named '____'

1.12.2. Q2) Read a Shapefile with GeoPandas’s read_file function#

# Read data with the geopandas function
# The path to the file should be /content/data/hurricane-data/gz_2010_us_040_00_5m.json
#@markdown Fill in the blank code. Double-click here to reveal
# Read data with the geopandas function
path = r"/content/data/hurricane-data/gz_2010_us_040_00_5m.json"
country = gpd.____(path)

1.12.3. Q3) Have a look at the data, what type of geometries does the data contain?#

# Print out the first few lines of the data
#@markdown Fill in the blank code. Double-click here to reveal
# The method to see the first few lines of the data
country.____()

1.12.4. Q4) Have a look at the data on a map using GeoPandas’s .plot() method. Exclude Alaska and Hawaii by the attribute ‘NAME’ and Pandas’s .isin() method. Specify the figsize to be 30 x 20.#

# Plot the US states (Alaska and Hawaii excluded)
#@markdown Fill in the blank code. Double-click here to reveal
# Plot the US states (Alaska and Hawaii excluded)
country[country[____].isin([____,____]) == False].____(figsize=(30,20))
# Read in the hurricane florence data, change the longitude, and have a look at the hurricane florence dataframe
path = r"/content/data/hurricane-data/florence.csv"
florence = pd.read_csv(path)
florence = florence.drop(['AdvisoryNumber', 'Forecaster', 'Received'], axis=1)
florence['Long'] = 0 - florence['Long']
florence.head(3)

1.12.5. Q5) Create a GeoDataFrame from florence DataFrame#

# Create a geodataframe from the hurricane florence dataframe
#@markdown Fill in the blank code. Double-click here to reveal
# Create a geodataframe from the hurricane florence dataframe
gdf_florence = gpd.____(florence, geometry=gpd.____(florence.Long, florence.Lat))
# Have a look at the hurricane geodataframe
gdf_florence.head(2)

1.12.6. Q6) Plot the US states map (without Alaska, Hawaii) and hurricane florence together#

# Plot to see the hurricane overlay over the US map

# And plot the hurricane position on top with red color to stand out:
#@markdown Fill in the blank code. Double-click here to reveal
# Plotting to see the hurricane overlay the US map:
fig, ax = plt.subplots(1, figsize=(30,20))
base = country[country[____].isin([____,____]) == False].____(ax=ax, color='#3B3C6E')

# plotting the hurricane position on top with red color to stand out:
gdf_florence.____(ax=base, color='darkred', marker="*", markersize=10)

1.12.7. Q7) What’s the coordinate reference system of the data?#

# Check the coordinate reference system of the geodataframe
#@markdown Fill in the blank code. Double-click here to reveal
# Check the coordinate reference system of the geodataframe
country.____

1.12.8. Q8) Which states are passed by the hurricane?#

Hint

One approach may be plot and see the trajectory of the hurricane on the US map. Other approaches are more than welcome.

# Plot the US states without Alaska and Hawaii, then
#  Annotate the US states with their names, then
# Select the hurricane trajectory points inside the US boundary with the overlay operation, then
# Plot the hurricane trajectory inside US boundary
#@markdown Fill in the blank code. Double-click here to reveal
# Plot the US states without Alaska and Hawaii
df = country[country[____].isin([____,____]) == False]
ax = df.plot(figsize=(30,20))
#  Annotate the US states with their names
_ = df.apply(lambda x: ax.____(s=x['NAME'], xy=x.geometry.centroid.coords[0], ha='center'), axis=1)
# Select the hurricane trajectory points inside the US boundary with the overlay operation
res_intersection = gdf_florence.____(country, how='intersection')
# Plot the hurricane trajectory inside US boundary
____.plot(ax=ax, color='red', marker="*", markersize=25)