Skip to content

Terrain

DEM visibility, viewsheds, slope and aspect from SQL

Connector software
Free and open source
Service credentials
Not needed
Use with an agent

Install the Orchard skill in your project to help your agent find connectors, configure them, and verify SQL results.

npx skills add Query-farm/vgi-skills --skill orchard

Copy this request into your coding agent to get started.

Use https://query.farm/products/orchard/connectors/terrain/index.md to help me query Terrain with DuckDB or Haybarn 2.0.1 and VGI protocol 3.4. Ask only for missing details about the result I need or whether I want local execution, self-hosting or Query Farm hosting. Check the connector's actual hosting availability, credentials and input requirements. Read one format of each needed reference and the exact object declarations before writing SQL. Use a released connector, and distinguish a saved example result from anything you actually execute. If you cannot run the query, tell me what remains to verify.

Inspect the two tower heights

Example output · captured
Connector release
v0.3.0
Engine
2.0.1
VGI protocol
3.4.0
SELECT pair_id, source_height_m
FROM terrain.main.ridge_tower_pairs
ORDER BY pair_id
Result
pair_id source_height_m
high 80
low 2

Quick start

Choose where to run Terrain, then follow the steps below.

The connector runs alongside DuckDB or Haybarn. No Query Farm account is needed.

1. Install the vgi extension

Run this in your SQL client. On DuckDB, keep FROM query_farm on LOAD in later sessions.

setup.sql
SET allow_extension_repositories = 'allowed';

CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
    WITH PREFIX 'https://duckdb-extensions.query.farm';

INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;

2. Attach the connector

Attach with uvx

Needs uv. uvx builds the worker from its release tag on first attach and reuses the cached environment after that.

ATTACH 'terrain' AS terrain (
  TYPE vgi,
  LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-terrain-worker'
);

3. Run a query

query.sql
SELECT pair_id, source_height_m
FROM terrain.main.ridge_tower_pairs
ORDER BY pair_id
More example queries →
Technical details
Verified connector release
v0.3.0
Engine
2.0.1
VGI protocol
3.4.0
Verified on
December 1, 2026
Release notes on GitHub
Pin a version for production

When running the connector yourself, keep a specific release until you’re ready to upgrade. Replace the ATTACH statement above with this one and follow the same extension and credential setup.

Attach with uvx

Needs uv. uvx builds the worker from its release tag on first attach and reuses the cached environment after that.

ATTACH 'terrain' AS terrain (
  TYPE vgi,
  LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-terrain-worker'
);

Overview

Terrain analysis over projected digital elevation models: line-of-sight visibility between tower, building or other point pairs, accounting for earth curvature, plus per-cell elevation, slope and aspect. A synthetic ridge DEM and sample tower pairs are included.

Catalog

Everything terrain exposes once attached — read straight from the worker's released build.

terrain 1 schema
main 1 table · 2 functions

Spatial functions and bundled inputs for terrain analysis.

Schema examples (6)
Examples (6)

Inspect the two tower heights

SELECT pair_id, source_height_m
FROM terrain.main.ridge_tower_pairs
ORDER BY pair_id;
Result
pair_id source_height_m
high 80
low 2

A tall tower clears the ridge; a low tower is blocked

SELECT pair_id, visible, status
FROM terrain.main.visibility((SELECT * FROM terrain.main.ridge_tower_pairs))
ORDER BY pair_id;

Viewshed per tower: cross join each ridge_tower_pairs tower with every dem_cells cell centre, run visibility on the 242 sight lines, and count visible vs blocked cells (80 m tower 99/22, 2 m tower 55/66 of 121)

SELECT split_part(v.pair_id, ':', 1) AS tower,
  count(*) FILTER (WHERE v.visible) AS visible_cells,
  count(*) FILTER (WHERE NOT v.visible) AS blocked_cells
FROM terrain.main.visibility((SELECT t.pair_id || ':' || c.cell_id AS pair_id, t.source_x, t.source_y, c.x AS target_x, c.y AS target_y, t.source_height_m, 2.0 AS target_height_m FROM terrain.main.ridge_tower_pairs AS t CROSS JOIN terrain.main.dem_cells('demo://ridge.tif') AS c)) AS v
GROUP BY tower
ORDER BY tower;

