Skip to content

Geocode

Turn latitude/longitude into city, country, admin region and timezone offline, plus haversine distance

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

Add city and country names to coordinates

Example output · captured
Connector release
v0.5.0
Engine
2.0.1
VGI protocol
3.4.0
SELECT lat, lon, geocode.main.nearest_city(lat, lon) AS city,
  geocode.main.country_code(lat, lon) AS country
FROM (VALUES (40.7128, -74.0060), (48.8566, 2.3522), (35.6762, 139.6503)) AS t(lat, lon)
Result
lat lon city country
40.7128 -74.006 New York City US
48.8566 2.3522 Paris FR
35.6762 139.65 Tokyo JP

Quick start

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

3. Run a query

query.sql
SELECT lat, lon, geocode.main.nearest_city(lat, lon) AS city,
  geocode.main.country_code(lat, lon) AS country
FROM (VALUES (40.7128, -74.0060), (48.8566, 2.3522), (35.6762, 139.6503)) AS t(lat, lon)
More example queries →
Technical details
Verified connector release
v0.5.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 'geocode' AS geocode (
  TYPE vgi,
  LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-geocode-worker'
);

Overview

Offline reverse geocoding: lat/lon -> place, country, admin regions, timezone, plus haversine distance

Catalog

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

geocode 1 schema
main 7 functions

Offline reverse geocoding: lat/lon -> place, country, admin regions, timezone

Schema examples (8)
Examples (8)

Enrich a set of coordinates with nearest city and ISO-3166 country code.

SELECT lat, lon, geocode.main.nearest_city(lat, lon) AS city,
  geocode.main.country_code(lat, lon) AS country
FROM (VALUES (40.7128, -74.0060), (48.8566, 2.3522), (35.6762, 139.6503)) AS t(lat, lon);
Result
lat lon city country
40.7128 -74.006 New York City US
48.8566 2.3522 Paris FR
35.6762 139.65 Tokyo JP

Count coordinates per country by reverse-geocoding to a country code.

SELECT geocode.main.country_code(lat, lon) AS country, count(*) AS n
FROM (VALUES (40.7, -74.0), (34.0, -118.2), (48.8, 2.35)) AS t(lat, lon)
GROUP BY country
ORDER BY n DESC;

Keep only points within 1000 km of Paris using the haversine distance.

SELECT name,
  round(geocode.main.distance_km(lat, lon, 48.8566, 2.3522)) AS km_to_paris
FROM (VALUES ('London', 51.5074, -0.1278), ('New York', 40.7128, -74.0060)) AS t(name, lat, lon)
WHERE geocode.main.distance_km(lat, lon, 48.8566, 2.3522) < 1000;

Pull the first-level admin region straight out of the reverse_geocode STRUCT.

SELECT geocode.main.reverse_geocode(40.7128, -74.0060).admin1 AS state;
4 more examples

Resolve the IANA timezone name for a coordinate to localize timestamps.

SELECT geocode.main.timezone(40.7128, -74.0060) AS tz;

Split a GPS trace (Paris -> Lyon -> Geneva -> Zurich -> Milan) into legs with LAG, measure each leg with distance_km and flag the legs that cross a border by comparing country_code at both ends (FR->CH and CH->IT cross; FR->FR and CH->CH do not).

WITH trace AS (SELECT * FROM (VALUES (1, 48.8566, 2.3522), (2, 45.7640, 4.8357), (3, 46.2044, 6.1432), (4, 47.3769, 8.5417), (5, 45.4642, 9.1900)) AS t(seq, lat, lon)), legs AS (SELECT seq, lag(lat) OVER w AS prev_lat, lag(lon) OVER w AS prev_lon, lat, lon FROM trace WINDOW w AS (ORDER BY seq)) SELECT seq, geocode.main.country_code(prev_lat, prev_lon) AS from_country, geocode.main.country_code(lat, lon) AS to_country, round(geocode.main.distance_km(prev_lat, prev_lon, lat, lon)) AS leg_km, from_country <> to_country AS crosses_border
FROM legs
WHERE prev_lat IS NOT NULL
ORDER BY seq;

Measure how far each point is from the GeoNames place it was labelled with by feeding the reverse_geocode STRUCT's place_lat/place_lon into distance_km; a large gap (an ocean point ~1079 km from Ribeira Grande, PT) flags a city label that should not be trusted, while Times Square is ~3 km from its match.

SELECT name, p.city, p.country_code,
  round(geocode.main.distance_km(lat, lon, p.place_lat, p.place_lon), 1) AS km_to_matched_place
FROM (SELECT name, lat, lon, geocode.main.reverse_geocode(lat, lon) AS p FROM (VALUES ('Times Square', 40.7580, -73.9855), ('Nevada desert', 38.5, -116.5), ('Mid-Atlantic', 35.0, -40.0), ('Null Island', 0.0, 0.0)) AS t(name, lat, lon))
ORDER BY km_to_matched_place DESC;

Convert UTC event timestamps to each event's local wall-clock time: timezone(lat, lon) supplies the IANA zone for DuckDB's timezone(zone, ts), labelled with nearest_city (14:00 UTC is 09:00 in New York and 23:00 in Tokyo in March, 15:00 in London in July).

SELECT event_id, geocode.main.nearest_city(lat, lon) AS city, tz, ts_utc,
  timezone(tz, ts_utc) AS local_time
FROM (SELECT *, geocode.main.timezone(lat, lon) AS tz FROM (VALUES (1, 40.7128, -74.0060, TIMESTAMPTZ '2026-03-01 14:00:00+00'), (2, 35.6762, 139.6503, TIMESTAMPTZ '2026-03-01 14:00:00+00'), (3, 51.5074, -0.1278, TIMESTAMPTZ '2026-07-01 14:00:00+00')) AS t(event_id, lat, lon, ts_utc))
ORDER BY event_id;
Scalar functions
admin1 (lat DOUBLE, lon DOUBLE) 1 example

First-level admin region (state / region) of the nearest place; NULL if out of range

Arguments
lat DOUBLE
Latitude in degrees (-90..90); NULL or out of range -> NULL.
lon DOUBLE
Longitude in degrees (-180..180); NULL or out of range -> NULL.
ReturnsVARCHAR
Example

First-level admin region (prefecture) for the Tokyo coordinates.

SELECT geocode.main.admin1(35.6762, 139.6503) AS region;
Link
admin2 (lat DOUBLE, lon DOUBLE) 1 example

Second-level admin region (county/district) of the nearest place; NULL if out of range

Arguments
lat DOUBLE
Latitude in degrees (-90..90); NULL or out of range -> NULL.
lon DOUBLE
Longitude in degrees (-180..180); NULL or out of range -> NULL.
ReturnsVARCHAR
Example

Second-level admin region (county) for the Los Angeles coordinates.

SELECT geocode.main.admin2(34.0522, -118.2437) AS county;
Link
country_code (lat DOUBLE, lon DOUBLE) 1 example

ISO-3166 alpha-2 country code of the nearest place; NULL if out of range

Arguments
lat DOUBLE
Latitude in degrees (-90..90); NULL or out of range -> NULL.
lon DOUBLE
Longitude in degrees (-180..180); NULL or out of range -> NULL.
ReturnsVARCHAR
Example

ISO-3166 country code for the Paris coordinates (returns 'FR').

SELECT geocode.main.country_code(48.8566, 2.3522) AS country;
Link
distance_km (lat1 DOUBLE, lon1 DOUBLE, lat2 DOUBLE, lon2 DOUBLE) 2 examples

Great-circle (haversine) distance in km between two points; NULL if out of range

Arguments
lat1 DOUBLE
Latitude of point 1 (-90..90).
lon1 DOUBLE
Longitude of point 1 (-180..180).
lat2 DOUBLE
Latitude of point 2 (-90..90).
lon2 DOUBLE
Longitude of point 2 (-180..180).
ReturnsDOUBLE
Examples (2)

Great-circle distance from New York to London, in kilometers (~5570).

SELECT geocode.main.distance_km(40.7128, -74.0060, 51.5074, -0.1278) AS km;

Split a GPS trace (Paris -> Lyon -> Geneva -> Zurich -> Milan) into legs with LAG, measure each leg with distance_km and flag the legs that cross a border by comparing country_code at both ends (FR->CH and CH->IT cross; FR->FR and CH->CH do not).

