这个库允许你查询和更新 GTFS 数据,数据来源于 Swift 中的 SQLite 数据库。
添加以下依赖到你的 Package.swift
文件
dependencies: [.package(url: "https://github.com/phisakel/gtfs-db-swift.git", from: "0.1.0")]
并添加到 target
dependencies: [.product(name: "GtfsDb", package: "gtfs-db-swift")]
你可以使用 gtfs-import
命令行工具创建 sqlite 数据库。你可以从 node-GTFS 库中安装它。首先,下载 GTFS zip 文件,然后运行导入命令,例如
gtfs-import --gtfsPath ./9_google_transit.zip --sqlitePath ./9_google_transit.sqlite
从 sqlite 数据库中读取 GTFS 数据,使用优秀的 GRDB 库,该库作为此库的依赖项包含在内。以下代码将从数据库中读取 stops
import GtfsDb
import GRDB
func fetchStops() async throws -> [Stop] {
let dbPath = "path/to/your/database.sqlite"
var config = Configuration()
config.readonly = true
dbQueue = try DatabaseQueue(path: dbPath, configuration: config)
let stops = try await dbQueue.read { db in try Stop.fetchAll(db) }
return stops
}