Skip to content

Nearest

Find the N nearest features in a dataset to a given coordinate. Returns features sorted by distance, measured as geodesic distance to the nearest polygon boundary (WGS84 ellipsoid).

GET /v1/nearest
ParameterTypeRequiredDefaultDescription
latnumberYes—Latitude (-90 to 90)
lngnumberYes—Longitude (-180 to 180)
system_datasetstringOne required—System dataset slug (e.g., us-counties)
datasetstringOne required—Your dataset slug
dataset_idstringOne required—Dataset UUID
nintegerNo1Number of results (1–20)
radiusnumberNo—Maximum distance in meters (up to 500,000)

Provide exactly one of system_dataset, dataset, or dataset_id.

Terminal window
curl -s "https://api.terranode.co/v1/nearest?lat=40.7128&lng=-74.006&system_dataset=us-counties" \
-H "x-api-key: YOUR_API_KEY" | jq .
{
"match": true,
"matches": [
{
"properties": {
"STATEFP": "36",
"COUNTYFP": "061",
"GEOID": "36061",
"NAME": "New York",
"NAMELSAD": "New York County",
"STUSPS": "NY",
"STATE_NAME": "New York"
},
"distance_m": 1382,
"distance_mi": 0.86
}
],
"dataset": "us-counties",
"version": 1,
"latency_ms": 435,
"api_version": "v1",
"semantics_version": "0.1.0"
}
FieldDescription
matchtrue if at least one nearest feature was found
matchesArray of nearest features, sorted by distance
matches[].distance_mGeodesic distance in meters (0 if point is inside the polygon)
matches[].distance_miGeodesic distance in miles
Terminal window
curl -s "https://api.terranode.co/v1/nearest?lat=40.7128&lng=-74.006&system_dataset=us-counties&n=5" \
-H "x-api-key: YOUR_API_KEY" | jq .

Only return features within 5 km:

Terminal window
curl -s "https://api.terranode.co/v1/nearest?lat=40.7128&lng=-74.006&system_dataset=us-counties&n=10&radius=5000" \
-H "x-api-key: YOUR_API_KEY" | jq .

Features beyond the radius are excluded from results. If no features fall within the radius, matches is an empty array.

Distances are geodesic measurements on the WGS84 ellipsoid using Karney’s algorithm — the same math as PostGIS ST_Distance_Spheroid. For polygon datasets, distance is measured to the nearest point on the polygon boundary. A distance of 0 means the query point is inside the polygon.


POST /v1/nearest/batch

Send multiple points in a single request. See Batch Requests for input formats.

ParameterTypeRequiredDefaultDescription
system_datasetstringOne required—System dataset slug
datasetstringOne required—Your dataset slug
dataset_idstringOne required—Dataset UUID
nintegerNo1Number of results per point (1–20)
radiusnumberNo—Maximum distance in meters
Terminal window
curl -s -X POST "https://api.terranode.co/v1/nearest/batch?system_dataset=us-counties" \
-H "x-api-key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '[
{"lat": 40.7128, "lng": -74.006, "id": "nyc"},
{"lat": 34.0522, "lng": -118.2437, "id": "la"}
]' | jq .

Up to 100 points per request on the Free tier. Each point counts as one request against your monthly quota.