Nearest warehouse and delivery zones

Each order should go out from the closest depot, and each van should get a fair share of stops. Both questions start from the same geocoded list.

New Places and zones Command line Logistics, field work Retail, e-commerce

What you have

The orders with addresses, and the depots with their points (a column of addresses works too).

orders.csvfile
order,address
1001,"233 S Wacker Dr, Chicago, IL 60606"
1002,"600 N Michigan Ave, Chicago, IL 60611"
1003,"200 E Randolph St, Chicago, IL 60601"
1004,"2100 S Halsted St, Chicago, IL 60608"
1005,"3600 N Clark St, Chicago, IL 60613"
1006,"1060 W Addison St, Chicago, IL 60613"
depots.csvfile
id,name,lat,lon
D1,Depot North,41.9484,-87.6553
D2,Depot South,41.8517,-87.6466

Steps

1. Geocode the orders and give each the nearest depot:

terminalCLI
$ gridpin nearest orders.csv depots.csv --sheet usa.bin -o near.csv
0 of 6 rows have no answer (0 without a point)

2. Read the result:

terminalCLI
$ cat near.csv
ordergp_precisionnearest_idnearest_distance_m
1001houseD23126.5
1002houseD24924.2
1003houseD24273.1
1004nearD2266.8
1005nearD1183.4
1006houseD1123.5

3. cluster reads columns named lat and lon, so rename the two answer columns first:

terminalCLI
$ sed '1s/gp_lat/lat/;1s/gp_lon/lon/' near.csv > stops.csv

4. Split the stops into two zones of at most four stops:

terminalCLI
$ gridpin cluster stops.csv --k 2 --max-size 4 -o zones.csv
6 points in 2 zones
terminalCLI
$ cat zones.csv
ordernearest_idgp_clustergp_cluster_size
1001D214
1002D214
1003D214
1004D214
1005D122
1006D122

What you get

nearest_id and nearest_distance_m name the depot and how far it is in a straight line; gp_cluster is the zone, with its centre and size. --geojson zones.geojson also writes the outline of each zone for a map.

Why it works

nearest geocodes the rows that have no point and measures to every point of the second file. cluster groups points by distance (k-means from a fixed seed, so the same input gives the same zones) and moves stops out of a zone that is over --max-size.

Limits

  • Distances are in a straight line, not by road: a river without a bridge is not seen.
  • Zones are even in count, not in driving time.
  • Rows answered only at near or street level are placed less precisely: check gp_precision before you plan a route.

All its keys: nearest, cluster. The functions: nearest and join, delivery zones.