/journeys: How do I get the total distance of a journey?
Closed this issue · 3 comments
Hello! i couldn't find on the documentation more info about the body response for /journeys endpoint. I want to know if i can get the total distance of a journey? and in which measure unit?
Hello! 👋
As db-rest
is just a thin wrapper around hafas-rest-api@5
, which uses hafas-client@6
do fetch the data, the /journeys
endpoint's response is determined by hafas-client@6
.
If you pass ?polylines=true
, you will get journeys with a polyline
on each leg. The docs on this are quite sparse and hidden:
From hafas-client
's journeys()
docs:
If you pass
polylines: true
, each journey leg will have apolyline
field. Refer to the section in thetrip()
docs for details.
From hafas-client
's trip()
docs:
If you pass
polyline: true
, the trip will have apolyline
field, containing a GeoJSONFeatureCollection
ofPoint
s. EveryPoint
next to a station will haveproperties
containing the station's metadata.We'll look at an example for U6 from Alt-Mariendorf to Alt-Tegel, taken from the VBB profile:
{ type: 'FeatureCollection', features: [ { type: 'Feature', properties: { type: 'station', id: '900000070301', name: 'U Alt-Mariendorf', /* … */ }, geometry: { type: 'Point', coordinates: [13.3875, 52.43993] // longitude, latitude } }, /* … */ { type: 'Feature', properties: { type: 'station', id: '900000017101', name: 'U Mehringdamm', /* … */ }, geometry: { type: 'Point', coordinates: [13.38892, 52.49448] // longitude, latitude } }, /* … */ { // intermediate point, without associated station type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [13.28599, 52.58742] // longitude, latitude } }, { type: 'Feature', properties: { type: 'station', id: '900000089301', name: 'U Alt-Tegel', /* … */ }, geometry: { type: 'Point', coordinates: [13.28406, 52.58915] // longitude, latitude } } ] }
You can determine each leg's distance by, for each of the journey's legs,
a. transforming the GeoJSON FeatureCollection
into a GeoJSON LineString
,
b. using either Turf's distance()
(to get the "crow-flight distance") or Turf's length()
(to get the travelled distance),
c. summing these distances up to get the journey's total distance.
Does that answer your question?
I'll close this issue. Please re-open if you question is not answered.
Hello @derhuerst, sorry for the delay. Thank you for your response it was very helpful and clear.