Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

motis gtfs attributes #734

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 34 additions & 17 deletions app/itinerary/transit/motisRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { lightenColor } from '@/components/utils/colors'
import { distance, point } from '@turf/turf'
import transportIcon from './transportIcon'
import { datePlusHours, defaultRouteColor, nowStamp, stamp } from './utils'
import motisClaszToGtfsRouteType from '@/components/transit/motisClaszToGtfsRouteType'

// For onTrip, see https://github.com/motis-project/motis/issues/471#issuecomment-2247099832
const buildRequestBody = (start, destination, date, searchParams) => {
Expand Down Expand Up @@ -146,24 +147,25 @@ export const computeMotisTrip = async (
/(.+)\|([^_]+)\_(.+)/,
(correspondance, p1, p2, p3, decalage, chaine) => p1 + p3
)
const doFetch = async () => {
try {
if (!tripId) return {}
const request = await fetch(
`${gtfsServerUrl}/routes/trip/${encodeURIComponent(tripId)}`
)
const json = await request.json()
const safeAttributes = json.routes[0] || {}
return safeAttributes
} catch (e) {
console.error('Unable to fetch route color from GTFS server')
return {}
}

let route_color, route_text_color, route_type

route_color = transport.move.route_color
route_text_color = transport.move.route_text_color

if (transport.move.clasz) {
route_type = motisClaszToGtfsRouteType[transport.move.clasz]
}
const gtfsAttributes = await doFetch()
const { route_color, route_text_color, route_type } =
gtfsAttributes,
isTrain = route_type === 2

if (!route_color) {
const gtfsAttributes = await fetchGtfsAttributes(tripId)

route_color = gtfsAttributes.route_color
route_text_color = gtfsAttributes.route_text_color
route_type = gtfsAttributes.route_type
}

const isTrain = route_type === 2

const isBretagneTGV = trip?.id.id.startsWith('bretagne_SNCF2')

Expand Down Expand Up @@ -295,3 +297,18 @@ export const isNotTransitConnection = (connection) =>
connection.transports.every((transport) =>
notTransitType.includes(transport.move_type)
)

const fetchGtfsAttributes = async (tripId) => {
try {
if (!tripId) return {}
const request = await fetch(
`${gtfsServerUrl}/routes/trip/${encodeURIComponent(tripId)}`
)
const json = await request.json()
const safeAttributes = json.routes[0] || {}
return safeAttributes
} catch (e) {
console.error('Unable to fetch route color from GTFS server')
return {}
}
}
26 changes: 26 additions & 0 deletions components/transit/motisClaszToGtfsRouteType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//route_type to clasz
//https://github.com/motis-project/nigiri/blob/master/src/loader/gtfs/route.cc
//
//clasz to clasz index
//https://github.com/motis-project/nigiri/blob/master/include/nigiri/types.h#L295

// ⚠️ this is not lossless. We're losing information from extended route_types
// by using Motis's clasz. But it saves us a call to our API for each trip

const correspondance = {
0: 1100,
1: 101,
2: 102,
3: 209,
4: 105,
5: 2,
6: 100,
7: 401,
8: 1,
9: 0,
10: 3,
11: 4,
12: null,
}

export default correspondance