Skip to content

Commit

Permalink
Return trip headsign from GTFS static data when available
Browse files Browse the repository at this point in the history
  • Loading branch information
cedarbaum committed Oct 30, 2023
1 parent 7eb3140 commit 1e3c625
Show file tree
Hide file tree
Showing 8 changed files with 552 additions and 510 deletions.
3 changes: 3 additions & 0 deletions api/public.proto
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,9 @@ message Trip {

optional Shape.Reference shape = 8;

// Headsign of the trip inferred from static GTFS data.
optional string headsign = 9;

// Reference is the reference type for the trip resource.
message Reference {
string id = 1;
Expand Down
40 changes: 24 additions & 16 deletions db/queries/trip_queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,27 @@ SELECT lss.trip_pk, stop.pk destination_pk
ON trip_stop_time.stop_pk = stop.pk;

-- name: ListTrips :many
WITH shapes_for_scheduled_trips_in_system AS (
SELECT scheduled_trip.id as trip_id, shape.id as shape_id
FROM shape
INNER JOIN scheduled_trip ON shape.pk = scheduled_trip.shape_pk
WHERE shape.system_pk = sqlc.arg(system_pk)
WITH static_trip_data AS (
SELECT scheduled_trip.id as trip_id,
shape.id as shape_id,
scheduled_trip.headsign as headsign
FROM scheduled_trip
LEFT JOIN shape ON scheduled_trip.shape_pk = shape.pk
INNER JOIN scheduled_service ON scheduled_trip.service_pk = scheduled_service.pk
WHERE scheduled_service.system_pk = sqlc.arg(system_pk)
)
SELECT trip.*,
vehicle.id as vehicle_id,
vehicle.latitude as vehicle_latitude,
vehicle.longitude as vehicle_longitude,
vehicle.bearing as vehicle_bearing,
vehicle.updated_at as vehicle_updated_at,
shapes_for_scheduled_trips_in_system.shape_id as shape_id
static_trip_data.shape_id as shape_id,
static_trip_data.headsign as headsign
FROM trip
LEFT JOIN vehicle ON trip.pk = vehicle.trip_pk
LEFT JOIN shapes_for_scheduled_trips_in_system
ON trip.id = shapes_for_scheduled_trips_in_system.trip_id
LEFT JOIN static_trip_data
ON trip.id = static_trip_data.trip_id
WHERE trip.route_pk = ANY(sqlc.arg(route_pks)::bigint[])
ORDER BY trip.route_pk, trip.id;

Expand All @@ -42,23 +46,27 @@ WHERE trip.id = ANY(sqlc.arg(trip_ids)::text[])
AND feed.system_pk = sqlc.arg(system_pk);

-- name: GetTrip :one
WITH shapes_for_scheduled_trips_in_system AS (
SELECT scheduled_trip.id as trip_id, shape.id as shape_id
FROM shape
INNER JOIN scheduled_trip ON shape.pk = scheduled_trip.shape_pk
WHERE shape.system_pk = sqlc.arg(system_pk)
WITH static_trip_data AS (
SELECT scheduled_trip.id as trip_id,
shape.id as shape_id,
scheduled_trip.headsign as headsign
FROM scheduled_trip
LEFT JOIN shape ON scheduled_trip.shape_pk = shape.pk
INNER JOIN scheduled_service ON scheduled_trip.service_pk = scheduled_service.pk
WHERE scheduled_service.system_pk = sqlc.arg(system_pk)
)
SELECT trip.*,
vehicle.id as vehicle_id,
vehicle.latitude as vehicle_latitude,
vehicle.longitude as vehicle_longitude,
vehicle.bearing as vehicle_bearing,
vehicle.updated_at as vehicle_updated_at,
shapes_for_scheduled_trips_in_system.shape_id as shape_id
static_trip_data.shape_id as shape_id,
static_trip_data.headsign as headsign
FROM trip
LEFT JOIN vehicle ON trip.pk = vehicle.trip_pk
LEFT JOIN shapes_for_scheduled_trips_in_system
ON trip.id = shapes_for_scheduled_trips_in_system.trip_id
LEFT JOIN static_trip_data
ON trip.id = static_trip_data.trip_id
WHERE trip.id = sqlc.arg(trip_id)
AND trip.route_pk = sqlc.arg(route_pk);

Expand Down
1 change: 1 addition & 0 deletions docs/src/api/public_resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,7 @@ Enum describing the possible statuses of a system.
| direction_id | bool |
| stop_times | [StopTime](public_resources.md#StopTime) |
| shape | [Shape.Reference](public_resources.md#Shape.Reference) |
| headsign | string | Headsign of the trip inferred from static GTFS data.



Expand Down
956 changes: 484 additions & 472 deletions internal/gen/api/public.pb.go

Large diffs are not rendered by default.

44 changes: 28 additions & 16 deletions internal/gen/db/trip_queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/public/endpoints/trip.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func GetTrip(ctx context.Context, r *Context, req *api.GetTripRequest) (*api.Tri
VehicleLatitude: trip.VehicleLatitude,
VehicleLongitude: trip.VehicleLongitude,
ShapeID: trip.ShapeID,
Headsign: trip.Headsign,
}})
if err != nil {
return nil, err
Expand All @@ -83,6 +84,7 @@ func buildApiTrips(ctx context.Context, r *Context, system *db.System, route *db
StartedAt: convert.SQLNullTime(trip.StartedAt),
Route: r.Reference.Route(route.ID, system.ID, route.Color),
Shape: nullShapeReference(r, trip.ShapeID, system.ID),
Headsign: convert.SQLNullString(trip.Headsign),
}
if trip.VehicleID.Valid {
reply.Vehicle = r.Reference.Vehicle(trip.VehicleID.String, system.ID)
Expand Down
13 changes: 7 additions & 6 deletions tests/endtoend/data/gtfsstatic/trips.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
route_id,service_id,trip_id,direction_id,shape_id
A,Weekday,A-S-A01-Weekday-1100,1,
A,Weekday,A-N-A01-Weekend-1100,0,
A,Weekday,A-S-A01-Weekday-1130,1,
A,Weekday,A-N-A01-Weekend-1130,0,
A,Weekday,trip_id_1,1,shape_1
route_id,service_id,trip_id,direction_id,shape_id,trip_headsign
A,Weekday,trip_id,1,,Rockaway Park
A,Weekday,A-S-A01-Weekday-1100,1,,
A,Weekday,A-N-A01-Weekend-1100,0,,
A,Weekday,A-S-A01-Weekday-1130,1,,
A,Weekday,A-N-A01-Weekend-1130,0,,
A,Weekday,trip_id_1,1,shape_1,
3 changes: 3 additions & 0 deletions tests/endtoend/test_trips.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ def test_trip_view(
assert expected_past_stop_ids == actual_past_stop_ids
assert expected_future_stop_ids == actual_future_stop_ids

# headsign from the GTFS static feed
assert "Rockaway Park" == response["headsign"]


def build_gtfs_rt_message(
current_time, stop_id_to_time, use_stop_sequences, direction_id=True
Expand Down

0 comments on commit 1e3c625

Please sign in to comment.