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
57 changes: 40 additions & 17 deletions src/main/java/org/mtransit/parser/db/DBUtils.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.mtransit.parser.db

import org.mtransit.commons.FeatureFlags
import org.mtransit.commons.sql.SQLCreateBuilder
import org.mtransit.commons.sql.fromSQL
import org.mtransit.commons.sql.getStringOrNull
import org.mtransit.commons.sql.toSQL
import org.mtransit.parser.DefaultAgencyTools
import org.mtransit.parser.FileUtils
import org.mtransit.parser.MTLog
Expand Down Expand Up @@ -84,6 +87,11 @@ object DBUtils {
.appendColumn(GTripStop.TRIP_ID, SQLUtilsCommons.INT)
.appendColumn(GTripStop.STOP_ID, SQLUtilsCommons.INT)
.appendColumn(GTripStop.STOP_SEQUENCE, SQLUtilsCommons.INT)
.apply {
if (FeatureFlags.F_EXPORT_DIRECTION_STOP_LAST) {
appendColumn(GTripStop.LAST_TRIP_STOP, SQLUtilsCommons.INT) // as BOOLEAN
}
}
.build()
)
SQLUtils.executeUpdate(
Expand Down Expand Up @@ -332,12 +340,21 @@ object DBUtils {
connection.createStatement().use { statement ->
val rs = SQLUtils.executeUpdate(
statement,
SQLUtilsCommons.INSERT_INTO + TRIP_STOPS_TABLE_NAME + SQLUtilsCommons.VALUES_P1 +
"${gTripStop.routeIdInt}," +
"${gTripStop.tripIdInt}," +
"${gTripStop.stopIdInt}," +
"${gTripStop.stopSequence}" +
SQLUtilsCommons.P2
buildString {
append(SQLUtilsCommons.INSERT_INTO)
append(TRIP_STOPS_TABLE_NAME)
append(
buildList {
add(gTripStop.routeIdInt.toString())
add(gTripStop.tripIdInt.toString())
add(gTripStop.stopIdInt.toString())
add(gTripStop.stopSequence.toString())
if (FeatureFlags.F_EXPORT_DIRECTION_STOP_LAST) {
add(gTripStop.isLastTripStop.toSQL().toString())
}
}.joinToString(separator = ",", prefix = SQLUtilsCommons.VALUES_P1, postfix = (SQLUtilsCommons.P2))
)
}
)
insertRowCount++
insertCount++
Expand Down Expand Up @@ -384,11 +401,7 @@ object DBUtils {
query += " WHERE ${GTripStop.TRIP_ID} IN ${
tripIdInts
.distinct()
.joinToString(
separator = ",",
prefix = "(",
postfix = ")"
) { "$it" }
.joinToString(separator = ",", prefix = "(", postfix = ")") { "$it" }
}"
}
limitMaxNbRow?.let {
Expand All @@ -402,12 +415,22 @@ object DBUtils {
val rs = SQLUtils.executeQuery(statement, query)
while (rs.next()) {
result.add(
GTripStop(
rs.getInt(GTripStop.ROUTE_ID),
rs.getInt(GTripStop.TRIP_ID),
rs.getInt(GTripStop.STOP_ID),
rs.getInt(GTripStop.STOP_SEQUENCE)
)
if (FeatureFlags.F_EXPORT_DIRECTION_STOP_LAST) {
GTripStop(
routeIdInt = rs.getInt(GTripStop.ROUTE_ID),
tripIdInt = rs.getInt(GTripStop.TRIP_ID),
stopIdInt = rs.getInt(GTripStop.STOP_ID),
stopSequence = rs.getInt(GTripStop.STOP_SEQUENCE),
isLastTripStop = rs.getInt(GTripStop.LAST_TRIP_STOP).fromSQL(),
)
} else {
GTripStop(
routeIdInt = rs.getInt(GTripStop.ROUTE_ID),
tripIdInt = rs.getInt(GTripStop.TRIP_ID),
stopIdInt = rs.getInt(GTripStop.STOP_ID),
stopSequence = rs.getInt(GTripStop.STOP_SEQUENCE),
)
}
)
selectRowCount++
}
Expand Down
47 changes: 28 additions & 19 deletions src/main/java/org/mtransit/parser/gtfs/data/GSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ public List<GFrequency> getFrequencies(@Nullable Integer optTripIdInt) {
}

@NotNull
private Collection<Integer> getFrequencyTripIds() {
private Collection<Integer> getFrequencyTripIdInts() {
if (USE_DB_ONLY) {
return GIDs.getInts(GTFSDataBase.selectFrequencyTripIds());
}
Expand Down Expand Up @@ -623,8 +623,9 @@ public void generateTripStops() {
String uid;
String tripUID;
GStopTime gStopTime;
GStopTime gStopTimeNext;
boolean lastTripStop;
List<GStopTime> tripStopTimes;
GTripStop gTripStop;
final int stopTimesCount = readStopTimesCount();
int tripStopsCount = 0;
int offset = 0;
Expand All @@ -639,6 +640,7 @@ public void generateTripStops() {
offset += tripStopTimes.size();
for (int i = 0; i < tripStopTimes.size(); i++) {
gStopTime = tripStopTimes.get(i);
gStopTimeNext = i < tripStopTimes.size() - 1 ? tripStopTimes.get(i + 1) : null;
tripUID = this.tripIdIntsUIDs.get(gStopTime.getTripIdInt());
if (tripUID == null) {
continue;
Expand All @@ -648,8 +650,10 @@ public void generateTripStops() {
MTLog.log("Generating GTFS trip stops from stop times... > (uid: %s) SKIP %s", uid, gStopTime);
continue;
}
gTripStop = new GTripStop(tripUID, gStopTime.getTripIdInt(), gStopTime.getStopIdInt(), gStopTime.getStopSequence());
addTripStops(gTripStop);
lastTripStop = gStopTimeNext == null || gStopTimeNext.getTripIdInt() != gStopTime.getTripIdInt();
addTripStops(
new GTripStop(tripUID, gStopTime.getTripIdInt(), gStopTime.getStopIdInt(), gStopTime.getStopSequence(), lastTripStop)
);
}
MTLog.log("Generating GTFS trip stops from stop times... (created %s trip stops)", (this.tripStopsUIDs.size() - tripStopsCount));
tripStopsCount = this.tripStopsUIDs.size();
Expand All @@ -673,18 +677,18 @@ public void generateStopTimesFromFrequencies(@SuppressWarnings("unused") @NotNul
int t = 0;
int ts = 0;
int st = 0;
for (Integer tripIdInt : getFrequencyTripIds()) {
for (Integer tripIdInt : getFrequencyTripIdInts()) {
if (!this.tripIdIntsUIDs.containsKey(tripIdInt)) {
continue; // excluded service ID
}
final GTrip gOriginalTrip = getTrip(tripIdInt);
if (gOriginalTrip == null) {
throw new MTLog.Fatal("Cannot find original trip for ID '%s' (%d)!", GIDs.getString(tripIdInt), tripIdInt);
}
List<GStopTime> tripStopTimes = GStopTime.from(GTFSDataBase.selectStopTimes(Collections.singletonList(GIDs.getString(tripIdInt))));
ArrayList<GStopTime> newGStopTimes = new ArrayList<>();
Calendar stopTimeCal = Calendar.getInstance();
HashMap<Long, Integer> gStopTimeIncInSec = new HashMap<>();
final List<GStopTime> tripStopTimes = GStopTime.from(GTFSDataBase.selectStopTimes(Collections.singletonList(GIDs.getString(tripIdInt))));
final ArrayList<GStopTime> newGStopTimes = new ArrayList<>();
final Calendar stopTimeCal = Calendar.getInstance();
final HashMap<Long, Integer> gStopTimeIncInSec = new HashMap<>();
Integer previousStopTimeInSec = null;
long lastFirstStopTimeInMs = -1L;
for (GStopTime gStopTime : tripStopTimes) {
Expand Down Expand Up @@ -716,7 +720,7 @@ public void generateStopTimesFromFrequencies(@SuppressWarnings("unused") @NotNul
long firstStopTimeInMs = frequencyStartInMs;
int f = 0;
while (frequencyStartInMs <= firstStopTimeInMs && firstStopTimeInMs <= frequencyEndInMs) {
int newGeneratedTripIdInt = GIDs.getInt(GIDs.getString(tripIdInt) + "-" + f); // DB primary keys > [trip ID + sequence]
final int newGeneratedTripIdInt = GIDs.getInt(GIDs.getString(tripIdInt) + "-" + f); // DB primary keys > [trip ID + sequence]
addTrip(new GTrip(
newGeneratedTripIdInt,
gOriginalTrip.getRouteIdInt(),
Expand All @@ -732,14 +736,13 @@ public void generateStopTimesFromFrequencies(@SuppressWarnings("unused") @NotNul
));
t++;
stopTimeCal.setTimeInMillis(firstStopTimeInMs);
for (int i = 0; i < tripStopTimes.size(); i++) {
GStopTime gStopTime = tripStopTimes.get(i);
for (GStopTime gStopTime : tripStopTimes) {
stopTimeCal.add(Calendar.SECOND, gStopTimeIncInSec.get(gStopTime.getUID()));
int newDepartureTime = getNewDepartureTime(stopTimeCal);
GPickupType pickupType = gStopTime.getPickupType();
GDropOffType dropOffType = gStopTime.getDropOffType();
GTimePoint timePoint = gStopTime.getTimePoint();
GStopTime newGStopTime = new GStopTime(
final int newDepartureTime = getNewDepartureTime(stopTimeCal);
final GPickupType pickupType = gStopTime.getPickupType();
final GDropOffType dropOffType = gStopTime.getDropOffType();
final GTimePoint timePoint = gStopTime.getTimePoint();
final GStopTime newGStopTime = new GStopTime(
newGeneratedTripIdInt,
newDepartureTime,
newDepartureTime,
Expand All @@ -759,9 +762,14 @@ public void generateStopTimesFromFrequencies(@SuppressWarnings("unused") @NotNul
throw new MTLog.Fatal(e, "Error while generating stop times for frequency '%s'!", gFrequency);
}
}
GStopTime newGStopTime;
GStopTime gStopTimeNext;
boolean lastTripStop;
String tripUID;
String uid;
for (GStopTime newGStopTime : newGStopTimes) {
for (int i = 0; i < newGStopTimes.size(); i++) {
newGStopTime = newGStopTimes.get(i);
gStopTimeNext = i < newGStopTimes.size() - 1 ? newGStopTimes.get(i + 1) : null;
addStopTime(newGStopTime, true);
st++;
tripUID = this.tripIdIntsUIDs.get(newGStopTime.getTripIdInt());
Expand All @@ -777,8 +785,9 @@ public void generateStopTimesFromFrequencies(@SuppressWarnings("unused") @NotNul
MTLog.log("Generating GTFS trip stop from frequencies... > (uid: %s) SKIP %s", uid, newGStopTime);
continue;
}
lastTripStop = gStopTimeNext == null || gStopTimeNext.getTripIdInt() != newGStopTime.getTripIdInt();
addTripStops(
new GTripStop(tripUID, newGStopTime.getTripIdInt(), newGStopTime.getStopIdInt(), newGStopTime.getStopSequence())
new GTripStop(tripUID, newGStopTime.getTripIdInt(), newGStopTime.getStopIdInt(), newGStopTime.getStopSequence(), lastTripStop)
);
ts++;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/mtransit/parser/gtfs/data/GStopTime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import java.util.Date
// https://gtfs.org/schedule/reference/#stop_timestxt
data class GStopTime(
val tripIdInt: Int,
private val _arrivalTime: Int,
private val _departureTime: Int,
private val _arrivalTime: Int, // HHmmss
private val _departureTime: Int, // HHmmss
val stopIdInt: Int,
val stopSequence: Int,
val stopHeadsign: String?,
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/org/mtransit/parser/gtfs/data/GTrip.kt
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,17 @@ data class GTrip(

private const val UID_SEPARATOR = "+" // int IDs can be negative

@Suppress("unused")
@JvmStatic
fun extractRouteIdInt(tripUID: String) = split(tripUID).second
fun extractRouteIdInt(tripUID: String) = split(tripUID).first

@Suppress("unused")
@JvmStatic
fun extractTripIdInt(tripUID: String) = split(tripUID).first
fun extractTripIdInt(tripUID: String) = split(tripUID).second

@JvmStatic
fun split(tripUID: String) = try {
tripUID.split(UID_SEPARATOR).let { s ->
Pair(s[0].toInt(), s[1].toInt())
s[0].toInt() to s[1].toInt()
}
} catch (e: Exception) {
throw MTLog.Fatal(e, "Error while trying to split $tripUID!")
Expand Down
44 changes: 17 additions & 27 deletions src/main/java/org/mtransit/parser/gtfs/data/GTripStop.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,34 @@ data class GTripStop(
val routeIdInt: Int,
val tripIdInt: Int,
val stopIdInt: Int,
val stopSequence: Int
val stopSequence: Int,
val isLastTripStop: Boolean = false,
) {

@Suppress("unused")
constructor(
routeAndTripUID: String,
stopId: Int,
stopSequence: Int
) : this(
GTrip.split(routeAndTripUID),
stopId,
stopSequence
)

@Suppress("unused")
constructor(
routeAndTripUID: String,
tripIdInt: Int,
stopId: Int,
stopSequence: Int
stopSequence: Int,
lastTripStop: Boolean,
) : this(
GTrip.extractRouteIdInt(routeAndTripUID),
tripIdInt,
stopId,
stopSequence
)

constructor(
routeAndTripUID: Pair<Int, Int>,
stopId: Int,
stopSequence: Int
) : this(
routeAndTripUID.first,
routeAndTripUID.second,
stopId,
stopSequence
stopSequence,
lastTripStop,
)

val uID by lazy { getNewUID(routeIdInt, tripIdInt, stopIdInt, stopSequence) }

@Suppress("unused")
@get:Discouraged(message = "Not memory efficient")
val routeId: String get() = _routeId

@Suppress("unused")
private val _routeId: String
get() = GIDs.getString(routeIdInt)

@Suppress("unused")
@get:Discouraged(message = "Not memory efficient")
val tripId: String get() = _tripId
Expand All @@ -68,14 +56,16 @@ data class GTripStop(
fun toStringPlus(): String {
return toString() +
"+(stopId:$_stopId)" +
"+(tripId:$_tripId)"
"+(tripId:$_tripId)" +
"+(routeId:$_routeId)"
}

companion object {
const val ROUTE_ID = GRoute.ROUTE_ID
const val TRIP_ID = GTrip.TRIP_ID
const val STOP_ID = GStop.STOP_ID
const val STOP_SEQUENCE = GStopTime.STOP_SEQUENCE
const val LAST_TRIP_STOP = "last_trip_stop"

private const val UID_SEPARATOR = "+" // int IDs can be negative

Expand Down
Loading