Skip to content

Commit 506ac2f

Browse files
authored
Remove route long name/trip head-sign FROM direction/stop head-sign (#26)
1 parent e2c811f commit 506ac2f

File tree

7 files changed

+88
-29
lines changed

7 files changed

+88
-29
lines changed

src/main/java/org/mtransit/parser/DefaultAgencyTools.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public class DefaultAgencyTools implements GAgencyTools {
129129

130130
private static final SimpleDateFormat DATE_FORMAT = GFieldTypes.makeDateFormat();
131131

132-
@SuppressWarnings("unused")
132+
@SuppressWarnings({"unused", "WeakerAccess"})
133133
public int getTodayDateInt() {
134134
return Integer.parseInt(DATE_FORMAT.format(Calendar.getInstance().getTime()));
135135
}
@@ -653,10 +653,10 @@ public void setDirectionHeadsign(@NotNull MRoute mRoute, @NotNull MDirection mDi
653653
//noinspection DiscouragedApi
654654
throw new MTLog.Fatal("Trying to set direction head-sign w/o valid GTFS route (ID: %s)", gTrip.getRouteId());
655655
}
656-
boolean fromStopName = mDirection.getHeadsignType() == MDirection.HEADSIGN_TYPE_STOP_ID;
656+
final boolean fromStopName = mDirection.getHeadsignType() == MDirection.HEADSIGN_TYPE_STOP_ID;
657657
if (directionFinderEnabled(mRoute.getId(), gRoute)) {
658658
mDirection.setHeadsignString(
659-
cleanDirectionHeadsign(gTrip.getDirectionIdOrDefault(), fromStopName, gTrip.getTripHeadsignOrDefault()),
659+
cleanDirectionHeadsign(gRoute, gTrip.getDirectionIdOrDefault(), fromStopName, gTrip.getTripHeadsignOrDefault()),
660660
gTrip.getDirectionIdOrDefault()
661661
);
662662
return;
@@ -666,7 +666,7 @@ public void setDirectionHeadsign(@NotNull MRoute mRoute, @NotNull MDirection mDi
666666
}
667667
try {
668668
mDirection.setHeadsignString(
669-
cleanDirectionHeadsign(gTrip.getDirectionIdOrDefault(), fromStopName, gTrip.getTripHeadsignOrDefault()),
669+
cleanDirectionHeadsign(gRoute, gTrip.getDirectionIdOrDefault(), fromStopName, gTrip.getTripHeadsignOrDefault()),
670670
gTrip.getDirectionIdOrDefault()
671671
);
672672
} catch (NumberFormatException nfe) {
@@ -739,9 +739,27 @@ public boolean directionOverrideId(long routeId) {
739739
return false;
740740
}
741741

742+
@Override
743+
public boolean removeRouteLongNameFromDirectionHeadsign() {
744+
return Configs.getRouteConfig().getDirectionHeadsignRemoveRouteLongName();
745+
}
746+
742747
/**
743748
* @param directionId {@link org.mtransit.parser.gtfs.data.GDirectionId} (0 or 1 or missing/generated)
744749
*/
750+
@Override
751+
public @NotNull String cleanDirectionHeadsign(@Nullable GRoute gRoute, int directionId, boolean fromStopName, @NotNull String directionHeadSign) {
752+
if (gRoute != null && removeRouteLongNameFromDirectionHeadsign()
753+
&& directionHeadSign.equals(gRoute.getRouteLongNameOrDefault())) {
754+
//noinspection deprecation
755+
return cleanDirectionHeadsign(directionId, fromStopName, "");
756+
}
757+
//noinspection deprecation
758+
return cleanDirectionHeadsign(directionId, fromStopName, directionHeadSign);
759+
}
760+
761+
@SuppressWarnings("DeprecatedIsStillUsed")
762+
@Deprecated
745763
@NotNull
746764
@Override
747765
public String cleanDirectionHeadsign(int directionId, boolean fromStopName, @NotNull String directionHeadSign) {
@@ -1028,9 +1046,25 @@ public String cleanStopName(@NotNull String gStopName) {
10281046
return org.mtransit.commons.CleanUtils.cleanLabel(getFirstLanguageNN(), gStopName);
10291047
}
10301048

1049+
@Override
1050+
public boolean removeTripHeadsignFromStopHeadsign() {
1051+
return Configs.getRouteConfig().getStopHeadsignRemoveTripHeadsign();
1052+
}
1053+
1054+
@Override
1055+
public boolean removeRouteLongNameFromStopHeadsign() {
1056+
return Configs.getRouteConfig().getStopHeadsignRemoveRouteLongName();
1057+
}
1058+
10311059
@NotNull
10321060
@Override
10331061
public String cleanStopHeadSign(@NotNull GRoute gRoute, @NotNull GTrip gTrip, @NotNull GStopTime gStopTime, @NotNull String stopHeadsign) {
1062+
if (removeTripHeadsignFromStopHeadsign() && stopHeadsign.equals(gTrip.getTripHeadsignOrDefault())) {
1063+
return cleanStopHeadSign("");
1064+
}
1065+
if (removeRouteLongNameFromStopHeadsign() && stopHeadsign.equals(gRoute.getRouteLongNameOrDefault())) {
1066+
return cleanStopHeadSign("");
1067+
}
10341068
return cleanStopHeadSign(stopHeadsign);
10351069
}
10361070

src/main/java/org/mtransit/parser/config/gtfs/data/RouteConfig.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ data class RouteConfig(
4040
val tripHeadsignCleaners: List<Cleaner> = emptyList(),
4141
@SerialName("trip_headsign_remove_via")
4242
val tripHeadsignRemoveVia: Boolean = false, // OPT-IN feature
43-
// DIRECTION
4443
@SerialName("trip_id_cleanup_regex")
4544
val tripIdCleanupRegex: String? = null, // optional
4645
@SerialName("trip_id_not_unique_allowed")
4746
val tripIdNotUniqueAllowed: Boolean = false, // OPT-IN feature
47+
// DIRECTION
4848
@SerialName("direction_headsign_cleaners")
4949
val directionHeadsignCleaners: List<Cleaner> = emptyList(),
50+
@SerialName("direction_headsign_remove_route_long_name")
51+
val directionHeadsignRemoveRouteLongName: Boolean = false, // OPT-IN feature
5052
@SerialName("direction_finder_enabled")
5153
val directionFinderEnabled: Boolean = false, // OPT-IN feature
5254
// STOP
@@ -58,6 +60,11 @@ data class RouteConfig(
5860
val useStopCodeForStopId: Boolean = false, // OPT-IN feature
5961
@SerialName("stop_code_to_stop_id_configs")
6062
val stopCodeToStopIdConfigs: List<StopCodeToStopIdConfig> = emptyList(),
63+
@SerialName("stop_headsign_remove_trip_headsign")
64+
val stopHeadsignRemoveTripHeadsign: Boolean = false, // OPT-IN feature
65+
@SerialName("stop_headsign_remove_route_long_name")
66+
val stopHeadsignRemoveRouteLongName: Boolean = false, // OPT-IN feature
67+
6168
) {
6269

6370
@Serializable

src/main/java/org/mtransit/parser/gtfs/GAgencyTools.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public interface GAgencyTools {
3737

3838
int getThreadPoolSize();
3939

40-
@Deprecated // TO BE REMOVED
41-
boolean defaultExcludeEnabled();
40+
@Deprecated
41+
boolean defaultExcludeEnabled(); // TO BE REMOVED
4242

4343
boolean defaultStringsCleanerEnabled();
4444

@@ -52,12 +52,12 @@ public interface GAgencyTools {
5252
@Nullable
5353
List<Locale> getSupportedLanguages();
5454

55-
@Deprecated // TO BE REMOVED
56-
void setAgencyName(@Nullable String agencyName);
55+
@Deprecated
56+
void setAgencyName(@Nullable String agencyName); // TO BE REMOVED
5757

58-
@Deprecated // TO BE REMOVED
58+
@Deprecated
5959
@NotNull
60-
String getAgencyName();
60+
String getAgencyName(); // TO BE REMOVED
6161

6262
@NotNull
6363
String getAgencyColor();
@@ -177,6 +177,12 @@ public interface GAgencyTools {
177177

178178
boolean directionOverrideId(long routeId);
179179

180+
boolean removeRouteLongNameFromDirectionHeadsign();
181+
182+
@NotNull
183+
String cleanDirectionHeadsign(@Nullable GRoute gRoute, int directionId, boolean fromStopName, @NotNull String directionHeadSign);
184+
185+
@Deprecated
180186
@NotNull
181187
String cleanDirectionHeadsign(int directionId, boolean fromStopName, @NotNull String directionHeadSign);
182188

@@ -265,6 +271,10 @@ public interface GAgencyTools {
265271
@NotNull
266272
String cleanStopName(@NotNull String gStopName);
267273

274+
boolean removeTripHeadsignFromStopHeadsign();
275+
276+
boolean removeRouteLongNameFromStopHeadsign();
277+
268278
@NotNull
269279
String cleanStopHeadSign(@NotNull GRoute gRoute, @NotNull GTrip gTrip, @NotNull GStopTime gStopTime, @NotNull String stopHeadsign);
270280

src/main/java/org/mtransit/parser/gtfs/GReader.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -532,20 +532,20 @@ private static void processRoute(GAgencyTools agencyTools, GSpec gSpec, HashMap<
532532
return;
533533
}
534534
if (agencyTools.getRouteIdCleanupRegex() != null) { // IF route ID cleanup regex set DO
535-
final GRoute previousRoute = gSpec.getRoute(gRoute.getRouteIdInt());
536-
if (previousRoute != null && previousRoute.equals(gRoute)) {
535+
final GRoute previousGRoute = gSpec.getRoute(gRoute.getRouteIdInt());
536+
if (previousGRoute != null && previousGRoute.equals(gRoute)) {
537537
return; // ignore if route already exists with same values
538538
}
539-
if (previousRoute != null && previousRoute.equalsExceptMergeable(gRoute)) {
540-
final String mergedRouteLongName = GRoute.mergeRouteLongNames(previousRoute.getRouteLongName(), gRoute.getRouteLongName());
541-
final String mergedRouteColor = GRoute.mergeRouteColors(previousRoute.getRouteColor(), gRoute.getRouteColor());
539+
if (previousGRoute != null && previousGRoute.equalsExceptMergeable(gRoute)) {
540+
final String mergedRouteLongName = GRoute.mergeRouteLongNames(previousGRoute.getRouteLongName(), gRoute.getRouteLongName());
541+
final String mergedRouteColor = GRoute.mergeRouteColors(previousGRoute.getRouteColor(), gRoute.getRouteColor());
542542
if (mergedRouteLongName != null) { // merge successful
543-
gSpec.addRoute(previousRoute.clone(mergedRouteLongName, mergedRouteColor), true);
543+
gSpec.addRoute(previousGRoute.clone(mergedRouteLongName, mergedRouteColor), true);
544544
return;
545545
}
546546
}
547-
if (previousRoute != null) {
548-
MTLog.log("Duplicate route ID!\n - %s\n - %s", gRoute.toStringPlus(), previousRoute.toStringPlus());
547+
if (previousGRoute != null) {
548+
MTLog.log("Duplicate route ID!\n - %s\n - %s", gRoute.toStringPlus(), previousGRoute.toStringPlus());
549549
}
550550
}
551551
gSpec.addRoute(gRoute);

src/main/java/org/mtransit/parser/gtfs/data/GSpec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public List<GRoute> getRoutes(@Nullable Long optMRouteId) {
248248
if (optMRouteId != null) {
249249
final List<GRoute> routes = new ArrayList<>();
250250
for (Integer gRouteIdInt : this.mRouteIdToGRouteIdInts.get(optMRouteId)) {
251-
GRoute gRoute = getRoute(gRouteIdInt);
251+
final GRoute gRoute = getRoute(gRouteIdInt);
252252
if (gRoute != null) {
253253
routes.add(gRoute);
254254
}

src/main/java/org/mtransit/parser/mt/MDirectionHeadSignFinder.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,17 @@ object MDirectionHeadSignFinder {
6262
val amPmDirectionHeadSigns = mutableMapOf<Int, String>()
6363
for ((directionId, _) in directionHeadSigns) {
6464
val firstAndLastTime = directionAmPm[directionId] ?: continue
65+
val gRoute = directionRouteIdInts[directionId].takeIf { it?.size == 1 }?.getOrNull(0)?.let { routeGTFS.getRoute(it) }
6566
if (GTime.areAM(firstAndLastTime)) {
6667
amPmDirectionHeadSigns[directionId] = agencyTools.cleanDirectionHeadsign(
68+
gRoute,
6769
directionId,
6870
false,
6971
"AM"
7072
)
7173
} else if (GTime.arePM(firstAndLastTime)) {
7274
amPmDirectionHeadSigns[directionId] = agencyTools.cleanDirectionHeadsign(
75+
gRoute,
7376
directionId,
7477
false,
7578
"PM"
@@ -88,16 +91,14 @@ object MDirectionHeadSignFinder {
8891
MTLog.log("$routeId: Direction head-signs '$directionHeadSigns' not descriptive, using route long name...")
8992
val routeDirectionHeadSigns = mutableMapOf<Int, String>()
9093
for ((directionId, _) in directionHeadSigns) {
91-
val routeIdInts = directionRouteIdInts[directionId] ?: continue
92-
if (routeIdInts.size != 1) {
93-
continue
94-
}
95-
val route = routeGTFS.getRoute(routeIdInts[0]) ?: continue
96-
val rln = route.routeLongNameOrDefault
94+
val gRoute = directionRouteIdInts[directionId].takeIf { it?.size == 1 }?.getOrNull(0)?.let { routeGTFS.getRoute(it) }
95+
?: continue
96+
val rln = gRoute.routeLongNameOrDefault
9797
if (rln.isBlank()) {
9898
continue
9999
}
100100
routeDirectionHeadSigns[directionId] = agencyTools.cleanDirectionHeadsign(
101+
gRoute,
101102
directionId,
102103
false,
103104
agencyTools.cleanRouteLongName(rln)
@@ -118,7 +119,9 @@ object MDirectionHeadSignFinder {
118119
val stopIdInt = directionStopIdInts[directionId] ?: continue
119120
@Suppress("DEPRECATION")
120121
val stop = routeGTFS.getStop(stopIdInt) ?: continue
122+
val gRoute = directionRouteIdInts[directionId].takeIf { it?.size == 1 }?.getOrNull(0)?.let { routeGTFS.getRoute(it) }
121123
lastStopDirectionHeadSigns[directionId] = agencyTools.cleanDirectionHeadsign(
124+
gRoute,
122125
directionId,
123126
true,
124127
agencyTools.cleanStopName(stop.stopName)
@@ -153,9 +156,11 @@ object MDirectionHeadSignFinder {
153156
val stopTimesDirectionHeadSigns = mutableMapOf<Int, String>()
154157
for ((directionId, _) in directionHeadSigns) {
155158
val stopTimeHeadSigns = distinctDirectionStopTimeHeadSigns[directionId] ?: continue
159+
val gRoute = directionRouteIdInts[directionId].takeIf { it?.size == 1 }?.getOrNull(0)?.let { routeGTFS.getRoute(it) }
156160
var cleanDirectionHeadsign: String? = null
157161
for (stopTimeHeadSign in stopTimeHeadSigns) {
158162
cleanDirectionHeadsign = agencyTools.cleanDirectionHeadsign(
163+
gRoute,
159164
directionId,
160165
false,
161166
stopTimeHeadSign // already cleaned
@@ -209,8 +214,9 @@ object MDirectionHeadSignFinder {
209214
gTrip.directionIdOrDefault == directionId
210215
}.map { gTrip ->
211216
val routeIdInt = gTrip.routeIdInt
217+
val gRoute = routeGTFS.getRoute(routeIdInt)
212218
val headSign = gTrip.tripHeadsign
213-
?.let { agencyTools.cleanDirectionHeadsign(directionId, false, it) } ?: EMPTY
219+
?.let { agencyTools.cleanDirectionHeadsign(gRoute, directionId, false, it) } ?: EMPTY
214220
val stopTimes = routeGTFS.getStopTimes(routeId, gTrip.tripIdInt, null, null)
215221
Triple(routeIdInt, headSign, stopTimes)
216222
}.filterNot { (_, _, stopTimes) ->
@@ -1142,6 +1148,7 @@ object MDirectionHeadSignFinder {
11421148
}
11431149

11441150
private fun logMerge(debug: Boolean = false, format: String, vararg args: Any?) {
1151+
@Suppress("SimplifyBooleanWithConstants")
11451152
if (!LOG_MERGE && debug) {
11461153
return
11471154
}

src/test/java/org/mtransit/parser/mt/MDirectionHeadSignFinderTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.mockito.ArgumentMatchers.anyBoolean
88
import org.mockito.ArgumentMatchers.anyInt
99
import org.mockito.ArgumentMatchers.anyString
1010
import org.mockito.Mockito.mock
11+
import org.mockito.kotlin.anyOrNull
1112
import org.mockito.kotlin.whenever
1213
import org.mtransit.parser.gtfs.GAgencyTools
1314
import org.mtransit.parser.gtfs.data.GDirectionId
@@ -41,9 +42,9 @@ class MDirectionHeadSignFinderTest {
4142
@Suppress("DEPRECATION")
4243
@Before
4344
fun setUp() {
44-
whenever(agencyTools.cleanDirectionHeadsign(anyInt(), anyBoolean(), anyString()))
45+
whenever(agencyTools.cleanDirectionHeadsign(anyOrNull<GRoute>(), anyInt(), anyBoolean(), anyString()))
4546
.then {
46-
it.arguments[2]
47+
it.arguments[3]
4748
}
4849
whenever(agencyTools.cleanStopHeadSign(any(), any(), any(), anyString()))
4950
.then {

0 commit comments

Comments
 (0)