Skip to content

Commit

Permalink
Actually use speed limit from osm2streets now, not some dummy other v…
Browse files Browse the repository at this point in the history
…alue
  • Loading branch information
dabreegster committed May 16, 2023
1 parent 3ac7f74 commit 9ee546f
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions route_info/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod map_matching;
use std::collections::BTreeSet;

use geojson::Feature;
use geom::{Distance, FindClosest, PolyLine, Pt2D};
use geom::{Distance, FindClosest, PolyLine, Pt2D, Speed};
use osm2streets::{Direction, IntersectionID, StreetNetwork};
use serde::Deserialize;
use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -84,7 +84,8 @@ impl RouteInfo {
}

/// Given the JSON waypoints array produced by route-snapper, generate GeoJSON LineStrings,
/// covering the entire route, each with a `speed_limit` property.
/// covering the entire route, each with a `speed_limit` property (in km/h) when that data is
/// known.
#[wasm_bindgen(js_name = speedLimitForRoute)]
pub fn speed_limit_for_route(&self, raw_waypoints: JsValue) -> Result<String, JsValue> {
let raw_waypoints: Vec<RawRouteWaypoint> = serde_wasm_bindgen::from_value(raw_waypoints)?;
Expand All @@ -93,13 +94,14 @@ impl RouteInfo {
// Generate output LineStrings in order, covering the whole route. For snapped sections,
// chop it up whenever the speed limit changes.
let mut output = Vec::new();

for pair in waypoints.windows(2) {
if let (Waypoint::Snapped(_, i1), Waypoint::Snapped(_, i2)) = (&pair[0], &pair[1]) {
if let Some(path) = self.geometric_path(*i1, *i2) {
// Walk along the path, building up a LineString as long as the speed limit is
// the same
let mut pts = Vec::new();
let mut osm_ids = Vec::new();
let mut speed_limit: Option<Speed> = None;

for (r, dir) in path {
let road = &self.network.roads[&r];
Expand All @@ -108,9 +110,7 @@ impl RouteInfo {
road_pts.reverse();
}

// TODO Need to add speed_limit to osm2streets! For now, actually split
// when the OSM ID changes, just to wire something up
if road.osm_ids == osm_ids {
if road.speed_limit == speed_limit {
pts.extend(road_pts);
} else {
// Start a new segment
Expand All @@ -121,10 +121,12 @@ impl RouteInfo {
.to_geojson(Some(&self.network.gps_bounds)),
);
feature.set_property("type", "snapped");
feature.set_property("osm_ids", abstutil::to_json(&osm_ids));
if let Some(speed) = speed_limit {
feature.set_property("speed_limit", to_kmph(speed));
}
output.push(feature);
}
osm_ids = road.osm_ids.clone();
speed_limit = road.speed_limit;
}
}

Expand All @@ -136,7 +138,9 @@ impl RouteInfo {
.to_geojson(Some(&self.network.gps_bounds)),
);
feature.set_property("type", "snapped");
feature.set_property("osm_ids", abstutil::to_json(&osm_ids));
if let Some(speed) = speed_limit {
feature.set_property("speed_limit", to_kmph(speed));
}
output.push(feature);
}
} else {
Expand Down Expand Up @@ -188,3 +192,8 @@ impl Waypoint {
}
}
}

// TODO Move to geom
fn to_kmph(s: Speed) -> f64 {
3.6 * s.inner_meters_per_second()
}

0 comments on commit 9ee546f

Please sign in to comment.