4 · RGB/HSV composites & unstructured meshes¶
Two specialty families:
- Composites —
rgb_composite/hsv_compositestack three bands into a colour image (cleopatra'sArrayGlyphRGB path). Here three terrain-derived bands — elevation, gradient magnitude, and smoothed elevation — make a false-colour view. - Unstructured meshes —
tricontourf/tricontour/tripcolortriangulate scattered points (cleopatraMeshGlyph). Any point set works; here we triangulate the (resampled) DEM cell centres.
Composites use the full-resolution DEM; the triangulated views use the coarse grid so the mesh is legible.
Setup. Inline plots + resolve the DEM path.
%matplotlib inline
from pathlib import Path
# Resolve the repo root so the bundled sample DEM is found whether this runs from
# docs/examples/ (mkdocs) or the repository root.
ROOT = Path.cwd()
while not (ROOT / "examples" / "data" / "LisbonElevation.tif").exists() and ROOT != ROOT.parent:
ROOT = ROOT.parent
DEM = str(ROOT / "examples" / "data" / "LisbonElevation.tif")
print("DEM:", DEM)
DEM: /home/runner/work/Digital-Earth/Digital-Earth/examples/data/LisbonElevation.tif
Derive three bands from the DEM — raw elevation, gradient magnitude, and a box-smoothed elevation — and stack them into a 3-band pyramids Dataset to feed the composite.
import numpy as np
from pyramids.dataset import Dataset, GeoReference
from digitalearth.static import Map
ds = Dataset.read_file(DEM)
z = np.nan_to_num(ds.read_array(band=0).astype('float32'))
dy, dx = np.gradient(z)
grad = np.hypot(dx, dy).astype('float32')
k = 5
kern = np.ones((k, k), 'float32') / (k * k)
from numpy.lib.stride_tricks import sliding_window_view as swv
smooth = np.pad(z, k // 2, mode='edge')
smooth = swv(smooth, (k, k)).mean(axis=(-1, -2)).astype('float32')
rgb = Dataset.from_array(
arr=np.stack([z, grad, smooth]),
geo_ref=GeoReference(geo=ds.geotransform, epsg=ds.epsg),
)
print('composite bands:', rgb.band_count)
composite bands: 3
RGB composite (elevation / gradient / smoothed)¶
rgb_composite — map the three bands to red/green/blue. Map contrast-stretches each band and hands cleopatra a band-first array; the result is a false-colour terrain image.
m = Map(crs=rgb.epsg, figsize=(9, 7))
m.rgb_composite(rgb)
m.set_title('rgb_composite')
m.fig;
HSV composite¶
hsv_composite — the same three bands interpreted as hue/saturation/value, then converted to RGB. Good for emphasising structure (here, ridges and slopes).
m = Map(crs=rgb.epsg, figsize=(9, 7))
m.hsv_composite(rgb)
m.set_title('hsv_composite')
m.fig;
Triangulated fields from (resampled) cell centres¶
Switch to the coarse DEM and take its cell centres as scattered points for triangulation.
coarse = ds.resample(ds.cell_size * 8)
m = Map(crs=coarse.epsg, figsize=(9, 7))
m.tricontourf(coarse, cmap='terrain')
m.colorbar(label='elevation (m)')
m.set_title('tricontourf')
m.fig;
0...10...20...30...40...50...60...70...80...90...
tricontourf then tripcolor — triangulate those points (cleopatra MeshGlyph) and render filled contours / flat-shaded triangles. This is how genuinely unstructured data (meshes, irregular samples) is plotted.
m = Map(crs=coarse.epsg, figsize=(9, 7))
m.tripcolor(coarse, cmap='terrain')
m.colorbar(label='elevation (m)')
m.set_title('tripcolor')
m.fig;
100 - done. 0
...10...20...30...40...50...60...70...80...90...