# Meet in the middle, and other graph primitives

`POST /centroid` takes several locations and returns the point in the road network where paths from all of them converge for the least total cost, plus one route per participant to that shared point. We found no equivalent in the Google, Mapbox, HERE, TomTom or NextBillion APIs. It ships alongside two more primitives cut from the same cloth: `POST /locate` snaps a coordinate to the routing graph and reports the edges it finds there, and `GET /boundary` answers point-in-polygon against the administrative-boundary database our routing already consults. None of the three needed new engine capability. The data and the services were already underneath; the endpoints expose them.

## What does POST /centroid actually compute?

A least-cost convergence point on the road graph, not an average of coordinates on a sphere. The request is the same shape as `POST /route`: `locations` (at least two), `costing`, optional `costing_options`. The costing applies identically to every participant's path, so `truck` with a dimensioned profile gives you a meeting point every lorry in the group can legally reach.

```bash
export BASE=https://api.mapmap.ai
export API_KEY=snk_...

curl -fsS -X POST "$BASE/centroid" \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "locations": [
    { "lat": 51.5074, "lon": -0.1278 },
    { "lat": 51.4545, "lon": -0.9781 },
    { "lat": 52.2405, "lon": -0.9027 }
  ],
  "costing": "auto"
}'
```

The response is a Valhalla route response with a fixed convention: `trip` is the path from `locations[0]` to the centroid, and each `alternates[i]` is the path from `locations[i+1]` to the same centroid.

```json
{
  "trip": { "locations": ["..."], "legs": ["..."], "summary": ["..."] },
  "alternates": [
    { "trip": ["..."] },
    { "trip": ["..."] }
  ]
}
```

Every path's last location carries an identical latitude and longitude. That shared coordinate is the meeting point, and each participant's leg gives you their full turn-by-turn route to it. Each leg's `summary.time` is the real driving time to that point, the whole approach plus half of the edge the convergence point sits on, and it agrees with `POST /route` over the same journey to within 10 ms. That is worth saying because stock Valhalla discounted the whole path rather than that final edge, and reported every leg at very nearly half its true time while the distances stayed correct. The fix is ours (`fork/patches/0007`), it ships in the distro and it is what the hosted engine runs, so a self-host on an unpatched build is the only place the halved numbers survive. Three field engineers converging on one emergency job, a haulier and a subcontractor agreeing a trailer swap, four friends picking a fair place to meet: it is one request each time.

## Why is the graph answer different from the geographic midpoint?

Because the point halfway between people on a map is frequently somewhere none of them can drive to, and equidistant is not the same as equal effort. Average the coordinates and you can land in a river, a field or the middle of a pedestrian zone. Snap that average to the nearest road and you have fixed the first problem while keeping the second: a candidate that is 20 minutes from one person and 50 from another, because straight-line distance ignores one-way systems, motorway access and everything else the road network actually does.

The centroid is computed on the graph itself, so the meeting point is always on the network, reachable under the chosen costing, and chosen for least total cost across every participant rather than least distance on a sphere. Someone sitting next to a motorway junction covers 30 km more cheaply than someone crossing a town centre, and the answer reflects that.

## What does POST /locate tell you about a coordinate?

The edges the routing graph holds at that point: way ids, the correlated point on each edge, the side of street and how far along the way the match sits. Set `"verbose": true` and each location also returns nodes and a dense administrative, access and geometry attribute tree; the default `false` keeps the answer to the closest point, way id and side of street per edge. The `costing` you pass gates which edges count as reachable, so a coordinate can locate differently for a pedestrian and a 44-tonne lorry.

```bash
curl -fsS -X POST "$BASE/locate" \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "locations": [{ "lat": 51.5074, "lon": -0.1278 }],
  "costing": "auto",
  "verbose": false
}'
```

