Skip to content

Trajectory

Reconstruct visits, dwell and trips from GPS observations

Connector software
Free and open source
Service credentials
Not needed
Query Farm hosting
Not available yet
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/trajectory/index.md to help me query Trajectory 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.

Count observations per synthetic entity

Example output · captured
Connector release
v0.3.0
Engine
2.0.1
VGI protocol
3.4.0
SELECT entity_id, count(*)
FROM trajectory.main.sample_fixes
GROUP BY entity_id
ORDER BY entity_id
Result
entity_id count_star()
crossing 2
stopped 2

Quick start

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

3. Run a query

query.sql
SELECT entity_id, count(*)
FROM trajectory.main.sample_fixes
GROUP BY entity_id
ORDER BY entity_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 'trajectory' AS trajectory (
  TYPE vgi,
  LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-trajectory-worker'
);

Overview

GPS track analysis: turns timestamped position fixes into geofence visits, trips and explicit observation gaps, interpolating crossings between fixes but never inferring visits across long gaps, implausible speeds or poor-accuracy fixes. Sample fixes and fences are included.

Catalog

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

trajectory 1 schema
main 1 table · 1 function

Spatial functions and bundled inputs for trajectory analysis.

Schema examples (4)
Examples (4)

Count observations per synthetic entity

SELECT entity_id, count(*)
FROM trajectory.main.sample_fixes
GROUP BY entity_id
ORDER BY entity_id;
Result
entity_id count_star()
crossing 2
stopped 2

Recover a visit even though neither recorded fix is inside the fence

SELECT entity_id, duration_s, fix_count
FROM trajectory.main.episodes((SELECT * FROM trajectory.main.sample_fixes))
WHERE episode_type='visit'
ORDER BY entity_id;

ASOF-join each reconstructed visit back to the last sample_fixes fix at or before its entry: the crossing entity entered the yard 30 seconds after its last fix and has zero fixes inside, while the stopped entity's visit starts at a recorded fix

SELECT v.entity_id, v.fence_id, v.started_at, f.observed_at AS last_fix_at,
  date_diff('second', f.observed_at, v.started_at) AS seconds_after_fix,
  v.fix_count
FROM trajectory.main.episodes((SELECT * FROM trajectory.main.sample_fixes)) AS v
ASOF JOIN trajectory.main.sample_fixes AS f ON v.entity_id = f.entity_id AND v.started_at >= f.observed_at
WHERE v.episode_type = 'visit'
ORDER BY v.entity_id, v.started_at;

Share of each entity's observed span (first to last sample_fixes fix) spent in fence visits: crossing spends 60 of 120 seconds in the yard (0.5), stopped all 600 seconds (1.0)

WITH span AS (SELECT entity_id, date_diff('second', min(observed_at), max(observed_at)) AS observed_s FROM trajectory.main.sample_fixes GROUP BY entity_id), visits AS (SELECT entity_id, sum(duration_s) AS visit_s FROM trajectory.main.episodes((SELECT * FROM trajectory.main.sample_fixes)) WHERE episode_type = 'visit' GROUP BY entity_id) SELECT span.entity_id, span.observed_s, coalesce(visits.visit_s, 0) AS visit_s, round(coalesce(visits.visit_s, 0) / span.observed_s, 3) AS visit_share
FROM span
LEFT JOIN visits USING (entity_id)
ORDER BY span.entity_id;
Tables
sample_fixes 1 example4 cols

Two synthetic GPS tracks demonstrating a between-fix crossing and a stop

Columns
entity_idnot null VARCHAR
Caller-owned vehicle or device identity.
observed_at TIMESTAMP
UTC GPS observation time; naive timestamps are interpreted as UTC.
x DOUBLE
Position X in input_crs.
y DOUBLE
Position Y in input_crs.
ConstraintsPRIMARY KEY (entity_id, observed_at)
Example

Count observations per synthetic entity

SELECT entity_id, count(*)
FROM trajectory.main.sample_fixes
GROUP BY entity_id
ORDER BY entity_id;
Result
entity_id count_star()
crossing 2
stopped 2
Link
Table in-out functions
episodes (data TABLE, analysis_crs := VARCHAR, fence_buffer_m := DOUBLE, fence_crs := VARCHAR, fences := VARCHAR, input_crs := VARCHAR, max_accuracy_m := DOUBLE, max_episodes := BIGINT, max_fence_vertices := BIGINT, max_fences := BIGINT, max_gap_s := DOUBLE, max_input_bytes := BIGINT, max_json_bytes := BIGINT, max_path_vertices := BIGINT, max_points := BIGINT, max_segment_splits := BIGINT, max_speed_m_s := DOUBLE, min_dwell_s := DOUBLE) 3 examples

Reconstruct visits, dwell, trips and gaps from timestamped GPS rows

Positional arguments
data TABLE
Rows with entity_id, observed_at, x and y; optional accuracy_m. Unsorted and interleaved entities are accepted.
Named arguments
analysis_crs := VARCHAR
Suitable local projected metre CRS for segment interpolation and distances. · default "EPSG:32618"
fence_buffer_m := DOUBLE
Explicit outward polygon tolerance for GPS jitter, metres; changes the spatial definition of visits. · default 0.0
fence_crs := VARCHAR
Explicit CRS of fence coordinates; use EPSG:4326 for standard GeoJSON. · default "EPSG:32618"
fences := VARCHAR
Local FeatureCollection JSON path with unique feature id or properties.fence_id and Polygon/MultiPolygon geometry. · default "demo://fences.json"
input_crs := VARCHAR
CRS of GPS XY input coordinates; always XY axis order. · default "EPSG:32618"
max_accuracy_m := DOUBLE
Optional accuracy_m values above this bound prevent interpolation through that fix. · default 100.0
max_episodes := BIGINT
Maximum reconstructed episodes, including visits later removed by min_dwell_s. · default 1000000
max_fence_vertices := BIGINT
Maximum total fence coordinates after projection and optional buffering. · default 250000
max_fences := BIGINT
Maximum geofence polygons in the collection. · default 10000
max_gap_s := DOUBLE
Maximum seconds between fixes permitting movement interpolation; longer intervals become gap rows. · default 900.0
max_input_bytes := BIGINT
Maximum total Arrow bytes of used input columns before global reconstruction. · default 268435456
max_json_bytes := BIGINT
Maximum bytes in the geofence JSON file. · default 16777216
max_path_vertices := BIGINT
Maximum coordinates stored across reconstructed paths, including overlapping visits. · default 2000000
max_points := BIGINT
Hard input-point budget before global sorting; partition large histories by entity and time with overlap. · default 1000000
max_segment_splits := BIGINT
Maximum distinct boundary cut positions along one GPS segment. · default 10000
max_speed_m_s := DOUBLE
Maximum plausible straight-line speed; faster intervals become gaps. · default 80.0
min_dwell_s := DOUBLE
Exclude visit episodes shorter than this duration; trips are not merged across excluded visits. · default 0.0
Returns columns
entity_id VARCHAR
Input device/vehicle identity.
episode_id BIGINT
Stable chronological episode number within an entity for the supplied query.
episode_type VARCHAR
visit, trip, or gap. Overlapping fences produce independent visit rows.
fence_id VARCHAR
Visited fence identity, or NULL for trips and gaps.
started_at TIMESTAMP
UTC episode start; inferred boundary time when a segment crosses a fence.
ended_at TIMESTAMP
UTC episode end; inferred boundary time when a segment exits a fence.
duration_s DOUBLE
Elapsed seconds under the piecewise-linear interpolation model.
distance_m DOUBLE
Length of reconstructed projected track in metres; NULL for unknown movement gaps.
fix_count BIGINT
Number of observation timestamps inside this episode's time interval; may be zero for a between-fix visit.
interpolated_boundaries BOOLEAN
True if at least one episode boundary occurs between recorded GPS fixes.
left_censored BOOLEAN
True if the episode was already in progress at the start of available continuous observations.
right_censored BOOLEAN
True if the episode may continue past the end of available continuous observations.
status VARCHAR
linear_interpolation, long_gap, implausible_speed, or poor_accuracy.
geometry_wkb BLOB
Reconstructed episode LineString WKB in analysis_crs; NULL for gaps.
analysis_crs VARCHAR
Projected metre CRS used for lengths and reconstructed geometry.
Examples (3)

Recover a visit even though neither recorded fix is inside the fence

SELECT entity_id, duration_s, fix_count
FROM trajectory.main.episodes((SELECT * FROM trajectory.main.sample_fixes))
WHERE episode_type='visit'
ORDER BY entity_id;

ASOF-join each reconstructed visit back to the last sample_fixes fix at or before its entry: the crossing entity entered the yard 30 seconds after its last fix and has zero fixes inside, while the stopped entity's visit starts at a recorded fix

SELECT v.entity_id, v.fence_id, v.started_at, f.observed_at AS last_fix_at,
  date_diff('second', f.observed_at, v.started_at) AS seconds_after_fix,
  v.fix_count
FROM trajectory.main.episodes((SELECT * FROM trajectory.main.sample_fixes)) AS v
ASOF JOIN trajectory.main.sample_fixes AS f ON v.entity_id = f.entity_id AND v.started_at >= f.observed_at
WHERE v.episode_type = 'visit'
ORDER BY v.entity_id, v.started_at;

Share of each entity's observed span (first to last sample_fixes fix) spent in fence visits: crossing spends 60 of 120 seconds in the yard (0.5), stopped all 600 seconds (1.0)

WITH span AS (SELECT entity_id, date_diff('second', min(observed_at), max(observed_at)) AS observed_s FROM trajectory.main.sample_fixes GROUP BY entity_id), visits AS (SELECT entity_id, sum(duration_s) AS visit_s FROM trajectory.main.episodes((SELECT * FROM trajectory.main.sample_fixes)) WHERE episode_type = 'visit' GROUP BY entity_id) SELECT span.entity_id, span.observed_s, coalesce(visits.visit_s, 0) AS visit_s, round(coalesce(visits.visit_s, 0) / span.observed_s, 3) AS visit_share
FROM span
LEFT JOIN visits USING (entity_id)
ORDER BY span.entity_id;
Link

Try these queries

Recover a visit even though neither recorded fix is inside the fence

SELECT entity_id, duration_s, fix_count
FROM trajectory.main.episodes((SELECT * FROM trajectory.main.sample_fixes))
WHERE episode_type='visit'
ORDER BY entity_id

Uses episodessample_fixes

ASOF-join each reconstructed visit back to the last sample_fixes fix at or before its entry: the crossing entity entered the yard 30 seconds after its last fix and has zero fixes inside, while the stopped entity's visit starts at a recorded fix

SELECT v.entity_id, v.fence_id, v.started_at, f.observed_at AS last_fix_at,
  date_diff('second', f.observed_at, v.started_at) AS seconds_after_fix,
  v.fix_count
FROM trajectory.main.episodes((SELECT * FROM trajectory.main.sample_fixes)) AS v
ASOF JOIN trajectory.main.sample_fixes AS f ON v.entity_id = f.entity_id AND v.started_at >= f.observed_at
WHERE v.episode_type = 'visit'
ORDER BY v.entity_id, v.started_at

Uses episodessample_fixes

Share of each entity's observed span (first to last sample_fixes fix) spent in fence visits: crossing spends 60 of 120 seconds in the yard (0.5), stopped all 600 seconds (1.0)

WITH span AS (SELECT entity_id, date_diff('second', min(observed_at), max(observed_at)) AS observed_s FROM trajectory.main.sample_fixes GROUP BY entity_id), visits AS (SELECT entity_id, sum(duration_s) AS visit_s FROM trajectory.main.episodes((SELECT * FROM trajectory.main.sample_fixes)) WHERE episode_type = 'visit' GROUP BY entity_id) SELECT span.entity_id, span.observed_s, coalesce(visits.visit_s, 0) AS visit_s, round(coalesce(visits.visit_s, 0) / span.observed_s, 3) AS visit_share
FROM span
LEFT JOIN visits USING (entity_id)
ORDER BY span.entity_id

Uses sample_fixesepisodes

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