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
- 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/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)
| 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.
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 'geocode' AS geocode (
TYPE vgi,
LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-geocode-worker'
);
Attach the signed image
Needs Docker, Podman or another OCI runtime. The extension runs the container and talks to it over stdio.
ATTACH 'geocode' AS geocode (
TYPE vgi,
LOCATION 'oci://ghcr.io/query-farm/vgi-geocode:0.5.0'
);
3. Run a query
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)
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 'geocode' AS geocode
(TYPE vgi, LOCATION 'https://your-connector.example.com');
4. Run a query
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)
Query Farm operates Geocode 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 geocode as the catalog alias for the query below.
4. Run a query
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)
Technical details
- Verified connector release
- v0.5.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 'geocode' AS geocode (
TYPE vgi,
LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-geocode-worker'
);
Attach the signed image
Needs Docker, Podman or another OCI runtime. The extension runs the container and talks to it over stdio.
ATTACH 'geocode' AS geocode (
TYPE vgi,
LOCATION 'oci://ghcr.io/query-farm/vgi-geocode:0.5.0'
);
Overview
Offline reverse geocoding: lat/lon -> place, country, admin regions, timezone, plus haversine distance
Turn latitude/longitude coordinates into the nearest city, country, admin regions and IANA timezone -- entirely offline, with no API keys, no rate limits and no network calls -- directly in DuckDB.
geocode is a VGI worker that brings fast, hermetic reverse geocoding to SQL. Point it at any column of (lat, lon) coordinates and it enriches each row with the nearest known place name, its ISO-3166 country code, first- and second-level administrative regions (state/region, county/district), and the local IANA timezone. Because every lookup runs against bundled spatial databases rather than a hosted geocoding API, it works on air-gapped hosts, scales to millions of rows without per-request billing, and returns deterministic results. It is built for data engineers, analysts and GIS practitioners who need to attach human-readable geography and time zones to coordinate data sets -- IoT and GPS traces, telemetry, event logs, store/asset locations -- without leaving the database.
Under the hood, place lookups are powered by reverse_geocoder (PyPI), which queries a GeoNames cities KD-tree to find the nearest populated place in sub-millisecond time. Timezone resolution uses timezonefinder (documentation), which maps a point to its IANA time-zone polygon offline. Distances are computed with the great-circle haversine formula. Every function is total and NULL-safe: a NULL or out-of-range coordinate yields NULL rather than an error, so the functions are safe to drop into large batch queries.
What you can do
The worker groups its capabilities into three areas:
- Place lookup -- resolve a coordinate to the nearest city, its ISO-3166 country, and first- and second-level administrative regions (state/region and county/district), either one attribute at a time or as a single combined
STRUCTrecord for the whole place. - Timezone -- map a coordinate to its IANA timezone name (e.g.
America/New_York) for localizing timestamps. - Distance -- measure the great-circle (haversine) distance in kilometers between two coordinates.
Every operation is a per-row scalar you can use inline in any projection or predicate, so enriching a whole coordinate column -- or filtering it by proximity -- is a single SELECT with no joins, staging tables, or API round-trips.
Backed by GeoNames cities (reverse_geocoder, LGPL-3.0, used as an unmodified dependency) and offline IANA timezone polygons (timezonefinder, MIT).
Catalog
Everything geocode exposes once attached — read straight from the worker's
released build.
main 7 functions
Offline reverse geocoding: lat/lon -> place, country, admin regions, timezone
Schema 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);
| 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;
admin1 (lat DOUBLE, lon DOUBLE)
First-level admin region (state / region) of the nearest place; NULL if out of range
First-level admin region (prefecture) for the Tokyo coordinates.
SELECT geocode.main.admin1(35.6762, 139.6503) AS region;
admin2 (lat DOUBLE, lon DOUBLE)
Second-level admin region (county/district) of the nearest place; NULL if out of range
Second-level admin region (county) for the Los Angeles coordinates.
SELECT geocode.main.admin2(34.0522, -118.2437) AS county;
country_code (lat DOUBLE, lon DOUBLE)
ISO-3166 alpha-2 country code of the nearest place; NULL if out of range
ISO-3166 country code for the Paris coordinates (returns 'FR').
SELECT geocode.main.country_code(48.8566, 2.3522) AS country;
distance_km (lat1 DOUBLE, lon1 DOUBLE, lat2 DOUBLE, lon2 DOUBLE)
Great-circle (haversine) distance in km between two points; NULL if out of range
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;
nearest_city (lat DOUBLE, lon DOUBLE)
Name of the nearest known city to (lat, lon); NULL if out of range
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;
reverse_geocode (lat DOUBLE, lon DOUBLE)
Nearest place as STRUCT(city, admin1, admin2, country_code, place_lat, place_lon); NULL if out of range
-
cityVARCHAR
-
admin1VARCHAR
-
admin2VARCHAR
-
country_codeVARCHAR
-
place_latDOUBLE
-
place_lonDOUBLE
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;
timezone (lat DOUBLE, lon DOUBLE)
IANA timezone name (e.g. 'America/New_York') for (lat, lon); NULL if out of range
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;
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.
Related workers
libpostal
Maps & places
Parse and normalize international street addresses into house number, road, city, postcode and more
PROJ
Maps & places
Reproject coordinates between CRSs and measure geodesic distances on the WGS84 ellipsoid
ArcGIS
Maps & places
Live ArcGIS feature queries, server summaries and raster sampling from service URLs worldwide