QGIS Live Compute (experiment)
Terrain modes computed inside QGIS with numpy, no tile service - 16 modes (hillshade, multidirectional hillshade, slope, aspect, curvature, TPI, TRI, roughness, LRM, sky view factor, openness, shadows, Phong, matcap, contours as a mask and as lines, hypsometric tint) checked against the app's kernels, the tile service and gdaldem; Colour and Values per mode; how QGIS picks tile zooms and resamples; parallel tiles timed; numpy, numba, cupy and the tile service compared.
Step 2 of the QGIS plugin plan asked whether the plugin can draw the app's modes without a tile service, in QGIS's own Python. This experiment (2026-11-25) answers yes: a first round ported three modes of increasing cost and measured them, a second ported eleven more, gave QGIS the values and made the server parallel, a third gave every mode row its Colour and Values boxes, added Phong, matcap and contour lines, and measured how QGIS picks tile zooms and resamples. The code is in qgis-plugin/terrain_viewer/live/, behind a panel checkbox that is off by default (Experimental: compute locally).
Third round: Colour and Values per mode, Phong, matcap, contour lines, resampling
Plugin 0.6.0, the same day, after a first try in QGIS 4.2 by the user: checking only "values" next to curvature drew nothing, toggling did not add or remove layers reliably, and QGIS 4 showed no local compute at all. The last one was an old install: both QGIS profiles still had 0.2.2.
A mode's row
Every mode row, of the tile service's groups and of the local compute box alike, is the same widget (FormsRow in dock.py): the mode's checkbox on the left, its forms on the right, Colour, Values and, where there is one, Vector, then the settings cog.
- Checking the mode turns its Colour layer on (the first time; afterwards the forms it had). Each form can be checked alone or with the others; checking a form checks the mode, unchecking the last one unchecks it.
- Each form is one layer, added when checked and removed when unchecked. 0.5.0 created the values layer only when the mode was also checked, and unchecking only hid layers, so a box and the layer tree could disagree.
- A layer removed by hand (or by File > New) unchecks its form: the panel listens to the project's
layersWillBeRemovedand tells its own removals apart. - The cog shows while Colour (or Vector) is on, with a fixed place so that the boxes line up from row to row.
- In the local box, Colour is the server's PNG tiles, now coloured with the mode's ramp: QGIS's own ramp sampled at 256 steps on the main thread, so the same colours as the values layer's default renderer; grey and multiplied for the shades, RGB for matcap, the two browns of the mask on a clear background for contours. Values is the Float32 layer GDAL reads, as before. The Draw as choice is gone. A
c=2parameter in the colour tiles' address keeps QGIS's disk cache from serving 0.5.0's grey tiles, which had the same address.
Checked in QGIS 4.2 and 3.44: test_plugin.py (section 2d) and test_live.py (section 6) walk curvature through values only, both, mode off, mode on (both back), Colour off, Values off, mode on (Colour back), and a layer removed by hand, comparing the layers in the project and the cog after each step.
Phong, matcap, contour lines
| Mode | Port of | Against | Match |
|---|---|---|---|
| Phong (defaults; light 120°/25° with specular 0.6; Fresnel rim 0.6) | the app's live Phong layer as the tile service draws it (tile-server/direct.mjs liveLighting, lib/phong-live-gl-layer.ts): the normal from Horn's sums with no /8, ambient 0.35, shininess 32, one white light, a light map | the tile service's picture over its white background | 100 % within 3 grey levels, median 0, p99 0 to 1 |
| Matcap (Clay Studio; Normal Sphere rotated 40°) | the live matcap layer, light relative to the camera (its default), bilinear lookup into the app's matcap pictures (lib/matcap-textures.ts: the app's two from terrain-viewer.iconem.com, Potree's from GitHub, fetched once) | the tile service's picture | 100 % within 3 levels per channel, median 0, p99 0 |
| Contour lines (vector) | GDAL's ContourGenerate over the elevation of the view | the contour mask, rasterized | 100 % (recall and precision within 1 px), z12 and z13 |
Phong's values are the multiply factor plus the highlight (0 to 1, highlights above). The colour layer is their grey, multiplied over the layers under it: the same as the tile service's Phong layer in QGIS (its picture is the shading over white, also multiplied), so a highlight brighter than the layer under it is not drawn in either. Matcap is a picture, with no values form.
Contour lines as a vector layer, not vector tiles. For each view, once the canvas stops (400 ms), a QgsTask mosaics the elevation tiles over the view (the zoom whose pixel is the canvas pixel, coarser until 64 tiles or fewer cover it), runs gdal.ContourGenerate at the minor interval, and replaces the features of a memory layer (elev, major). A later view's result wins over an older one still running. QGIS redraws a memory layer at once on pan and zoom, the lines run whole across the view (no tile seams or clipping), and Identify, the attribute table and Save As work. A vector tile path would mean a tile server writing MVT, with lines cut at every tile edge. The trade-off: the layer holds the lines of the last view only (a frozen copy keeps them).
Resampling and tile zoom
Measured headless in QGIS 4.2 and 3.44 (same results in both) with QgsMapRendererParallelJob, a 1024×768 canvas whose pixel is the pixel of 256 px tiles at zoom 12.0, 12.3, 12.5 and 12.7, the localhost server logging the zoom of each tile asked for (LiveServer.seen):
| Canvas zoom | XYZ (colour) layers ask | GDAL (values) layers ask |
|---|---|---|
| 12.0 | z12, 20 tiles | z12, 20 tiles |
| 12.3 | z12, 16 tiles (scaled up 1.23×) | z13, 48 tiles (scaled down) |
| 12.5 | z13, 35 tiles | z13, 35 tiles |
| 12.7 | z13, 24 tiles | z13, 24 tiles |
| 12.0, device pixel ratio 2 | z13, 63 tiles | z13, 63 tiles |
| 12.3, device pixel ratio 2 | z13, 48 tiles | z14, 154 tiles |
- The XYZ provider rounds the canvas zoom to the nearest tile zoom; GDAL takes the level just finer (an overview is used only when at least as fine as the request). So between zooms x.0 and x.5, a values layer reads up to three times the tiles of the colour layer: right for numbers (never scaled up), and only on that half of the range.
- The device pixel ratio is honoured: at a ratio of 2 both ask one zoom deeper (four times the tiles, sharp on a HiDPI screen). No double fetching, no tiles of the wrong zoom.
- Default resampling of every raster layer: nearest neighbour zoomed in and out, late (after the renderer), maximum oversampling 2.
What resampling changes, measured by drawing the contour mask (two colours) at zooms 12.3 and 12.7 and counting the colours:
| Layer, setting | Colours | What happens |
|---|---|---|
| XYZ, default | 5532 / 2759 | the provider already scales its tiles smoothly into the canvas |
| XYZ, bilinear (late) | 4881 / 2535 | a second resampling of the already scaled picture: slightly blurrier |
| XYZ, early resampling | 1 / 1 | nothing drawn: the XYZ provider cannot resample (ProviderHintCanPerformProviderResampling is false) |
| GDAL values, default | 3 / 3 | nearest: pixels dropped or doubled between zooms |
| GDAL values, bilinear late | 2362 / 2269 | the rendered colours blended |
| GDAL values, bilinear early | 3 / 3 for the mask's palette | GDAL resamples the values before the renderer: one resampling, of the numbers (a palette still shows only its classes) |
What the plugin does since 0.6.0:
- Colour (XYZ) layers: nothing. The provider's own smooth scaling is the single resampling; a resampler would add a second one, and early resampling blanks the layer (worth knowing before ticking it in the layer's properties).
- Values (GDAL) layers: early resampling, bilinear zoomed in and out, set once when the layer is made (
values_resamplinginlayers.py), for the tile service's values and the local ones alike; the contour mask (a palette) stays nearest. Identify reads the provider at the point, never the resampled picture. - A user's choice is kept. A changed setting re-points the layer with
setDataSource, which keeps the resampling, renderer, opacity and blending (checked in both versions). The local layers used to be removed and made again on every change, losing all of them; they now usesetDataSourcetoo, and the panel's opacity is applied to a new layer only. - The local colour tiles now go to z18 (Mapterhorn's 512 px z17), the depth the values layers already read.
test_live.py checks that a values layer gets early bilinear resampling and that a new setting keeps a layer's own opacity and resampler.
A trap when comparing against a tile service: every service on a machine keeps its tiles in the same disk cache by default (%TEMP%/terrain-viewer-tiles, keyed by address), so a service of a newer commit serves the tiles an older one wrote. Aspect scored 0.4 % against a same-commit service until it was started with its own TILE_CACHE_DIR: the tiles were from before the aspect fix. With its own cache, every mode scores 100 % in QGIS 4.2 and 3.44, Phong and matcap included (median difference 0 grey levels).
Second round: 14 modes, values, parallel tiles
The same day, a second round (plugin 0.5.0) answered three questions the first one left: can the plugin serve more of the app's modes, can QGIS style them with its own colour ramps, and does the server use the cores. The box moved to the top of the panel (Experimental: compute locally (no tile service), in bold, off by default); checking it lists the modes, each with a gear for its settings, and a Draw as choice: values (the default) or grey image tiles.
The modes and how well they match
live/modes.py holds the modes; each gives its values over a 256 px tile in natural units, Float32, the same units as the tile service's /terrain-tiles/{z}/{x}/{y}.tif. qgis-plugin/test_live.py checks each one, tile by tile on Mont Blanc at z12, z13 and z14 (with --service, against a tile service of the same commit), and against GDAL's own tools where GDAL has the mode. The score is the share of pixels within the tolerance given; 100 % everywhere, in QGIS 4.2 and 3.44:
| Mode | Values | Port of | Against the tile service (the app's protocol in Node) | Against GDAL |
|---|---|---|---|---|
| Elevation (hypsometric tint) | m | the decoded tile | 100 % (exact) | |
| Hillshade | 0-1 | Horn, as gdaldem | 100 % within 1 grey level (gdaldem hillshade) | |
| Multidirectional hillshade | 0-1 | gdaldem -multidirectional (four lights, 225 to 360°, weighted by aspect) | 100 % within 1 grey level | |
| Slope | ° | lib/slope-protocol.ts (Horn) | 100 % within 0.1° + 0.5 %¹ | 100 % within 0.01° (gdaldem slope) |
| Aspect | ° from north | gdaldem aspect | 100 %² | 100 % within 0.05° (gdaldem aspect -zero_for_flat) |
| Curvature: combined, profile, plan, det-hessian, casorati, shape-index | 1/m, 1/m², unitless | lib/curvature-protocol.ts | 100 % within 1e-4 1/m + 0.5 %¹ ³ | |
| TPI | m | lib/tpi-protocol.ts | 100 % (exact) | 100 % (gdaldem TPI) |
| TRI | m | lib/tri-protocol.ts | 100 % (exact) | 100 % (gdaldem TRI, Riley) |
| Roughness | m | lib/roughness-protocol.ts | 100 % (exact) | 100 % (gdaldem roughness) |
| Local relief model | m | lib/lrm-protocol.ts (the tile minus its ancestor 4 levels up, bilinear) | 100 % within 0.05 m (median 0.001) | |
| Sky view factor | 0-1 | lib/horizon-kernel.ts, precise | 100 % within 0.001 (median 5e-5) | |
| Openness, positive and negative | ° | lib/horizon-kernel.ts, precise | 100 % within 0.05° (median 0.002) | |
| Shadows | 1 lit, 0 in shadow | lib/shadow-kernel.ts | 0 pixels differ from the kernel run in Node, six suns | |
| Contours (raster mask) | 1 minor, 2 major | a pixel whose 4-neighbour lies below a level it is above | 100 %: every GDAL contour line within 1 px of the mask and back (gdal.ContourGenerate, rasterized) |
¹ The service divides by the ground size at the centre of its 512 px Mapterhorn tile, the plugin at the centre of its 256 px tile: 0.1 to 0.3 % apart at 45° north, and slope also travels as Terrain-RGB, in steps of 0.1°.
² The app's aspect was mirrored east-west until 2026-11-25: lib/aspect-protocol.ts computed atan2(dy, -dx) with dx = west minus east, so an east-facing slope read 270° (gdaldem: 90°). Found by this comparison (they agreed to 3e-5° once mirrored back), fixed in the app (atan2(dy, dx), lib/aspect-protocol.test.ts); the test now compares unmirrored.
³ The service packs curvature as Terrarium times 1000 over the app's own scale, so values past ±0.33 1/m (±3.3e-3 1/m² for det-hessian) are clipped there; those pixels (steep summits at z14) are left out of the comparison.
Not ported: the modes that need a whole view (the mound detector, the eigen-based ones, local dominance, the fast sky view factor with far rings), and colour relief by the app's own ramps (QGIS's ramps take that role, below). Phong and matcap came in the third round (above).
Values and colour ramps: a GDAL source, not a Python provider
The first round's open question was how to give QGIS values (so that QGIS's own colour ramps, Identify, the raster calculator and Processing work) rather than pictures coloured in numpy. Two designs were on the table:
| Registered Python raster provider | The localhost server's Float32 tiles read by GDAL (built) | |
|---|---|---|
| How | a QgsRasterDataProvider subclass; QGIS calls block(band, extent, w, h) for the whole canvas | /{mode}/{z}/{x}/{y}.tif (Float32, NaN no data) and a GDAL WMS description (TMS minidriver, the same as the tile service's /gdal.xml), opened by QGIS's own gdal provider; the XML is the layer's source |
| Who computes | QGIS's render thread, holding the GIL through the Python parts; one block for the whole canvas, nothing shown until it is done | the server's threads; GDAL asks up to 16 tiles at once (MaxConnections) and caches the blocks it read |
| Values in QGIS | yes | yes: Identify, raster calculator, zonal statistics, Processing, Save As GeoTIFF |
| Styling | QGIS's renderers | QGIS's renderers (the plugin sets a coloured default: a ramp of QGIS's style over the mode's range, grey and multiplied for the shades, two colours for the contour mask) |
| Outside QGIS | no | yes: the same XML opens in gdalinfo, gdal_translate, rasterio |
| Saved project | needs the provider registered before the project opens | needs the server running (it asks port 3420 first, so a reopened project's layers work again once the box is checked) |
| Risk | the traps of the first probe (exit 127 on a wrong capabilities type, objects collected under QGIS) | GDAL WMS is a mature driver; the server is the one already built |
Chosen: GDAL over the server's Float32 tiles. It reuses the server, keeps the computing off QGIS's render thread, is parallel by construction, works outside QGIS, and has the same shape as the tile service's values, so the panel treats both alike. A Terrarium-encoded PNG read through a VRT was ruled out: it quantises (1/256 m) and needs a pixel function to decode. The grey image tiles stay as Draw as: Grey image tiles (XYZ), the form QGIS shows progressively, tile by tile.
The coloured default per mode: hypsometric ramp (the plugin's own: QGIS's style has none) over the 2nd to 98th percentile of the elevation in view, YlOrRd for slope, Turbo for aspect, RdBu for curvature, TPI and LRM, Magma for TRI and roughness, reversed Greys for sky view factor and openness. Any of them can be changed in the layer's Symbology like any raster's.
Parallelism
The first server already gave each request a thread (ThreadingHTTPServer) and fetched a tile's neighbours in parallel. This round added:
- A compute pool sized to the cores: a semaphore with one slot per logical core around the numpy part; fetching happens before taking a slot, so a slow network never holds a core.
- The listen backlog: socketserver's default queue of 5 connections overflowed as soon as more than 5 clients connected at once, and Windows retries a refused connection after about half a second. Sixteen clients got 60 to 75 tiles/s from the first server whatever the mode; raised to 128, 300 to 650.
- Caches: decoded elevation within a 256 MB budget (it was 512 tiles of up to 1 MB each), and the finished tiles (64 MB LRU), so a re-render or the same mode as values and as a picture is not computed twice.
- Prefetch: when the canvas moves, its elevation tiles and a ring around are fetched in the background before QGIS asks.
- A fix: the address of a grey XYZ layer was written into QGIS's source by hand, so its settings were cut at the first
&(a mode with two settings lost the second); it is now encoded byQgsDataSourceUri, which each QGIS version reads back (a percent-encoded address written by hand is decoded by 4.2, not by 3.44).
Measured with qgis-plugin/bench_canvas.py: a 1920×1080 canvas at z12, z13 and z14 around Mont Blanc (48, 40 and 45 tiles), elevation already cached, every tile fresh (a cache-busting parameter), median of 3 runs, QGIS 4.2 on the 20-core laptop; "before" is the cb7c545 server, timed the same way.
| Server, form | Hillshade z12 / z13 / z14 | Sky view factor r = 16 | Shadows, sun 30° |
|---|---|---|---|
| before, grey XYZ | 33 / 34 / 40 tiles/s (1.1 to 1.5 s a canvas) | 35 / 35 / 35 | 36 / 39 / 37 |
| after, grey XYZ | 54 / 57 / 54 (0.7 to 0.9 s) | 43 / 50 / 46 | 53 / 54 / 57 |
| after, values (GDAL) | 244 / 206 / 100 (0.2 to 0.45 s) | 104 / 83 / 93 (0.46 to 0.48 s) | 149 / 118 / 147 (0.3 to 0.34 s) |
QGIS 3.44, the same runs: before, grey XYZ 31 to 56 tiles/s (0.84 to 1.5 s a canvas); after, grey XYZ 28 to 46 (no gain: QGIS's pace, within the noise); after, values: hillshade 134 to 143 (0.3 s), sky view factor 57 to 62 (0.65 to 0.84 s), shadows 91 to 108 (0.42 to 0.48 s).
- The XYZ form is limited by QGIS, not by the server: QGIS's XYZ provider has at most 6 tiles in flight (the server saw a peak of 6) and paces them at about 50 tiles/s. GDAL asks 16 at a time, so the values form, the default, is 2.5 to 7 times faster than before (1.5 to 3 times in 3.44).
- The sky view factor is bound by the GIL: 8 directions × 16 steps × 3 small numpy calls per tile; numpy releases the GIL inside each call, but the calls are short, so more threads do not add much (about 60 to 100 tiles/s whatever the concurrency). numba, when it imports, is the remedy (
kernels_numba.py). - The server alone, the 40 z13 tiles from 16 client threads: hillshade 278, shadows 110 to 180, sky view factor 60 to 90 tiles/s (before: 60 to 75 for every mode, the backlog).
- Laptop noise is large (±30 % between runs); the ranking holds across runs.
First round
What was built
- Elevation (
live/dem.py): Mapterhorn's 512 px WebP Terrarium tiles (or the AWS Terrarium PNGs), decoded byQImage. Both QGIS 4.2 (Qt 6.11) and 3.44 (Qt 5.15) listwebpamong their image formats, andQImageis reentrant, so it decodes in worker threads. The RGB packing is turned into metres with numpy. The plugin always serves 256 px tiles: with a 512 px source, tile z/x/y is one quarter of tile z-1. Past the source's depth (a 404), the parent tile is upsampled. The padded grid is the 3×3 tiles around the one asked for (fetched in parallel), cropped to the halo, so the halo is at most 256 px. Decoded tiles go into an LRU cache with in-flight de-duplication, because the nine neighbours of one tile are shared by nine requests. - Kernels (
live/kernels.py), vectorised numpy written against an array module (xp), so cupy runs the same code:- Hillshade / slope: Horn's 3×3 gradient, halo 1.
- Sky view factor:
lib/horizon-kernel.ts"precise". For each of 8 directions, every pixel of the radius as one shifted-array operation over the whole tile;sin(atan t)is computed ast / sqrt(1 + t²), and steps that land on the same pixel are skipped. - Shadows:
computeShadowTileoflib/shadow-kernel.ts, the shadow-height sweep, with the same reach and the same exact early exit. The JavaScript walks the sheared grid row by row. Here the whole sheared grid is one gather, and the van Herk / Gil-Werman prefix and suffix maxima are twomaximum.accumulatecalls over blocks of J rows. live/kernels_numba.pyholds the same kernels as loops for numba (prangeover rows; over sheared columns for the sweep). It is only used when numba imports, and QGIS ships without numba.
- Delivery (
live/server.py): aThreadingHTTPServeron127.0.0.1on a free port, on a daemon thread, answering/{mode}/{z}/{x}/{y}.png?<settings>with a greyscale PNG (zlib level 1) andCache-Control: max-age=86400. Standard QGIS XYZ layers read from it (live/layers.py). - Checked against the app (
qgis-plugin/test_live.py):live_reference.mjsruns the app's own TypeScript kernels in Node on the same padded grids. On Mont Blanc's z12 tile, under six suns (azimuths 0 to 315°, altitudes 5 to 45°), the numpy shadows differ fromcomputeShadowTileby 0 pixels. The sky view factor at radius 8 and 32 differs by at most 0.004 %, which is the Terrarium quantisation of the reference's output. numba gives the same results as numpy. The same script then draws a 1024×768 canvas of each mode withQgsMapRendererParallelJoband checks the panel's checkbox (the group appears when it is checked and goes away, with the server, when it is unchecked). It passes in QGIS 4.2 and 3.44.
# headless, from qgis-plugin/ (python-qgis.bat resets PATH and PYTHONPATH)
QT_QPA_PLATFORM=offscreen NODE=<path to node.exe> python-qgis test_live.py [--service http://127.0.0.1:3461] [out-dir]
QT_QPA_PLATFORM=offscreen python-qgis bench_canvas.py [--old <folder with tv_old/live/>] --json canvas.json
QT_QPA_PLATFORM=offscreen NODE=... TV_EXTRA_PYTHONPATH=<numba/cupy pip --target dir> python-qgis bench_live.py --service http://localhost:3461 --json out.jsonTimings
The test machine is a laptop with 20 logical cores and an NVIDIA RTX A1000 Laptop GPU. Both QGIS installs run the same Python 3.12.13 and numpy 2.4.6; numba 0.68 and cupy 14.2 (CUDA 12.9) were pip-installed into a scratch folder with --target, never into QGIS. The test set is 16 z12 tiles around Mont Blanc. Each cell is the median ms per 256 px tile, QGIS 4.2 / QGIS 3.44. The two columns are separate runs of the same code, and the gap between them is laptop noise (power states), not Qt 5 against Qt 6. Expect ±50 % from run to run.
Kernels alone (grids in memory, colour included)
| Mode | numpy, 1 thread | numpy, 8 threads (throughput) | numba, all cores | numba, 1 thread | cupy (with host↔device) | app's JS kernel in Node, 1 thread |
|---|---|---|---|---|---|---|
| Hillshade (3×3) | 1.3 / 0.4 | 0.9 / 0.4 | 0.1 / 0.1 | 0.2 / 0.1 | 1.3 / 0.6 | (not run) |
| Sky view factor, r = 8 | 4.9 / 2.6 | 5.7 / 2.3 | 3.7 / 1.9 | 18 / 9.6 | 12 / 3.9 | 60 / 34 |
| Sky view factor, r = 32 | 14 / 8.0 | 13 / 8.4 | 9.2 / 5.9 | 43 / 28 | 18 / 14 | 108 / 80 |
| Shadows, r = 256, sun 45° | 4.8 / 4.5 | 2.1 / 1.6 | 2.1 / 1.7 | 3.1 / 2.4 | 16 / 14 | 3.6 / 2.5 |
| Shadows, r = 256, sun 15° | 9.3 / 6.7 | 2.8 / 2.1 | 2.5 / 1.9 | 3.5 / 2.8 | 23 / 14 | 5.0 / 3.4 |
Whole tiles over HTTP (elevation already cached)
From the request to the last byte of the PNG. One at a time measures latency; 8 at a time measures throughput (wall time divided by tiles). The tile service is tile-server/server.mjs from the same commit, direct engine with 8 workers, on port 3461. An opacity changed by a millionth on every run keeps it from answering from its tile cache.
| Mode | live/ server, 1 at a time | tile service, 1 at a time | live/ server, 8 at a time | tile service, 8 at a time |
|---|---|---|---|---|
| Hillshade | 10 / 5.2 | 96 / 65 | 2.9 / 33¹ | 50 / 18 |
| Sky view factor, r = 8 | 10 / 16 | 965 / 592 | 11 / 4.5 | 199 / 125 |
| Sky view factor, r = 32 | 30 / 18 | 2011 / 2360 | 17 / 9.7 | 504 / 403 |
| Shadows, r = 256, sun 45° | 12 / 16 | 170 / 91 | 4.9 / 4.2 | 38 / 21 |
| Shadows, r = 256, sun 15° | 16 / 14 | 154 / 109 | 7.0 / 9.7 | 37 / 44 |
¹ An outlier of that run; 4.2 gave 2.9.
The tile service columns are from before its fix of 2026-11-25. Each 256 px tile then computed the four 512 px source tiles its resampling margin touched; now it computes the one it reads, and keeps it for its neighbours. With the same 16 tiles and 8 workers, 1 at a time / 8 at a time, in ms: hillshade 39 / 11, sky view factor r = 8 15 / 29, r = 32 24 / 51, shadows (sun 15°) 15 / 14. The 1-at-a-time medians are mostly tiles whose source tile was already computed; a tile whose source tile is new costs 150 ms (r = 8) to 350 ms (r = 32), the 512 px kernel. See Tile service.
In QGIS itself, a 1024×768 canvas of each mode (24 tiles, Mapterhorn not yet cached) draws in 0.4 to 1.0 s. While QGIS's parallel renderer requests tiles, per-tile compute rises to about 40 ms for the sky view factor at r = 16 and 15 to 19 ms for shadows. That is GIL contention among concurrent requests (see below).
What the numbers say
- numpy is enough for these three modes. Every mode stays under 15 ms per tile in one thread, below the cost of a network fetch of the tile. Shadows and hillshade are memory-bound array operations, and the vectorised sweep is as fast as V8 running the app's own loop.
- Threads help when the operations are large. numpy releases the GIL inside each array operation. The shadow sweep is a few large gathers and scans, and gains 2 to 3× from 8 threads. The sky view factor is hundreds of small operations on 64k pixels (8 directions × radius), so threads mostly wait on the GIL and gain nothing (slightly worse at r = 8).
- numba, when it is there, is the cheapest speed-up: 1.5 to 2× on the sky view factor over all cores, and a 0.1 ms hillshade. But QGIS ships without it, and pip-installing into QGIS's Python is not something a plugin should do. The plugin would use it only when it imports (
kernels_numba.available). - cupy is slower than numpy per tile. A 256 px tile is too little work for a GPU. Each array operation is one kernel launch (dozens per tile for the sky view factor), plus the transfers. A fused
RawKernelwith one thread per pixel would remove the launches, but the transfers stay. The GPU pays off on a whole canvas computed in one dispatch, like the app's live GPU shadows over a Float32 view mosaic, not on tiles. - Against the tile service, the local tiles were 5 to 100× faster, end to end. The service pays for its whole pipeline on each tile: the layer stack rebuilt from URL parameters, a worker hop, the app's colour paint and PNG encoding by sharp. Its sky view factor (1 to 2 s per tile, even at r = 8) was far above what the kernel costs in Node (60 to 110 ms in the column above): each 256 px tile computed four 512 px source tiles. Fixed on 2026-11-25 (see the note under the table): the service now comes within 1.5 to 3× of the local server, slower mostly on tiles whose 512 px source tile is new.
Ways into QGIS
| (a) Python raster data provider | (b) Plugin layer / custom renderer | (c) Localhost XYZ server (built) | (d) Processing over the canvas extent | |
|---|---|---|---|---|
| How | QgsRasterDataProvider subclass registered with QgsProviderMetadata; QGIS calls block(band, extent, w, h) | QgsPluginLayer + Python QgsMapLayerRenderer drawing the canvas | numpy tiles over http://127.0.0.1:<port> to a plain XYZ layer | an algorithm writing a temporary GeoTIFF |
| Works in 3.44 and 4.2 | yes, probed headless in both (one block per render, from a render worker thread, a clone() per render) | yes (long-standing API), not probed | yes, tested end to end | yes |
| Responsiveness | one block for the whole canvas per render, so nothing shows until all of it is done; no tiles to cache unless the provider keeps its own | same as (a) | progressive: tiles appear as they finish; QGIS cancels the ones panned away | none: run, wait, load |
| Caching | its own | its own | QGIS's network cache (the tiles say max-age) plus the decoded elevation LRU | the file |
| Threading / GIL | runs in QGIS's render thread, holding the GIL for the Python parts | same | requests on their own threads; QGIS's renderer only waits on the network | Processing's task thread |
| Values (Float32) | yes: Identify, raster calculator, statistics | no, only pixels | no, only 8-bit colours (would need GDAL's WMS driver over a Float32 encoding, as the service's /gdal.xml) | yes |
| Saved project | the provider must be registered before the project opens | the plugin layer type must be registered | the address holds a port that changes each session | a file path |
| Code | moderate; crash-prone (below) | the most | the least | little |
Chosen for the prototype: (c). It is the least code, uses only API that QGIS 3.28 already has (tested in 3.44 and 4.2), and leaves tiling, threading, caching, resampling between zoom levels and reprojection to QGIS. Option (d) is the right shape for exports and batch runs. For values, the second round kept (c) and added Float32 tiles that GDAL reads (see values and colour ramps) rather than option (a), whose probe worked in both versions but found two traps the hard way:
capabilities()must return the flag type the version expects (Qgis.RasterInterfaceCapabilityin 3.44 and 4.x). Returning the oldQgsRasterDataProvider.Size | Identifymakes 3.44 exit with no traceback (exit code 127).- The Python objects returned by the factory and by
clone()must be kept referenced, or they are collected while QGIS still uses them.
GPU "at shader level"
- QGIS's own raster shading is a fixed C++ pipeline (
QgsRasterRenderersubclasses: single band, pseudocolour, hillshade). A PythonQgsRasterRenderersubclass is possible in principle, but it is called with blocks in a render thread like a provider. It is not a shader hook, and it can only colour values a provider already computed. The 2D canvas has no GLSL entry point; QGIS 3D's materials do, but they do not apply to 2D maps. - OpenGL compute through Qt: PyOpenGL ships with QGIS on Windows (3.1.10). A
QOffscreenSurfaceandQOpenGLContexton a worker thread could run the app's shaders. Per tile, it would pay the same upload and readback costs as cupy. It only makes sense for a canvas-sized mosaic in one dispatch, the way the app's live shadows work. - cupy: measured above. It works when installed (it needs the CUDA wheels), and it is slower than numpy per tile.
- Verdict: CPU numpy for tiles now. A GPU path only as a canvas-mosaic renderer (option a or b computing the whole view at once), and only if a mode is too slow on the CPU, which none of these three is.
Limits of the prototype
- Sixteen modes; the whole-view modes stay on the tile service (above).
- Elevation from Mapterhorn (or AWS) only. The halo is at most one tile (256 px), which is enough for the sweep's default radius but not for its 512 px cap.
- A project saved with the local layers points at the server's port: it asks 3420 first, so the layers work again once the box is checked after reopening (not when that port was taken and another was given).
- Elevation is fetched with
urllib, which ignores QGIS's proxy and authentication settings.QgsBlockingNetworkRequestwould respect them (the tile service installer uses it). - The sky view factor and openness are the "precise" variant; the app defaults to "fast" (far rings from coarser tiles), which is not ported.
Next steps
- numba used when present, with
nogil, for the sky view factor and openness (the GIL-bound modes). The app's aspect fixedDone 2026-11-25.QgsBlockingNetworkRequestfor the elevation, a disk cache of decoded tiles, the other terrain sources (any Terrarium or Terrain-RGB pyramid, a COG through GDAL).- A Processing provider over the same
modes.values(), for exports and batch runs. - The tile service's sky view factor (1 to 2 s a tile): profile it.
Related work and references
Tools that already compute terrain modes inside QGIS, and how they relate to this experiment (links checked 2026-11-25). They all work the same way: an algorithm reads a raster layer and writes a new raster, for the extent and resolution chosen when it runs. This experiment instead computes tiles as the canvas asks for them, from a tile pyramid. The papers behind each mode are on Terrain Analysis Pipeline, related work.
- gdaldem, in the Processing toolbox under GDAL > Raster analysis: hillshade (also
-multidirectional,-combined,-igor), slope, aspect, TRI, TPI and roughness. The reference the table above checks against. - Relief Visualization Toolbox (QGIS plugin of RVT_py): hillshade from one or several directions, slope, simple local relief model, sky-view factor (also anisotropic), positive and negative openness, local dominance, multi-scale topographic position and the VAT blends. It covers the relief modes ported here and several that are not (local dominance, the blends). Its sky-view factor uses the same per-direction formula over 16 directions by default, against 8 here and in the app.
- WhiteboxTools (geomorphometric tools; in QGIS through Alexander Bruy's Whitebox for Processing plugin, which needs the WhiteboxTools program installed): slope, aspect, profile, plan and other curvatures, RuggednessIndex, DiffFromMeanElev and DevFromMeanElev, MaxElevationDeviation and MultiscaleTopographicPositionImage, MultidirectionalHillshade, HorizonAngle. Its RuggednessIndex divides by the number of neighbours (a root-mean-square), where the app and gdaldem keep Riley's undivided root, so its values are those here divided by √8. DiffFromMeanElev is the app's TPI over a window of any size, computed with an integral image so the cost does not grow with the window.
- SAGA GIS (Conrad et al. 2015, below; in QGIS through the Processing Saga NextGen Provider plugin, with SAGA installed separately): slope, aspect and curvatures by several methods, Zevenbergen and Thorne's 9-parameter polynomial among them (the fit the app's profile and plan curvatures use), TPI, TRI, sky-view factor and topographic openness.
- GRASS GIS (QGIS's GRASS Processing provider): r.relief (hillshade from one azimuth and altitude), r.slope.aspect (slope, aspect, profile and tangential curvature, and the partial derivatives), r.param.scale (slope, curvatures and landform classes from a quadratic fitted by least squares to a window of any size, where the app's 3×3 modes have one fixed size). The addons r.skyview (sky-view factor from
r.horizon, or positive openness with-o) and r.local.relief (Hesse's full local relief model, which the app simplifies, see LRM) are installed in GRASS, not with QGIS.
Papers:
- J. B. Lindsay, Whitebox GAT: A case study in geomorphometric analysis, Computers & Geosciences 95, 2016. Whitebox GAT, the open-source GIS that WhiteboxTools grew out of, as a case study of software for geomorphometric analysis.
- O. Conrad, B. Bechtel, M. Bock, H. Dietrich, E. Fischer, L. Gerlitz, J. Wehberg, V. Wichmann, J. Böhner, System for Automated Geoscientific Analyses (SAGA) v. 2.1.4, Geoscientific Model Development 8(7), 2015. SAGA's architecture and tool libraries, terrain analysis among them.
QGIS Plugins
Two released QGIS plugins - Terrain Viewer 0.6.0, the app's sidebar as a QGIS panel whose modes are tile service layers (Colour and Values per mode), with an Install button for a local tile service and an experimental local compute of 16 modes in numpy - and Send2MapApp 0.5.1, the app's "Open in" list in QGIS.
UX flows
The /ux-flows page, a map of the app's screens and the flows a user walks through them, with a real screenshot on every screen - its three views, how the screens are captured by agent-browser, and how to add a screen or a flow