derhuerst / db-rest

A clean REST API wrapping around the Deutsche Bahn API.
https://v6.db.transport.rest/
ISC License
82 stars 10 forks source link

/journeys: How do I get the total distance of a journey? #45

Closed JennsiS closed 4 months ago

JennsiS commented 6 months ago

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?

derhuerst commented 5 months ago

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 a polyline field. Refer to the section in the trip() docs for details.

From hafas-client's trip() docs:

If you pass polyline: true, the trip will have a polyline field, containing a GeoJSON FeatureCollection of Points. Every Point next to a station will have properties 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?

derhuerst commented 4 months ago

I'll close this issue. Please re-open if you question is not answered.

JennsiS commented 4 months ago

Hello @derhuerst, sorry for the delay. Thank you for your response it was very helpful and clear.