feat(server/gtfs): service exception support
This commit is contained in:
parent
c9aeeb99c1
commit
58649b6171
10 changed files with 595 additions and 4 deletions
|
|
@ -21,12 +21,14 @@ import moe.lava.banksia.Constants
|
|||
import moe.lava.banksia.model.Route
|
||||
import moe.lava.banksia.model.RouteType
|
||||
import moe.lava.banksia.model.Service
|
||||
import moe.lava.banksia.model.ServiceException
|
||||
import moe.lava.banksia.model.Shape
|
||||
import moe.lava.banksia.model.Stop
|
||||
import moe.lava.banksia.model.StopTime
|
||||
import moe.lava.banksia.model.Trip
|
||||
import moe.lava.banksia.server.gtfs.structures.GtfsRoute
|
||||
import moe.lava.banksia.server.gtfs.structures.GtfsService
|
||||
import moe.lava.banksia.server.gtfs.structures.GtfsServiceException
|
||||
import moe.lava.banksia.server.gtfs.structures.GtfsShape
|
||||
import moe.lava.banksia.server.gtfs.structures.GtfsStop
|
||||
import moe.lava.banksia.server.gtfs.structures.GtfsStopTime
|
||||
|
|
@ -39,6 +41,7 @@ import kotlin.time.ExperimentalTime
|
|||
sealed class GtfsData {
|
||||
data class RouteChunk(val routes: List<Route>) : GtfsData()
|
||||
data class ServiceChunk(val services: List<Service>) : GtfsData()
|
||||
data class ServiceExceptionChunk(val exceptions: List<ServiceException>) : GtfsData()
|
||||
data class ShapeChunk(val shapes: List<Shape>) : GtfsData()
|
||||
data class StopChunk(val stops: List<Stop>) : GtfsData()
|
||||
data class StopTimeChunk(val stopTimes: List<StopTime>) : GtfsData()
|
||||
|
|
@ -77,7 +80,7 @@ class GtfsParser(
|
|||
.listFiles { it.isDirectory }
|
||||
.flatMap { d -> d.listFiles { f -> f.extension == "txt" }.toList() }
|
||||
.ifEmpty { extractAll(datasetPath) }
|
||||
.filter { it.parentFile.name == "2" }
|
||||
// .filter { it.parentFile.name == "2" }
|
||||
} else {
|
||||
extractAll(datasetPath)
|
||||
}
|
||||
|
|
@ -115,6 +118,10 @@ class GtfsParser(
|
|||
}
|
||||
.associateBy { it.id }
|
||||
|
||||
files
|
||||
.filter { it.name == "calendar_dates.txt" }
|
||||
.forEach { emit(GtfsData.ServiceExceptionChunk(parseServiceExceptions(it))) }
|
||||
|
||||
val trips = files
|
||||
.filter { it.name == "trips.txt" }
|
||||
.flatMap { fd ->
|
||||
|
|
@ -207,6 +214,16 @@ class GtfsParser(
|
|||
)
|
||||
} }
|
||||
|
||||
private fun parseServiceExceptions(fd: File) =
|
||||
fd.parseCsv<GtfsServiceException>()
|
||||
.map { with(it) {
|
||||
ServiceException(
|
||||
serviceId = service_id,
|
||||
date = LocalDate.parse(date, LocalDate.Formats.ISO_BASIC),
|
||||
type = exception_type,
|
||||
)
|
||||
} }
|
||||
|
||||
private fun parseTrips(fd: File, services: Map<String, Service>) =
|
||||
fd.parseCsv<GtfsTrip>()
|
||||
.map { with(it) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,11 @@
|
|||
package moe.lava.banksia.server.gtfs.structures
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Suppress("PropertyName")
|
||||
@Serializable
|
||||
internal data class GtfsServiceException(
|
||||
val service_id: String,
|
||||
val date: String,
|
||||
val exception_type: Int,
|
||||
)
|
||||
|
|
@ -5,6 +5,7 @@ import androidx.room.useWriterConnection
|
|||
import io.ktor.util.logging.Logger
|
||||
import moe.lava.banksia.model.Route
|
||||
import moe.lava.banksia.model.Service
|
||||
import moe.lava.banksia.model.ServiceException
|
||||
import moe.lava.banksia.model.Shape
|
||||
import moe.lava.banksia.model.Stop
|
||||
import moe.lava.banksia.model.StopTime
|
||||
|
|
@ -25,6 +26,7 @@ class GtfsImporter(
|
|||
transactor.immediateTransaction {
|
||||
database.routeDao.deleteAll()
|
||||
database.serviceDao.deleteAll()
|
||||
database.serviceExceptionDao.deleteAll()
|
||||
database.shapeDao.deleteAll()
|
||||
database.stopDao.deleteAll()
|
||||
database.stopTimeDao.deleteAll()
|
||||
|
|
@ -34,6 +36,7 @@ class GtfsImporter(
|
|||
when (chunk) {
|
||||
is GtfsData.RouteChunk -> addRoutes(chunk.routes)
|
||||
is GtfsData.ServiceChunk -> addServices(chunk.services)
|
||||
is GtfsData.ServiceExceptionChunk -> addServiceExceptions(chunk.exceptions)
|
||||
is GtfsData.ShapeChunk -> addShapes(chunk.shapes)
|
||||
is GtfsData.StopChunk -> addStops(chunk.stops)
|
||||
is GtfsData.StopTimeChunk -> addStopTimes(chunk.stopTimes)
|
||||
|
|
@ -67,6 +70,13 @@ class GtfsImporter(
|
|||
log.info("done")
|
||||
}
|
||||
|
||||
private suspend fun addServiceExceptions(exceptions: List<ServiceException>) {
|
||||
val dao = database.serviceExceptionDao
|
||||
log.info("inserting exceptions...")
|
||||
dao.insertOrReplaceAll(*exceptions.map { it.asEntity() }.toTypedArray())
|
||||
log.info("done")
|
||||
}
|
||||
|
||||
private suspend fun addShapes(shapes: List<Shape>) {
|
||||
val dao = database.shapeDao
|
||||
log.info("inserting shapes...")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue