92个版本 (40个破坏性更改)
0.41.3 | 2024年5月14日 |
---|---|
0.41.2 | 2024年3月22日 |
0.41.0 | 2024年2月24日 |
0.39.0 | 2023年12月19日 |
0.4.2 | 2018年5月2日 |
#191 in 解析器实现
每月下载量 884次
在8 crate中使用
150KB
2.5K SLoC
GTFS模型

通用公交数据规范(GTFS)是一个用于表示公共交通数据的常用模型。
这个crate提供了该模型的序列化结构以及读取GTFS存档的辅助工具。
使用
此crate有两个主要入口点。
Gtfs
最常见的做法是创建一个gtfs_structures::Gtfs
// Gtfs::new will try to guess if you provide a path, a local zip file or a remote zip file.
// You can also use Gtfs::from_path or Gtfs::from_url
let gtfs = gtfs_structures::Gtfs::new("path_of_a_zip_or_directory_or_url")?;
println!("there are {} stops in the gtfs", gtfs.stops.len());
// This structure is the easiest to use as the collections are `HashMap`,
// thus you can access an object by its id.
let route_1 = gtfs.routes.get("1").expect("no route 1");
println!("{}: {:?}", route_1.short_name, route_1);
RawGtfs
如果您想使用更低级别的模型,可以使用gtfs_structures::RawGtfs
let raw_gtfs = RawGtfs::new("fixtures/basic").expect("impossible to read gtfs");
for stop in raw_gtfs.stops.expect("impossible to read stops.txt") {
println!("stop: {}", stop.name);
}
与易于使用的HashMap
不同,每个集合都是一个Result
,如果读取过程中出现错误,则会返回错误。
这使得例如GTFS验证器能够显示更好的错误消息。
功能 'read-url'
默认情况下,功能 'read-url' 是激活的。这使得可以从URL读取Gtfs。
let gtfs = gtfs_structures::Gtfs::new("http://www.metromobilite.fr/data/Horaires/SEM-GTFS.zip")?;
或者您可以使用显式构造函数
let gtfs = gtfs_structures::Gtfs::from_url("http://www.metromobilite.fr/data/Horaires/SEM-GTFS.zip")?;
如果您不想依赖reqwest
,您可以移除此功能。
构建
您需要一个最新的rust工具链(通常与rustup一起安装)。
使用
cargo构建
您还可以运行单元测试
cargo测试
通过给出它们的名称来运行示例
cargo运行 --示例gtfs_reading
替代方案
如果您对交通数据感兴趣,也可以使用非常棒的crate transit_model,它也可以处理GTFS数据。
代价是学习曲线更加陡峭(并且文档有待改进 🙄),但这个crate提供了处理交通数据的非常好的易用性,以及很多实用工具,如数据格式转换、数据集合并等。
依赖项
~8–21MB
~283K SLoC