Week 7: Geospatial analysis in Python

2025-06-18

Welcome to Week 7!

Theme: Geospatial analysis in Python

This week we’ll explore how Python reads and manipulates geospatial data.

You’ll learn:

  • Some GIS essentials such as vector data, projection, file types
  • How to read and inspect geopatial data
  • How to create a GeoDataFrame from a CSV and perform a spatial join
  • We’ll end by building a choropleth map

Data models

Raster
- divides surface into regular cells
- similar to digital images
- e.g. remote sensing, elevation

Vector
- features defined by x,y coordinates
- classified into points, lines and polygons
- e.g. administrative data

Maps distort reality

Map projections

Figure 1: Mercator

Figure 2: Gall-Peters

Coordinate reference systems

Geographic CRS

- position on the Earth’s surface
- degrees
- latitude and longitude
- position relative to the equator and the prime meridian
- e.g. World Geodetic System (WGS84)

Projected CRS

- projects the surface of the earth onto a 2D plane
- metres
- x, y
- e.g. British National Grid (BNG)

Python

We use Python’s GeoPandas library to read and manipulate geospatial data.

Geospatial data is read as a GeoDataFrame, a regular Pandas DataFrame with an additional .geometry column. The .geometry column contains the coordinates of each feature such as a state boundary. The attributes of each geometry (e.g. state name) are stored in other columns of the dataset.

For example, the following code uses the GeoPandas library to read an ESRI shapefile of the countries of the world, selects Nigeria and then prints out the first few rows.

import geopandas as gpd
world = gpd.read_file("data/ne_110m_admin_0_countries.shp")
nigeria = world[world['ADMIN'] == 'Nigeria']
nigeria.head()

Further reading