Distance Matrix and Route Order from a Points CSV
Operator: Abdulla Al Fahad. How tools are built and checked
Planning a visit order for ten sites starts with one table: how far is every site from every other. This page builds that table from a CSV of latitude and longitude, adds each point's nearest neighbour and its distance and bearing from a reference point, and proposes a visit order with a nearest-neighbour start improved by 2-opt.
All distances are straight lines over the Earth's surface, not road distances, so they suit ranking, clustering and rough sizing rather than quoting a driving distance.
Key features
- Haversine or Vincenty (WGS84) distances in kilometres, miles, metres or nautical miles
- A full N x N matrix up to 300 points as a wide CSV and an XLSX sheet
- The nearest 1, 2 or 3 neighbours of every point, with distances
- Distance and compass bearing from a reference point you type or pick from the rows
- A route order by nearest neighbour then 2-opt, open or closed, with leg and cumulative distance and the saving 2-opt found
Haversine against Vincenty, what the matrix holds and how the route order is built
The table needs a latitude and a longitude column in decimal degrees or degrees-minutes-seconds text; a name column is optional, and rows without one are called Row 2, Row 3 and so on. Rows with blank, non-numeric or out-of-range coordinates are skipped and listed with a reason, and a row whose latitude exceeds 90 but whose longitude fits is flagged as swapped.
Up to 1,000 valid points are accepted. The matrix is shown and exported for up to 300 points, 90,000 cells; above that, the nearest-neighbour, reference-distance and route outputs still cover every point.
How it works
Haversine treats the Earth as a sphere of mean radius 6,371.0088 km, which can be off by up to about 0.3 percent depending on direction.
Vincenty's inverse formula iterates on the WGS84 ellipsoid until the longitude difference changes by less than 1e-12 radians. Geoscience Australia's Flinders Peak to Buninyong line comes out at 54,972.271 m with an initial azimuth of 306 degrees 52 minutes 5.37 seconds, the published values.
For nearly opposite points the iteration may not converge. Those pairs fall back to haversine and are counted in a warning above the tables.
The route starts at the chosen point and repeatedly moves to the nearest unvisited point. 2-opt then takes two legs a-b and c-d and reverses the stretch between them whenever a-c plus b-d is shorter, until no reversal helps.
Common use cases
- A field-service manager grouping 120 customer sites by distance from the nearest depot, using the reference-point and nearest-neighbour columns.
- A cyclist planning a tour of 12 villages who wants a plausible visiting order before opening a map.
- An analyst comparing two geocoders by exporting the matrix from each and looking at the largest differences.
- A school transport planner finding which of 40 stops lie within 2 km of one another.
How to use this tool
- Drop the points file, or paste the table, then check the name, latitude and longitude columns.
- Choose haversine or Vincenty and the unit.
- Optionally pick a reference point and how many neighbours to list.
- Tick the route option, choose the start and whether the tour returns to it.
- Press Calculate and download the tables.
Example
A ten-depot list for a Swiss delivery firm, haversine, kilometres, reference point Zurich HQ, route starting at Zurich HQ (row 2)
depot,lat,lon
Zurich HQ,47.3769,8.5417
Basel,47.5596,7.5886
Bern,46.9480,7.4474
Lucerne,47.0502,8.3093
St. Gallen,47.4245,9.3767
Geneva,46.2044,6.1432
Lausanne,46.5197,6.6323
Lugano,46.0037,8.9511
Chur,46.8499,9.5329
Sion,46.2290,7.3589Results
Zurich HQ to Basel: 74.47 km (haversine), 74.67 km (Vincenty)
Nearest neighbours: Zurich HQ to Lucerne 40.3 km, Geneva to Lausanne 51.4 km
From Zurich HQ: Basel 74.5 km at 286 degrees (WNW), Lucerne 40.3 km at 206 degrees (SSW)
Open route from Zurich HQ: 720.9 km before 2-opt, 710.5 km after
Closed tour: 945.2 km before 2-opt, 768.3 km after (18.7% shorter)Tips for best results
- Use Vincenty for survey or property work and haversine for ranking. On the Swiss example the two differ by 0.2 km in 74, and neither the nearest neighbours nor the route order change.
- Choose the route start deliberately. Starting at the depot rather than row 1 gives a different nearest-neighbour path, and 2-opt only improves the order it is given.
- Open and closed routes can differ in more than one leg. On the example the open route ends in Geneva, while the closed tour returns through Lucerne.
Common pitfalls
A 2-opt route is commonly several percent above the shortest possible and is not proven shortest; a solver such as OR-Tools is the tool for contractual optimisation.
Straight lines cross water and mountains, so a road or rail distance can be much longer than the figure shown.
Points sharing the same coordinates show a distance of 0 and are each other's nearest neighbour; deduplicate first if that is unwanted.
Latitude and longitude must be WGS84 decimal degrees or DMS text. Projected coordinates such as UTM metres are rejected as out of range.
Common errors and how to fix them
- 1,200 valid points; the limit is 1,000. Split the file by region and run each part.
- Filter the file to one region or one depot's customers. The cap keeps the Vincenty calculation over the full matrix quick in a browser tab.
Best practices
- Keep one row per place with a stable name: the matrix headers use the names, so duplicates make columns hard to tell apart.
- For more than 300 points, export the nearest neighbours and the route and keep the full matrix in a database.
Haversine against Vincenty in numbers
Over the 45 pairs among the ten Swiss depots the two methods differ by between 0.01 and 0.29 percent. The largest gap in kilometres is 0.72, on the Geneva to Chur line: 269.05 km by haversine against 269.76 km by Vincenty. Zurich HQ to Geneva shows 224.35 km against 224.76 km.
Reading the matrix
Rows and columns follow the order of your file, the diagonal is zero and the matrix is symmetric. In the nearest-neighbour table a tie goes to the earlier row.
What 2-opt changes
A nearest-neighbour route is greedy: it takes the closest unvisited point every time and can leave a long return leg at the end. 2-opt repairs such crossings locally. On the closed Swiss tour it reverses stretches until the length falls from 945.2 km to 768.3 km.
Frequently asked questions
Why does a pair show a haversine figure when I chose Vincenty?
Vincenty's iteration can fail for nearly opposite points on the globe. Those pairs use haversine and are counted in the warning above the tables.
Can the matrix be in miles while the route is in kilometres?
No. One unit applies to every output of a calculation. Change it and press Calculate again.