Skip to content

Commit

Permalink
Reduce dependencies on abstutil
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Nov 27, 2023
1 parent b9bc594 commit 8acc74c
Show file tree
Hide file tree
Showing 17 changed files with 131 additions and 26 deletions.
18 changes: 11 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ members = [
"tests",
]

resolver = "2"

# Specify the versions for common dependencies just once here, instead of
# repeating in a bunch of crates
[workspace.dependencies]
Expand Down
2 changes: 2 additions & 0 deletions osm2lanes-js/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ crate-type = ["cdylib", "rlib"]
[dependencies]
abstutil = { git = "https://github.com/a-b-street/abstreet" }
console_error_panic_hook = "0.1.6"
console_log = "1.0.0"
log = "0.4.20"
osm2lanes = { path = "../osm2lanes" }
wasm-bindgen = "0.2.84"
serde_json = { workspace = true }
Expand Down
8 changes: 6 additions & 2 deletions osm2lanes-js/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use std::sync::Once;

use wasm_bindgen::prelude::*;

use abstutil::Tags;
use osm2lanes::{MapConfig, get_lane_specs_ltr};
use osm2lanes::{get_lane_specs_ltr, MapConfig};

static SETUP_LOGGER: Once = Once::new();

#[wasm_bindgen(js_name = getLaneSpecs)]
pub fn get_lane_specs(tags: JsValue, config: JsValue) -> Result<String, JsValue> {
abstutil::logger::setup();
SETUP_LOGGER.call_once(|| console_log::init_with_level(log::Level::Info).unwrap());
// Panics shouldn't happen, but if they do, console.log them.
console_error_panic_hook::set_once();

Expand Down
3 changes: 3 additions & 0 deletions osm2lanes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ anyhow = { workspace = true }
enumset = { version = "1.0.12", features=["serde"] }
geom = { git = "https://github.com/a-b-street/abstreet" }
serde = { workspace = true }

[dev-dependencies]
env_logger = "0.10.1"
7 changes: 6 additions & 1 deletion osm2lanes/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use std::sync::Once;

use abstutil::Tags;
use env_logger::{Builder, Env};

use crate::{get_lane_specs_ltr, Direction, DrivingSide, MapConfig};

static SETUP_LOGGER: Once = Once::new();

#[test]
fn test_osm_to_specs() {
abstutil::logger::setup();
SETUP_LOGGER.call_once(|| Builder::from_env(Env::default().default_filter_or("info")).init());

let mut ok = true;
for (url, mut input, driving_side, expected_lt, expected_dir) in vec![
Expand Down
3 changes: 3 additions & 0 deletions osm2streets-js/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ crate-type = ["cdylib", "rlib"]
abstutil = { git = "https://github.com/a-b-street/abstreet" }
anyhow = { workspace = true }
console_error_panic_hook = "0.1.6"
console_log = "1.0.0"
geom = { git = "https://github.com/a-b-street/abstreet" }
experimental = { path = "../experimental" }
# TODO Upstream this in abstutil crate. WASM is missing some runtime dep otherwise.
instant = { version = "0.1.12", features = ["wasm-bindgen"] }
log = "0.4.20"
osm2streets = { path = "../osm2streets" }
serde = { workspace = true }
serde_json = { workspace = true }
streets_reader = { path = "../streets_reader" }
wasm-bindgen = "0.2.84"
serde-wasm-bindgen = "0.5.0"
15 changes: 9 additions & 6 deletions osm2streets-js/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::{BTreeMap, BTreeSet};
use std::sync::Once;

use abstutil::{Tags, Timer};
use geom::{Distance, LonLat, PolyLine, Polygon};
Expand All @@ -10,6 +11,8 @@ use osm2streets::{
StreetNetwork, Transformation,
};

static SETUP_LOGGER: Once = Once::new();

#[derive(Serialize, Deserialize)]
pub struct ImportOptions {
debug_each_step: bool,
Expand All @@ -33,7 +36,7 @@ impl JsStreetNetwork {
clip_pts_geojson: &str,
input: JsValue,
) -> Result<JsStreetNetwork, JsValue> {
abstutil::logger::setup();
SETUP_LOGGER.call_once(|| console_log::init_with_level(log::Level::Info).unwrap());
// Panics shouldn't happen, but if they do, console.log them.
console_error_panic_hook::set_once();

Expand Down Expand Up @@ -157,21 +160,21 @@ impl JsStreetNetwork {
.to_geojson(Some(&self.inner.gps_bounds)),
);
}
abstutil::to_json(&geom::geometries_to_geojson(polygons))
serde_json::to_string_pretty(&geom::geometries_to_geojson(polygons)).unwrap()
}

// TODO I think https://github.com/cloudflare/serde-wasm-bindgen would let us just return a
// HashMap
#[wasm_bindgen(js_name = getOsmTagsForWay)]
pub fn get_osm_tags_for_way(&self, id: i64) -> String {
abstutil::to_json(&self.ways[&osm::WayID(id)].tags)
serde_json::to_string_pretty(&self.ways[&osm::WayID(id)].tags).unwrap()
}

/// Returns the entire StreetNetwork as JSON. The API doesn't have guarantees about backwards
/// compatibility.
#[wasm_bindgen(js_name = toJson)]
pub fn to_json(&self) -> String {
abstutil::to_json(&self.inner)
serde_json::to_string_pretty(&self.inner).unwrap()
}

/// Returns a GeoJSON Polygon showing a wide buffer around the way's original geometry
Expand Down Expand Up @@ -217,7 +220,7 @@ impl JsStreetNetwork {
.iter()
.for_each(|c| polygon = polygon.difference(c).unwrap()[0].clone());

abstutil::to_json(&polygon.to_geojson(Some(&self.inner.gps_bounds)))
serde_json::to_string_pretty(&polygon.to_geojson(Some(&self.inner.gps_bounds))).unwrap()
}

/// Returns the XML string representing a way. Any OSM tags changed via
Expand Down Expand Up @@ -250,7 +253,7 @@ impl JsStreetNetwork {
#[wasm_bindgen(js_name = overwriteOsmTagsForWay)]
pub fn overwrite_osm_tags_for_way(&mut self, id: i64, tags: String) {
let id = osm::WayID(id);
let tags: Tags = abstutil::from_json(tags.as_bytes()).unwrap();
let tags: Tags = serde_json::from_slice(tags.as_bytes()).unwrap();

let mut intersections = BTreeSet::new();
for road in self.inner.roads.values_mut() {
Expand Down
2 changes: 1 addition & 1 deletion osm2streets/src/ids.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::fmt;

use abstutil::{deserialize_usize, serialize_usize};
use serde::{Deserialize, Serialize};

use crate::utils::{deserialize_usize, serialize_usize};
use crate::Road;

/// Opaque and non-contiguous
Expand Down
2 changes: 1 addition & 1 deletion osm2streets/src/intersection.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::collections::BTreeMap;

use abstutil::{deserialize_btreemap, serialize_btreemap};
use geom::{Circle, Distance, Polygon, Pt2D};
use serde::{Deserialize, Serialize};

use osm2lanes::osm;

use crate::utils::{deserialize_btreemap, serialize_btreemap};
use crate::{DrivingSide, IntersectionID, RoadID, StreetNetwork};
use TrafficConflict::*;

Expand Down
4 changes: 3 additions & 1 deletion osm2streets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use abstutil::{deserialize_btreemap, serialize_btreemap};
use geom::{GPSBounds, PolyLine, Polygon, Pt2D};

use self::utils::{deserialize_btreemap, serialize_btreemap};

pub use self::geometry::{intersection_polygon, InputRoad};
pub(crate) use self::ids::RoadWithEndpoints;
pub use self::ids::{CommonEndpoint, IntersectionID, LaneID, RoadID};
Expand Down Expand Up @@ -40,6 +41,7 @@ mod render;
mod road;
mod transform;
mod types;
pub mod utils;
mod validate;

#[derive(Clone, Debug, Serialize, Deserialize)]
Expand Down
4 changes: 3 additions & 1 deletion osm2streets/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use abstutil::{deserialize_btreemap, serialize_btreemap, Tags};
use abstutil::Tags;

use crate::utils::{deserialize_btreemap, serialize_btreemap};

/// None corresponds to the native name
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
Expand Down
64 changes: 64 additions & 0 deletions osm2streets/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copied from https://github.com/a-b-street/abstreet/tree/main/abstutil/src to reduce dependencies

use std::collections::BTreeMap;

use anyhow::Result;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// Format a number with commas
pub fn prettyprint_usize(x: usize) -> String {
let num = format!("{}", x);
let mut result = String::new();
let mut i = num.len();
for c in num.chars() {
result.push(c);
i -= 1;
if i > 0 && i % 3 == 0 {
result.push(',');
}
}
result
}

/// Serializes a `usize` as a `u32` to save space. Useful when you need `usize` for indexing, but
/// the values don't exceed 2^32.
pub fn serialize_usize<S: Serializer>(x: &usize, s: S) -> Result<S::Ok, S::Error> {
if let Ok(x) = u32::try_from(*x) {
x.serialize(s)
} else {
Err(serde::ser::Error::custom(format!("{} can't fit in u32", x)))
}
}

/// Deserializes a `usize` from a `u32`.
pub fn deserialize_usize<'de, D: Deserializer<'de>>(d: D) -> Result<usize, D::Error> {
let x = <u32>::deserialize(d)?;
Ok(x as usize)
}

/// Serializes a BTreeMap as a list of tuples. Necessary when the keys are structs; see
/// https://github.com/serde-rs/json/issues/402.
pub fn serialize_btreemap<S: Serializer, K: Serialize, V: Serialize>(
map: &BTreeMap<K, V>,
s: S,
) -> Result<S::Ok, S::Error> {
map.iter().collect::<Vec<(_, _)>>().serialize(s)
}

/// Deserializes a BTreeMap from a list of tuples. Necessary when the keys are structs; see
/// https://github.com/serde-rs/json/issues/402.
pub fn deserialize_btreemap<
'de,
D: Deserializer<'de>,
K: Deserialize<'de> + Ord,
V: Deserialize<'de>,
>(
d: D,
) -> Result<BTreeMap<K, V>, D::Error> {
let vec = <Vec<(K, V)>>::deserialize(d)?;
let mut map = BTreeMap::new();
for (k, v) in vec {
map.insert(k, v);
}
Ok(map)
}
3 changes: 2 additions & 1 deletion streets_reader/src/osm_reader/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use std::iter::Peekable;
use anyhow::Result;
use xmlparser::Token;

use abstutil::{prettyprint_usize, Tags, Timer};
use abstutil::{Tags, Timer};
use geom::{GPSBounds, LonLat};
use osm2streets::osm::{NodeID, OsmID, RelationID, WayID};
use osm2streets::utils::prettyprint_usize;

use super::{Document, Node, Relation, Way};

Expand Down
8 changes: 5 additions & 3 deletions streets_reader/src/split_ways.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::{hash_map::Entry, HashMap};

use abstutil::{Counter, Timer};
use abstutil::Timer;
use geom::{HashablePt2D, PolyLine, Pt2D};
use osm2streets::{
Direction, IntersectionControl, IntersectionID, IntersectionKind, Road, RoadID, StreetNetwork,
Expand All @@ -23,14 +23,16 @@ pub fn split_up_roads(

// Create intersections for any points shared by at least 2 roads, and for endpoints of every
// road.
let mut counts_per_pt = Counter::new();
let mut count_per_pt = HashMap::new();
let mut pt_to_intersection_id: HashMap<HashablePt2D, IntersectionID> = HashMap::new();
timer.start_iter("look for common points", input.roads.len());
for (_, pts, _) in &input.roads {
timer.next();
for (idx, pt) in pts.iter().enumerate() {
let hash_pt = pt.to_hashable();
let count = counts_per_pt.inc(hash_pt);
let entry = count_per_pt.entry(hash_pt).or_insert(0);
*entry += 1;
let count = *entry;

if count == 2 || idx == 0 || idx == pts.len() - 1 {
if let Entry::Vacant(entry) = pt_to_intersection_id.entry(hash_pt) {
Expand Down
1 change: 1 addition & 0 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
abstutil = { git = "https://github.com/a-b-street/abstreet" }
anyhow = { workspace = true }
env_logger = "0.10.1"
geom = { git = "https://github.com/a-b-street/abstreet" }
streets_reader = { path = "../streets_reader" }
serde = { workspace = true }
Expand Down
Loading

0 comments on commit 8acc74c

Please sign in to comment.