Locate what blocks each blocked pair: snap the visibility obstruction point to the nearest dem_cells cell centre (the 2 m tower's line passes 28 m under the 30 m ridge crest cell 60)

SELECT v.pair_id, round(v.min_clearance_m, 2) AS min_clearance_m, c.cell_id,
  c.x, c.y, c.elevation_m
FROM terrain.main.visibility((SELECT * FROM terrain.main.ridge_tower_pairs)) AS v
CROSS JOIN terrain.main.dem_cells('demo://ridge.tif') AS c
WHERE v.status = 'blocked'
QUALIFY row_number() OVER (PARTITION BY v.pair_id ORDER BY (c.x - v.obstruction_x) ^ 2 + (c.y - v.obstruction_y) ^ 2, c.cell_id) = 1
ORDER BY v.pair_id;
2 more examples

Tower height sweep: re-run the ridge_tower_pairs 'low' pair through visibility at heights 40-70 m in 5 m steps; heights up to 55 m are blocked by the ridge and 60 m is the first that clears it (+1.0 m)

WITH candidates AS (SELECT 'low@' || h.height_m AS pair_id, h.height_m, d.source_x, d.source_y, d.target_x, d.target_y, h.height_m::DOUBLE AS source_height_m, d.target_height_m FROM terrain.main.ridge_tower_pairs AS d CROSS JOIN range(40, 75, 5) AS h(height_m) WHERE d.pair_id = 'low') SELECT c.height_m, v.status, round(v.min_clearance_m, 2) AS min_clearance_m
FROM terrain.main.visibility((SELECT * FROM candidates)) AS v
JOIN candidates AS c USING (pair_id)
ORDER BY c.height_m;

Count cells and find the ridge elevation

SELECT count(*), max(elevation_m)
FROM terrain.main.dem_cells('demo://ridge.tif');
Tables
ridge_tower_pairs 1 example7 cols

Two tower/building pairs across a synthetic 30 metre ridge

Columns
pair_idnot null VARCHAR
Caller-owned identity for a tower/building pair.
source_x DOUBLE
Tower X in input_crs.
source_y DOUBLE
Tower Y in input_crs.
target_x DOUBLE
Building X in input_crs.
target_y DOUBLE
Building Y in input_crs.
source_height_m DOUBLE
Tower height above DEM surface, metres.
target_height_m DOUBLE
Receiver height above DEM surface, metres.
ConstraintsPRIMARY KEY (pair_id)
Example

Inspect the two tower heights

SELECT pair_id, source_height_m
FROM terrain.main.ridge_tower_pairs
ORDER BY pair_id;
Result
pair_id source_height_m
high 80
low 2
Link
Table functions
dem_cells (dem VARCHAR, max_cells := BIGINT) 1 example

Read DEM cell centres, elevation, slope and aspect

Positional arguments
dem VARCHAR
North-up DEM GeoTIFF path or URL, with projected metre coordinates and metre elevations.
Named arguments
max_cells := BIGINT
Maximum number of DEM cells to read. · default 2000000
Returns columns
cell_id BIGINT
Row-major DEM cell index.
x DOUBLE
Cell centre X in dem_crs.
y DOUBLE
Cell centre Y in dem_crs.
elevation_m DOUBLE
Elevation in metres; NULL for nodata.
slope_deg DOUBLE
Slope from finite differences in degrees; NULL where neighbouring data are missing.
aspect_deg DOUBLE
Downhill azimuth clockwise from north; NULL on flat or unknown terrain.
dem_crs VARCHAR
Projected horizontal CRS, with metre units.
Example

Count cells and find the ridge elevation

SELECT count(*), max(elevation_m)
FROM terrain.main.dem_cells('demo://ridge.tif');
Link
Table in-out functions
visibility (data TABLE, dem := VARCHAR, input_crs := VARCHAR, max_cells := BIGINT, max_samples := BIGINT, refraction_k := DOUBLE, step_m := DOUBLE) 4 examples

Assess terrain obstruction between tower and building pairs

Positional arguments
data TABLE
Rows with pair_id, source_x/y, target_x/y, source_height_m and target_height_m.
Named arguments
dem := VARCHAR
DEM GeoTIFF path or GDAL-supported URL; elevation units must be metres. · default "demo://ridge.tif"
input_crs := VARCHAR
CRS for endpoint XY coordinates; always XY axis order. · default "EPSG:32618"
max_cells := BIGINT
Maximum loaded DEM cells; rejects larger rasters instead of silently truncating. · default 2000000
max_samples := BIGINT
Maximum samples per sight line; excess raises an error. · default 200000
refraction_k := DOUBLE
Effective Earth radius factor for terrain curvature; 7/6 is the default. · default 1.1666666667
step_m := DOUBLE
Maximum sample spacing in metres, capped to half the smaller DEM cell size. · default 5.0
Returns columns
pair_id VARCHAR
Input pair identity, suitable for joining to towers and buildings.
visible BOOLEAN
True if all sampled terrain clears the sight line; NULL if coverage is unknown.
distance_m DOUBLE
Horizontal distance in the projected DEM CRS, metres.
min_clearance_m DOUBLE
Smallest interior sight-line clearance above terrain, metres.
obstruction_x DOUBLE
Worst obstruction X in dem_crs, only when blocked.
obstruction_y DOUBLE
Worst obstruction Y in dem_crs, only when blocked.
sample_count BIGINT
Number of elevation samples used along this sight line.
status VARCHAR
visible, blocked, outside_dem, or nodata; unknown terrain is never reported clear.
sightline_wkb BLOB
3D WKB sight line with absolute endpoint elevations; NULL without terrain coverage.
dem_crs VARCHAR
Horizontal coordinate reference system of the output geometry and obstruction.
Examples (4)

A tall tower clears the ridge; a low tower is blocked

SELECT pair_id, visible, status
FROM terrain.main.visibility((SELECT * FROM terrain.main.ridge_tower_pairs))
ORDER BY pair_id;

Viewshed per tower: cross join each ridge_tower_pairs tower with every dem_cells cell centre, run visibility on the 242 sight lines, and count visible vs blocked cells (80 m tower 99/22, 2 m tower 55/66 of 121)

SELECT split_part(v.pair_id, ':', 1) AS tower,
  count(*) FILTER (WHERE v.visible) AS visible_cells,
  count(*) FILTER (WHERE NOT v.visible) AS blocked_cells
FROM terrain.main.visibility((SELECT t.pair_id || ':' || c.cell_id AS pair_id, t.source_x, t.source_y, c.x AS target_x, c.y AS target_y, t.source_height_m, 2.0 AS target_height_m FROM terrain.main.ridge_tower_pairs AS t CROSS JOIN terrain.main.dem_cells('demo://ridge.tif') AS c)) AS v
GROUP BY tower
ORDER BY tower;

Locate what blocks each blocked pair: snap the visibility obstruction point to the nearest dem_cells cell centre (the 2 m tower's line passes 28 m under the 30 m ridge crest cell 60)

SELECT v.pair_id, round(v.min_clearance_m, 2) AS min_clearance_m, c.cell_id,
  c.x, c.y, c.elevation_m
FROM terrain.main.visibility((SELECT * FROM terrain.main.ridge_tower_pairs)) AS v
CROSS JOIN terrain.main.dem_cells('demo://ridge.tif') AS c
WHERE v.status = 'blocked'
QUALIFY row_number() OVER (PARTITION BY v.pair_id ORDER BY (c.x - v.obstruction_x) ^ 2 + (c.y - v.obstruction_y) ^ 2, c.cell_id) = 1
ORDER BY v.pair_id;
1 more example

Tower height sweep: re-run the ridge_tower_pairs 'low' pair through visibility at heights 40-70 m in 5 m steps; heights up to 55 m are blocked by the ridge and 60 m is the first that clears it (+1.0 m)

WITH candidates AS (SELECT 'low@' || h.height_m AS pair_id, h.height_m, d.source_x, d.source_y, d.target_x, d.target_y, h.height_m::DOUBLE AS source_height_m, d.target_height_m FROM terrain.main.ridge_tower_pairs AS d CROSS JOIN range(40, 75, 5) AS h(height_m) WHERE d.pair_id = 'low') SELECT c.height_m, v.status, round(v.min_clearance_m, 2) AS min_clearance_m
FROM terrain.main.visibility((SELECT * FROM candidates)) AS v
JOIN candidates AS c USING (pair_id)
ORDER BY c.height_m;
Link

Try these queries

A tall tower clears the ridge; a low tower is blocked

SELECT pair_id, visible, status
FROM terrain.main.visibility((SELECT * FROM terrain.main.ridge_tower_pairs))
ORDER BY pair_id

Uses visibilityridge_tower_pairs

Viewshed per tower: cross join each ridge_tower_pairs tower with every dem_cells cell centre, run visibility on the 242 sight lines, and count visible vs blocked cells (80 m tower 99/22, 2 m tower 55/66 of 121)

SELECT split_part(v.pair_id, ':', 1) AS tower,
  count(*) FILTER (WHERE v.visible) AS visible_cells,
  count(*) FILTER (WHERE NOT v.visible) AS blocked_cells
FROM terrain.main.visibility((SELECT t.pair_id || ':' || c.cell_id AS pair_id, t.source_x, t.source_y, c.x AS target_x, c.y AS target_y, t.source_height_m, 2.0 AS target_height_m FROM terrain.main.ridge_tower_pairs AS t CROSS JOIN terrain.main.dem_cells('demo://ridge.tif') AS c)) AS v
GROUP BY tower
ORDER BY tower

Uses visibilityridge_tower_pairsdem_cells

Locate what blocks each blocked pair: snap the visibility obstruction point to the nearest dem_cells cell centre (the 2 m tower's line passes 28 m under the 30 m ridge crest cell 60)

SELECT v.pair_id, round(v.min_clearance_m, 2) AS min_clearance_m, c.cell_id,
  c.x, c.y, c.elevation_m
FROM terrain.main.visibility((SELECT * FROM terrain.main.ridge_tower_pairs)) AS v
CROSS JOIN terrain.main.dem_cells('demo://ridge.tif') AS c
WHERE v.status = 'blocked'
QUALIFY row_number() OVER (PARTITION BY v.pair_id ORDER BY (c.x - v.obstruction_x) ^ 2 + (c.y - v.obstruction_y) ^ 2, c.cell_id) = 1
ORDER BY v.pair_id

Uses visibilityridge_tower_pairsdem_cells

Tower height sweep: re-run the ridge_tower_pairs 'low' pair through visibility at heights 40-70 m in 5 m steps; heights up to 55 m are blocked by the ridge and 60 m is the first that clears it (+1.0 m)

WITH candidates AS (SELECT 'low@' || h.height_m AS pair_id, h.height_m, d.source_x, d.source_y, d.target_x, d.target_y, h.height_m::DOUBLE AS source_height_m, d.target_height_m FROM terrain.main.ridge_tower_pairs AS d CROSS JOIN range(40, 75, 5) AS h(height_m) WHERE d.pair_id = 'low') SELECT c.height_m, v.status, round(v.min_clearance_m, 2) AS min_clearance_m
FROM terrain.main.visibility((SELECT * FROM candidates)) AS v
JOIN candidates AS c USING (pair_id)
ORDER BY c.height_m

Uses ridge_tower_pairsvisibility

Count cells and find the ridge elevation

SELECT count(*), max(elevation_m)
FROM terrain.main.dem_cells('demo://ridge.tif')

Uses dem_cells

Questions it can answer

Plain-English questions from the worker's agent test tasks. An AI agent with this catalog attached should answer each one with SQL.

  • Inspect the two tower heights. Use the bundled synthetic inputs and return the columns and ordering shown in its catalog example.
  • A tall tower clears the ridge; a low tower is blocked. Use the bundled synthetic inputs and return the columns and ordering shown in its catalog example.
  • Count cells and find the ridge elevation. Use the bundled synthetic inputs and return the columns and ordering shown in its catalog example.

Terrain is an independent open-source connector from Query.Farm. It is not affiliated with, endorsed by or sponsored by the services, projects or organisations it works with. Product names, logos and trademarks belong to their respective owners.