Skip to content

Commit

Permalink
Merge pull request #7 from bobertoyin/6-optional-tile-map-rendering
Browse files Browse the repository at this point in the history
feat: optional tile map plotting module
  • Loading branch information
bobertoyin authored May 20, 2022
2 parents 1f455d8 + a84ec20 commit ff5cba5
Show file tree
Hide file tree
Showing 10 changed files with 415 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.png filter=lfs diff=lfs merge=lfs -text
8 changes: 7 additions & 1 deletion .github/workflows/test.yml → .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ jobs:
MBTA_TOKEN: ${{ secrets.MBTA_TOKEN }}
steps:
- uses: actions/checkout@v3
- run: cargo test --verbose
with:
lfs: 'true'
- run: cargo test --verbose --all-features
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
lfs: 'true'
- run: rustup component add clippy
- run: rustup component add rustfmt
- run: cargo clippy --all-features -- -D warnings
Expand All @@ -31,4 +35,6 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
lfs: 'true'
- uses: webiny/action-conventional-commits@v1.0.3
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/target
Cargo.lock
.DS_Store
.DS_Store
16 changes: 14 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mbta-rs"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
authors = ["Robert Yin <bobertoyin@gmail.com>"]
description = "Simple Rust client for interacting with the MBTA V3 API."
Expand All @@ -11,12 +11,24 @@ documentation = "https://docs.rs/mbta-rs"
keywords = ["mbta", "public-transit", "massachusetts"]
categories = ["api-bindings", "web-programming::http-client"]

[package.metadata.docs.rs]
all-features = true

[dev-dependencies]
raster = "0.2.0"
rstest = "0.12.0"

[dependencies]
colors-transform = { version = "0.2.11", optional = true }
chrono = "0.4.19"
ureq = { version = "2.4.0", features = ["json"] }
geo-types = { version = "0.7.4", optional = true }
polyline = { version = "0.9.0", optional = true }
serde = { version = "1.0.136", features = ["derive"] }
serde_json = "1.0.79"
staticmap = { version = "0.4.0", optional = true }
thiserror = "1.0.31"
tiny-skia = { version = "0.6.3", optional = true }
ureq = { version = "2.4.0", features = ["json"] }

[features]
map = ["dep:staticmap", "dep:polyline", "dep:geo-types", "dep:tiny-skia", "dep:colors-transform"]
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,66 @@ if let Ok(response) = alerts_response {
}
```

## Map Feature

This library comes with an optional module for plotting location-related data models (stops, vehicles, shapes, etc.) onto a simple tile map.

In your `Cargo.toml` file:
```toml
[dependencies]
mbta-rs = { version = "*", features = ["map"] }

# necessary to create the map itself
staticmap = "*"
```

Simple example usage:
```rust
use std::{collections::HashMap, env};
use staticmap::StaticMapBuilder;
use mbta_rs::{Client, map::{Plottable, PlotStyle}};

let client = match env::var("MBTA_TOKEN") {
Ok(token) => Client::with_key(token),
Err(_) => Client::without_key()
};

let routes = client.routes(HashMap::from([("filter[type]".into(), "0,1".into())])).expect("failed to get routes");
let mut map = StaticMapBuilder::new()
.width(1000)
.height(1000)
.zoom(12)
.lat_center(42.326768)
.lon_center(-71.100099)
.build()
.expect("failed to build map");

for route in routes.data {
let query = HashMap::from([("filter[route]".into(), route.id)]);
let shapes = client
.shapes(query.clone())
.expect("failed to get shapes");
for shape in shapes.data {
shape
.plot(&mut map, true, PlotStyle::new((route.attributes.color.clone(), 3.0), Some(("#FFFFFF".into(), 1.0))))
.expect("failed to plot shape");
}
let stops = client
.stops(query.clone())
.expect("failed to get stops");
for stop in stops.data {
stop.plot(
&mut map,
true,
PlotStyle::new((route.attributes.color.clone(), 3.0), Some(("#FFFFFF".into(), 1.0))),
)
.expect("failed to plot stop");
}
}

// save to file...
```

<!-- CONTRIBUTE -->
## Contribute

Expand Down
3 changes: 3 additions & 0 deletions resources/test/expected_map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions resources/test/train.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ pub mod client;
pub use client::*;
pub mod error;
pub use error::*;
#[cfg(feature = "map")]
pub mod map;
pub mod models;
pub use models::*;
Loading

0 comments on commit ff5cba5

Please sign in to comment.