Skip to content

Hydrology

Upstream catchments and land-cover summaries for monitoring stations

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/hydrology/index.md to help me query Hydrology 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.

Trace a watershed from an outlet location

Uses the connector’s bundled synthetic terrain and a sample outlet location.

Example output · captured
Connector release
v0.3.0
Engine
2.0.1
VGI protocol
3.4.0
SELECT station_id, cell_count, area_m2
FROM hydrology.main.catchments((SELECT * FROM (VALUES ('outlet', 500035.0, 4100005.0)) AS gauges(station_id, x, y)))
ORDER BY station_id
Result
station_id cell_count area_m2
outlet 49 4900

Quick start

Choose where to run Hydrology, 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 'hydrology' AS hydrology (
  TYPE vgi,
  LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-hydrology-worker'
);

3. Run a query

query.sql
SELECT station_id, cell_count, area_m2
FROM hydrology.main.catchments((SELECT * FROM (VALUES ('outlet', 500035.0, 4100005.0)) AS gauges(station_id, x, y)))
ORDER BY station_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 'hydrology' AS hydrology (
  TYPE vgi,
  LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-hydrology-worker'
);

Overview

Watershed analysis over digital elevation models: upstream catchments for river gauges and monitoring stations from D8 flow routing, and the land cover inside each catchment. Drainage analysis only, not a flood model. Sample rasters are included.

Catalog

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

hydrology 1 schema
main 2 functions

Spatial functions for hydrology analysis.

Schema examples (4)
Examples (4)

Delineate the known synthetic valley

SELECT station_id, cell_count, area_m2
FROM hydrology.main.catchments((SELECT * FROM (VALUES ('outlet', 500035.0, 4100005.0)) AS gauges(station_id, x, y)))
ORDER BY station_id;
Result
station_id cell_count area_m2
outlet 49 4900

Dominant upstream land-cover class for four gauges on the synthetic valley: the two single-cell side gauges are pure class 10 (west) and class 20 (east), while the nested mid-valley gauge and the outlet are both mostly class 20

WITH basins AS (SELECT station_id, area_m2, geometry_wkb FROM hydrology.main.catchments((SELECT * FROM (VALUES ('west', 500015.0, 4100035.0), ('mid', 500035.0, 4100035.0), ('east', 500055.0, 4100035.0), ('outlet', 500035.0, 4100005.0)) AS gauges(station_id, x, y))) WHERE status = 'ok') SELECT b.station_id, b.area_m2, arg_max(l.class_code, l.area_m2) AS dominant_class, max(l.fraction_of_valid) AS dominant_share
FROM basins AS b
JOIN hydrology.main.landcover((SELECT station_id, geometry_wkb FROM basins)) AS l ON l.station_id = b.station_id
GROUP BY b.station_id, b.area_m2
ORDER BY b.station_id;

Confirm the entire synthetic upstream area has classified land cover

SELECT sum(area_m2), min(coverage_fraction)
FROM hydrology.main.landcover((SELECT station_id, geometry_wkb FROM hydrology.main.catchments((SELECT * FROM (VALUES ('outlet', 500035.0, 4100005.0)) AS gauges(station_id, x, y))) WHERE status='ok'));

Land cover of the intervening drainage between a mid-valley gauge and the valley outlet: per class, outlet catchment area minus the nested upstream gauge's catchment area (1,500 m2 of class 10 and 1,800 m2 of class 20)

WITH gauges AS (SELECT * FROM (VALUES ('outlet', 500035.0, 4100005.0), ('mid', 500035.0, 4100035.0)) AS g(station_id, x, y)), cover AS (SELECT station_id, class_code, area_m2 FROM hydrology.main.landcover((SELECT station_id, geometry_wkb FROM hydrology.main.catchments((SELECT * FROM gauges)) WHERE status = 'ok'))) SELECT class_code, sum(area_m2) FILTER (WHERE station_id = 'outlet') AS outlet_area_m2, sum(area_m2) FILTER (WHERE station_id = 'mid') AS upstream_gauge_area_m2, outlet_area_m2 - upstream_gauge_area_m2 AS intervening_area_m2
FROM cover
GROUP BY class_code
ORDER BY class_code;
Table in-out functions
catchments (data TABLE, dem := VARCHAR, input_crs := VARCHAR, max_cells := BIGINT, min_stream_area_m2 := DOUBLE, snap_radius_m := DOUBLE) 2 examples

Delineate upstream D8 catchments for monitoring stations

Positional arguments
data TABLE
Rows containing station_id, x and y. Every station is delineated independently, including nested stations.
Named arguments
dem := VARCHAR
North-up projected-metre DEM GeoTIFF path or URL; vertical elevations must use metres. · default "demo://basin.tif"
input_crs := VARCHAR
Explicit CRS for station coordinates, always XY order. · default "EPSG:32618"
max_cells := BIGINT
Maximum DEM cells for in-memory D8 routing. · default 2000000
min_stream_area_m2 := DOUBLE
Minimum upstream drainage area for candidate snap cells, square metres. · default 0.0
snap_radius_m := DOUBLE
Optional Euclidean snapping radius to cells meeting min_stream_area_m2; zero uses the containing cell. · default 0.0
Returns columns
station_id VARCHAR
Input monitoring-station identity.
cell_count BIGINT
Number of upstream DEM cells, including the outlet cell.
area_m2 DOUBLE
Cell count multiplied by projected DEM cell area, square metres.
outlet_x DOUBLE
Selected outlet cell centre X in dem_crs.
outlet_y DOUBLE
Selected outlet cell centre Y in dem_crs.
snap_distance_m DOUBLE
Euclidean distance from the supplied station to the selected cell centre, metres.
boundary_limited BOOLEAN
True if the catchment reaches the DEM boundary or missing-data boundary; extent may be incomplete.
geometry_wkb BLOB
Upstream Polygon/MultiPolygon WKB in dem_crs; use ST_GeomFromWKB for spatial joins.
dem_crs VARCHAR
Projected metre CRS for catchment geometry and outlet coordinates.
status VARCHAR
ok, outside_dem, nodata, or no_stream_within_radius.
Examples (2)

Delineate the known synthetic valley

SELECT station_id, cell_count, area_m2
FROM hydrology.main.catchments((SELECT * FROM (VALUES ('outlet', 500035.0, 4100005.0)) AS gauges(station_id, x, y)))
ORDER BY station_id;
Result
station_id cell_count area_m2
outlet 49 4900

Dominant upstream land-cover class for four gauges on the synthetic valley: the two single-cell side gauges are pure class 10 (west) and class 20 (east), while the nested mid-valley gauge and the outlet are both mostly class 20

WITH basins AS (SELECT station_id, area_m2, geometry_wkb FROM hydrology.main.catchments((SELECT * FROM (VALUES ('west', 500015.0, 4100035.0), ('mid', 500035.0, 4100035.0), ('east', 500055.0, 4100035.0), ('outlet', 500035.0, 4100005.0)) AS gauges(station_id, x, y))) WHERE status = 'ok') SELECT b.station_id, b.area_m2, arg_max(l.class_code, l.area_m2) AS dominant_class, max(l.fraction_of_valid) AS dominant_share
FROM basins AS b
JOIN hydrology.main.landcover((SELECT station_id, geometry_wkb FROM basins)) AS l ON l.station_id = b.station_id
GROUP BY b.station_id, b.area_m2
ORDER BY b.station_id;
Link
landcover (data TABLE, geometry_crs := VARCHAR, max_pixels := BIGINT, raster := VARCHAR) 2 examples

Summarize categorical land cover inside upstream catchment polygons

Positional arguments
data TABLE
Rows containing station_id and valid polygon geometry_wkb, such as successful catchments results.
Named arguments
geometry_crs := VARCHAR
CRS of input catchment WKB; use the catchment's dem_crs. · default "EPSG:32618"
max_pixels := BIGINT
Maximum cells in one catchment's land-cover bounding window. · default 2000000
raster := VARCHAR
Categorical land-cover GeoTIFF with projected metre coordinates and declared nodata. · default "demo://landcover.tif"
Returns columns
station_id VARCHAR
Caller-owned station identity.
class_code BIGINT
Integer land-cover class; NULL if the polygon has no valid raster data.
pixel_count BIGINT
Valid cell centres inside the catchment for this class.
area_m2 DOUBLE
Class pixel count times source raster cell area, square metres; pixel-centre approximation.
fraction_of_valid DOUBLE
This class's share of valid observed catchment pixels.
coverage_fraction DOUBLE
Fraction of all catchment grid centres with valid land-cover observations.
status VARCHAR
ok or no_data; unknown land cover is not classified as a real category.
Examples (2)

Confirm the entire synthetic upstream area has classified land cover

SELECT sum(area_m2), min(coverage_fraction)
FROM hydrology.main.landcover((SELECT station_id, geometry_wkb FROM hydrology.main.catchments((SELECT * FROM (VALUES ('outlet', 500035.0, 4100005.0)) AS gauges(station_id, x, y))) WHERE status='ok'));

Land cover of the intervening drainage between a mid-valley gauge and the valley outlet: per class, outlet catchment area minus the nested upstream gauge's catchment area (1,500 m2 of class 10 and 1,800 m2 of class 20)

WITH gauges AS (SELECT * FROM (VALUES ('outlet', 500035.0, 4100005.0), ('mid', 500035.0, 4100035.0)) AS g(station_id, x, y)), cover AS (SELECT station_id, class_code, area_m2 FROM hydrology.main.landcover((SELECT station_id, geometry_wkb FROM hydrology.main.catchments((SELECT * FROM gauges)) WHERE status = 'ok'))) SELECT class_code, sum(area_m2) FILTER (WHERE station_id = 'outlet') AS outlet_area_m2, sum(area_m2) FILTER (WHERE station_id = 'mid') AS upstream_gauge_area_m2, outlet_area_m2 - upstream_gauge_area_m2 AS intervening_area_m2
FROM cover
GROUP BY class_code
ORDER BY class_code;
Link

Try these queries

Dominant upstream land-cover class for four gauges on the synthetic valley: the two single-cell side gauges are pure class 10 (west) and class 20 (east), while the nested mid-valley gauge and the outlet are both mostly class 20

WITH basins AS (SELECT station_id, area_m2, geometry_wkb FROM hydrology.main.catchments((SELECT * FROM (VALUES ('west', 500015.0, 4100035.0), ('mid', 500035.0, 4100035.0), ('east', 500055.0, 4100035.0), ('outlet', 500035.0, 4100005.0)) AS gauges(station_id, x, y))) WHERE status = 'ok') SELECT b.station_id, b.area_m2, arg_max(l.class_code, l.area_m2) AS dominant_class, max(l.fraction_of_valid) AS dominant_share
FROM basins AS b
JOIN hydrology.main.landcover((SELECT station_id, geometry_wkb FROM basins)) AS l ON l.station_id = b.station_id
GROUP BY b.station_id, b.area_m2
ORDER BY b.station_id

Uses catchmentslandcover

Confirm the entire synthetic upstream area has classified land cover

SELECT sum(area_m2), min(coverage_fraction)
FROM hydrology.main.landcover((SELECT station_id, geometry_wkb FROM hydrology.main.catchments((SELECT * FROM (VALUES ('outlet', 500035.0, 4100005.0)) AS gauges(station_id, x, y))) WHERE status='ok'))

Uses landcovercatchments

Land cover of the intervening drainage between a mid-valley gauge and the valley outlet: per class, outlet catchment area minus the nested upstream gauge's catchment area (1,500 m2 of class 10 and 1,800 m2 of class 20)

WITH gauges AS (SELECT * FROM (VALUES ('outlet', 500035.0, 4100005.0), ('mid', 500035.0, 4100035.0)) AS g(station_id, x, y)), cover AS (SELECT station_id, class_code, area_m2 FROM hydrology.main.landcover((SELECT station_id, geometry_wkb FROM hydrology.main.catchments((SELECT * FROM gauges)) WHERE status = 'ok'))) SELECT class_code, sum(area_m2) FILTER (WHERE station_id = 'outlet') AS outlet_area_m2, sum(area_m2) FILTER (WHERE station_id = 'mid') AS upstream_gauge_area_m2, outlet_area_m2 - upstream_gauge_area_m2 AS intervening_area_m2
FROM cover
GROUP BY class_code
ORDER BY class_code

Uses landcovercatchments

Hydrology 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.