WITH trace AS (SELECT * FROM (VALUES (1, 48.8566, 2.3522), (2, 45.7640, 4.8357), (3, 46.2044, 6.1432), (4, 47.3769, 8.5417), (5, 45.4642, 9.1900)) AS t(seq, lat, lon)), legs AS (SELECT seq, lag(lat) OVER w AS prev_lat, lag(lon) OVER w AS prev_lon, lat, lon FROM trace WINDOW w AS (ORDER BY seq)) SELECT seq, geocode.main.country_code(prev_lat, prev_lon) AS from_country, geocode.main.country_code(lat, lon) AS to_country, round(geocode.main.distance_km(prev_lat, prev_lon, lat, lon)) AS leg_km, from_country <> to_country AS crosses_border
FROM legs
WHERE prev_lat IS NOT NULL
ORDER BY seq;
Link
nearest_city (lat DOUBLE, lon DOUBLE) 7 examples

Name of the nearest known city to (lat, lon); NULL if out of range

Arguments
lat DOUBLE
Latitude in degrees (-90..90); NULL or out of range -> NULL.
lon DOUBLE
Longitude in degrees (-180..180); NULL or out of range -> NULL.
ReturnsVARCHAR
Examples (7)

Nearest city to the New York City coordinates.

SELECT geocode.main.nearest_city(40.7128, -74.0060) AS city;
SELECT geocode.main.country_code(48.8566, 2.3522) AS cc;
SELECT geocode.main.admin1(35.6762, 139.6503) AS region;
4 more examples
SELECT geocode.main.reverse_geocode(40.7128, -74.0060) AS place;
SELECT geocode.main.timezone(40.7128, -74.0060) AS tz;
SELECT geocode.main.distance_km(40.7128, -74.0060, 51.5074, -0.1278) AS km;
WITH trace AS (SELECT * FROM (VALUES (1, 48.8566, 2.3522), (2, 45.7640, 4.8357), (3, 46.2044, 6.1432), (4, 47.3769, 8.5417), (5, 45.4642, 9.1900)) AS t(seq, lat, lon)), legs AS (SELECT seq, lag(lat) OVER w AS prev_lat, lag(lon) OVER w AS prev_lon, lat, lon FROM trace WINDOW w AS (ORDER BY seq)) SELECT seq, geocode.main.country_code(prev_lat, prev_lon) AS from_country, geocode.main.country_code(lat, lon) AS to_country, round(geocode.main.distance_km(prev_lat, prev_lon, lat, lon)) AS leg_km, from_country <> to_country AS crosses_border
FROM legs
WHERE prev_lat IS NOT NULL
ORDER BY seq;
Link
reverse_geocode (lat DOUBLE, lon DOUBLE) 3 examples

Nearest place as STRUCT(city, admin1, admin2, country_code, place_lat, place_lon); NULL if out of range

Arguments
lat DOUBLE
Latitude in degrees (-90..90); NULL or out of range -> NULL.
lon DOUBLE
Longitude in degrees (-180..180); NULL or out of range -> NULL.
ReturnsSTRUCT of 6 fields
STRUCT 6 fields
  • cityVARCHAR
  • admin1VARCHAR
  • admin2VARCHAR
  • country_codeVARCHAR
  • place_latDOUBLE
  • place_lonDOUBLE
Examples (3)

Full nearest-place STRUCT for the New York City coordinates.

SELECT geocode.main.reverse_geocode(40.7128, -74.0060) AS place;

Pluck the city field from the nearest-place STRUCT (Tokyo).

SELECT geocode.main.reverse_geocode(35.6762, 139.6503).city AS city;

Measure how far each point is from the GeoNames place it was labelled with by feeding the reverse_geocode STRUCT's place_lat/place_lon into distance_km; a large gap (an ocean point ~1079 km from Ribeira Grande, PT) flags a city label that should not be trusted, while Times Square is ~3 km from its match.

SELECT name, p.city, p.country_code,
  round(geocode.main.distance_km(lat, lon, p.place_lat, p.place_lon), 1) AS km_to_matched_place
FROM (SELECT name, lat, lon, geocode.main.reverse_geocode(lat, lon) AS p FROM (VALUES ('Times Square', 40.7580, -73.9855), ('Nevada desert', 38.5, -116.5), ('Mid-Atlantic', 35.0, -40.0), ('Null Island', 0.0, 0.0)) AS t(name, lat, lon))
ORDER BY km_to_matched_place DESC;
Link
timezone (lat DOUBLE, lon DOUBLE) 2 examples

IANA timezone name (e.g. 'America/New_York') for (lat, lon); NULL if out of range

Arguments
lat DOUBLE
Latitude in degrees (-90..90); NULL or out of range -> NULL.
lon DOUBLE
Longitude in degrees (-180..180); NULL or out of range -> NULL.
ReturnsVARCHAR
Examples (2)

IANA timezone name for the New York City coordinates.

SELECT geocode.main.timezone(40.7128, -74.0060) AS tz;

Convert UTC event timestamps to each event's local wall-clock time: timezone(lat, lon) supplies the IANA zone for DuckDB's timezone(zone, ts), labelled with nearest_city (14:00 UTC is 09:00 in New York and 23:00 in Tokyo in March, 15:00 in London in July).

SELECT event_id, geocode.main.nearest_city(lat, lon) AS city, tz, ts_utc,
  timezone(tz, ts_utc) AS local_time
FROM (SELECT *, geocode.main.timezone(lat, lon) AS tz FROM (VALUES (1, 40.7128, -74.0060, TIMESTAMPTZ '2026-03-01 14:00:00+00'), (2, 35.6762, 139.6503, TIMESTAMPTZ '2026-03-01 14:00:00+00'), (3, 51.5074, -0.1278, TIMESTAMPTZ '2026-07-01 14:00:00+00')) AS t(event_id, lat, lon, ts_utc))
ORDER BY event_id;
Link

Try these queries

Count coordinates per country by reverse-geocoding to a country code.

SELECT geocode.main.country_code(lat, lon) AS country, count(*) AS n
FROM (VALUES (40.7, -74.0), (34.0, -118.2), (48.8, 2.35)) AS t(lat, lon)
GROUP BY country
ORDER BY n DESC

Uses country_code

Keep only points within 1000 km of Paris using the haversine distance.

SELECT name,
  round(geocode.main.distance_km(lat, lon, 48.8566, 2.3522)) AS km_to_paris
FROM (VALUES ('London', 51.5074, -0.1278), ('New York', 40.7128, -74.0060)) AS t(name, lat, lon)
WHERE geocode.main.distance_km(lat, lon, 48.8566, 2.3522) < 1000

Uses distance_km

Pull the first-level admin region straight out of the reverse_geocode STRUCT.

SELECT geocode.main.reverse_geocode(40.7128, -74.0060).admin1 AS state

Uses reverse_geocode

Resolve the IANA timezone name for a coordinate to localize timestamps.

SELECT geocode.main.timezone(40.7128, -74.0060) AS tz

Uses timezone

Convert UTC event timestamps to each event's local wall-clock time: timezone(lat, lon) supplies the IANA zone for DuckDB's timezone(zone, ts), labelled with nearest_city (14:00 UTC is 09:00 in New York and 23:00 in Tokyo in March, 15:00 in London in July).

SELECT event_id, geocode.main.nearest_city(lat, lon) AS city, tz, ts_utc,
  timezone(tz, ts_utc) AS local_time
FROM (SELECT *, geocode.main.timezone(lat, lon) AS tz FROM (VALUES (1, 40.7128, -74.0060, TIMESTAMPTZ '2026-03-01 14:00:00+00'), (2, 35.6762, 139.6503, TIMESTAMPTZ '2026-03-01 14:00:00+00'), (3, 51.5074, -0.1278, TIMESTAMPTZ '2026-07-01 14:00:00+00')) AS t(event_id, lat, lon, ts_utc))
ORDER BY event_id

Uses nearest_citytimezone

First-level admin region (prefecture) for the Tokyo coordinates.

SELECT geocode.main.admin1(35.6762, 139.6503) AS region

Uses admin1

9 more examples in the Catalog, each listed under the table or function it uses.

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.

  • What ISO-3166 alpha-2 country code corresponds to latitude 48.8566, longitude 2.3522?
  • What is the name of the nearest known city to latitude 48.8566, longitude 2.3522?
  • Which first-level administrative region (state/prefecture/region) is nearest to latitude 35.6762, longitude 139.6503?
  • Which second-level administrative region (county/district) is nearest to latitude 34.0522, longitude -118.2437?
  • Using the combined nearest-place lookup for latitude 40.7128, longitude -74.0060, return the ISO-3166 country code of that place.
  • What IANA timezone name applies at latitude 40.7128, longitude -74.0060?

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