Skip to content

Commit

Permalink
trying to merge ..
Browse files Browse the repository at this point in the history
  • Loading branch information
Mason Pike committed Dec 2, 2023
2 parents 275bd47 + bb4d1cd commit 4d92156
Show file tree
Hide file tree
Showing 21 changed files with 385 additions and 138 deletions.
360 changes: 253 additions & 107 deletions Cargo.lock

Large diffs are not rendered by default.

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

resolver = "2"

# Specify the versions for common dependencies just once here, instead of
# repeating in a bunch of crates
[workspace.dependencies]
anyhow = "1.0.38"
serde = "1.0.123"
serde_json = "1.0.61"
geom = { git = "https://github.com/a-b-street/geom" }
serde = "1.0.193"
serde_json = "1.0.108"

# To temporarily work on dependencies locally, uncomment this
#[patch."https://github.com/a-b-street/abstreet"]
#abstutil = { path = "/home/dabreegster/abstreet/abstutil" }
#geom = { path = "/home/dabreegster/abstreet/geom" }
#geom = { path = "/home/dabreegster/geom" }
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ offline without running an external server).
- [osm2streets-vector-tileserver](https://github.com/jakecoppinger/osm2streets-vector-tileserver): Dynamically generate vector tiles and view with QGIS, Mapbox, etc
- [safe-cycling-map](https://github.com/jakecoppinger/safe-cycling-map) is an example Mapbox use
- [lane editor](https://a-b-street.github.io/osm2streets/lane_editor.html) to edit OSM tags and see visual results
- [ATIP](https://github.com/acteng/atip): sketching active travel infrastructure, using `osm2streets` for contextual layers about speed and lane configuration
- _Planned: plugins for [iD](https://github.com/openstreetmap/iD) and
[JOSM](https://josm.openstreetmap.de/) to display streets in detail and
visually edit lane tagging_
Expand Down
2 changes: 1 addition & 1 deletion experimental/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
itertools = "0.10.2"
itertools = "0.11.0"
petgraph = "0.6.3"

# utilities from abstreet
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
5 changes: 4 additions & 1 deletion osm2lanes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ edition = "2021"
abstutil = { git = "https://github.com/a-b-street/abstreet" }
anyhow = { workspace = true }
enumset = { version = "1.0.12", features=["serde"] }
geom = { git = "https://github.com/a-b-street/abstreet" }
geom = { workspace = true }
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
5 changes: 4 additions & 1 deletion 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"
geom = { git = "https://github.com/a-b-street/abstreet" }
console_log = "1.0.0"
geom = { workspace = true }
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, Polygon, PolyLine};
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
6 changes: 3 additions & 3 deletions osm2streets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ rust-version = "1.66"
abstutil = { git = "https://github.com/a-b-street/abstreet" }
anyhow = { workspace = true }
enumset = { version = "1.0.12", features=["serde"] }
geo = "0.26.0"
geo = "0.27.0"
geojson = "0.24.1"
geom = { git = "https://github.com/a-b-street/abstreet" }
itertools = "0.10.5"
geom = { workspace = true }
itertools = "0.11.0"
log = "0.4.14"
osm2lanes = { path = "../osm2lanes" }
petgraph = { version = "0.6.3" }
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)
}
2 changes: 1 addition & 1 deletion streets_reader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"
abstutil = { git = "https://github.com/a-b-street/abstreet" }
anyhow = { workspace = true }
country-geocoder = { git = "https://github.com/a-b-street/country-geocoder" }
geom = { git = "https://github.com/a-b-street/abstreet" }
geom = { workspace = true }
log = "0.4.14"
osm2streets = { path = "../osm2streets" }
osmpbf = "0.3.2"
Expand Down
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 @@ -2,13 +2,14 @@ use std::collections::{BTreeMap, HashMap};
use std::io::Cursor;
use std::iter::Peekable;

use abstutil::{prettyprint_usize, Tags, Timer};
use abstutil::{Tags, Timer};
use anyhow::Result;
use geom::{GPSBounds, LonLat};
use osmpbf::{BlobDecode, BlobReader, Element as PbfElement, IndexedReader, RelMemberType};
use xmlparser::Token;

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
3 changes: 2 additions & 1 deletion tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ edition = "2021"
[dependencies]
abstutil = { git = "https://github.com/a-b-street/abstreet" }
anyhow = { workspace = true }
geom = { git = "https://github.com/a-b-street/abstreet" }
env_logger = "0.10.1"
geom = { workspace = true }
streets_reader = { path = "../streets_reader" }
serde = { workspace = true }
serde_json = { workspace = true }
Expand Down
11 changes: 9 additions & 2 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@
mod tests {
use std::path::Path;

use abstutil::Timer;
use std::sync::Once;

use anyhow::{bail, Result};
use geom::LonLat;

use env_logger::{Builder, Env};

use abstutil::Timer;
use experimental::RoadNetwork;
use osm2streets::{MapConfig, Transformation};

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

include!(concat!(env!("OUT_DIR"), "/tests.rs"));

fn test(path: &str) -> Result<()> {
abstutil::logger::setup();
SETUP_LOGGER
.call_once(|| Builder::from_env(Env::default().default_filter_or("info")).init());

let mut timer = Timer::new("test osm2streets");

Expand Down

0 comments on commit 4d92156

Please sign in to comment.