This is the debugging primitive for every "why did the route start there" question: it shows you what the graph believes exists at a coordinate before any routing happens. A delivery pin that snaps to the wrong side of a dual carriageway, a depot gate that turns out to sit on a service road the truck costing excludes, both are visible in one call. A location with no nearby edges at all (open sea, an unmapped area) answers an entry with empty `edges` and `nodes` arrays, never an error.

## What does GET /boundary return, and where does the data come from?

The containing country and sub-divisions for a point, read from the same administrative-boundary database Valhalla's own routing already consults for facts like which side of the road to drive on. Each hit carries `admin_level`, `iso_code`, `name` and `drive_on_right`, sorted country before sub-division:

```bash
curl -fsS "$BASE/boundary?lat=51.5074&lon=-0.1278" \
  -H "Authorization: Bearer $API_KEY"
```

```json
{
  "point": { "lat": 51.5074, "lon": -0.1278 },
  "admins": [
    { "admin_level": 2, "iso_code": "GB", "name": "United Kingdom", "drive_on_right": false },
    { "admin_level": 4, "iso_code": "ENG", "name": "England", "drive_on_right": false }
  ]
}
```

An empty `admins` array is a normal answer (open sea, or a genuine gap in a deployment's admin-boundary coverage), never an error. This is the thesis of the whole trio: the cheapest features are the ones your data already contains. The boundary database was already on disk because routing needs it; `/boundary` just makes it answer questions directly, and its sibling `POST /route/report` does the same trick by aggregating `/trace_attributes` internally rather than making a second engine call. The full contract for all of them is in the [API reference](/docs/api-reference).

## What do they cost?

One Standard call per request, 0.05p at the entry band, unless the request carries truck costing, in which case `/locate` and `/centroid` price as Premium at 1p, [the same rule as /matrix and /isochrone](/news/matrix-api). `GET /boundary` takes no costing, so it is always Standard.

| Endpoint | Price class | Entry-band price |
|---|---|---|
| `POST /centroid` | Standard; Premium with truck costing | 0.05p; 1p with truck costing |
| `POST /locate` | Standard; Premium with truck costing | 0.05p; 1p with truck costing |
| `GET /boundary` | Standard | 0.05p |

The free tier covers 50,000 included standard calls a month after email verification, and a premium call draws 20 of them.

## What they do not do

They do not pretend to run on data or engines a deployment does not have: the failure mode is a clean 501, by design. The specifics:

- `/locate` and `/centroid` are Valhalla-only. A deployment running `SN_ROUTING_ENGINE=graphhopper` answers 501 rather than attempting a call that cannot succeed, because GraphHopper has no equivalent service. The `valhalla` and `auto` engine modes (`auto` is the default) both serve them. That is a self-host concern rather than a hosted one: `api.mapmap.ai` runs Valhalla, so both answer there.
- `/boundary` answers 501 when the operator has not set `SN_ADMIN_BOUNDARIES_DB`. No database, no lookup, and we would rather say so than guess. Also a self-host concern: the hosted gateway has the boundary database configured.
- `/centroid` needs at least two locations; one location is a 400 (Valhalla error code 120).
- `/centroid` returns a coordinate and paths, not a venue. If you want somewhere to actually meet, reverse-geocode the meeting point with `GET /geocode/reverse?kinds=poi` and pick from what is nearby.
- Boundary coverage follows the deployment's data. An empty `admins` array can mean open sea or a real gap, and the response does not distinguish the two.

Errors across all three follow the gateway's problem+json envelope, documented once in the [API conventions](/docs/conventions).

## Try it

A key is one call (`POST /v1/keys` with an email and terms acceptance) or a [browser signup](/signup), and the free tier's 50,000 monthly calls are more than enough to find out whether a graph centroid beats the midpoint arithmetic you are running today. Start with three real locations from your own operation and compare where each method tells you to meet.

The administrative-boundary data derives from OpenStreetMap. Credit "© OpenStreetMap contributors" when you render or republish it.
