Terrain
DEM visibility, viewsheds, slope and aspect from SQL
- Connector software
- Free and open source
- Service credentials
- Not needed
- Query Farm hosting
- Available · $10/month organization minimum
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
| 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.
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;
INSTALL vgi FROM community;
LOAD vgi;
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'
);
Attach the signed image
Needs Docker, Podman or another OCI runtime. The extension runs the container and talks to it over stdio.
ATTACH 'terrain' AS terrain (
TYPE vgi,
LOCATION 'oci://ghcr.io/query-farm/vgi-terrain:0.3.0'
);
3. Run a query
SELECT pair_id, source_height_m
FROM terrain.main.ridge_tower_pairs
ORDER BY pair_id
Run the connector on a server you manage. You operate the service and choose who can connect.
1. Deploy the connector on your server
Follow the connector’s deployment instructions. Use its supported HTTP transport, or launch it remotely over SSH. The connection guide explains both options.
2. Install the vgi extension
Run this in your SQL client. On DuckDB, keep FROM query_farm on LOAD in later sessions.
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;
INSTALL vgi FROM community;
LOAD vgi;
3. Attach the connector
For an HTTP deployment, replace the example URL with your endpoint. Add the authentication options your deployment requires. For SSH, use the SSH connection instructions.
ATTACH 'terrain' AS terrain
(TYPE vgi, LOCATION 'https://your-connector.example.com');
4. Run a query
SELECT pair_id, source_height_m
FROM terrain.main.ridge_tower_pairs
ORDER BY pair_id
Query Farm operates Terrain for your team. Hosting has a $10 monthly minimum per organization; usage counts toward it. From $2.97 per instance-month ($10 monthly minimum).
1. Create your hosted connector
Open the console, choose your organization and deployment size, and add any service credentials there.
2. Install the vgi extension
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;
INSTALL vgi FROM community;
LOAD vgi;
3. Attach your deployment
Copy the ATTACH statement from your deployment in the console and run it in DuckDB or Haybarn. Use terrain as the catalog alias for the query below.
4. Run a query
SELECT pair_id, source_height_m
FROM terrain.main.ridge_tower_pairs
ORDER BY pair_id
Technical details
- Verified connector release
- v0.3.0
- Engine
- 2.0.1
- VGI protocol
- 3.4.0
- Verified on
- December 1, 2026
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'
);
Attach the signed image
Needs Docker, Podman or another OCI runtime. The extension runs the container and talks to it over stdio.
ATTACH 'terrain' AS terrain (
TYPE vgi,
LOCATION 'oci://ghcr.io/query-farm/vgi-terrain:0.3.0'
);
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.
GIS analysis
Evaluate terrain visibility between query-supplied tower/building pairs and inspect DEM elevation, slope and aspect. Distances and heights are metres. Input coordinates have an explicit CRS; DEMs must be projected in metres. Missing terrain yields unknown visibility. Synthetic sample assets ship in the wheel. The main schema exposes visibility, dem_cells and the ridge_tower_pairs sample table.
SQL workflow
Pass query results to the main schema functions, retaining IDs for joins. Use bundled synthetic data to try the executable examples without network access. CRS, coverage, inference limits and resource bounds are explicit function parameters.
Sources and references
- Rasterio on GDAL: reads and samples the DEM rasters.
- OGC GeoTIFF: the georeferenced raster format expected for DEM inputs such as the bundled
demo://ridge.tif. - pyproj (PROJ): coordinate transforms from endpoint CRSs into the DEM's projected metre CRS.
Catalog
Everything terrain exposes once attached — read straight from the worker's
released build.
main 1 table · 2 functions
Spatial functions and bundled inputs for terrain analysis.
Schema examples (6)
Inspect the two tower heights
SELECT pair_id, source_height_m
FROM terrain.main.ridge_tower_pairs
ORDER BY pair_id;
| 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');
ridge_tower_pairs
Two tower/building pairs across a synthetic 30 metre ridge
Inspect the two tower heights
SELECT pair_id, source_height_m
FROM terrain.main.ridge_tower_pairs
ORDER BY pair_id;
| pair_id | source_height_m |
|---|---|
| high | 80 |
| low | 2 |
dem_cells (dem VARCHAR, max_cells := BIGINT)
Read DEM cell centres, elevation, slope and aspect
Count cells and find the ridge elevation
SELECT count(*), max(elevation_m)
FROM terrain.main.dem_cells('demo://ridge.tif');
visibility (data TABLE, dem := VARCHAR, input_crs := VARCHAR, max_cells := BIGINT, max_samples := BIGINT, refraction_k := DOUBLE, step_m := DOUBLE)
Assess terrain obstruction between tower and building pairs
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;
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
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
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')
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.
Related workers
ArcGIS
Maps & places
Live ArcGIS feature queries, server summaries and raster sampling from service URLs worldwide
Lidar
Maps & places
Classified point-cloud heights and vegetation clearance around power lines
Vector tiles (MVT)
Maps & places
Decode PMTiles, MBTiles, XYZ and .mvt vector tiles into DuckDB rows with GEOMETRY columns