-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0456127
commit f71d522
Showing
5 changed files
with
147 additions
and
8 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
core/data/src/main/kotlin/xyz/ksharma/krail/data/gtfs_static/parser/StopTimesParser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package xyz.ksharma.krail.data.gtfs_static.parser | ||
|
||
import timber.log.Timber | ||
import xyz.ksharma.krail.model.gtfs_static.StopTimes | ||
import java.io.BufferedReader | ||
import java.io.FileReader | ||
import java.io.IOException | ||
import java.nio.file.Path | ||
|
||
object StopTimesParser { | ||
|
||
fun Path.parseStopTimes(): List<StopTimes> { | ||
val stopTimesList = mutableListOf<StopTimes>() | ||
|
||
try { | ||
BufferedReader(FileReader(this.toString())).use { reader -> | ||
val headersList = reader.readLine().split(",").trimQuotes() | ||
// todo use headers instead of hard code later. | ||
//Log.d(TAG, "headersList: $headersList") | ||
|
||
var line: String? | ||
|
||
while (true) { | ||
line = reader.readLine() ?: break | ||
|
||
// Process the line in a buffered manner | ||
val fieldsList = mutableListOf<String>() | ||
var currentField = "" | ||
var inQuotes = false | ||
|
||
for (char in line) { | ||
if (inQuotes) { | ||
if (char == '"') { | ||
inQuotes = false | ||
fieldsList.add(currentField) | ||
currentField = "" | ||
} else { | ||
currentField += char | ||
} | ||
} else { | ||
if (char == '"') { | ||
inQuotes = true | ||
} else if (char == ',') { | ||
fieldsList.add(currentField.trim('\"')) | ||
currentField = "" | ||
} else { | ||
currentField += char | ||
} | ||
} | ||
} | ||
|
||
fieldsList.add(currentField.trim('\"')) | ||
|
||
stopTimesList.add( | ||
StopTimes( | ||
trip_id = fieldsList[0], | ||
arrival_time = fieldsList[1], | ||
departure_time = fieldsList[2], | ||
stop_id = fieldsList[3], | ||
stop_sequence = fieldsList[4].toIntOrNull(), | ||
stop_headsign = fieldsList[5], | ||
pickup_type = fieldsList[6].toIntOrNull(), | ||
drop_off_type = fieldsList[7].toIntOrNull(), | ||
) | ||
) | ||
} | ||
} | ||
} catch (e: IOException) { | ||
e.printStackTrace() | ||
Timber.e(e, "readStopsFromCSV: ") | ||
} catch (e: IllegalArgumentException) { | ||
e.printStackTrace() | ||
Timber.e(e, "readStopsFromCSV: ") | ||
} | ||
|
||
return stopTimesList | ||
} | ||
|
||
private fun List<String>.trimQuotes(): List<String> = this.map { it.trim('\"') } | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
core/data/src/main/kotlin/xyz/ksharma/krail/data/gtfs_static/parser/TripParser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package xyz.ksharma.krail.data.gtfs_static.parser | ||
|
||
import timber.log.Timber | ||
import xyz.ksharma.krail.model.gtfs_static.Trip | ||
import java.io.BufferedReader | ||
import java.io.FileReader | ||
import java.io.IOException | ||
import java.nio.file.Path | ||
|
||
object TripParser { | ||
|
||
internal fun Path.parseTrips(): List<Trip> { | ||
val trips = mutableListOf<Trip>() | ||
|
||
try { | ||
BufferedReader(FileReader(this.toString())).use { reader -> | ||
val headersList = reader.readLine().split(",").trimQuotes() | ||
// todo use headers instead of hard code later. | ||
//Log.d(TAG, "headersList: $headersList") | ||
|
||
while (true) { | ||
val line = reader.readLine() ?: break | ||
val fieldsList = line.split(",").trimQuotes() | ||
|
||
trips.add( | ||
Trip( | ||
route_id = fieldsList[0], | ||
service_id = fieldsList[1], | ||
trip_id = fieldsList[2], | ||
trip_headsign = fieldsList[3], | ||
trip_short_name = fieldsList[4], | ||
direction_id = fieldsList[5].toIntOrNull(), | ||
block_id = fieldsList[6], | ||
shape_id = fieldsList[7], | ||
wheelchair_accessible = fieldsList[8].toIntOrNull(), | ||
) | ||
) | ||
} | ||
} | ||
} catch (e: IOException) { | ||
e.printStackTrace() | ||
Timber.e(e, "readStopsFromCSV: ") | ||
} catch (e: IllegalArgumentException) { | ||
e.printStackTrace() | ||
Timber.e(e, "readStopsFromCSV: ") | ||
} | ||
|
||
return trips | ||
} | ||
|
||
private fun List<String>.trimQuotes(): List<String> = this.map { it.trim('\"') } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters