Interaction — clipping, slicing, thresholding and picking¶
These live below the figure seam: each attaches a PyVista widget/picker to the render window and
returns its handle, leaving the declarative figure unchanged. They are meant for a live window (serve()
or jupyter(), notebook 16) — here, headless, we construct each and screenshot the result. The surface
widgets run on the real Grand Canyon DEM; the volume widgets run on the real SoilGrids SOC cube.
DEM: Copernicus WorldDEM-30 © DLR/Airbus (EU/ESA). Soil: SoilGrids 2.0 © ISRIC — CC-BY-4.0.
Data. All layers below are built from real data downloaded with
earthlens and committed as small fixtures under
examples/data/; vector and 3-D layers are derived from those real rasters through pyramids (the GIS
engine), so nothing here is synthetic. Attributions are given per dataset.
%matplotlib inline
from pathlib import Path
import numpy as np
import matplotlib.pyplot as plt
import pyvista as pv
pv.OFF_SCREEN = True # headless: every scene renders to an image, no interactive window
import pyramids # import first: bootstraps pyramids' vendored GDAL
from pyramids.dataset import Dataset
from digitalearth.three_d import Scene3D
ROOT = Path.cwd()
while not (ROOT / 'examples' / 'data' / 'real_dem_mont_blanc.tif').exists() and ROOT != ROOT.parent:
ROOT = ROOT.parent
DATA = ROOT / 'examples' / 'data'
def show(scene, title=''):
"""Screenshot a Scene3D off-screen and show it inline, asserting the scene area is not blank."""
img = scene.screenshot()
bg = img[0, 0].astype(int)
area = img[: int(img.shape[0] * 0.8)]
content = (np.abs(area.astype(int) - bg).sum(-1) > 30).mean()
assert content > 0.01, f'render looks blank: only {content:.1%} differs from the background'
plt.figure(figsize=(6, 4)); plt.imshow(img); plt.axis('off')
if title:
plt.title(title)
plt.show()
def show_image(img, title=''):
"""Show an already-captured RGB frame (e.g. from a pyvista Plotter) inline."""
plt.figure(figsize=(7, 4)); plt.imshow(img); plt.axis('off')
if title:
plt.title(title)
plt.show()
GRAND_CANYON = DATA / 'real_dem_grand_canyon.tif'
clip_plane() — cut the terrain with a draggable plane¶
dem = Dataset.read_file(str(GRAND_CANYON))
scene = Scene3D(off_screen=True)
scene.terrain(dem, z_exaggeration=2.0, cmap='terrain', name='dem')
scene.clip_plane('dem', normal=(1, 0, 0))
show(scene, 'clip_plane() — interactive cut plane')
scene.close()
clip_box() and threshold() — crop by box, or keep a value range¶
dem = Dataset.read_file(str(GRAND_CANYON))
scene = Scene3D(off_screen=True)
scene.terrain(dem, z_exaggeration=2.0, cmap='viridis', name='dem')
scene.clip_box('dem')
show(scene, 'clip_box() — interactive crop box')
scene.close()
scene = Scene3D(off_screen=True)
scene.terrain(dem, z_exaggeration=2.0, cmap='viridis', name='dem')
scene.threshold('dem')
show(scene, 'threshold() — value-range filter')
scene.close()
isovalue() — a moving contour on the terrain surface¶
Contours the layer's active scalar (point data — a terrain surface works).
dem = Dataset.read_file(str(GRAND_CANYON))
scene = Scene3D(off_screen=True)
scene.terrain(dem, z_exaggeration=2.0, cmap='plasma', name='dem')
scene.isovalue('dem')
show(scene, 'isovalue() — a draggable contour')
scene.close()
Volume widgets — clip_plane and threshold on the real soil cube¶
The same widgets attach to a ray-cast volume(); here the SoilGrids soil-organic-carbon cube.
cube = np.asarray(Dataset.read_file(str(DATA / 'soilgrids_soc.tif')).read_array(), dtype=float) # (6 depths, ny, nx)
cube = np.nan_to_num(cube, nan=float(np.nanmin(cube)))
scene = Scene3D(off_screen=True)
scene.volume(cube, cmap='YlOrBr', name='soc')
scene.clip_plane('soc', normal=(0, 0, 1))
scene.view('isometric')
show(scene, 'clip_plane() on a ray-cast volume (SoilGrids SOC)')
scene.close()
slider() — drive a parameter from a callback¶
The callback runs on every slider move in a live window; here we construct it.
scene = Scene3D(off_screen=True)
scene.add_mesh(pv.Sphere(), color='teal', name='ball')
def on_change(value):
scene.actor_of('ball').prop.opacity = value
handle = scene.slider(on_change, (0.0, 1.0), value=1.0, title='opacity')
print('slider handle:', type(handle).__name__)
show(scene, 'slider() — a value widget wired to a callback')
scene.close()
slider handle: vtkSliderWidget
Picking — identify the layer under the cursor¶
After a click in a live window picked_layer() returns the layer id; headless there is no click, so it is None.
scene = Scene3D(off_screen=True)
scene.add_mesh(pv.Cube(), name='box')
scene.enable_picking(mode='point')
print('picked so far:', scene.picked_layer()) # None — nothing clicked headless
scene.disable_picking()
print('picking disabled')
scene.close()
picked so far: None picking disabled