Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/dkter/stop_tree

download GTFS stops and put them in a k-d tree
https://github.com/dkter/stop_tree

Last synced: about 2 months ago
JSON representation

download GTFS stops and put them in a k-d tree

Awesome Lists containing this project

README

        

This is a quick tool I made to download stops from multiple GTFS feeds, insert them into a k-d tree by latitude and longitude with [kiddo](https://github.com/sdd/kiddo) and save it to a file using [rkyv](https://github.com/rkyv/rkyv). The resulting file can be used as follows -

```rust
use std::io::Read;
use std::fs::File;
use std::path::Path;
use kiddo::{KdTree, SquaredEuclidean};

#[derive(Debug, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize, PartialEq)]
struct Stop {
agency: String,
stop_id: String,
stop_code: Option,
stop_name: String,
tts_stop_name: Option,
stop_desc: Option,
stop_lat: f64,
stop_lon: f64,
zone_id: Option,
stop_url: Option,
location_type: Option,
parent_station: Option,
stop_timezone: Option,
wheelchair_boarding: Option,
level_id: Option,
platform_code: Option,
}

fn main() {
let args: Vec<_> = std::env::args().collect();
let fpath = &args[1];
let path = Path::new(&fpath);

let mut file = File::open(&path).unwrap();
let mut bytes = vec![];
file.read_to_end(&mut bytes).unwrap();
let deserialized = unsafe {
rkyv::from_bytes_unchecked::<(Vec, KdTree)>(&bytes)
.expect("Failed to deserialize KdTree")
};
let (stops, tree) = deserialized;

let nearest_n: Vec<_> = tree.nearest_n::(&[43.6528525f64,-79.3982886f64], 5);
for neighbour in nearest_n {
let stop = &stops[neighbour.item as usize];
println!("{} away - {:?}", neighbour.distance, stop);
}
}
```

I'm using this to get stops nearby to a specific location in [Departure Board](https://github.com/dkter/departure-board), since all the existing APIs I could find were too slow or unreliable. Keep in mind that this uses the assumption that latitudes/longitudes can be treated as rectangular coordinates on a small scale, which works fine on the scale of bus stops -- however, if your transit system straddles the International Date Line or something things might get weird.