Skip to content
Ahmad Ammar

1. Before you start

This guide rewrites the Py4Geo tutorials of 2020 for current tools. Those notebooks read local files with the GDAL bindings; here everything runs on one real Sentinel-2 image of Belfast, read directly from Microsoft’s Planetary Computer, which needs no account, so the notebook runs the same on any computer or in Google Colab.

1.1 What you will learn

  • Finding data: what a STAC catalogue is and how to search it by place, date and cloud cover.

  • Rasters: bands, coordinate reference systems, pixel size and transforms; reading only the part of a cloud-optimised image you need; turning stored numbers into reflectance, including the offset newer Sentinel-2 images carry.

  • Indices: true and false colour, and what NDVI, NDWI and NDBI measure and how to read them.

  • Vectors: points and buffers with geopandas, and zonal statistics.

  • Methods: k-means clustering and how to interpret its clusters; regridding by aggregation and decimation; interpolation, and how to measure its error honestly.

1.2 Getting Python and the notebook

Open the Notebooks page and choose Open in Colab; the first cell installs what Colab lacks. On your own computer:

conda create -n sat python=3.12 numpy pandas matplotlib scipy scikit-learn rasterio geopandas pystac-client planetary-computer jupyterlab
conda activate sat
jupyter lab

Boxes labelled In [n] are code and Out [n] what it printed or drew. Run the cells in order from the top. The image is read over the internet, so the first run takes a minute or two.

1.3 Setup

This notebook is the modern version of the Py4Geo tutorials (2020): raster and vector data, spectral indices, unsupervised classification and regridding, now on a real Sentinel-2 image read straight from the cloud. The image comes from Microsoft's Planetary Computer, which needs no account. The area is Belfast and Belfast Lough.

Libraries: pystac-client searches catalogues of satellite images; planetary-computer signs the links so the files can be read; rasterio reads the images; numpy does arithmetic on them; geopandas handles vector data (points and polygons); scikit-learn does the clustering; scipy the interpolation; matplotlib the figures. The first cell installs what Google Colab lacks.

In [1]
import importlib.util, os, subprocess, sys
os.environ.setdefault("LOKY_MAX_CPU_COUNT", "4")          # avoids a harmless CPU-count warning from scikit-learn on Windows
need = [p for p in ["pystac_client", "planetary_computer", "rasterio", "geopandas"] if importlib.util.find_spec(p) is None]
if need:
    subprocess.run([sys.executable, "-m", "pip", "install", "-q", *[p.replace("_", "-") for p in need]], check=True)
import numpy as np
import pandas as pd
import geopandas as gpd
import rasterio
from rasterio.windows import from_bounds
from rasterio.warp import transform_bounds
from shapely.geometry import Point
import pystac_client, planetary_computer
from sklearn.cluster import KMeans
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
plt.rcParams.update({"figure.dpi": 110})
rng = np.random.default_rng(1)