Skip to content
Merged
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
23 changes: 17 additions & 6 deletions src/main/java/org/mtransit/parser/config/gtfs/data/RouteConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,20 @@ data class RouteConfig(
@SerialName("route_id")
val routeId: String? = null,
@SerialName("route_short_name")
val routeShortName: String?,
val routeShortName: String? = null,
@SerialName("route_short_name_regex")
val routeShortNameRegex: String? = null,
@SerialName("route_long_name")
val routeLongName: String? = null,
@SerialName("original_route_color")
val originalRouteColor: String? = null,
@SerialName("color")
val color: String,
)
) {
internal val parsedRouteShortNameRegex by lazy {
routeShortNameRegex?.toRegex(RegexOption.IGNORE_CASE)
}
Comment thread
mmathieum marked this conversation as resolved.
}

@Serializable
data class StopIdConfig(
Expand Down Expand Up @@ -303,10 +311,13 @@ data class RouteConfig(

fun getRouteColor(gRoute: GRoute) =
//noinspection DiscouragedApi
(this.routeColors.singleOrNull { gRoute.routeId == it.routeId }
?: this.routeColors.singleOrNull { gRoute.routeShortName == it.routeShortName }
?: this.routeColors.singleOrNull { gRoute.routeLongNameOrDefault == it.routeLongName })
?.color
this.routeColors.firstOrNull { // order is important, 1st config match found wins
it.routeId == gRoute.routeId
|| it.routeShortName == gRoute.routeShortName
|| it.routeLongName == gRoute.routeLongNameOrDefault
|| it.originalRouteColor?.let { originalRouteColor -> originalRouteColor == gRoute.routeColor } == true
Comment thread
mmathieum marked this conversation as resolved.
|| it.parsedRouteShortNameRegex?.containsMatchIn(gRoute.routeShortName) == true
}?.color

fun isRouteColorIgnored(routeColor: String) =
this.routeColorsIgnored.any { it.equals(routeColor, ignoreCase = true) }
Expand Down
116 changes: 64 additions & 52 deletions src/main/java/org/mtransit/parser/mt/GenerateMObjectsTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,17 +281,19 @@ private MSpec doCall() {
return mRouteSpec;
}

private void parseRDS(HashMap<String, MSchedule> mSchedules,
HashMap<String, MFrequency> mFrequencies,
HashMap<Integer, MAgency> mAgencies,
HashMap<Long, MRoute> mRoutes,
HashMap<Long, MDirection> mDirections,
HashMap<Integer, MTrip> mTrips,
HashMap<Integer, MStop> mStops,
HashMap<String, MDirectionStop> allMDirectionStops,
HashSet<Integer> directionStopIds,
HashSet<Integer> serviceIdInts,
GSpec routeGTFS) {
private void parseRDS(
HashMap<String, MSchedule> mSchedules,
HashMap<String, MFrequency> mFrequencies,
HashMap<Integer, MAgency> mAgencies,
HashMap<Long, MRoute> mRoutes,
HashMap<Long, MDirection> mDirections,
HashMap<Integer, MTrip> mTrips,
HashMap<Integer, MStop> mStops,
HashMap<String, MDirectionStop> allMDirectionStops,
HashSet<Integer> directionStopIds,
HashSet<Integer> serviceIdInts,
GSpec routeGTFS
) {
boolean mergeSuccessful;
HashMap<Long, String> mDirectionStopTimesHeadsign;
HashMap<Long, ArrayList<MDirectionStop>> directionIdToMDirectionStops = new HashMap<>();
Expand Down Expand Up @@ -431,7 +433,7 @@ private void fixRouteLongName(HashMap<Long, MRoute> mRoutes, HashMap<Long, MDire
}
if (sb.length() == 0) {
final List<Locale> supportedLanguages = this.agencyTools.getSupportedLanguages();
if (supportedLanguages != null && supportedLanguages.size() >= 1) {
if (supportedLanguages != null && !supportedLanguages.isEmpty()) {
final Locale supportedLanguage = supportedLanguages.get(0);
if (Locale.ENGLISH.equals(supportedLanguage)) {
sb.append("Route ").append(mRoute.getShortNameOrDefault());
Expand All @@ -450,18 +452,20 @@ private void fixRouteLongName(HashMap<Long, MRoute> mRoutes, HashMap<Long, MDire
}
}

private void parseGTrips(HashMap<String, MSchedule> mSchedules,
HashMap<String, MFrequency> mFrequencies,
HashMap<Long, MDirection> mDirections,
HashMap<Integer, MTrip> mTrips,
HashMap<Integer, MStop> mStops,
HashSet<Integer> serviceIdInts,
MRoute mRoute,
HashMap<Long, String> mDirectionStopTimesHeadsign,
HashMap<Long, ArrayList<MDirectionStop>> directionIdToMDirectionStops,
GRoute gRoute,
Map<Integer, String> gDirectionHeadSigns,
GSpec routeGTFS) {
private void parseGTrips(
HashMap<String, MSchedule> mSchedules,
HashMap<String, MFrequency> mFrequencies,
HashMap<Long, MDirection> mDirections,
HashMap<Integer, MTrip> mTrips,
HashMap<Integer, MStop> mStops,
HashSet<Integer> serviceIdInts,
MRoute mRoute,
HashMap<Long, String> mDirectionStopTimesHeadsign,
HashMap<Long, ArrayList<MDirectionStop>> directionIdToMDirectionStops,
GRoute gRoute,
Map<Integer, String> gDirectionHeadSigns,
GSpec routeGTFS
) {
boolean mergeSuccessful;
HashMap<Long, HashSet<String>> mergedDirectionIdToMDirectionStops = new HashMap<>();
HashMap<Long, Pair<Integer, String>> originalDirectionHeadsign;
Expand Down Expand Up @@ -614,16 +618,18 @@ private void parseGTrips(HashMap<String, MSchedule> mSchedules,
MTLog.log("%s: parsing %d trips for route ID '%s'... DONE", this.routeId, routeGTrips.size(), gRouteId);
}

private HashMap<Long, String> parseGTripStops(HashMap<String, MSchedule> mSchedules,
HashSet<Integer> serviceIdInts,
HashMap<Integer, MStop> mStops,
GRoute gRoute,
GTrip gTrip,
HashMap<Long, Pair<Integer, String>> originalDirectionHeadsign,
ArrayList<MDirection> splitDirections,
Integer serviceIdInt,
HashMap<Long, HashMap<String, MDirectionStop>> splitDirectionStops,
GSpec routeGTFS) {
private HashMap<Long, String> parseGTripStops(
HashMap<String, MSchedule> mSchedules,
HashSet<Integer> serviceIdInts,
HashMap<Integer, MStop> mStops,
GRoute gRoute,
GTrip gTrip,
HashMap<Long, Pair<Integer, String>> originalDirectionHeadsign,
ArrayList<MDirection> splitDirections,
Integer serviceIdInt,
HashMap<Long, HashMap<String, MDirectionStop>> splitDirectionStops,
GSpec routeGTFS
) {
HashMap<Long, String> splitDirectionStopTimesHeadSign = new HashMap<>();
int mStopId;
GStop gStop;
Expand Down Expand Up @@ -716,17 +722,19 @@ private HashMap<Long, String> parseGTripStops(HashMap<String, MSchedule> mSchedu
private final DateFormat DATE_TIME_FORMAT = GFieldTypes.makeDateAndTimeFormat();
private final DateFormat DATE_FORMAT = GFieldTypes.makeDateFormat();

private String parseGStopTimes(HashMap<String, MSchedule> mSchedules,
long mDirectionId,
Integer serviceIdInt,
int originalDirectionHeadsignType,
@Nullable String originalDirectionHeadsignValue,
String directionStopTimesHeadsign,
@NotNull GRoute gRoute,
@NotNull GTrip gTrip,
@NotNull GTripStop gTripStop,
int mStopId,
HashMap<String, Integer> addedMDirectionIdAndGStopIds) {
private String parseGStopTimes(
HashMap<String, MSchedule> mSchedules,
long mDirectionId,
Integer serviceIdInt,
int originalDirectionHeadsignType,
@Nullable String originalDirectionHeadsignValue,
String directionStopTimesHeadsign,
@NotNull GRoute gRoute,
@NotNull GTrip gTrip,
@NotNull GTripStop gTripStop,
int mStopId,
HashMap<String, Integer> addedMDirectionIdAndGStopIds
) {
MSchedule mSchedule;
String stopHeadsign;
boolean noPickup;
Expand Down Expand Up @@ -804,11 +812,13 @@ private String parseGStopTimes(HashMap<String, MSchedule> mSchedules,
return directionStopTimesHeadsign;
}

private void parseFrequencies(HashMap<String, MFrequency> mFrequencies,
GTrip gTrip,
ArrayList<MDirection> splitDirections,
Integer serviceIdInt,
GSpec routeGTFS) {
private void parseFrequencies(
HashMap<String, MFrequency> mFrequencies,
GTrip gTrip,
ArrayList<MDirection> splitDirections,
Integer serviceIdInt,
GSpec routeGTFS
) {
MFrequency mFrequency;
for (GFrequency gFrequency : routeGTFS.getFrequencies(gTrip.getTripIdInt())) {
if (gFrequency.getTripIdInt() != gTrip.getTripIdInt()) {
Expand Down Expand Up @@ -842,8 +852,10 @@ private static String setDirectionStopTimesHeadsign(String directionStopTimesHea
return directionStopTimesHeadsign;
}

private void setDirectionStopNoPickup(@NotNull ArrayList<MDirectionStop> mDirectionStopsList,
@NotNull Collection<MSchedule> mSchedules) {
private void setDirectionStopNoPickup(
@NotNull ArrayList<MDirectionStop> mDirectionStopsList,
@NotNull Collection<MSchedule> mSchedules
) {
for (MDirectionStop directionStop : mDirectionStopsList) {
boolean noPickup = false;
for (MSchedule schedule : mSchedules) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.mtransit.parser.config.gtfs.data

import org.mtransit.parser.config.gtfs.data.RouteConfig.RouteColor
import org.mtransit.parser.mt.data.makeGRoute
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

Expand All @@ -10,6 +13,48 @@ class RouteConfigTest {
private const val TODAY_DATE = 20260528
}

@Test
fun test_getRouteColor() {
with(
RouteConfig(
routeColors = listOf(
RouteColor(
routeShortNameRegex = "^3\\d{2}$",
color = "000000",
),
RouteColor(
routeShortNameRegex = "^4\\d{2}$",
color = "007339",
),
RouteColor(
originalRouteColor = "009EE0",
color = "0060AA",
),
RouteColor(
originalRouteColor = "",
color = "0060AA",
),
)
)
) {
getRouteColor(makeGRoute(shortName = "10", color = "009EE0")).let { result ->
assertEquals("0060AA", result)
}
getRouteColor(makeGRoute(shortName = "31", color = "009EE0")).let { result ->
assertEquals("0060AA", result)
}
getRouteColor(makeGRoute(shortName = "350", color = "009EE0")).let { result ->
assertEquals("000000", result)
}
getRouteColor(makeGRoute(shortName = "350", color = null)).let { result ->
assertEquals("000000", result)
}
getRouteColor(makeGRoute(shortName = "427", color = "009EE0")).let { result ->
assertEquals("007339", result)
}
Comment thread
mmathieum marked this conversation as resolved.
}
}

@Test
fun test_directionFinderEnabled() {
RouteConfig()
Expand Down
8 changes: 5 additions & 3 deletions src/test/java/org/mtransit/parser/mt/data/GTestsFixtures.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import org.mtransit.parser.gtfs.data.GRoute
fun makeGRoute(
agencyId: String = "agency_id",
id: String = "1",
shortName: String = "RSN$id",
longName: String = "Long Name $id",
routeType: Int = 0,
color: String = "000000",
color: String? = "000000",
) = GRoute(
agencyIdInt = GIDs.getInt(agencyId),
routeIdInt = GIDs.getInt(id),
originalRouteIdInt = GIDs.getInt(id),
routeShortName = "RSN$id",
routeLongName = "Long Name $id",
routeShortName = shortName,
routeLongName = longName,
routeDesc = null,
routeType = routeType,
routeColor = color,
Expand Down