Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Open In Colab Open In Kaggle

Exercise 1: Build a GeoDataFrame

Create a GeoDataFrame of two points, A at (7.4, 46.9) and B at (8.5, 47.4), in EPSG:4326. Print the epsg code and the geometry column.

Exercise 2: Write and read GeoJSON

Write the GeoDataFrame from exercise 1 to _files/points.geojson, read it back, and confirm the shape and that the crs is preserved.

Exercise 3: Reproject

Reproject the points to EPSG:2056 and print the new epsg code and the projected coordinates of point A (in metres, rounded to the nearest metre).

Exercise 4: Measure correctly

Compute the distance between A and B in kilometres. Reproject to EPSG:2056 first, and state in a comment why measuring in EPSG:4326 would be wrong.

Exercise 5: Spatial join

Given the polygon below (EPSG:4326), use sjoin with the within predicate to find which of the two points lie inside it.

from shapely.geometry import Polygon
poly = gpd.GeoDataFrame({"zone": ["z"]},
    geometry=[Polygon([(7, 46.5), (8, 46.5), (8, 47.5), (7, 47.5)])], crs="EPSG:4326")

Exercise 6: Buffer and area

Reproject the points to EPSG:2056, buffer each by 10 km, and print the area of one buffer in km² (it should be close to the analytical value pi times 10² = 314 km²).

Exercise 7: Dissolve

Create a GeoDataFrame of two adjacent polygons that share the attribute type = "flood", then dissolve by that attribute and confirm the result is a single merged polygon.

Exercise 8: Hurricane track analysis

A satellite view of Hurricane Florence over the Atlantic, a clear eye at the centre of a broad spiral of cloud

Figure 1:Hurricane Florence on 11 September 2018, three days before landfall in North Carolina. Image from NASA Worldview via Wikimedia Commons, public domain.

Can you quickly find out which US states Hurricane Florence passed through using geopandas?

Apply geopandas to read in the geospatial data, plot, and analyse the track of Hurricane Florence from 30 August to 18 September 2018. The track is the archived advisory record from the National Hurricane Center; the state boundaries are a US Census Bureau cartographic boundary file.

References:

  1. Introduction to GeoPandas — geopandas’ official website

  2. Geopandas: an introduction — Automating GIS Processes

  3. Use Data for Earth and Environmental Science in Open Source Python

  4. The Shapely User Manual

  5. Geospatial Analysis with Python and R

Q1) Import geopandas and pandas.

Import geopandas as gpd, pandas as pd, and matplotlib’s pyplot as plt.

Q2) Read the state boundary file with geopandas’ read_file function.

The file is cached at states_path. Call the result country.

Q3) Have a look at the data. What type of geometries does it contain?

Print the first few rows, and answer the geometry question in a comment.

Q4) Have a look at the data on a map using geopandas’ .plot() method.

Exclude Alaska and Hawaii using the NAME attribute and pandas’ .isin() method. Specify the figsize to be 30 x 20.

Q5) Create a GeoDataFrame from the florence DataFrame.

Build the geometry column from the Long and Lat columns with gpd.points_from_xy, and call the result gdf_florence. Then look at its first few rows.

Q6) Plot the US states map (without Alaska and Hawaii) and hurricane Florence together.

Draw the states as a base map, then plot the hurricane positions on top in a colour that stands out.

Q7) What is the coordinate reference system of the data?

Check it for both layers, and say in a comment whether they match — two layers only line up on one pair of axes if they share a crs.

Q8) Which states did the hurricane pass through?