# Distance calculation

Hello openrouteservice team,

I want to know the recommended foot-walking route for these two points:

``````curl -X POST \
'https://api.openrouteservice.org/v2/directions/foot-walking/geojson' \
-H 'Content-Type: application/json; charset=utf-8' \
-H 'Accept: application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8' \
-H 'Authorization: my-api-key' \
-d '{"coordinates":[[13.052859129106047,52.397979591034705],[13.123259216624628,52.38326030528367]]}'
``````

Why is the sum of the individual steps not equal to the distance of the segments?

What coordinate system do you use to calculate the distance? I’m trying to calculate the distance using the coordinates and my route is 15m longer.

Best regards.

Now I tried to calculate the distance using PostGIS. I used the coordinate system EPSG:4326. PostGIS differentiates between spheroid and sphere when calculating the distance (ST_Length). The result for the calculation on the sphere corresponds to the information received from the API. Do you also calculate the distances based on a sphere and if so why?

Hey,

I just ran the response to your request through some python to sum the individual step distances/durations, and that equals the segment distance - within a small margin of rounding error.

All our coordinates are in WGS 84 - WGS84 - World Geodetic System 1984, used in GPS - EPSG:4326.

Given that we’re talking about a 5.7 km route, I’d assume the difference between sphere and spheroid distances is negligible?

Best regards

2 Likes

There are use cases where the calculation and therefore the deviations play a role.
Why does the API perform the calculation on a sphere? Has this performance reasons?

Our simplified use case: We have two locations and one destination and the shorter route allows us to choose one location. Here is an example where the calculations on the sphere or spheroid would lead to different decisions.

Location A to destination

``````curl -X POST \
'https://api.openrouteservice.org/v2/directions/foot-walking/geojson' \
-H 'Content-Type: application/json; charset=utf-8' \
-H 'Accept: application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8' \
-H 'Authorization: my-api-key' \
-d '{"coordinates":[[13.036397469155776,52.39315853500565],[13.052552205007487,52.40286262099294]]}'
``````

Location B to destination

``````curl -X POST \
'https://api.openrouteservice.org/v2/directions/foot-walking/geojson' \
-H 'Content-Type: application/json; charset=utf-8' \
-H 'Accept: application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8' \
-H 'Authorization: my-api-key' \
-d '{"coordinates":[[13.031140292796865,52.39823769627204],[13.052552205007487,52.40286262099294]]}'
``````

Comparison of the results with calculation on sphere
Location A to destination: 1753.1 (1753.061444906453 - EPSG:4326, PostGIS)
Location B to destination: 1752.6 (1752.4256782853463 - EPSG:4326, PostGIS)
Decision for location B.

Comparison of the results with calculation on spheroid
Location A to destination: 1756.7763399376963 - EPSG:4326, PostGIS
Location B to destination: 1757.2482782162165 - EPSG:4326, PostGIS
Decision for location A.