"""Hillshade a Defra Survey Data Download DTM tile and save it as a PNG. python hillshade_tile.py data/lidar_composite_dtm-2022-1-SY68ne/SY68ne_DTM_1m.tif \ --window 365900 87700 368100 89100 --out maiden-castle-hillshade.png Needs rasterio, numpy and matplotlib (see requirements.txt). """ from __future__ import annotations import argparse import matplotlib.pyplot as plt import numpy as np import rasterio from matplotlib.colors import LightSource from rasterio.windows import from_bounds def main() -> None: p = argparse.ArgumentParser() p.add_argument("tif") p.add_argument("--window", nargs=4, type=float, metavar=("XMIN", "YMIN", "XMAX", "YMAX"), help="British National Grid bounds to crop to (default: whole tile)") p.add_argument("--out", default="hillshade.png") a = p.parse_args() with rasterio.open(a.tif) as src: win = from_bounds(*a.window, transform=src.transform) if a.window else None z = src.read(1, window=win, masked=True).astype("float64") print(f"CRS {src.crs}, pixel {src.res[0]:g} m, nodata {src.nodata:g}") z = z.filled(np.nan) print(f"{z.shape[1]} x {z.shape[0]} pixels, height {np.nanmin(z):.1f} to {np.nanmax(z):.1f} m above Ordnance Datum") # Light from the north-west, exaggerated 2x so low earthworks show, over a terrain colour ramp. ls = LightSource(azdeg=315, altdeg=35) rgb = ls.shade(np.nan_to_num(z, nan=np.nanmin(z)), cmap=plt.cm.gist_earth, blend_mode="soft", vert_exag=2, dx=1, dy=1, vmin=np.nanmin(z) - 40, vmax=np.nanmax(z) + 10) plt.imsave(a.out, rgb) print(f"wrote {a.out}") if __name__ == "__main__": main()