Skip to content

Commit

Permalink
fix: Propper jam factor for traffic info in [0.0, 10.0] range
Browse files Browse the repository at this point in the history
  • Loading branch information
Stepan Kizim committed Oct 14, 2024
1 parent cb82f28 commit 796416a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{env, num::NonZero};
use std::{collections::HashMap, env, num::NonZero};

use axum::{
extract::State,
Expand Down Expand Up @@ -160,8 +160,8 @@ async fn traffic(State(state): State<AppState>) -> Result<Json<Value>, (StatusCo
// todo: this is really heavy compute operation
reader.get_tile_traffic_flows(tile_id)
})
.map(|edge| (edge.shape, edge.jam_factor))
.collect::<Vec<_>>();
.map(|edge| (edge.shape, 10 - (edge.jam_factor * 10.0).round() as i32))
.collect::<HashMap<_, _>>();
Ok(Json(serde_json::to_value(edges).unwrap()))
}

Expand Down
12 changes: 6 additions & 6 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@
"interpolate",
["linear"],
["get", "jamFactor"], // Access the jam factor property
0, "#33cc33", // Green for jamFactor 0.0 (no traffic)
0.25, "#ffcc00", // Yellow for jamFactor 0.25 (moderate traffic)
0.5, "#ffb700", // Orange for jamFactor 0.5 (slow traffic)
0.75, "#ff0000", // Red for jamFactor 0.75 (heavy traffic)
1.0, "#800000" // Dark red for jamFactor 1.0 (severe traffic)
0.0, "#33cc33", // Green for no traffic
2.5, "#ffcc00", // Yellow for moderate traffic
5.0, "#ffb700", // Orange for medium traffic
7.5, "#ff0000", // Red for heavy traffic
10., "#000000", // Black for blocked roads
],
"line-width": 5,
// todo: Use different width depending on the edge level
Expand Down Expand Up @@ -563,7 +563,7 @@
coordinates: segment[1],
},
properties: {
jamFactor: 1.0 - Math.min(segment[0] / freeFlowSpeed, 1.0),
jamFactor: 10.0 - 10.0 * Math.min(segment[0] / freeFlowSpeed, 1.0),
},
};
});
Expand Down

0 comments on commit 796416a

Please sign in to comment.