Skip to content
oslew
Get a free key

Use case

Last-mile delivery route planning

Last-mile route planning turns a day’s orders into a run a driver can follow: every stop located, put in a sensible order, with a line on the map and an expected time at each door. With oslew it is three calls — geocode each address, optimise the visit order, route each leg — and one rule that matters more than any of them: an address that cannot be pinned to a single door goes back to the office, not onto the van.

Last reviewed

A van leaves the depot in the morning with a list of orders. What the driver needs is a sequence of doors, a line on the map for each leg, and a time for each door that customers can be told. What the office needs is a list of the orders that should not be on the van at all. This page walks through building both, and where the pieces stop.

The run in three calls

  1. Locate every order with the geocoding API, and turn away the ones that do not resolve to a single door.
  2. Order the stops that remain with route optimisation, the depot first, as a round trip back to it.
  3. Route each consecutive pair with the routing API, for the leg’s line and its own time, and add the minutes spent at each door to get arrival times.

Geocoding is on the address rate and the other two on the routing rate, so locating tomorrow’s orders never spends today’s routing rate. The delivery planning example is a complete program in Python and TypeScript.

Step one: which addresses go on the van

Every geocode answer carries the evidence to decide this, and a delivery system should act on it rather than on the coordinates alone.

What the geocode says What the planner does
No results Sends the order back: there is nothing to drive to
tied_at_top above one Sends it back: the address matches several places equally well, and the first is only one of them
Precision is only street Sends it back: the street is right, the door is unknown, and a long street is a long walk
An address point, an interpolated number or a named place Puts it on the van; for an interpolated number, precision_m says how far off the door may be

The list of refusals, each with its reason, is the most useful thing the planner produces for the office: a phone call before the van leaves costs less than a wasted stop. The cheapest way to have fewer of them is to store the postcode with every order and send it after the last comma, where it narrows the search before scoring; geocoding precision levels explains what each level is good for. For a whole day’s orders at once, batch geocoding runs the same triage over a file.

A geocoded point is often the middle of a building. If drivers need the kerb, snap each accepted stop to its nearest road and use that position, keeping the original for the door.

Step two: the order of the stops

Send the depot as the first point and the accepted stops after it. The default round trip starts and ends at the depot. A courier who finishes at home instead sends roundtrip=false with destination=last and the home address as the last point; one already out on the road sends its current position as the first point, with roundtrip=false.

Compare the answer’s duration_s with nearest_neighbour_s. The difference is how much driving the ordering saved over going to the closest stop each time — the figure that answers a driver who asks why the run is not in postcode order.

If a single stop has no road connection to the rest, the whole request fails, because no order visits every stop. A robust planner catches that refusal, routes from the depot to each stop to find the one that comes back unreachable, removes it to the refusal list, and asks again.

Step three: a line and a time for every leg

Ask the optimiser for the order with geometry=false, then route each pair of consecutive stops. Each leg’s answer is its own line, distance and duration. Check found before adding anything up: a leg with no path is a 200 with null numbers, not an error.

Arrival times are the departure, plus each leg’s duration, plus a fixed allowance per door. That allowance does not change the best order — every order includes it the same number of times — so it belongs in the clock, not the optimisation. Leg durations are free-flow times, so the honest thing to tell a customer is a window; the width is yours to calibrate against how long your drivers actually take.

More than one van

Once a second vehicle, a delivery window or a van’s capacity enters the problem, it is no longer the single-vehicle problem the optimiser solves; TSP vs VRP explains the difference. The workable pattern keeps oslew for what it does and adds a solver for the rest:

  1. Request a distance matrix of the depot and every accepted stop. One request covers up to 100 × 100 on the Routing plan and 200 × 200 on Platform.
  2. Give it, with your windows, capacities and shifts, to a vehicle routing solver of your choice, which assigns stops to vans.
  3. For each van, either send its stops back to the optimiser for the order or keep the solver’s order, then route each leg as above.

Replace nulls in the matrix with a very large number before handing it to a solver, so an unreachable pair is never chosen.

Choosing a plan by rate

Plans are rates, not allowances, so the question is how fast a planner needs to go. A single run of forty stops is forty geocodes, one optimisation and forty-one route requests. The geocodes use the address rate; the rest use the routing rate, where the burst absorbs the first requests at once and the remainder follow at the sustained rate. On the Routing plan — $299 a month for 5 requests a second sustained, burst 25 — a run is ready in seconds. A planner that re-plans every van in a large depot at the same moment each morning pushes many runs through that bucket together, which is where the Platform plan’s 20 requests a second sustained, burst 100 earns its price. Either way, pace the leg loop at the rate the response headers report and wait out any HTTP 429. The pricing page lists every plan.

Bottom line

Refuse addresses that do not resolve to a door, order one van’s stops with the optimiser, route every leg for its line and its time, and give customers windows built from free-flow times. When the day has more than one van or any delivery windows, add a matrix and a solver of your own; oslew supplies the travel times and the single-van order, and does not pretend to be a fleet planner.

Where this workflow falls short

  • Arrival times are free-flow estimates — the posted limit or a class speed, with nothing for congestion — so customers should be given a window rather than a minute, wider for city-centre runs at busy times.
  • The optimiser orders one van’s stops and knows nothing of delivery windows, load, driver shifts or breaks; any of those needs a solver of your own, fed with a travel-time matrix.
  • A stop far from any road still routes, because each end snaps to the nearest road; the walk from that point to the door is not in the time, so check the snapped distance on every leg.
  • Whether an order can be geocoded to a door, and whether its stop is reachable by road, depends on the address and road data loaded where it is.

Questions

Can oslew plan routes for a whole fleet of vans?

Not on its own. It orders one vehicle’s stops. For several vans, request a matrix of the depot and every stop, let a vehicle routing solver of your choice split the stops between vans under your constraints, then send each van’s list back for ordering, or route its legs in the sequence the solver chose.

Which geocoding results should a delivery planner refuse?

Any with no result, any where tied_at_top is above one, and any whose precision is only a street. A tie means the address names several places equally well; a street-level answer finds the road but not the door, and on a long road that can be a long way from the customer. Send each refusal back with its reason.

How do I give customers an expected arrival time?

Start from the departure time, add each leg’s routed duration in order, and add a fixed allowance at every door for parking, stairs and a signature. The durations are free-flow, so round the result to a window rather than a minute, and widen it for runs through a city centre at busy times of day.

Which plan does a delivery planner need?

Geocoding draws on the address rate and ordering and routing on the routing rate, as separate buckets. The Routing plan carries both, at 20 requests a second sustained, burst 100 for addresses and 5 requests a second sustained, burst 25 for routing. A planner that builds many runs at the start of a shift is better served by the Platform plan’s 20 requests a second sustained, burst 100.

Why route each leg separately after optimising the order?

Because the optimiser gives each leg’s time but only one distance for the whole run, and its geometry runs every leg together in a single list. A driver’s map wants a line and a distance per leg, so ask for the order without geometry and then route each consecutive pair of stops.

Browse every page →