Skip to content

JSON > + direction_id_use_other_trips_with_same_headsign #sncf#80

Draft
mmathieum wants to merge 1 commit into
masterfrom
mm/json_direction_id_use_other_trips_with_same_headsign
Draft

JSON > + direction_id_use_other_trips_with_same_headsign #sncf#80
mmathieum wants to merge 1 commit into
masterfrom
mm/json_direction_id_use_other_trips_with_same_headsign

Conversation

@mmathieum

@mmathieum mmathieum commented Jul 22, 2026

Copy link
Copy Markdown
Member

Try to fix missing trips.direction_id when other route trips with same head-sign have a direction_id...

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an opt-in GTFS parsing step to infer and fill missing trips.direction_id values by looking at other trips on the same route with the same trip_headsign, aligning with the PR goal of reducing missing direction IDs (e.g., SNCF feeds).

Changes:

  • Introduces GSpec.fixMissingTripDirectionIds() (guarded by direction_id_use_other_trips_with_same_headsign) and wires it into the parser startup flow.
  • Extends route JSON config (RouteConfig) with a new opt-in flag to enable this behavior.
  • Minor logging/formatting adjustments and a small iteration refactor.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/main/java/org/mtransit/parser/mt/MDirectionHeadSignFinder.kt Tweaks debug log message formatting for headsign/merge diagnostics.
src/main/java/org/mtransit/parser/gtfs/data/GTrip.kt Refactors list-update iteration style for trip updates.
src/main/java/org/mtransit/parser/gtfs/data/GSpecExt.kt Adds opt-in direction-id auto-fix logic based on same-route same-headsign trips.
src/main/java/org/mtransit/parser/gtfs/data/GSpec.java Exposes route-id enumeration for trips and includes minor formatting updates.
src/main/java/org/mtransit/parser/DefaultAgencyTools.java Calls the new direction-id auto-fix step during parsing startup.
src/main/java/org/mtransit/parser/config/gtfs/data/RouteConfig.kt Adds new JSON config flag direction_id_use_other_trips_with_same_headsign.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 357 to 359
@NotNull
private Collection<Integer> getAllTripRouteIdInts() {
public Collection<Integer> getAllTripRouteIdInts() {
if (USE_DB_ONLY) {
Comment on lines 377 to 381
public void updateTripDirectionId(int directionId, @Nullable Collection<Integer> tripIdInts) {
if (tripIdInts == null) {
return;
}
if (tripIdInts == null) return;
List<Integer> routeIdInts = new ArrayList<>();
for (Integer tripIdInt : tripIdInts) {
routeIdInts.add(getTripRouteId(tripIdInt));
Comment on lines +12 to +26
getAllTripRouteIdInts().forEach { routeIdInt ->
getRouteTrips(routeIdInt)
.filter { it.directionIdOrOriginal == null }
.groupBy { it.tripHeadsign }
.forEach { (tripHeadsign, gTripsNoDirectionId) ->
if (tripHeadsign == null) return@forEach
val allRouteTripsWithSameHeadsign = getRouteTrips(routeIdInt)
.filter { it.tripHeadsign == tripHeadsign }
.takeIf { it.isNotEmpty() } ?: return@forEach
val distinctOriginalDirectionIds = allRouteTripsWithSameHeadsign.mapNotNull { it.directionIdOrOriginal }.distinct()
distinctOriginalDirectionIds.singleOrNull()?.let { originalDirectionId ->
updateTripDirectionId(originalDirectionId, gTripsNoDirectionId.map { it.tripIdInt })
tripIdDirectionFixed += gTripsNoDirectionId.size
}
}
Comment on lines 225 to 229
fun updateList(gTrips: MutableList<GTrip>, condition: (GTrip) -> Boolean, updateTrip: (GTrip) -> GTrip): List<GTrip> {
for (i in 0 until gTrips.size) {
val gTrip = gTrips[i]
for ((i, element) in gTrips.withIndex()) {
val gTrip = element
if (condition(gTrip)) {
gTrips[i] = updateTrip(gTrip)
Comment on lines +8 to +10
fun GSpec.fixMissingTripDirectionIds() {
if (!Configs.routeConfig.directionIdUseOtherTripsWithSameHeadsign) return
MTLog.log("Try fixing GTFS trips w/o direction IDs...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants